github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/std/account.go (about) 1 package std 2 3 import ( 4 "fmt" 5 6 "github.com/gnolang/gno/tm2/pkg/crypto" 7 "github.com/gnolang/gno/tm2/pkg/errors" 8 9 _ "github.com/gnolang/gno/tm2/pkg/crypto/ed25519" 10 _ "github.com/gnolang/gno/tm2/pkg/crypto/mock" 11 _ "github.com/gnolang/gno/tm2/pkg/crypto/multisig" 12 _ "github.com/gnolang/gno/tm2/pkg/crypto/secp256k1" 13 ) 14 15 // Account is an interface used to store coins at a given address within state. 16 // It presumes a notion of sequence numbers for replay protection, a notion of 17 // account numbers for replay protection for previously pruned accounts, and a 18 // pubkey for authentication purposes. 19 // 20 // Many complex conditions can be used in the concrete struct which implements Account. 21 type Account interface { 22 GetAddress() crypto.Address 23 SetAddress(crypto.Address) error // errors if already set. 24 25 GetPubKey() crypto.PubKey // can return nil. 26 SetPubKey(crypto.PubKey) error 27 28 GetAccountNumber() uint64 29 SetAccountNumber(uint64) error 30 31 GetSequence() uint64 32 SetSequence(uint64) error 33 34 GetCoins() Coins 35 SetCoins(Coins) error 36 37 // Ensure that account implements stringer 38 String() string 39 } 40 41 //---------------------------------------- 42 // BaseAccount 43 44 // BaseAccount - a base account structure. 45 // This can be extended by embedding within in your *Account structure. 46 type BaseAccount struct { 47 Address crypto.Address `json:"address" yaml:"address"` 48 Coins Coins `json:"coins" yaml:"coins"` 49 PubKey crypto.PubKey `json:"public_key" yaml:"public_key"` 50 AccountNumber uint64 `json:"account_number" yaml:"account_number"` 51 Sequence uint64 `json:"sequence" yaml:"sequence"` 52 } 53 54 // NewBaseAccount creates a new BaseAccount object 55 func NewBaseAccount(address crypto.Address, coins Coins, 56 pubKey crypto.PubKey, accountNumber uint64, sequence uint64, 57 ) *BaseAccount { 58 return &BaseAccount{ 59 Address: address, 60 Coins: coins, 61 PubKey: pubKey, 62 AccountNumber: accountNumber, 63 Sequence: sequence, 64 } 65 } 66 67 // String implements fmt.Stringer 68 func (acc BaseAccount) String() string { 69 var pubkey string 70 71 if acc.PubKey != nil { 72 pubkey = crypto.PubKeyToBech32(acc.PubKey) 73 } 74 75 return fmt.Sprintf(`Account: 76 Address: %s 77 Pubkey: %s 78 Coins: %s 79 AccountNumber: %d 80 Sequence: %d`, 81 acc.Address, pubkey, acc.Coins, acc.AccountNumber, acc.Sequence, 82 ) 83 } 84 85 // ProtoBaseAccount - a prototype function for BaseAccount 86 func ProtoBaseAccount() Account { 87 return &BaseAccount{} 88 } 89 90 // NewBaseAccountWithAddress - returns a new base account with a given address 91 func NewBaseAccountWithAddress(addr crypto.Address) BaseAccount { 92 return BaseAccount{ 93 Address: addr, 94 } 95 } 96 97 // GetAddress - Implements Account. 98 func (acc BaseAccount) GetAddress() crypto.Address { 99 return acc.Address 100 } 101 102 // SetAddress - Implements Account. 103 func (acc *BaseAccount) SetAddress(addr crypto.Address) error { 104 if !acc.Address.IsZero() { 105 return errors.New("cannot override BaseAccount address") 106 } 107 acc.Address = addr 108 return nil 109 } 110 111 // GetPubKey - Implements Account. 112 func (acc BaseAccount) GetPubKey() crypto.PubKey { 113 return acc.PubKey 114 } 115 116 // SetPubKey - Implements Account. 117 func (acc *BaseAccount) SetPubKey(pubKey crypto.PubKey) error { 118 acc.PubKey = pubKey 119 return nil 120 } 121 122 // GetCoins - Implements Account. 123 func (acc *BaseAccount) GetCoins() Coins { 124 return acc.Coins 125 } 126 127 // SetCoins - Implements Account. 128 func (acc *BaseAccount) SetCoins(coins Coins) error { 129 acc.Coins = coins 130 return nil 131 } 132 133 // GetAccountNumber - Implements Account 134 func (acc *BaseAccount) GetAccountNumber() uint64 { 135 return acc.AccountNumber 136 } 137 138 // SetAccountNumber - Implements Account 139 func (acc *BaseAccount) SetAccountNumber(accNumber uint64) error { 140 acc.AccountNumber = accNumber 141 return nil 142 } 143 144 // GetSequence - Implements Account. 145 func (acc *BaseAccount) GetSequence() uint64 { 146 return acc.Sequence 147 } 148 149 // SetSequence - Implements Account. 150 func (acc *BaseAccount) SetSequence(seq uint64) error { 151 acc.Sequence = seq 152 return nil 153 }