github.com/0chain/gosdk@v1.17.11/zcnbridge/authorizer/proofBurnTicket.go (about) 1 package authorizer 2 3 import ( 4 "encoding/json" 5 "fmt" 6 7 "github.com/0chain/gosdk/zcncore" 8 9 "github.com/0chain/gosdk/core/zcncrypto" 10 11 "github.com/0chain/gosdk/zcnbridge" 12 13 "github.com/0chain/gosdk/core/encryption" 14 "github.com/0chain/gosdk/zcnbridge/errors" 15 ) 16 17 type ProofOfBurn struct { 18 TxnID string `json:"0chain_txn_id"` 19 Nonce int64 `json:"nonce"` 20 Amount int64 `json:"amount"` 21 EthereumAddress string `json:"ethereum_address"` 22 Signature []byte `json:"signature,omitempty"` 23 } 24 25 func (pb *ProofOfBurn) Encode() []byte { 26 return encryption.RawHash(pb) 27 } 28 29 func (pb *ProofOfBurn) Decode(input []byte) error { 30 return json.Unmarshal(input, pb) 31 } 32 33 func (pb *ProofOfBurn) Verify() (err error) { 34 switch { 35 case pb.TxnID == "": 36 err = errors.NewError("failed to verify proof of burn ticket", "0chain txn id is required") 37 case pb.Nonce == 0: 38 err = errors.NewError("failed to verify proof of burn ticket", "Nonce is required") 39 case pb.Amount == 0: 40 err = errors.NewError("failed to verify proof of burn ticket", "Amount is required") 41 case pb.EthereumAddress == "": 42 err = errors.NewError("failed to verify proof of burn ticket", "Receiving client id is required") 43 } 44 return 45 } 46 47 func (pb *ProofOfBurn) UnsignedMessage() string { 48 return fmt.Sprintf("%v:%v:%v:%v", pb.TxnID, pb.Amount, pb.Nonce, pb.EthereumAddress) 49 } 50 51 func (pb *ProofOfBurn) SignWithEthereum(b *zcnbridge.BridgeClient) (err error) { 52 sig, err := b.SignWithEthereumChain(pb.UnsignedMessage()) 53 if err != nil { 54 return errors.Wrap("signature_ethereum", "failed to sign proof-of-burn ticket", err) 55 } 56 pb.Signature = sig 57 58 return 59 } 60 61 // Sign can sign if chain config is initialized 62 func (pb *ProofOfBurn) Sign() (err error) { 63 hash := zcncrypto.Sha3Sum256(pb.UnsignedMessage()) 64 sig, err := zcncore.Sign(hash) 65 if err != nil { 66 return errors.Wrap("signature_0chain", "failed to sign proof-of-burn ticket using walletString ID ", err) 67 } 68 pb.Signature = []byte(sig) 69 70 return 71 } 72 73 // SignWith0Chain can sign with the provided walletString 74 func (pb *ProofOfBurn) SignWith0Chain(w *zcncrypto.Wallet) (err error) { 75 hash := zcncrypto.Sha3Sum256(pb.UnsignedMessage()) 76 sig, err := zcncore.SignWith0Wallet(hash, w) 77 if err != nil { 78 return errors.Wrap("signature_0chain", "failed to sign proof-of-burn ticket using walletString ID "+w.ClientID, err) 79 } 80 pb.Signature = []byte(sig) 81 82 return 83 }