github.com/iotexproject/iotex-core@v1.14.1-rc1/action/signedaction.go (about) 1 // Copyright (c) 2019 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package action 7 8 import ( 9 "encoding/hex" 10 "math/big" 11 12 "github.com/iotexproject/go-pkgs/crypto" 13 14 "github.com/pkg/errors" 15 ) 16 17 // vars 18 var ( 19 ValidSig, _ = hex.DecodeString("15e73ad521ec9e06600c59e49b127c9dee114ad64fb2fcbe5e0d9f4c8d2b766e73d708cca1dc050dd27b20f2ee607f30428bf035f45d4da8ec2fb04a90c2c30901") 20 ) 21 22 // SignedTransfer return a signed transfer 23 func SignedTransfer(recipientAddr string, senderPriKey crypto.PrivateKey, nonce uint64, amount *big.Int, payload []byte, gasLimit uint64, gasPrice *big.Int) (*SealedEnvelope, error) { 24 transfer, err := NewTransfer(nonce, amount, recipientAddr, payload, gasLimit, gasPrice) 25 if err != nil { 26 return nil, err 27 } 28 bd := &EnvelopeBuilder{} 29 elp := bd.SetNonce(nonce). 30 SetGasPrice(gasPrice). 31 SetGasLimit(gasLimit). 32 SetAction(transfer).Build() 33 selp, err := Sign(elp, senderPriKey) 34 if err != nil { 35 return nil, errors.Wrapf(err, "failed to sign transfer %v", elp) 36 } 37 return selp, nil 38 } 39 40 // SignedExecution return a signed execution 41 func SignedExecution(contractAddr string, executorPriKey crypto.PrivateKey, nonce uint64, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) (*SealedEnvelope, error) { 42 execution, err := NewExecution(contractAddr, nonce, amount, gasLimit, gasPrice, data) 43 if err != nil { 44 return nil, err 45 } 46 bd := &EnvelopeBuilder{} 47 elp := bd.SetNonce(nonce). 48 SetGasPrice(gasPrice). 49 SetGasLimit(gasLimit). 50 SetAction(execution).Build() 51 selp, err := Sign(elp, executorPriKey) 52 if err != nil { 53 return nil, errors.Wrapf(err, "failed to sign execution %v", elp) 54 } 55 return selp, nil 56 } 57 58 // SignedCandidateRegister returns a signed candidate register 59 func SignedCandidateRegister( 60 nonce uint64, 61 name, operatorAddrStr, rewardAddrStr, ownerAddrStr, amountStr string, 62 duration uint32, 63 autoStake bool, 64 payload []byte, 65 gasLimit uint64, 66 gasPrice *big.Int, 67 registererPriKey crypto.PrivateKey, 68 ) (*SealedEnvelope, error) { 69 cr, err := NewCandidateRegister(nonce, name, operatorAddrStr, rewardAddrStr, ownerAddrStr, amountStr, 70 duration, autoStake, payload, gasLimit, gasPrice) 71 if err != nil { 72 return nil, err 73 } 74 bd := &EnvelopeBuilder{} 75 elp := bd.SetNonce(nonce). 76 SetGasPrice(gasPrice). 77 SetGasLimit(gasLimit). 78 SetAction(cr).Build() 79 selp, err := Sign(elp, registererPriKey) 80 if err != nil { 81 return nil, errors.Wrapf(err, "failed to sign candidate register %v", elp) 82 } 83 return selp, nil 84 } 85 86 // SignedCandidateUpdate returns a signed candidate update 87 func SignedCandidateUpdate( 88 nonce uint64, 89 name, operatorAddrStr, rewardAddrStr string, 90 gasLimit uint64, 91 gasPrice *big.Int, 92 registererPriKey crypto.PrivateKey, 93 ) (*SealedEnvelope, error) { 94 cu, err := NewCandidateUpdate(nonce, name, operatorAddrStr, rewardAddrStr, gasLimit, gasPrice) 95 if err != nil { 96 return nil, err 97 } 98 bd := &EnvelopeBuilder{} 99 elp := bd.SetNonce(nonce). 100 SetGasPrice(gasPrice). 101 SetGasLimit(gasLimit). 102 SetAction(cu).Build() 103 selp, err := Sign(elp, registererPriKey) 104 if err != nil { 105 return nil, errors.Wrapf(err, "failed to sign candidate update %v", elp) 106 } 107 return selp, nil 108 } 109 110 // SignedCandidateActivate returns a signed candidate selfstake 111 func SignedCandidateActivate( 112 nonce uint64, 113 bucketIndex uint64, 114 gasLimit uint64, 115 gasPrice *big.Int, 116 registererPriKey crypto.PrivateKey, 117 ) (*SealedEnvelope, error) { 118 cu := NewCandidateActivate(nonce, gasLimit, gasPrice, bucketIndex) 119 bd := &EnvelopeBuilder{} 120 elp := bd.SetNonce(nonce). 121 SetGasPrice(gasPrice). 122 SetGasLimit(gasLimit). 123 SetAction(cu).Build() 124 selp, err := Sign(elp, registererPriKey) 125 if err != nil { 126 return nil, errors.Wrapf(err, "failed to sign candidate selfstake %v", elp) 127 } 128 return selp, nil 129 } 130 131 // SignedCandidateEndorsement returns a signed candidate endorsement 132 func SignedCandidateEndorsement( 133 nonce uint64, 134 bucketIndex uint64, 135 endorse bool, 136 gasLimit uint64, 137 gasPrice *big.Int, 138 registererPriKey crypto.PrivateKey, 139 ) (*SealedEnvelope, error) { 140 cu := NewCandidateEndorsement(nonce, gasLimit, gasPrice, bucketIndex, endorse) 141 bd := &EnvelopeBuilder{} 142 elp := bd.SetNonce(nonce). 143 SetGasPrice(gasPrice). 144 SetGasLimit(gasLimit). 145 SetAction(cu).Build() 146 selp, err := Sign(elp, registererPriKey) 147 if err != nil { 148 return nil, errors.Wrapf(err, "failed to sign candidate endorsement %v", elp) 149 } 150 return selp, nil 151 } 152 153 // SignedCreateStake returns a signed create stake 154 func SignedCreateStake(nonce uint64, 155 candidateName, amount string, 156 duration uint32, 157 autoStake bool, 158 payload []byte, 159 gasLimit uint64, 160 gasPrice *big.Int, 161 stakerPriKey crypto.PrivateKey, 162 ) (*SealedEnvelope, error) { 163 cs, err := NewCreateStake(nonce, candidateName, amount, duration, autoStake, 164 payload, gasLimit, gasPrice) 165 if err != nil { 166 return nil, err 167 } 168 bd := &EnvelopeBuilder{} 169 elp := bd.SetNonce(nonce). 170 SetGasPrice(gasPrice). 171 SetGasLimit(gasLimit). 172 SetAction(cs).Build() 173 selp, err := Sign(elp, stakerPriKey) 174 if err != nil { 175 return nil, errors.Wrapf(err, "failed to sign create stake %v", elp) 176 } 177 return selp, nil 178 } 179 180 // SignedReclaimStake returns a signed unstake or withdraw stake 181 func SignedReclaimStake( 182 withdraw bool, 183 nonce uint64, 184 bucketIndex uint64, 185 payload []byte, 186 gasLimit uint64, 187 gasPrice *big.Int, 188 reclaimerPriKey crypto.PrivateKey, 189 ) (*SealedEnvelope, error) { 190 bd := &EnvelopeBuilder{} 191 eb := bd.SetNonce(nonce). 192 SetGasPrice(gasPrice). 193 SetGasLimit(gasLimit) 194 var elp Envelope 195 // unstake 196 if !withdraw { 197 us, err := NewUnstake(nonce, bucketIndex, payload, gasLimit, gasPrice) 198 if err != nil { 199 return nil, err 200 } 201 elp = eb.SetAction(us).Build() 202 } else { 203 w, err := NewWithdrawStake(nonce, bucketIndex, payload, gasLimit, gasPrice) 204 if err != nil { 205 return nil, err 206 } 207 elp = eb.SetAction(w).Build() 208 } 209 selp, err := Sign(elp, reclaimerPriKey) 210 if err != nil { 211 return nil, errors.Wrapf(err, "failed to sign reclaim stake %v", elp) 212 } 213 return selp, nil 214 } 215 216 // SignedChangeCandidate returns a signed change candidate 217 func SignedChangeCandidate( 218 nonce uint64, 219 candName string, 220 bucketIndex uint64, 221 payload []byte, 222 gasLimit uint64, 223 gasPrice *big.Int, 224 stakerPriKey crypto.PrivateKey, 225 ) (*SealedEnvelope, error) { 226 cc, err := NewChangeCandidate(nonce, candName, bucketIndex, payload, gasLimit, gasPrice) 227 if err != nil { 228 return nil, err 229 } 230 bd := &EnvelopeBuilder{} 231 elp := bd.SetNonce(nonce). 232 SetGasPrice(gasPrice). 233 SetGasLimit(gasLimit). 234 SetAction(cc).Build() 235 selp, err := Sign(elp, stakerPriKey) 236 if err != nil { 237 return nil, errors.Wrapf(err, "failed to sign change candidate %v", elp) 238 } 239 return selp, nil 240 } 241 242 // SignedTransferStake returns a signed transfer stake 243 func SignedTransferStake( 244 nonce uint64, 245 voterAddress string, 246 bucketIndex uint64, 247 payload []byte, 248 gasLimit uint64, 249 gasPrice *big.Int, 250 stakerPriKey crypto.PrivateKey, 251 ) (*SealedEnvelope, error) { 252 ts, err := NewTransferStake(nonce, voterAddress, bucketIndex, payload, gasLimit, gasPrice) 253 if err != nil { 254 return nil, err 255 } 256 bd := &EnvelopeBuilder{} 257 elp := bd.SetNonce(nonce). 258 SetGasPrice(gasPrice). 259 SetGasLimit(gasLimit). 260 SetAction(ts).Build() 261 selp, err := Sign(elp, stakerPriKey) 262 if err != nil { 263 return nil, errors.Wrapf(err, "failed to sign transfer stake %v", elp) 264 } 265 return selp, nil 266 } 267 268 // SignedDepositToStake returns a signed deposit to stake 269 func SignedDepositToStake( 270 nonce uint64, 271 index uint64, 272 amount string, 273 payload []byte, 274 gasLimit uint64, 275 gasPrice *big.Int, 276 depositorPriKey crypto.PrivateKey, 277 ) (*SealedEnvelope, error) { 278 ds, err := NewDepositToStake(nonce, index, amount, payload, gasLimit, gasPrice) 279 if err != nil { 280 return nil, err 281 } 282 bd := &EnvelopeBuilder{} 283 elp := bd.SetNonce(nonce). 284 SetGasPrice(gasPrice). 285 SetGasLimit(gasLimit). 286 SetAction(ds).Build() 287 selp, err := Sign(elp, depositorPriKey) 288 if err != nil { 289 return nil, errors.Wrapf(err, "failed to sign deposit to stake %v", elp) 290 } 291 return selp, nil 292 } 293 294 // SignedRestake returns a signed restake 295 func SignedRestake( 296 nonce uint64, 297 index uint64, 298 duration uint32, 299 autoStake bool, 300 payload []byte, 301 gasLimit uint64, 302 gasPrice *big.Int, 303 restakerPriKey crypto.PrivateKey, 304 ) (*SealedEnvelope, error) { 305 rs, err := NewRestake(nonce, index, duration, autoStake, payload, gasLimit, gasPrice) 306 if err != nil { 307 return nil, err 308 } 309 bd := &EnvelopeBuilder{} 310 elp := bd.SetNonce(nonce). 311 SetGasPrice(gasPrice). 312 SetGasLimit(gasLimit). 313 SetAction(rs).Build() 314 selp, err := Sign(elp, restakerPriKey) 315 if err != nil { 316 return nil, errors.Wrapf(err, "failed to sign restake %v", elp) 317 } 318 return selp, nil 319 }