-
Notifications
You must be signed in to change notification settings - Fork 111
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can remove the |
||
} | ||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I want to use literal number for some reasons.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
if that's the case, wouldn't you want to export the offset constants rather than using literals? |
||
} | ||
|
||
const TokenAccountSize = 165 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,9 @@ package types | |
|
||
import ( | ||
"crypto/ed25519" | ||
"encoding/hex" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/portto/solana-go-sdk/common" | ||
) | ||
|
@@ -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) | ||
} | ||
|
||
|
@@ -23,3 +29,19 @@ func AccountFromPrivateKeyBytes(privateKey []byte) Account { | |
PrivateKey: sk, | ||
} | ||
} | ||
|
||
func (a *Account) UnmarshalText(b []byte) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I more prefer AccountFromHex. WDYT There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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