package helpers import ( "bytes" "context" "crypto/ecdsa" "fmt" "log" "math/big" "math/rand" "testing" "time" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto" validator "github.com/metarare/metarare_api/contracts/Validator" "github.com/metarare/metarare_api/contracts/operator" ) var fromPrivateKey string = "a515318d067530ba1dfbebcf00ad549eaa90b0d334dd324cdf3d739cb5311840" var fromAddress string = "0xb0A4b81d9e31276C7Ec25F0eD1c25D85aCC831b3" var signerPrivateKey string = fromPrivateKey var signerAddress string = fromAddress var signerNotCollectionOwnerPrivateKey string = "a91e138b57ca7fb7560dd91e26bd18fcecf03feb4a912c84df4b380378a53231" var signerNotCollectionOwnerAddress string = "0xc4F0AD24c3Bdf17e39988ee2f8386e495B31bda3" var buyerPrivatekey string = "a515318d067530ba1dfbebcf00ad549eaa90b0d334dd324cdf3d739cb5311840" var buyerAddress string = "0x3bF06584120898E4aB801218379cF0f2b3Fd8E3A" var resellerPrivateKey string = buyerPrivatekey var resellerAddress string = buyerAddress var receiptorPrivateKey string = "0826109c35ce56ff35173251d13223dc2861e4f8a72408041206cd8ef131d1e9" var receiptorAddress string = "0xBD5CA15AFd02699bDf52235FBFD5eba65A5b8cB9" func Test_getBalance(t *testing.T) { mf1, mr, eth := GetBalance(buyerAddress) t.Logf("mf1(%f)\nmr(%f)\neth(%f)", mf1, mr, eth) } func Test_generateWallet(t *testing.T) { privKey, pubKey := GenerateWallet() t.Logf("privateKey(%s)\n address(%s)", privKey, pubKey) } func Test_gasPrice(t *testing.T) { _, _clientSet := UseContract() gasPrice, err := _clientSet.RpcClient.SuggestGasPrice(context.Background()) if err != nil { } t.Errorf("Estimated gas price:%s\n", gasPrice.String()) } func Test_ApproveMR(t *testing.T) { _amount := big.NewInt(300) _amount = _amount.Mul(_amount, Decimal) tx, spender, value, err := ApproveForERC20(MetaRare, fromPrivateKey, _amount) if err != nil { t.Error(err) return } t.Logf("\n\tApprove done, \n\t\ttxhash : %s\n\t\tspender : %s\n\t\tvalue : %s", tx.Hash().String(), spender, value) } func Test_ApproveMF1(t *testing.T) { _amount := big.NewInt(300) _amount = _amount.Mul(_amount, Decimal) tx, spender, value, err := ApproveForERC20(MetaFinance, fromPrivateKey, _amount) if err != nil { t.Error(err) return } t.Logf("\n\tApprove done, \n\t\ttxhash : %s\n\t\tspender : %s\n\t\tvalue : %s", tx.Hash().String(), spender, value) } func Test_Allowance(t *testing.T) { _contracts, _ := UseContract() ownerAddress := "0x3bF06584120898E4aB801218379cF0f2b3Fd8E3A" collectionAddress := Addr_Operator options := bind.CallOpts{} allowanceAmount, err := _contracts.MetaRare.Allowance(&options, common.HexToAddress(ownerAddress), common.HexToAddress(collectionAddress)) if err != nil { t.Error(err) return } t.Log("allowanceAmount :", allowanceAmount.String()) } func Test_AllowanceForOperator(t *testing.T) { address := "0xb0A4b81d9e31276C7Ec25F0eD1c25D85aCC831b3" mtraAmount, err := GetAllowance(MetaRare, address) if err != nil { t.Error(err) } mf1Amount, err := GetAllowance(MetaFinance, address) if err != nil { t.Error(err) } t.Logf("Allownacne MTRA(%s) / MF1(%s)", mtraAmount.String(), mf1Amount.String()) } func Test_TransferMetarer(t *testing.T) { _amount := big.NewInt(300) _amount = _amount.Mul(_amount, Decimal) err, tx := TransferMetaRare(fromPrivateKey, receiptorAddress, _amount) if err != nil { t.Error(err) } t.Logf("txhash : %s", string(tx.Hash().Hex())) } func Test_TransferFianace(t *testing.T) { _amount := big.NewInt(300) _amount = _amount.Mul(_amount, Decimal) err, tx := TransferMetaFinance(fromPrivateKey, receiptorAddress, _amount) if err != nil { t.Error(err) } t.Logf("txhash : %s", string(tx.Hash().Hex())) } func Test_TransferETH(t *testing.T) { _amount := big.NewInt(150) _amount = _amount.Mul(_amount, Decimal) err, tx := TransferETH(fromPrivateKey, buyerAddress, _amount) if err != nil { t.Error(err) } t.Logf("txhash : %s", string(tx.Hash().Hex())) } func BenchmarkSum(b *testing.B) { _amount := big.NewInt(300) _amount = _amount.Mul(_amount, Decimal) err, tx := TransferMetaRare(fromPrivateKey, receiptorAddress, _amount) if err != nil { b.Error(err) } b.Errorf("txhash : %s", string(tx.Hash().Hex())) } func Test_ExcuteOrderWith721MintingAndResale(t *testing.T) { rand.Seed(time.Now().Unix()) _v := operator.MetaRareOperatorNFTVoucher{ Signer: common.HexToAddress(signerAddress), TokenAddress: common.HexToAddress("0xe991f1cae62315fd6eb80c7e3e16f712b34fe913"), // ganache // TokenAddress: common.HexToAddress("0x9d882514a1bdF0cb8E23F3F45320F3E1f6e31231"), // rinkeby TokenType: big.NewInt(0), TokenId: big.NewInt(int64(rand.Intn(100000))), Price: big.NewInt(500000000000000000), // 0.05 eth Balance: big.NewInt(10), CreationTax: big.NewInt(1000), Uri: "nftinfo.metarare-net.com/721/", AssetAddress: common.HexToAddress("0x0000000000000000000000000000000000000000"), TotalBalance: big.NewInt(0), TreasuryTax: big.NewInt(1000), TreasuryAddress: common.HexToAddress("0x3A3d5A9395b5A8210008951aAe286A1169cD3A6C"), } fmt.Println(">>>>>>>>>>>>>>>>>>>>>> Singinig <<<<<<<<<<<<<<<<<<<<<<<<") signature, hashedData, err := Signing(_v, signerPrivateKey) if err != nil { t.Error(err) } var _hashedData [32]byte var _signature [65]byte copy(_hashedData[:], hashedData) copy(_signature[:], signature) fmt.Println(">>>>>>>>>>>>>>>>>>>>>> Order721 With Mint <<<<<<<<<<<<<<<<<<<<<<<<") tx, _, err := Order721WithMinting(_v, _hashedData, buyerPrivatekey, _signature, false) if err != nil { t.Error(err) } t.Logf("\tTokenInfo: TokenID(%d)", _v.TokenId) t.Logf("\tTxHash: %s", tx.Hash().String()) fmt.Println(">>>>>>>>>>>>>>>>>>>>>> 2nd Signing <<<<<<<<<<<<<<<<<<<<<<<<") //resale _v.Signer = common.HexToAddress(resellerAddress) signature, hashedData, err = Signing(_v, resellerPrivateKey) if err != nil { t.Error(err) } copy(_hashedData[:], hashedData) copy(_signature[:], signature) fmt.Println(">>>>>>>>>>>>>>>>>>>>>> Order721 Only transfer <<<<<<<<<<<<<<<<<<<<<<<<") tx, _, err = Order721OnlyTransfer(_v, _hashedData, receiptorPrivateKey, _signature) if err != nil { t.Error(err) } t.Logf("\tTokenInfo: TokenID(%d)", _v.TokenId) t.Logf("\tTxHash: %s", tx.Hash()) } func Test_ExcuteOrderWithGenenal721MintingAndResale(t *testing.T) { rand.Seed(time.Now().Unix()) _v := operator.MetaRareOperatorNFTVoucher{ Signer: common.HexToAddress(signerNotCollectionOwnerAddress), TokenAddress: common.HexToAddress("0x2cc3a50c023edfc9039b72e97bad31f2739c71c1"), // ganache TokenType: big.NewInt(0), TokenId: big.NewInt(int64(rand.Intn(100000))), Price: big.NewInt(500000000000000), // 0.05 eth Balance: big.NewInt(10), CreationTax: big.NewInt(1000), Uri: "nftinfo.metarare-net.com/721/", AssetAddress: common.HexToAddress("0x0000000000000000000000000000000000000000"), TotalBalance: big.NewInt(0), TreasuryTax: big.NewInt(1000), TreasuryAddress: common.HexToAddress("0x3A3d5A9395b5A8210008951aAe286A1169cD3A6C"), } fmt.Println(">>>>>>>>>>>>>>>>>>>>>> Singinig <<<<<<<<<<<<<<<<<<<<<<<<") signature, hashedData, err := Signing(_v, signerNotCollectionOwnerPrivateKey) if err != nil { t.Error(err) } var _hashedData [32]byte var _signature [65]byte copy(_hashedData[:], hashedData) copy(_signature[:], signature) fmt.Println(">>>>>>>>>>>>>>>>>>>>>> Order721 With Mint <<<<<<<<<<<<<<<<<<<<<<<<") tx, _, err := Order721WithMinting(_v, _hashedData, buyerPrivatekey, _signature, true) if err != nil { t.Error(err) return } t.Logf("\tTokenInfo: TokenID(%d)", _v.TokenId) t.Logf("\tTxHash: %s", tx.Hash().String()) fmt.Println(">>>>>>>>>>>>>>>>>>>>>> 2nd Signing <<<<<<<<<<<<<<<<<<<<<<<<") //resale _v.Signer = common.HexToAddress(resellerAddress) signature, hashedData, err = Signing(_v, resellerPrivateKey) if err != nil { t.Error(err) } copy(_hashedData[:], hashedData) copy(_signature[:], signature) fmt.Println(">>>>>>>>>>>>>>>>>>>>>> Order721 Only transfer <<<<<<<<<<<<<<<<<<<<<<<<") tx, _, err = Order721OnlyTransfer(_v, _hashedData, receiptorPrivateKey, _signature) if err != nil { t.Error(err) } t.Logf("\tTokenInfo: TokenID(%d)", _v.TokenId) t.Logf("\tTxHash: %s", tx.Hash()) } func Test_ExcuteOrderWith721MintingUsingMetaRare(t *testing.T) { UseContract() privateKey, err := crypto.HexToECDSA(signerPrivateKey) if err != nil { log.Fatal(err) } publicKey := privateKey.Public() publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey) if !ok { log.Fatal("error casting public key to ECDSA") } publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA) // Sign rand.Seed(time.Now().Unix()) _price := big.NewInt(10) _price = _price.Mul(_price, Decimal) _v := operator.MetaRareOperatorNFTVoucher{ Signer: common.HexToAddress(signerAddress), TokenAddress: common.HexToAddress("0xa1696e6ef9dd93d699990cce3142a482883bb320"), // ganache TokenType: big.NewInt(0), TokenId: big.NewInt(int64(rand.Intn(100000))), Price: _price, Balance: big.NewInt(10), CreationTax: big.NewInt(1000), Uri: "nftinfo.metarare-net.com/721/", AssetAddress: common.HexToAddress(Addr_metaRare), TotalBalance: big.NewInt(0), TreasuryTax: big.NewInt(1000), TreasuryAddress: common.HexToAddress("0x3A3d5A9395b5A8210008951aAe286A1169cD3A6C"), } signature, hashedData, err := Signing(_v, signerPrivateKey) if err != nil { t.Error(err) } // fmt.Printf("\nsignature :%s\n", hexutil.Encode(signature)) /////////////////////////////////////////////////////// verify signature sigPublicKey, err := crypto.Ecrecover(hashedData, signature) if err != nil { log.Fatal(err) } matches := bytes.Equal(sigPublicKey, publicKeyBytes) // fmt.Println("sigPublicKey:", sigPublicKey) // fmt.Println("publicKeyBytes:", publicKeyBytes) // fmt.Printf("verify result :%+v ", matches) // true if !matches { t.Errorf("match result %v:", matches) } ///////////////////////////////////////////////////////////// verify with smartcontract var _hashedData [32]byte var _signature [65]byte copy(_hashedData[:], hashedData) copy(_signature[:], signature) /* recovered_address, err := RecoverAddress(_hashedData, _signature) if err != nil { fmt.Print(err) } fmt.Println("recovered_address :", hexutil.Encode(recovered_address[:])) */ ///////////////////////////////////////////////////// end of verification tx, _, _, err := ApproveForERC20(MetaRare, buyerPrivatekey, _price) if err != nil { t.Error(err) return } copy(_hashedData[:], hashedData) copy(_signature[:], signature) tx, _, err = Order721WithMinting(_v, _hashedData, buyerPrivatekey, _signature, false) if err != nil { t.Error(err) return } t.Logf("\tTokenInfo: TokenID(%d)", _v.TokenId) t.Logf("\tTxHash: %s", tx.Hash()) } func Test_ExcuteOrderWith721Minting(t *testing.T) { privateKey, err := crypto.HexToECDSA(signerPrivateKey) if err != nil { log.Fatal(err) } publicKey := privateKey.Public() publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey) if !ok { log.Fatal("error casting public key to ECDSA") } publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA) // Sign rand.Seed(time.Now().Unix()) _v := operator.MetaRareOperatorNFTVoucher{ Signer: common.HexToAddress(signerAddress), TokenAddress: common.HexToAddress("0xe991f1cae62315fd6eb80c7e3e16f712b34fe913"), // ganache TokenType: big.NewInt(0), TokenId: big.NewInt(int64(rand.Intn(100000))), Price: big.NewInt(200000000000000000), // 0.05 eth Balance: big.NewInt(10), CreationTax: big.NewInt(1000), Uri: "nftinfo.metarare-net.com/721/", AssetAddress: common.HexToAddress("0x0000000000000000000000000000000000000000"), TotalBalance: big.NewInt(0), TreasuryTax: big.NewInt(1000), TreasuryAddress: common.HexToAddress("0x3A3d5A9395b5A8210008951aAe286A1169cD3A6C"), } signature, hashedData, err := Signing(_v, signerPrivateKey) if err != nil { t.Error(err) } // fmt.Printf("\nsignature :%s\n", hexutil.Encode(signature)) /////////////////////////////////////////////////////// verify signature sigPublicKey, err := crypto.Ecrecover(hashedData, signature) if err != nil { log.Fatal(err) } matches := bytes.Equal(sigPublicKey, publicKeyBytes) // fmt.Println("sigPublicKey:", sigPublicKey) // fmt.Println("publicKeyBytes:", publicKeyBytes) // fmt.Printf("verify result :%+v ", matches) // true if !matches { t.Errorf("match result %v:", matches) } ///////////////////////////////////////////////////////////// verify with smartcontract var _hashedData [32]byte var _signature [65]byte copy(_hashedData[:], hashedData) copy(_signature[:], signature) /* recovered_address, err := RecoverAddress(_hashedData, _signature) if err != nil { fmt.Print(err) } fmt.Println("recovered_address :", hexutil.Encode(recovered_address[:])) */ ///////////////////////////////////////////////////// end of verification copy(_hashedData[:], hashedData) copy(_signature[:], signature) tx, _, err := Order721WithMinting(_v, _hashedData, buyerPrivatekey, _signature, false) if err != nil { t.Error(err) } t.Logf("\tTokenInfo: TokenID(%d)", _v.TokenId) t.Logf("\tTxHash: %s", tx.Hash()) } func Test_ExcuteOrderWith1155MintingAndResale(t *testing.T) { rand.Seed(time.Now().Unix()) _v := operator.MetaRareOperatorNFTVoucher{ Signer: common.HexToAddress(signerAddress), TokenAddress: common.HexToAddress("0x22d5bfef4624bcba4c82cdc35355933f95c234a7"), TokenType: big.NewInt(0), TokenId: big.NewInt(int64(rand.Intn(100000))), Price: big.NewInt(500000000000000), // 0.05 eth Balance: big.NewInt(2), CreationTax: big.NewInt(1000), Uri: "nftinfo.metarare-net.com/1155/", AssetAddress: common.HexToAddress("0x0000000000000000000000000000000000000000"), TotalBalance: big.NewInt(10), TreasuryTax: big.NewInt(1000), TreasuryAddress: common.HexToAddress("0x3A3d5A9395b5A8210008951aAe286A1169cD3A6C"), } fmt.Println(">>>>>>>>>>>>>>>>>>>>>> Singinig <<<<<<<<<<<<<<<<<<<<<<<<") signature, hashedData, err := Signing(_v, signerPrivateKey) if err != nil { t.Error(err) } var _hashedData [32]byte var _signature [65]byte copy(_hashedData[:], hashedData) copy(_signature[:], signature) fmt.Println(">>>>>>>>>>>>>>>>>>>>>> Order1155 With Mint <<<<<<<<<<<<<<<<<<<<<<<<") tx, _, err := Order1155WithMinting(_v, _hashedData, buyerPrivatekey, _signature, false) if err != nil { t.Error(err) } t.Logf("\tTokenInfo: TokenID(%d)", _v.TokenId) t.Logf("\tTxHash: %s", tx.Hash().String()) fmt.Println(">>>>>>>>>>>>>>>>>>>>>> 2nd Signing <<<<<<<<<<<<<<<<<<<<<<<<") //resale _v.Signer = common.HexToAddress(resellerAddress) signature, hashedData, err = Signing(_v, resellerPrivateKey) if err != nil { t.Error(err) } copy(_hashedData[:], hashedData) copy(_signature[:], signature) fmt.Println(">>>>>>>>>>>>>>>>>>>>>> Order1155 Only transfer <<<<<<<<<<<<<<<<<<<<<<<<") tx, _, err = Order1155OnlyTransfer(_v, _hashedData, receiptorPrivateKey, _signature) if err != nil { t.Error(err) } t.Logf("\tTokenInfo: TokenID(%d)", _v.TokenId) t.Logf("\tTxHash: %s", tx.Hash()) } func Test_ExcuteOrderWithGenenal1155MintingAndResale(t *testing.T) { fmt.Print(` =========================================================================== =========================================================================== ===========This test for genenal collection ========= =========================================================================== `) rand.Seed(time.Now().Unix()) _v := operator.MetaRareOperatorNFTVoucher{ Signer: common.HexToAddress(signerNotCollectionOwnerAddress), TokenAddress: common.HexToAddress("0x22d5bfef4624bcba4c82cdc35355933f95c234a7"), TokenType: big.NewInt(0), TokenId: big.NewInt(int64(rand.Intn(100000))), Price: big.NewInt(500000000000000), // 0.05 eth Balance: big.NewInt(2), CreationTax: big.NewInt(1000), Uri: "nftinfo.metarare-net.com/1155/", AssetAddress: common.HexToAddress("0x0000000000000000000000000000000000000000"), TotalBalance: big.NewInt(10), TreasuryTax: big.NewInt(1000), TreasuryAddress: common.HexToAddress("0x3A3d5A9395b5A8210008951aAe286A1169cD3A6C"), } fmt.Println(">>>>>>>>>>>>>>>>>>>>>> Singinig <<<<<<<<<<<<<<<<<<<<<<<<") signature, hashedData, err := Signing(_v, signerNotCollectionOwnerPrivateKey) if err != nil { t.Error(err) } var _hashedData [32]byte var _signature [65]byte copy(_hashedData[:], hashedData) copy(_signature[:], signature) fmt.Println(">>>>>>>>>>>>>>>>>>>>>> Order1155 With Mint <<<<<<<<<<<<<<<<<<<<<<<<") tx, _, err := Order1155WithMinting(_v, _hashedData, buyerPrivatekey, _signature, true) if err != nil { t.Error(err) return } t.Logf("\tTokenInfo: TokenID(%d)", _v.TokenId) t.Logf("\tTxHash: %s", tx.Hash().String()) fmt.Println(">>>>>>>>>>>>>>>>>>>>>> 2nd Signing <<<<<<<<<<<<<<<<<<<<<<<<") //resale _v.Signer = common.HexToAddress(resellerAddress) signature, hashedData, err = Signing(_v, resellerPrivateKey) if err != nil { t.Error(err) } copy(_hashedData[:], hashedData) copy(_signature[:], signature) fmt.Println(">>>>>>>>>>>>>>>>>>>>>> Order1155 Only transfer <<<<<<<<<<<<<<<<<<<<<<<<") tx, _, err = Order1155OnlyTransfer(_v, _hashedData, receiptorPrivateKey, _signature) if err != nil { t.Error(err) } t.Logf("\tTokenInfo: TokenID(%d)", _v.TokenId) t.Logf("\tTxHash: %s", tx.Hash()) } func Test_ExcuteOrderWith1155Minting(t *testing.T) { privateKey, err := crypto.HexToECDSA(signerPrivateKey) if err != nil { log.Fatal(err) } publicKey := privateKey.Public() publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey) if !ok { log.Fatal("error casting public key to ECDSA") } publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA) // Sign rand.Seed(time.Now().Unix()) _v := operator.MetaRareOperatorNFTVoucher{ Signer: common.HexToAddress(signerAddress), TokenAddress: common.HexToAddress("0x22d5bfef4624bcba4c82cdc35355933f95c234a7"), // ganache TokenType: big.NewInt(0), TokenId: big.NewInt(int64(rand.Intn(100000))), Price: big.NewInt(500000000000000), // 0.05 eth Balance: big.NewInt(3), TotalBalance: big.NewInt(10), CreationTax: big.NewInt(1000), Uri: "nftinfo.metarare-net.com/1155/", AssetAddress: common.HexToAddress("0x0000000000000000000000000000000000000000"), TreasuryTax: big.NewInt(1000), TreasuryAddress: common.HexToAddress("0x3A3d5A9395b5A8210008951aAe286A1169cD3A6C"), } signature, hashedData, err := Signing(_v, signerPrivateKey) // _data := "this is a message for you" // signature, hashedData, err := SigningString(_data, signerPrivateKey) if err != nil { t.Error(err) } // fmt.Printf("signature :%s\n", hexutil.Encode(signature)) /////////////////////////////////////////////////////// verify signature sigPublicKey, err := crypto.Ecrecover(hashedData, signature) if err != nil { log.Fatal(err) } matches := bytes.Equal(sigPublicKey, publicKeyBytes) // fmt.Println("sigPublicKey:", sigPublicKey) // fmt.Println("publicKeyBytes:", publicKeyBytes) // fmt.Printf("verify result :%+v ", matches) // true if !matches { t.Errorf("match result %v:", matches) } ///////////////////////////////////////////////////////////// verify with smartcontract var _hashedData [32]byte var _signature [65]byte copy(_hashedData[:], hashedData) copy(_signature[:], signature) /* recovered_address, err := RecoverAddress(_hashedData, _signature) if err != nil { fmt.Print(err) } fmt.Println("recovered_address :", hexutil.Encode(recovered_address[:])) */ ///////////////////////////////////////////////////// end of verification copy(_hashedData[:], hashedData) copy(_signature[:], signature) tx, _, err := Order1155WithMinting(_v, _hashedData, buyerPrivatekey, _signature, false) if err != nil { t.Error(err) } t.Logf("\tTokenInfo: TokenID(%d)", _v.TokenId) t.Logf("\tTxHash: %s", tx.Hash()) } func Test_Create721Collection(t *testing.T) { info := EIP721{ NFTBasic: &NFTBasic{ Name: "Name721", Symbol: "Symbol721", }, BaseURI: "https://nftinfo.meta-rare.net/721", } err, resp := CreateCollection721(info, fromPrivateKey) if err != nil { t.Error(err) return } t.Log("\tcreated 721 collection address:", resp.CollectionAddress) t.Log("\ttxhash : ", string(resp.Tx.Hash().Hex())) } func Test_Create1155Collection(t *testing.T) { info := EIP1155{ NFTBasic: &NFTBasic{ Name: "Name1155", Symbol: "Symbol1155", }, CustomURI: "https://nftinfo.meta-rare.net/1155/", } err, resp := CreateCollection1155(info, fromPrivateKey) if err != nil { t.Error(err) return } t.Log("\tcreated 1155 collection address:", resp.CollectionAddress) t.Log("\ttxhash : ", string(resp.Tx.Hash().Hex())) } func Test_SignAndVerify(t *testing.T) { // make contract instance _rpc, _ := getClient() fromPrivateKey := "9f96a06224761b4d3c804111d86244559e1c93a16146a2b21b1b36feda091c87" _validator, _ := validator.NewValidator(common.HexToAddress("0x0E3Faa3C4b3224570Af8023050845990222cE1f6"), _rpc) // err, auth := makeAuth(_rpc, fromPrivateKey, "0x0E3Faa3C4b3224570Af8023050845990222cE1f6", nil) // if err != nil { // } //sign privKey, err := crypto.HexToECDSA(fromPrivateKey) publicKey := privKey.Public() publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey) if !ok { log.Fatal("error casting public key to ECDSA") } _address := crypto.PubkeyToAddress(*publicKeyECDSA).Hex() fmt.Println("address : ", _address) // signing _data := "this is a message for you" hashRaw := crypto.Keccak256([]byte(_data)) signature, _ := crypto.Sign(hashRaw, privKey) fmt.Println("signature:", hexutil.Encode(signature)) // transaction options := bind.CallOpts{} var hash32 [32]byte var r32 [32]byte var s32 [32]byte copy(hash32[:], hashRaw[:]) copy(r32[:], signature[:32]) copy(s32[:], signature[32:64]) fmt.Println("signature's length :", len(signature)) fmt.Println("R: ", signature[64]) decodeAddress, err := _validator.RecoverAddress(&options, hash32, signature[64]+27, r32, s32) if err != nil { t.Error(err) } fmt.Printf("decodedAddress :%s", decodeAddress) // t.Error(decodeAddress) }