github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/rpcclient/notary/contract.go (about) 1 /* 2 Package notary provides an RPC-based wrapper for the Notary subsystem. 3 4 It provides both regular ContractReader/Contract interfaces for the notary 5 contract and notary-specific Actor as well as some helper functions to simplify 6 creation of notary requests. 7 */ 8 package notary 9 10 import ( 11 "errors" 12 "fmt" 13 "math" 14 "math/big" 15 16 "github.com/nspcc-dev/neo-go/pkg/core/native/nativehashes" 17 "github.com/nspcc-dev/neo-go/pkg/core/transaction" 18 "github.com/nspcc-dev/neo-go/pkg/neorpc/result" 19 "github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap" 20 "github.com/nspcc-dev/neo-go/pkg/smartcontract" 21 "github.com/nspcc-dev/neo-go/pkg/util" 22 "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" 23 ) 24 25 const ( 26 setMaxNVBDeltaMethod = "setMaxNotValidBeforeDelta" 27 setFeePKMethod = "setNotaryServiceFeePerKey" 28 ) 29 30 // ContractInvoker is used by ContractReader to perform read-only calls. 31 type ContractInvoker interface { 32 Call(contract util.Uint160, operation string, params ...any) (*result.Invoke, error) 33 } 34 35 // ContractActor is used by Contract to create and send transactions. 36 type ContractActor interface { 37 ContractInvoker 38 39 MakeCall(contract util.Uint160, method string, params ...any) (*transaction.Transaction, error) 40 MakeRun(script []byte) (*transaction.Transaction, error) 41 MakeUnsignedCall(contract util.Uint160, method string, attrs []transaction.Attribute, params ...any) (*transaction.Transaction, error) 42 MakeUnsignedRun(script []byte, attrs []transaction.Attribute) (*transaction.Transaction, error) 43 SendCall(contract util.Uint160, method string, params ...any) (util.Uint256, uint32, error) 44 SendRun(script []byte) (util.Uint256, uint32, error) 45 } 46 47 // ContractReader represents safe (read-only) methods of Notary. It can be 48 // used to query various data, but `verify` method is not exposed there because 49 // it can't be successful in standalone invocation (missing transaction with the 50 // NotaryAssisted attribute and its signature). 51 type ContractReader struct { 52 invoker ContractInvoker 53 } 54 55 // Contract provides full Notary interface, both safe and state-changing methods. 56 // The only method omitted is onNEP17Payment which can only be called 57 // successfully from the GASToken native contract. 58 type Contract struct { 59 ContractReader 60 61 actor ContractActor 62 } 63 64 // OnNEP17PaymentData is the data set that is accepted by the notary contract 65 // onNEP17Payment handler. It's mandatory for GAS tranfers to this contract. 66 type OnNEP17PaymentData struct { 67 // Account can be nil, in this case transfer sender (from) account is used. 68 Account *util.Uint160 69 // Till specifies the deposit lock time (in blocks). 70 Till uint32 71 } 72 73 // OnNEP17PaymentData have to implement stackitem.Convertible interface to be 74 // compatible with emit package. 75 var _ = stackitem.Convertible(&OnNEP17PaymentData{}) 76 77 // Hash stores the hash of the native Notary contract. 78 var Hash = nativehashes.Notary 79 80 // NewReader creates an instance of ContractReader to get data from the Notary 81 // contract. 82 func NewReader(invoker ContractInvoker) *ContractReader { 83 return &ContractReader{invoker} 84 } 85 86 // New creates an instance of Contract to perform state-changing actions in the 87 // Notary contract. 88 func New(actor ContractActor) *Contract { 89 return &Contract{*NewReader(actor), actor} 90 } 91 92 // BalanceOf returns the locked GAS balance for the given account. 93 func (c *ContractReader) BalanceOf(account util.Uint160) (*big.Int, error) { 94 return unwrap.BigInt(c.invoker.Call(Hash, "balanceOf", account)) 95 } 96 97 // ExpirationOf returns the index of the block when the GAS deposit for the given 98 // account will expire. 99 func (c *ContractReader) ExpirationOf(account util.Uint160) (uint32, error) { 100 res, err := c.invoker.Call(Hash, "expirationOf", account) 101 ret, err := unwrap.LimitedInt64(res, err, 0, math.MaxUint32) 102 return uint32(ret), err 103 } 104 105 // GetMaxNotValidBeforeDelta returns the maximum NotValidBefore attribute delta 106 // that can be used in notary-assisted transactions. 107 func (c *ContractReader) GetMaxNotValidBeforeDelta() (uint32, error) { 108 res, err := c.invoker.Call(Hash, "getMaxNotValidBeforeDelta") 109 ret, err := unwrap.LimitedInt64(res, err, 0, math.MaxUint32) 110 return uint32(ret), err 111 } 112 113 // LockDepositUntil creates and sends a transaction that extends the deposit lock 114 // time for the given account. The return result from the "lockDepositUntil" 115 // method is checked to be true, so transaction fails (with FAULT state) if not 116 // successful. The returned values are transaction hash, its ValidUntilBlock 117 // value and an error if any. 118 func (c *Contract) LockDepositUntil(account util.Uint160, index uint32) (util.Uint256, uint32, error) { 119 return c.actor.SendRun(lockScript(account, index)) 120 } 121 122 // LockDepositUntilTransaction creates a transaction that extends the deposit lock 123 // time for the given account. The return result from the "lockDepositUntil" 124 // method is checked to be true, so transaction fails (with FAULT state) if not 125 // successful. The returned values are transaction hash, its ValidUntilBlock 126 // value and an error if any. The transaction is signed, but not sent to the 127 // network, instead it's returned to the caller. 128 func (c *Contract) LockDepositUntilTransaction(account util.Uint160, index uint32) (*transaction.Transaction, error) { 129 return c.actor.MakeRun(lockScript(account, index)) 130 } 131 132 // LockDepositUntilUnsigned creates a transaction that extends the deposit lock 133 // time for the given account. The return result from the "lockDepositUntil" 134 // method is checked to be true, so transaction fails (with FAULT state) if not 135 // successful. The returned values are transaction hash, its ValidUntilBlock 136 // value and an error if any. The transaction is not signed and just returned to 137 // the caller. 138 func (c *Contract) LockDepositUntilUnsigned(account util.Uint160, index uint32) (*transaction.Transaction, error) { 139 return c.actor.MakeUnsignedRun(lockScript(account, index), nil) 140 } 141 142 func lockScript(account util.Uint160, index uint32) []byte { 143 // We know parameters exactly (unlike with nep17.Transfer), so this can't fail. 144 script, _ := smartcontract.CreateCallWithAssertScript(Hash, "lockDepositUntil", account.BytesBE(), int64(index)) 145 return script 146 } 147 148 // SetMaxNotValidBeforeDelta creates and sends a transaction that sets the new 149 // maximum NotValidBefore attribute value delta that can be used in 150 // notary-assisted transactions. The action is successful when transaction 151 // ends in HALT state. Notice that this setting can be changed only by the 152 // network's committee, so use an appropriate Actor. The returned values are 153 // transaction hash, its ValidUntilBlock value and an error if any. 154 func (c *Contract) SetMaxNotValidBeforeDelta(blocks uint32) (util.Uint256, uint32, error) { 155 return c.actor.SendCall(Hash, setMaxNVBDeltaMethod, blocks) 156 } 157 158 // SetMaxNotValidBeforeDeltaTransaction creates a transaction that sets the new 159 // maximum NotValidBefore attribute value delta that can be used in 160 // notary-assisted transactions. The action is successful when transaction 161 // ends in HALT state. Notice that this setting can be changed only by the 162 // network's committee, so use an appropriate Actor. The transaction is signed, 163 // but not sent to the network, instead it's returned to the caller. 164 func (c *Contract) SetMaxNotValidBeforeDeltaTransaction(blocks uint32) (*transaction.Transaction, error) { 165 return c.actor.MakeCall(Hash, setMaxNVBDeltaMethod, blocks) 166 } 167 168 // SetMaxNotValidBeforeDeltaUnsigned creates a transaction that sets the new 169 // maximum NotValidBefore attribute value delta that can be used in 170 // notary-assisted transactions. The action is successful when transaction 171 // ends in HALT state. Notice that this setting can be changed only by the 172 // network's committee, so use an appropriate Actor. The transaction is not 173 // signed and just returned to the caller. 174 func (c *Contract) SetMaxNotValidBeforeDeltaUnsigned(blocks uint32) (*transaction.Transaction, error) { 175 return c.actor.MakeUnsignedCall(Hash, setMaxNVBDeltaMethod, nil, blocks) 176 } 177 178 // Withdraw creates and sends a transaction that withdraws the deposit belonging 179 // to "from" account and sends it to "to" account. The return result from the 180 // "withdraw" method is checked to be true, so transaction fails (with FAULT 181 // state) if not successful. The returned values are transaction hash, its 182 // ValidUntilBlock value and an error if any. 183 func (c *Contract) Withdraw(from util.Uint160, to util.Uint160) (util.Uint256, uint32, error) { 184 return c.actor.SendRun(withdrawScript(from, to)) 185 } 186 187 // WithdrawTransaction creates a transaction that withdraws the deposit belonging 188 // to "from" account and sends it to "to" account. The return result from the 189 // "withdraw" method is checked to be true, so transaction fails (with FAULT 190 // state) if not successful. The transaction is signed, but not sent to the 191 // network, instead it's returned to the caller. 192 func (c *Contract) WithdrawTransaction(from util.Uint160, to util.Uint160) (*transaction.Transaction, error) { 193 return c.actor.MakeRun(withdrawScript(from, to)) 194 } 195 196 // WithdrawUnsigned creates a transaction that withdraws the deposit belonging 197 // to "from" account and sends it to "to" account. The return result from the 198 // "withdraw" method is checked to be true, so transaction fails (with FAULT 199 // state) if not successful. The transaction is not signed and just returned to 200 // the caller. 201 func (c *Contract) WithdrawUnsigned(from util.Uint160, to util.Uint160) (*transaction.Transaction, error) { 202 return c.actor.MakeUnsignedRun(withdrawScript(from, to), nil) 203 } 204 205 func withdrawScript(from util.Uint160, to util.Uint160) []byte { 206 // We know parameters exactly (unlike with nep17.Transfer), so this can't fail. 207 script, _ := smartcontract.CreateCallWithAssertScript(Hash, "withdraw", from.BytesBE(), to.BytesBE()) 208 return script 209 } 210 211 // ToStackItem implements stackitem.Convertible interface. 212 func (d *OnNEP17PaymentData) ToStackItem() (stackitem.Item, error) { 213 return stackitem.NewArray([]stackitem.Item{ 214 stackitem.Make(d.Account), 215 stackitem.Make(d.Till), 216 }), nil 217 } 218 219 // FromStackItem implements stackitem.Convertible interface. 220 func (d *OnNEP17PaymentData) FromStackItem(si stackitem.Item) error { 221 arr, ok := si.Value().([]stackitem.Item) 222 if !ok { 223 return fmt.Errorf("unexpected stackitem type: %s", si.Type()) 224 } 225 if len(arr) != 2 { 226 return fmt.Errorf("unexpected number of fields: %d vs %d", len(arr), 2) 227 } 228 229 if arr[0] != stackitem.Item(stackitem.Null{}) { 230 accBytes, err := arr[0].TryBytes() 231 if err != nil { 232 return fmt.Errorf("failed to retrieve account bytes: %w", err) 233 } 234 acc, err := util.Uint160DecodeBytesBE(accBytes) 235 if err != nil { 236 return fmt.Errorf("failed to decode account bytes: %w", err) 237 } 238 d.Account = &acc 239 } 240 till, err := arr[1].TryInteger() 241 if err != nil { 242 return fmt.Errorf("failed to retrieve till: %w", err) 243 } 244 if !till.IsInt64() { 245 return errors.New("till is not an int64") 246 } 247 val := till.Int64() 248 if val > math.MaxUint32 { 249 return fmt.Errorf("till is larger than max uint32 value: %d", val) 250 } 251 d.Till = uint32(val) 252 253 return nil 254 }