2020-11-25 11:25:32 -08:00
|
|
|
package solana
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Account struct {
|
|
|
|
PrivateKey PrivateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAccount() *Account {
|
|
|
|
_, privateKey, err := NewRandomPrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to generate private key: %s", err))
|
|
|
|
}
|
|
|
|
return &Account{
|
|
|
|
PrivateKey: privateKey,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func AccountFromPrivateKeyBase58(privateKey string) (*Account, error) {
|
|
|
|
k, err := PrivateKeyFromBase58(privateKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("account from private key: private key from b58: %w", err)
|
|
|
|
}
|
|
|
|
return &Account{
|
|
|
|
PrivateKey: k,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Account) PublicKey() PublicKey {
|
|
|
|
return a.PrivateKey.PublicKey()
|
|
|
|
}
|
2020-11-26 14:05:05 -08:00
|
|
|
|
|
|
|
type AccountMeta struct {
|
|
|
|
PublicKey PublicKey
|
|
|
|
IsWritable bool
|
2021-07-29 08:19:13 -07:00
|
|
|
IsSigner bool
|
|
|
|
}
|
|
|
|
|
2021-07-29 11:18:19 -07:00
|
|
|
// Meta intializes a new AccountMeta with the provided pubKey.
|
|
|
|
func Meta(
|
|
|
|
pubKey PublicKey,
|
|
|
|
) *AccountMeta {
|
|
|
|
return &AccountMeta{
|
|
|
|
PublicKey: pubKey,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WRITE sets IsWritable to true.
|
|
|
|
func (meta *AccountMeta) WRITE() *AccountMeta {
|
|
|
|
meta.IsWritable = true
|
|
|
|
return meta
|
|
|
|
}
|
|
|
|
|
|
|
|
// SIGNER sets IsSigner to true.
|
|
|
|
func (meta *AccountMeta) SIGNER() *AccountMeta {
|
|
|
|
meta.IsSigner = true
|
|
|
|
return meta
|
|
|
|
}
|
|
|
|
|
2021-07-29 08:19:13 -07:00
|
|
|
func NewAccountMeta(
|
|
|
|
pubKey PublicKey,
|
|
|
|
WRITE bool,
|
|
|
|
SIGNER bool,
|
|
|
|
) *AccountMeta {
|
|
|
|
return &AccountMeta{
|
|
|
|
PublicKey: pubKey,
|
|
|
|
IsWritable: WRITE,
|
|
|
|
IsSigner: SIGNER,
|
|
|
|
}
|
2020-11-26 14:05:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AccountMeta) less(act *AccountMeta) bool {
|
|
|
|
if a.IsSigner && !act.IsSigner {
|
|
|
|
return true
|
|
|
|
} else if !a.IsSigner && act.IsSigner {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.IsWritable {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2021-07-29 08:19:13 -07:00
|
|
|
|
|
|
|
type AccountMetaSlice []*AccountMeta
|
|
|
|
|
|
|
|
func (slice *AccountMetaSlice) Append(account *AccountMeta) {
|
|
|
|
*slice = append(*slice, account)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (slice *AccountMetaSlice) SetAccounts(accounts []*AccountMeta) error {
|
|
|
|
*slice = accounts
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (slice AccountMetaSlice) GetAccounts() []*AccountMeta {
|
|
|
|
return slice
|
|
|
|
}
|
2021-08-02 07:56:12 -07:00
|
|
|
|
|
|
|
// GetSigners returns the accounts that are signers.
|
|
|
|
func (slice AccountMetaSlice) GetSigners() []*AccountMeta {
|
|
|
|
signers := make([]*AccountMeta, 0)
|
|
|
|
for _, ac := range slice {
|
|
|
|
if ac.IsSigner {
|
|
|
|
signers = append(signers, ac)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return signers
|
|
|
|
}
|