Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor features and improvements #13

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/rpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type TransactionMeta struct {
PostBalances []int64 `json:"postBalances"`
PreTokenBalances []TransactionMetaTokenBalance `json:"preTokenBalances"`
PostTokenBalances []TransactionMetaTokenBalance `json:"postTokenBalances"`
LogMessages []string `json:"logMesssages"`
LogMessages []string `json:"logMessages"`
InnerInstructions []struct {
Index uint64 `json:"index"`
Instructions []Instruction `json:"instructions"`
Expand Down
16 changes: 16 additions & 0 deletions common/public_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/sha256"
"encoding/json"
"errors"
"fmt"

"filippo.io/edwards25519"
"github.com/mr-tron/base58"
Expand All @@ -17,6 +18,21 @@ const (

type PublicKey [PublicKeyLength]byte

func (pk *PublicKey) UnmarshalJSON(b []byte) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you add test for this ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'll handle this a bit later

buf, err := base58.Decode(string(b[1 : len(b)-1]))
if err != nil {
return fmt.Errorf("invalid public key: %w", err)
}

if len(buf) != PublicKeyLength {
return errors.New("invalid public key length")
}

copy(pk[:], buf)

return nil
}

func (p PublicKey) String() string {
return p.ToBase58()
}
Expand Down
53 changes: 46 additions & 7 deletions program/tokenprog/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,52 @@ const MultisigAccountSize uint64 = 355
const MintAccountSize = 82

type MintAccount struct {
MintAuthorityOption uint32
MintAuthority common.PublicKey
Supply uint64
Decimals uint8
IsInitialized bool
FreezeAuthorityOption uint32
FreezeAuthority common.PublicKey
// MintAuthorityOption uint32
MintAuthority *common.PublicKey
Supply uint64
Decimals uint8
IsInitialized bool
// FreezeAuthorityOption uint32
FreezeAuthority *common.PublicKey
Comment on lines +16 to +22
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can remove the // MintAuthorityOption uint32 and // FreezeAuthorityOption uint32.

}

const (
mintMintAuthorityOptionOffset = 0
mintMintAuthorityOffset = mintMintAuthorityOptionOffset + 4
mintSupplyOffset = mintMintAuthorityOffset + 32
mintDecimalsOffset = mintSupplyOffset + 8
mintIsInitializedOffset = mintDecimalsOffset + 1
mintFreezeAuthorityOptionOffset = mintIsInitializedOffset + 1
mintFreezeAuthorityOffset = mintFreezeAuthorityOptionOffset + 4
)

func isSome(option []byte) bool {
return bytes.Equal(option, Some)
}

func MintAccountFromData(data []byte) (*MintAccount, error) {
if len(data) != MintAccountSize {
return nil, fmt.Errorf("mint account data length mismatch")
}

var mint MintAccount

mintAuthorityOption := data[0:4]
if isSome(mintAuthorityOption) {
key := common.PublicKeyFromBytes(data[mintMintAuthorityOffset : mintSupplyOffset+32])
mint.MintAuthority = &key
}

mint.Supply = binary.LittleEndian.Uint64(data[mintSupplyOffset : mintSupplyOffset+8])
mint.Decimals = uint8(data[mintDecimalsOffset])
mint.IsInitialized = data[mintIsInitializedOffset] == 1

if isSome(data[mintFreezeAuthorityOptionOffset:mintFreezeAuthorityOptionOffset+4]) {
key := common.PublicKeyFromBytes(data[mintFreezeAuthorityOffset : mintFreezeAuthorityOffset+32])
mint.FreezeAuthority = &key
}

return &mint, nil
Comment on lines +25 to +61
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess maybe you want to make code more readable but I would like to use literal number like I decode token account and would you add a test for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how important is this to you to use literal number? using literal offsets is pretty unreadable to be honest.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to use literal number for some reasons.

  1. if someday I want to use get accountInfo to fetch data and using offset. the literal number is more intuitive to me. I don't need to recalculate it if I miss in the bytes array.
  2. I don't think there are someone will read this code, I want to make them as clear as possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if someday I want to use get accountInfo to fetch data and using offset.

if that's the case, wouldn't you want to export the offset constants rather than using literals?

}

const TokenAccountSize = 165
Expand Down
24 changes: 23 additions & 1 deletion types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package types

import (
"crypto/ed25519"
"encoding/hex"
"errors"
"fmt"

"github.com/portto/solana-go-sdk/common"
)
Expand All @@ -12,7 +15,10 @@ type Account struct {
}

func NewAccount() Account {
_, X, _ := ed25519.GenerateKey(nil)
_, X, err := ed25519.GenerateKey(nil)
if err != nil {
panic(fmt.Sprintf("ed25519 genkey error: %s", err))
}
return AccountFromPrivateKeyBytes(X)
}

Expand All @@ -23,3 +29,19 @@ func AccountFromPrivateKeyBytes(privateKey []byte) Account {
PrivateKey: sk,
}
}

func (a *Account) UnmarshalText(b []byte) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I more prefer AccountFromHex. WDYT

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UnmarshalText is an interface from the standard library, to support some parsing libraries.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! Ok. Would you add some test for it?

// hex.Decode(dst []byte, src []byte)
key, err := hex.DecodeString(string(b))
if err != nil {
return fmt.Errorf("decode private key: %w", err)
}

if len(key) != ed25519.PrivateKeySize {
return errors.New("invalid private key size")
}

*a = AccountFromPrivateKeyBytes(key)

return nil
}