github.com/amazechain/amc@v0.1.3/contracts/deposit/NFT/NFT.go (about) 1 // Copyright 2023 The AmazeChain Authors 2 // This file is part of the AmazeChain library. 3 // 4 // The AmazeChain library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The AmazeChain library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package nftdeposit 18 19 import ( 20 "bytes" 21 "embed" 22 "github.com/amazechain/amc/accounts/abi" 23 "github.com/amazechain/amc/common/crypto" 24 "github.com/amazechain/amc/common/hexutil" 25 "github.com/amazechain/amc/common/types" 26 "github.com/amazechain/amc/log" 27 "github.com/holiman/uint256" 28 "github.com/pkg/errors" 29 "math/big" 30 ) 31 32 //go:embed abi.json 33 var abiJson embed.FS 34 var contractAbi abi.ABI 35 var depositSignature = crypto.Keccak256Hash([]byte("DepositEvent(bytes,uint256,bytes)")) 36 var withdrawnSignature = crypto.Keccak256Hash([]byte("WithdrawnEvent(uint256)")) 37 38 func init() { 39 var ( 40 depositAbiCode []byte 41 err error 42 ) 43 if depositAbiCode, err = abiJson.ReadFile("abi.json"); err != nil { 44 panic("Could not open abi.json") 45 } 46 47 if contractAbi, err = abi.JSON(bytes.NewReader(depositAbiCode)); err != nil { 48 panic("unable to parse AMT deposit contract abi") 49 } 50 } 51 52 type Contract struct { 53 } 54 55 func (c Contract) IsDepositAction(sigdata [4]byte) bool { 56 var ( 57 method *abi.Method 58 err error 59 ) 60 if method, err = contractAbi.MethodById(sigdata[:]); err != nil { 61 return false 62 } 63 64 if !bytes.Equal(method.ID, contractAbi.Methods["deposit"].ID) { 65 return false 66 } 67 return true 68 } 69 70 func (Contract) WithdrawnSignature() types.Hash { 71 return withdrawnSignature 72 } 73 74 func (Contract) DepositSignature() types.Hash { 75 return depositSignature 76 } 77 78 func (Contract) UnpackDepositLogData(data []byte) (publicKey []byte, signature []byte, depositAmount *uint256.Int, err error) { 79 var ( 80 unpackedLogs []interface{} 81 overflow bool 82 ) 83 // 84 if unpackedLogs, err = contractAbi.Unpack("DepositEvent", data); err != nil { 85 err = errors.Wrap(err, "unable to unpack logs") 86 return 87 } 88 // 89 if depositAmount, overflow = uint256.FromBig(unpackedLogs[1].(*big.Int)); overflow { 90 err = errors.New("unable to unpack amount") 91 return 92 } 93 publicKey, signature = unpackedLogs[0].([]byte), unpackedLogs[2].([]byte) 94 log.Debug("unpacked DepositEvent Logs", "publicKey", hexutil.Encode(unpackedLogs[0].([]byte)), "signature", hexutil.Encode(unpackedLogs[2].([]byte)), "message", hexutil.Encode(depositAmount.Bytes())) 95 return 96 }