eth_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. package helpers
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/ecdsa"
  6. "fmt"
  7. "log"
  8. "math/big"
  9. "math/rand"
  10. "testing"
  11. "time"
  12. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  13. "github.com/ethereum/go-ethereum/common"
  14. "github.com/ethereum/go-ethereum/common/hexutil"
  15. "github.com/ethereum/go-ethereum/crypto"
  16. validator "github.com/metarare/metarare_api/contracts/Validator"
  17. "github.com/metarare/metarare_api/contracts/operator"
  18. )
  19. var fromPrivateKey string = "a515318d067530ba1dfbebcf00ad549eaa90b0d334dd324cdf3d739cb5311840"
  20. var fromAddress string = "0xb0A4b81d9e31276C7Ec25F0eD1c25D85aCC831b3"
  21. var signerPrivateKey string = fromPrivateKey
  22. var signerAddress string = fromAddress
  23. var signerNotCollectionOwnerPrivateKey string = "a91e138b57ca7fb7560dd91e26bd18fcecf03feb4a912c84df4b380378a53231"
  24. var signerNotCollectionOwnerAddress string = "0xc4F0AD24c3Bdf17e39988ee2f8386e495B31bda3"
  25. var buyerPrivatekey string = "a515318d067530ba1dfbebcf00ad549eaa90b0d334dd324cdf3d739cb5311840"
  26. var buyerAddress string = "0x3bF06584120898E4aB801218379cF0f2b3Fd8E3A"
  27. var resellerPrivateKey string = buyerPrivatekey
  28. var resellerAddress string = buyerAddress
  29. var receiptorPrivateKey string = "0826109c35ce56ff35173251d13223dc2861e4f8a72408041206cd8ef131d1e9"
  30. var receiptorAddress string = "0xBD5CA15AFd02699bDf52235FBFD5eba65A5b8cB9"
  31. func Test_getBalance(t *testing.T) {
  32. mf1, mr, eth := GetBalance(buyerAddress)
  33. t.Logf("mf1(%f)\nmr(%f)\neth(%f)", mf1, mr, eth)
  34. }
  35. func Test_generateWallet(t *testing.T) {
  36. privKey, pubKey := GenerateWallet()
  37. t.Logf("privateKey(%s)\n address(%s)", privKey, pubKey)
  38. }
  39. func Test_gasPrice(t *testing.T) {
  40. _, _clientSet := UseContract()
  41. gasPrice, err := _clientSet.RpcClient.SuggestGasPrice(context.Background())
  42. if err != nil {
  43. }
  44. t.Errorf("Estimated gas price:%s\n", gasPrice.String())
  45. }
  46. func Test_ApproveMR(t *testing.T) {
  47. _amount := big.NewInt(300)
  48. _amount = _amount.Mul(_amount, Decimal)
  49. tx, spender, value, err := ApproveForERC20(MetaRare, fromPrivateKey, _amount)
  50. if err != nil {
  51. t.Error(err)
  52. return
  53. }
  54. t.Logf("\n\tApprove done, \n\t\ttxhash : %s\n\t\tspender : %s\n\t\tvalue : %s", tx.Hash().String(), spender, value)
  55. }
  56. func Test_ApproveMF1(t *testing.T) {
  57. _amount := big.NewInt(300)
  58. _amount = _amount.Mul(_amount, Decimal)
  59. tx, spender, value, err := ApproveForERC20(MetaFinance, fromPrivateKey, _amount)
  60. if err != nil {
  61. t.Error(err)
  62. return
  63. }
  64. t.Logf("\n\tApprove done, \n\t\ttxhash : %s\n\t\tspender : %s\n\t\tvalue : %s", tx.Hash().String(), spender, value)
  65. }
  66. func Test_Allowance(t *testing.T) {
  67. _contracts, _ := UseContract()
  68. ownerAddress := "0x3bF06584120898E4aB801218379cF0f2b3Fd8E3A"
  69. collectionAddress := Addr_Operator
  70. options := bind.CallOpts{}
  71. allowanceAmount, err := _contracts.MetaRare.Allowance(&options, common.HexToAddress(ownerAddress), common.HexToAddress(collectionAddress))
  72. if err != nil {
  73. t.Error(err)
  74. return
  75. }
  76. t.Log("allowanceAmount :", allowanceAmount.String())
  77. }
  78. func Test_AllowanceForOperator(t *testing.T) {
  79. address := "0xb0A4b81d9e31276C7Ec25F0eD1c25D85aCC831b3"
  80. mtraAmount, err := GetAllowance(MetaRare, address)
  81. if err != nil {
  82. t.Error(err)
  83. }
  84. mf1Amount, err := GetAllowance(MetaFinance, address)
  85. if err != nil {
  86. t.Error(err)
  87. }
  88. t.Logf("Allownacne MTRA(%s) / MF1(%s)", mtraAmount.String(), mf1Amount.String())
  89. }
  90. func Test_TransferMetarer(t *testing.T) {
  91. _amount := big.NewInt(300)
  92. _amount = _amount.Mul(_amount, Decimal)
  93. err, tx := TransferMetaRare(fromPrivateKey, receiptorAddress, _amount)
  94. if err != nil {
  95. t.Error(err)
  96. }
  97. t.Logf("txhash : %s", string(tx.Hash().Hex()))
  98. }
  99. func Test_TransferFianace(t *testing.T) {
  100. _amount := big.NewInt(300)
  101. _amount = _amount.Mul(_amount, Decimal)
  102. err, tx := TransferMetaFinance(fromPrivateKey, receiptorAddress, _amount)
  103. if err != nil {
  104. t.Error(err)
  105. }
  106. t.Logf("txhash : %s", string(tx.Hash().Hex()))
  107. }
  108. func Test_TransferETH(t *testing.T) {
  109. _amount := big.NewInt(150)
  110. _amount = _amount.Mul(_amount, Decimal)
  111. err, tx := TransferETH(fromPrivateKey, buyerAddress, _amount)
  112. if err != nil {
  113. t.Error(err)
  114. }
  115. t.Logf("txhash : %s", string(tx.Hash().Hex()))
  116. }
  117. func BenchmarkSum(b *testing.B) {
  118. _amount := big.NewInt(300)
  119. _amount = _amount.Mul(_amount, Decimal)
  120. err, tx := TransferMetaRare(fromPrivateKey, receiptorAddress, _amount)
  121. if err != nil {
  122. b.Error(err)
  123. }
  124. b.Errorf("txhash : %s", string(tx.Hash().Hex()))
  125. }
  126. func Test_ExcuteOrderWith721MintingAndResale(t *testing.T) {
  127. rand.Seed(time.Now().Unix())
  128. _v := operator.MetaRareOperatorNFTVoucher{
  129. Signer: common.HexToAddress(signerAddress),
  130. TokenAddress: common.HexToAddress("0xe991f1cae62315fd6eb80c7e3e16f712b34fe913"), // ganache
  131. // TokenAddress: common.HexToAddress("0x9d882514a1bdF0cb8E23F3F45320F3E1f6e31231"), // rinkeby
  132. TokenType: big.NewInt(0),
  133. TokenId: big.NewInt(int64(rand.Intn(100000))),
  134. Price: big.NewInt(500000000000000000), // 0.05 eth
  135. Balance: big.NewInt(10),
  136. CreationTax: big.NewInt(1000),
  137. Uri: "nftinfo.metarare-net.com/721/",
  138. AssetAddress: common.HexToAddress("0x0000000000000000000000000000000000000000"),
  139. TotalBalance: big.NewInt(0),
  140. TreasuryTax: big.NewInt(1000),
  141. TreasuryAddress: common.HexToAddress("0x3A3d5A9395b5A8210008951aAe286A1169cD3A6C"),
  142. }
  143. fmt.Println(">>>>>>>>>>>>>>>>>>>>>> Singinig <<<<<<<<<<<<<<<<<<<<<<<<")
  144. signature, hashedData, err := Signing(_v, signerPrivateKey)
  145. if err != nil {
  146. t.Error(err)
  147. }
  148. var _hashedData [32]byte
  149. var _signature [65]byte
  150. copy(_hashedData[:], hashedData)
  151. copy(_signature[:], signature)
  152. fmt.Println(">>>>>>>>>>>>>>>>>>>>>> Order721 With Mint <<<<<<<<<<<<<<<<<<<<<<<<")
  153. tx, _, err := Order721WithMinting(_v, _hashedData, buyerPrivatekey, _signature, false)
  154. if err != nil {
  155. t.Error(err)
  156. }
  157. t.Logf("\tTokenInfo: TokenID(%d)", _v.TokenId)
  158. t.Logf("\tTxHash: %s", tx.Hash().String())
  159. fmt.Println(">>>>>>>>>>>>>>>>>>>>>> 2nd Signing <<<<<<<<<<<<<<<<<<<<<<<<")
  160. //resale
  161. _v.Signer = common.HexToAddress(resellerAddress)
  162. signature, hashedData, err = Signing(_v, resellerPrivateKey)
  163. if err != nil {
  164. t.Error(err)
  165. }
  166. copy(_hashedData[:], hashedData)
  167. copy(_signature[:], signature)
  168. fmt.Println(">>>>>>>>>>>>>>>>>>>>>> Order721 Only transfer <<<<<<<<<<<<<<<<<<<<<<<<")
  169. tx, _, err = Order721OnlyTransfer(_v, _hashedData, receiptorPrivateKey, _signature)
  170. if err != nil {
  171. t.Error(err)
  172. }
  173. t.Logf("\tTokenInfo: TokenID(%d)", _v.TokenId)
  174. t.Logf("\tTxHash: %s", tx.Hash())
  175. }
  176. func Test_ExcuteOrderWithGenenal721MintingAndResale(t *testing.T) {
  177. rand.Seed(time.Now().Unix())
  178. _v := operator.MetaRareOperatorNFTVoucher{
  179. Signer: common.HexToAddress(signerNotCollectionOwnerAddress),
  180. TokenAddress: common.HexToAddress("0x2cc3a50c023edfc9039b72e97bad31f2739c71c1"), // ganache
  181. TokenType: big.NewInt(0),
  182. TokenId: big.NewInt(int64(rand.Intn(100000))),
  183. Price: big.NewInt(500000000000000), // 0.05 eth
  184. Balance: big.NewInt(10),
  185. CreationTax: big.NewInt(1000),
  186. Uri: "nftinfo.metarare-net.com/721/",
  187. AssetAddress: common.HexToAddress("0x0000000000000000000000000000000000000000"),
  188. TotalBalance: big.NewInt(0),
  189. TreasuryTax: big.NewInt(1000),
  190. TreasuryAddress: common.HexToAddress("0x3A3d5A9395b5A8210008951aAe286A1169cD3A6C"),
  191. }
  192. fmt.Println(">>>>>>>>>>>>>>>>>>>>>> Singinig <<<<<<<<<<<<<<<<<<<<<<<<")
  193. signature, hashedData, err := Signing(_v, signerNotCollectionOwnerPrivateKey)
  194. if err != nil {
  195. t.Error(err)
  196. }
  197. var _hashedData [32]byte
  198. var _signature [65]byte
  199. copy(_hashedData[:], hashedData)
  200. copy(_signature[:], signature)
  201. fmt.Println(">>>>>>>>>>>>>>>>>>>>>> Order721 With Mint <<<<<<<<<<<<<<<<<<<<<<<<")
  202. tx, _, err := Order721WithMinting(_v, _hashedData, buyerPrivatekey, _signature, true)
  203. if err != nil {
  204. t.Error(err)
  205. return
  206. }
  207. t.Logf("\tTokenInfo: TokenID(%d)", _v.TokenId)
  208. t.Logf("\tTxHash: %s", tx.Hash().String())
  209. fmt.Println(">>>>>>>>>>>>>>>>>>>>>> 2nd Signing <<<<<<<<<<<<<<<<<<<<<<<<")
  210. //resale
  211. _v.Signer = common.HexToAddress(resellerAddress)
  212. signature, hashedData, err = Signing(_v, resellerPrivateKey)
  213. if err != nil {
  214. t.Error(err)
  215. }
  216. copy(_hashedData[:], hashedData)
  217. copy(_signature[:], signature)
  218. fmt.Println(">>>>>>>>>>>>>>>>>>>>>> Order721 Only transfer <<<<<<<<<<<<<<<<<<<<<<<<")
  219. tx, _, err = Order721OnlyTransfer(_v, _hashedData, receiptorPrivateKey, _signature)
  220. if err != nil {
  221. t.Error(err)
  222. }
  223. t.Logf("\tTokenInfo: TokenID(%d)", _v.TokenId)
  224. t.Logf("\tTxHash: %s", tx.Hash())
  225. }
  226. func Test_ExcuteOrderWith721MintingUsingMetaRare(t *testing.T) {
  227. UseContract()
  228. privateKey, err := crypto.HexToECDSA(signerPrivateKey)
  229. if err != nil {
  230. log.Fatal(err)
  231. }
  232. publicKey := privateKey.Public()
  233. publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
  234. if !ok {
  235. log.Fatal("error casting public key to ECDSA")
  236. }
  237. publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
  238. // Sign
  239. rand.Seed(time.Now().Unix())
  240. _price := big.NewInt(10)
  241. _price = _price.Mul(_price, Decimal)
  242. _v := operator.MetaRareOperatorNFTVoucher{
  243. Signer: common.HexToAddress(signerAddress),
  244. TokenAddress: common.HexToAddress("0xa1696e6ef9dd93d699990cce3142a482883bb320"), // ganache
  245. TokenType: big.NewInt(0),
  246. TokenId: big.NewInt(int64(rand.Intn(100000))),
  247. Price: _price,
  248. Balance: big.NewInt(10),
  249. CreationTax: big.NewInt(1000),
  250. Uri: "nftinfo.metarare-net.com/721/",
  251. AssetAddress: common.HexToAddress(Addr_metaRare),
  252. TotalBalance: big.NewInt(0),
  253. TreasuryTax: big.NewInt(1000),
  254. TreasuryAddress: common.HexToAddress("0x3A3d5A9395b5A8210008951aAe286A1169cD3A6C"),
  255. }
  256. signature, hashedData, err := Signing(_v, signerPrivateKey)
  257. if err != nil {
  258. t.Error(err)
  259. }
  260. // fmt.Printf("\nsignature :%s\n", hexutil.Encode(signature))
  261. /////////////////////////////////////////////////////// verify signature
  262. sigPublicKey, err := crypto.Ecrecover(hashedData, signature)
  263. if err != nil {
  264. log.Fatal(err)
  265. }
  266. matches := bytes.Equal(sigPublicKey, publicKeyBytes)
  267. // fmt.Println("sigPublicKey:", sigPublicKey)
  268. // fmt.Println("publicKeyBytes:", publicKeyBytes)
  269. // fmt.Printf("verify result :%+v ", matches) // true
  270. if !matches {
  271. t.Errorf("match result %v:", matches)
  272. }
  273. ///////////////////////////////////////////////////////////// verify with smartcontract
  274. var _hashedData [32]byte
  275. var _signature [65]byte
  276. copy(_hashedData[:], hashedData)
  277. copy(_signature[:], signature)
  278. /*
  279. recovered_address, err := RecoverAddress(_hashedData, _signature)
  280. if err != nil {
  281. fmt.Print(err)
  282. }
  283. fmt.Println("recovered_address :", hexutil.Encode(recovered_address[:]))
  284. */
  285. ///////////////////////////////////////////////////// end of verification
  286. tx, _, _, err := ApproveForERC20(MetaRare, buyerPrivatekey, _price)
  287. if err != nil {
  288. t.Error(err)
  289. return
  290. }
  291. copy(_hashedData[:], hashedData)
  292. copy(_signature[:], signature)
  293. tx, _, err = Order721WithMinting(_v, _hashedData, buyerPrivatekey, _signature, false)
  294. if err != nil {
  295. t.Error(err)
  296. return
  297. }
  298. t.Logf("\tTokenInfo: TokenID(%d)", _v.TokenId)
  299. t.Logf("\tTxHash: %s", tx.Hash())
  300. }
  301. func Test_ExcuteOrderWith721Minting(t *testing.T) {
  302. privateKey, err := crypto.HexToECDSA(signerPrivateKey)
  303. if err != nil {
  304. log.Fatal(err)
  305. }
  306. publicKey := privateKey.Public()
  307. publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
  308. if !ok {
  309. log.Fatal("error casting public key to ECDSA")
  310. }
  311. publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
  312. // Sign
  313. rand.Seed(time.Now().Unix())
  314. _v := operator.MetaRareOperatorNFTVoucher{
  315. Signer: common.HexToAddress(signerAddress),
  316. TokenAddress: common.HexToAddress("0xe991f1cae62315fd6eb80c7e3e16f712b34fe913"), // ganache
  317. TokenType: big.NewInt(0),
  318. TokenId: big.NewInt(int64(rand.Intn(100000))),
  319. Price: big.NewInt(200000000000000000), // 0.05 eth
  320. Balance: big.NewInt(10),
  321. CreationTax: big.NewInt(1000),
  322. Uri: "nftinfo.metarare-net.com/721/",
  323. AssetAddress: common.HexToAddress("0x0000000000000000000000000000000000000000"),
  324. TotalBalance: big.NewInt(0),
  325. TreasuryTax: big.NewInt(1000),
  326. TreasuryAddress: common.HexToAddress("0x3A3d5A9395b5A8210008951aAe286A1169cD3A6C"),
  327. }
  328. signature, hashedData, err := Signing(_v, signerPrivateKey)
  329. if err != nil {
  330. t.Error(err)
  331. }
  332. // fmt.Printf("\nsignature :%s\n", hexutil.Encode(signature))
  333. /////////////////////////////////////////////////////// verify signature
  334. sigPublicKey, err := crypto.Ecrecover(hashedData, signature)
  335. if err != nil {
  336. log.Fatal(err)
  337. }
  338. matches := bytes.Equal(sigPublicKey, publicKeyBytes)
  339. // fmt.Println("sigPublicKey:", sigPublicKey)
  340. // fmt.Println("publicKeyBytes:", publicKeyBytes)
  341. // fmt.Printf("verify result :%+v ", matches) // true
  342. if !matches {
  343. t.Errorf("match result %v:", matches)
  344. }
  345. ///////////////////////////////////////////////////////////// verify with smartcontract
  346. var _hashedData [32]byte
  347. var _signature [65]byte
  348. copy(_hashedData[:], hashedData)
  349. copy(_signature[:], signature)
  350. /*
  351. recovered_address, err := RecoverAddress(_hashedData, _signature)
  352. if err != nil {
  353. fmt.Print(err)
  354. }
  355. fmt.Println("recovered_address :", hexutil.Encode(recovered_address[:]))
  356. */
  357. ///////////////////////////////////////////////////// end of verification
  358. copy(_hashedData[:], hashedData)
  359. copy(_signature[:], signature)
  360. tx, _, err := Order721WithMinting(_v, _hashedData, buyerPrivatekey, _signature, false)
  361. if err != nil {
  362. t.Error(err)
  363. }
  364. t.Logf("\tTokenInfo: TokenID(%d)", _v.TokenId)
  365. t.Logf("\tTxHash: %s", tx.Hash())
  366. }
  367. func Test_ExcuteOrderWith1155MintingAndResale(t *testing.T) {
  368. rand.Seed(time.Now().Unix())
  369. _v := operator.MetaRareOperatorNFTVoucher{
  370. Signer: common.HexToAddress(signerAddress),
  371. TokenAddress: common.HexToAddress("0x22d5bfef4624bcba4c82cdc35355933f95c234a7"),
  372. TokenType: big.NewInt(0),
  373. TokenId: big.NewInt(int64(rand.Intn(100000))),
  374. Price: big.NewInt(500000000000000), // 0.05 eth
  375. Balance: big.NewInt(2),
  376. CreationTax: big.NewInt(1000),
  377. Uri: "nftinfo.metarare-net.com/1155/",
  378. AssetAddress: common.HexToAddress("0x0000000000000000000000000000000000000000"),
  379. TotalBalance: big.NewInt(10),
  380. TreasuryTax: big.NewInt(1000),
  381. TreasuryAddress: common.HexToAddress("0x3A3d5A9395b5A8210008951aAe286A1169cD3A6C"),
  382. }
  383. fmt.Println(">>>>>>>>>>>>>>>>>>>>>> Singinig <<<<<<<<<<<<<<<<<<<<<<<<")
  384. signature, hashedData, err := Signing(_v, signerPrivateKey)
  385. if err != nil {
  386. t.Error(err)
  387. }
  388. var _hashedData [32]byte
  389. var _signature [65]byte
  390. copy(_hashedData[:], hashedData)
  391. copy(_signature[:], signature)
  392. fmt.Println(">>>>>>>>>>>>>>>>>>>>>> Order1155 With Mint <<<<<<<<<<<<<<<<<<<<<<<<")
  393. tx, _, err := Order1155WithMinting(_v, _hashedData, buyerPrivatekey, _signature, false)
  394. if err != nil {
  395. t.Error(err)
  396. }
  397. t.Logf("\tTokenInfo: TokenID(%d)", _v.TokenId)
  398. t.Logf("\tTxHash: %s", tx.Hash().String())
  399. fmt.Println(">>>>>>>>>>>>>>>>>>>>>> 2nd Signing <<<<<<<<<<<<<<<<<<<<<<<<")
  400. //resale
  401. _v.Signer = common.HexToAddress(resellerAddress)
  402. signature, hashedData, err = Signing(_v, resellerPrivateKey)
  403. if err != nil {
  404. t.Error(err)
  405. }
  406. copy(_hashedData[:], hashedData)
  407. copy(_signature[:], signature)
  408. fmt.Println(">>>>>>>>>>>>>>>>>>>>>> Order1155 Only transfer <<<<<<<<<<<<<<<<<<<<<<<<")
  409. tx, _, err = Order1155OnlyTransfer(_v, _hashedData, receiptorPrivateKey, _signature)
  410. if err != nil {
  411. t.Error(err)
  412. }
  413. t.Logf("\tTokenInfo: TokenID(%d)", _v.TokenId)
  414. t.Logf("\tTxHash: %s", tx.Hash())
  415. }
  416. func Test_ExcuteOrderWithGenenal1155MintingAndResale(t *testing.T) {
  417. fmt.Print(`
  418. ===========================================================================
  419. ===========================================================================
  420. ===========This test for genenal collection =========
  421. ===========================================================================
  422. `)
  423. rand.Seed(time.Now().Unix())
  424. _v := operator.MetaRareOperatorNFTVoucher{
  425. Signer: common.HexToAddress(signerNotCollectionOwnerAddress),
  426. TokenAddress: common.HexToAddress("0x22d5bfef4624bcba4c82cdc35355933f95c234a7"),
  427. TokenType: big.NewInt(0),
  428. TokenId: big.NewInt(int64(rand.Intn(100000))),
  429. Price: big.NewInt(500000000000000), // 0.05 eth
  430. Balance: big.NewInt(2),
  431. CreationTax: big.NewInt(1000),
  432. Uri: "nftinfo.metarare-net.com/1155/",
  433. AssetAddress: common.HexToAddress("0x0000000000000000000000000000000000000000"),
  434. TotalBalance: big.NewInt(10),
  435. TreasuryTax: big.NewInt(1000),
  436. TreasuryAddress: common.HexToAddress("0x3A3d5A9395b5A8210008951aAe286A1169cD3A6C"),
  437. }
  438. fmt.Println(">>>>>>>>>>>>>>>>>>>>>> Singinig <<<<<<<<<<<<<<<<<<<<<<<<")
  439. signature, hashedData, err := Signing(_v, signerNotCollectionOwnerPrivateKey)
  440. if err != nil {
  441. t.Error(err)
  442. }
  443. var _hashedData [32]byte
  444. var _signature [65]byte
  445. copy(_hashedData[:], hashedData)
  446. copy(_signature[:], signature)
  447. fmt.Println(">>>>>>>>>>>>>>>>>>>>>> Order1155 With Mint <<<<<<<<<<<<<<<<<<<<<<<<")
  448. tx, _, err := Order1155WithMinting(_v, _hashedData, buyerPrivatekey, _signature, true)
  449. if err != nil {
  450. t.Error(err)
  451. return
  452. }
  453. t.Logf("\tTokenInfo: TokenID(%d)", _v.TokenId)
  454. t.Logf("\tTxHash: %s", tx.Hash().String())
  455. fmt.Println(">>>>>>>>>>>>>>>>>>>>>> 2nd Signing <<<<<<<<<<<<<<<<<<<<<<<<")
  456. //resale
  457. _v.Signer = common.HexToAddress(resellerAddress)
  458. signature, hashedData, err = Signing(_v, resellerPrivateKey)
  459. if err != nil {
  460. t.Error(err)
  461. }
  462. copy(_hashedData[:], hashedData)
  463. copy(_signature[:], signature)
  464. fmt.Println(">>>>>>>>>>>>>>>>>>>>>> Order1155 Only transfer <<<<<<<<<<<<<<<<<<<<<<<<")
  465. tx, _, err = Order1155OnlyTransfer(_v, _hashedData, receiptorPrivateKey, _signature)
  466. if err != nil {
  467. t.Error(err)
  468. }
  469. t.Logf("\tTokenInfo: TokenID(%d)", _v.TokenId)
  470. t.Logf("\tTxHash: %s", tx.Hash())
  471. }
  472. func Test_ExcuteOrderWith1155Minting(t *testing.T) {
  473. privateKey, err := crypto.HexToECDSA(signerPrivateKey)
  474. if err != nil {
  475. log.Fatal(err)
  476. }
  477. publicKey := privateKey.Public()
  478. publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
  479. if !ok {
  480. log.Fatal("error casting public key to ECDSA")
  481. }
  482. publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
  483. // Sign
  484. rand.Seed(time.Now().Unix())
  485. _v := operator.MetaRareOperatorNFTVoucher{
  486. Signer: common.HexToAddress(signerAddress),
  487. TokenAddress: common.HexToAddress("0x22d5bfef4624bcba4c82cdc35355933f95c234a7"), // ganache
  488. TokenType: big.NewInt(0),
  489. TokenId: big.NewInt(int64(rand.Intn(100000))),
  490. Price: big.NewInt(500000000000000), // 0.05 eth
  491. Balance: big.NewInt(3),
  492. TotalBalance: big.NewInt(10),
  493. CreationTax: big.NewInt(1000),
  494. Uri: "nftinfo.metarare-net.com/1155/",
  495. AssetAddress: common.HexToAddress("0x0000000000000000000000000000000000000000"),
  496. TreasuryTax: big.NewInt(1000),
  497. TreasuryAddress: common.HexToAddress("0x3A3d5A9395b5A8210008951aAe286A1169cD3A6C"),
  498. }
  499. signature, hashedData, err := Signing(_v, signerPrivateKey)
  500. // _data := "this is a message for you"
  501. // signature, hashedData, err := SigningString(_data, signerPrivateKey)
  502. if err != nil {
  503. t.Error(err)
  504. }
  505. // fmt.Printf("signature :%s\n", hexutil.Encode(signature))
  506. /////////////////////////////////////////////////////// verify signature
  507. sigPublicKey, err := crypto.Ecrecover(hashedData, signature)
  508. if err != nil {
  509. log.Fatal(err)
  510. }
  511. matches := bytes.Equal(sigPublicKey, publicKeyBytes)
  512. // fmt.Println("sigPublicKey:", sigPublicKey)
  513. // fmt.Println("publicKeyBytes:", publicKeyBytes)
  514. // fmt.Printf("verify result :%+v ", matches) // true
  515. if !matches {
  516. t.Errorf("match result %v:", matches)
  517. }
  518. ///////////////////////////////////////////////////////////// verify with smartcontract
  519. var _hashedData [32]byte
  520. var _signature [65]byte
  521. copy(_hashedData[:], hashedData)
  522. copy(_signature[:], signature)
  523. /*
  524. recovered_address, err := RecoverAddress(_hashedData, _signature)
  525. if err != nil {
  526. fmt.Print(err)
  527. }
  528. fmt.Println("recovered_address :", hexutil.Encode(recovered_address[:]))
  529. */
  530. ///////////////////////////////////////////////////// end of verification
  531. copy(_hashedData[:], hashedData)
  532. copy(_signature[:], signature)
  533. tx, _, err := Order1155WithMinting(_v, _hashedData, buyerPrivatekey, _signature, false)
  534. if err != nil {
  535. t.Error(err)
  536. }
  537. t.Logf("\tTokenInfo: TokenID(%d)", _v.TokenId)
  538. t.Logf("\tTxHash: %s", tx.Hash())
  539. }
  540. func Test_Create721Collection(t *testing.T) {
  541. info := EIP721{
  542. NFTBasic: &NFTBasic{
  543. Name: "Name721",
  544. Symbol: "Symbol721",
  545. },
  546. BaseURI: "https://nftinfo.meta-rare.net/721",
  547. }
  548. err, resp := CreateCollection721(info, fromPrivateKey)
  549. if err != nil {
  550. t.Error(err)
  551. return
  552. }
  553. t.Log("\tcreated 721 collection address:", resp.CollectionAddress)
  554. t.Log("\ttxhash : ", string(resp.Tx.Hash().Hex()))
  555. }
  556. func Test_Create1155Collection(t *testing.T) {
  557. info := EIP1155{
  558. NFTBasic: &NFTBasic{
  559. Name: "Name1155",
  560. Symbol: "Symbol1155",
  561. },
  562. CustomURI: "https://nftinfo.meta-rare.net/1155/",
  563. }
  564. err, resp := CreateCollection1155(info, fromPrivateKey)
  565. if err != nil {
  566. t.Error(err)
  567. return
  568. }
  569. t.Log("\tcreated 1155 collection address:", resp.CollectionAddress)
  570. t.Log("\ttxhash : ", string(resp.Tx.Hash().Hex()))
  571. }
  572. func Test_SignAndVerify(t *testing.T) {
  573. // make contract instance
  574. _rpc, _ := getClient()
  575. fromPrivateKey := "9f96a06224761b4d3c804111d86244559e1c93a16146a2b21b1b36feda091c87"
  576. _validator, _ := validator.NewValidator(common.HexToAddress("0x0E3Faa3C4b3224570Af8023050845990222cE1f6"), _rpc)
  577. // err, auth := makeAuth(_rpc, fromPrivateKey, "0x0E3Faa3C4b3224570Af8023050845990222cE1f6", nil)
  578. // if err != nil {
  579. // }
  580. //sign
  581. privKey, err := crypto.HexToECDSA(fromPrivateKey)
  582. publicKey := privKey.Public()
  583. publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
  584. if !ok {
  585. log.Fatal("error casting public key to ECDSA")
  586. }
  587. _address := crypto.PubkeyToAddress(*publicKeyECDSA).Hex()
  588. fmt.Println("address : ", _address)
  589. // signing
  590. _data := "this is a message for you"
  591. hashRaw := crypto.Keccak256([]byte(_data))
  592. signature, _ := crypto.Sign(hashRaw, privKey)
  593. fmt.Println("signature:", hexutil.Encode(signature))
  594. // transaction
  595. options := bind.CallOpts{}
  596. var hash32 [32]byte
  597. var r32 [32]byte
  598. var s32 [32]byte
  599. copy(hash32[:], hashRaw[:])
  600. copy(r32[:], signature[:32])
  601. copy(s32[:], signature[32:64])
  602. fmt.Println("signature's length :", len(signature))
  603. fmt.Println("R: ", signature[64])
  604. decodeAddress, err := _validator.RecoverAddress(&options, hash32, signature[64]+27, r32, s32)
  605. if err != nil {
  606. t.Error(err)
  607. }
  608. fmt.Printf("decodedAddress :%s", decodeAddress)
  609. // t.Error(decodeAddress)
  610. }