nft.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. const jsonfile = require('jsonfile');
  2. const elliptic = require('elliptic');
  3. const BigNumber = require('bignumber.js');
  4. const Wormhole = artifacts.require("Wormhole");
  5. const NFTBridge = artifacts.require("NFTBridgeEntrypoint");
  6. const NFTBridgeImplementation = artifacts.require("NFTBridgeImplementation");
  7. const NFTImplementation = artifacts.require("NFTImplementation");
  8. const MockBridgeImplementation = artifacts.require("MockNFTBridgeImplementation");
  9. const testSigner1PK = "cfb12303a19cde580bb4dd771639b0d26bc68353645571a8cff516ab2ee113a0";
  10. const testSigner2PK = "892330666a850761e7370376430bb8c2aa1494072d3bfeaed0c4fa3d5a9135fe";
  11. const WormholeImplementationFullABI = jsonfile.readFileSync("build/contracts/Implementation.json").abi
  12. const BridgeImplementationFullABI = jsonfile.readFileSync("build/contracts/NFTBridgeImplementation.json").abi
  13. const NFTImplementationFullABI = jsonfile.readFileSync("build/contracts/NFTImplementation.json").abi
  14. contract("NFT", function () {
  15. const testSigner1 = web3.eth.accounts.privateKeyToAccount(testSigner1PK);
  16. const testSigner2 = web3.eth.accounts.privateKeyToAccount(testSigner2PK);
  17. const testChainId = "2";
  18. const testFinality = "1";
  19. const testGovernanceChainId = "1";
  20. const testGovernanceContract = "0x0000000000000000000000000000000000000000000000000000000000000004";
  21. let WETH = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
  22. const testForeignChainId = "1";
  23. const testForeignBridgeContract = "0x000000000000000000000000000000000000000000000000000000000000ffff";
  24. const testBridgedAssetChain = "0003";
  25. const testBridgedAssetAddress = "000000000000000000000000b7a2211e8165943192ad04f5dd21bedc29ff003e";
  26. it("should be initialized with the correct signers and values", async function () {
  27. const initialized = new web3.eth.Contract(BridgeImplementationFullABI, NFTBridge.address);
  28. const tokenImplentation = await initialized.methods.tokenImplementation().call();
  29. assert.equal(tokenImplentation, NFTImplementation.address);
  30. // test beacon functionality
  31. const beaconImplementation = await initialized.methods.implementation().call();
  32. assert.equal(beaconImplementation, NFTImplementation.address);
  33. // chain id
  34. const chainId = await initialized.methods.chainId().call();
  35. assert.equal(chainId, testChainId);
  36. // finality
  37. const finality = await initialized.methods.finality().call();
  38. assert.equal(finality, testFinality);
  39. // governance
  40. const governanceChainId = await initialized.methods.governanceChainId().call();
  41. assert.equal(governanceChainId, testGovernanceChainId);
  42. const governanceContract = await initialized.methods.governanceContract().call();
  43. assert.equal(governanceContract, testGovernanceContract);
  44. })
  45. it("should register a foreign bridge implementation correctly", async function () {
  46. const initialized = new web3.eth.Contract(BridgeImplementationFullABI, NFTBridge.address);
  47. const accounts = await web3.eth.getAccounts();
  48. let data = [
  49. "0x",
  50. "00000000000000000000000000000000000000000000004e4654427269646765",
  51. "01",
  52. "0000",
  53. web3.eth.abi.encodeParameter("uint16", testForeignChainId).substring(2 + (64 - 4)),
  54. web3.eth.abi.encodeParameter("bytes32", testForeignBridgeContract).substring(2),
  55. ].join('')
  56. const vm = await signAndEncodeVM(
  57. 1,
  58. 1,
  59. testGovernanceChainId,
  60. testGovernanceContract,
  61. 0,
  62. data,
  63. [
  64. testSigner1PK
  65. ],
  66. 0,
  67. 0
  68. );
  69. let before = await initialized.methods.bridgeContracts(testForeignChainId).call();
  70. assert.equal(before, "0x0000000000000000000000000000000000000000000000000000000000000000");
  71. await initialized.methods.registerChain("0x" + vm).send({
  72. value: 0,
  73. from: accounts[0],
  74. gasLimit: 2000000
  75. });
  76. let after = await initialized.methods.bridgeContracts(testForeignChainId).call();
  77. assert.equal(after, testForeignBridgeContract);
  78. })
  79. it("should accept a valid upgrade", async function () {
  80. const initialized = new web3.eth.Contract(BridgeImplementationFullABI, NFTBridge.address);
  81. const accounts = await web3.eth.getAccounts();
  82. const mock = await MockBridgeImplementation.new();
  83. let data = [
  84. "0x",
  85. "00000000000000000000000000000000000000000000004e4654427269646765",
  86. "02",
  87. web3.eth.abi.encodeParameter("uint16", testChainId).substring(2 + (64 - 4)),
  88. web3.eth.abi.encodeParameter("address", mock.address).substring(2),
  89. ].join('')
  90. const vm = await signAndEncodeVM(
  91. 1,
  92. 1,
  93. testGovernanceChainId,
  94. testGovernanceContract,
  95. 0,
  96. data,
  97. [
  98. testSigner1PK
  99. ],
  100. 0,
  101. 0
  102. );
  103. let before = await web3.eth.getStorageAt(NFTBridge.address, "0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc");
  104. assert.equal(before.toLowerCase(), NFTBridgeImplementation.address.toLowerCase());
  105. await initialized.methods.upgrade("0x" + vm).send({
  106. value: 0,
  107. from: accounts[0],
  108. gasLimit: 2000000
  109. });
  110. let after = await web3.eth.getStorageAt(NFTBridge.address, "0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc");
  111. assert.equal(after.toLowerCase(), mock.address.toLowerCase());
  112. const mockImpl = new web3.eth.Contract(MockBridgeImplementation.abi, NFTBridge.address);
  113. let isUpgraded = await mockImpl.methods.testNewImplementationActive().call();
  114. assert.ok(isUpgraded);
  115. })
  116. it("bridged tokens should only be mint- and burn-able by owner", async function () {
  117. const accounts = await web3.eth.getAccounts();
  118. // initialize our template token contract
  119. const token = new web3.eth.Contract(NFTImplementation.abi, NFTImplementation.address);
  120. await token.methods.initialize(
  121. "TestToken",
  122. "TT",
  123. accounts[0],
  124. 0,
  125. "0x0"
  126. ).send({
  127. value: 0,
  128. from: accounts[0],
  129. gasLimit: 2000000
  130. });
  131. await token.methods.mint(accounts[0], 10, "").send({
  132. from: accounts[0],
  133. gasLimit: 2000000
  134. });
  135. let failed = false
  136. try {
  137. await token.methods.mint(accounts[0], 11, "").send({
  138. from: accounts[1],
  139. gasLimit: 2000000
  140. });
  141. } catch (e) {
  142. failed = true
  143. }
  144. assert.ok(failed)
  145. failed = false
  146. try {
  147. await token.methods.burn(10).send({
  148. from: accounts[1],
  149. gasLimit: 2000000
  150. });
  151. } catch (e) {
  152. failed = true
  153. }
  154. assert.ok(failed)
  155. await token.methods.burn(10).send({
  156. from: accounts[0],
  157. gasLimit: 2000000
  158. });
  159. })
  160. it("should deposit and log transfers correctly", async function () {
  161. const accounts = await web3.eth.getAccounts();
  162. const tokenId = "1000000000000000000";
  163. // mint and approve tokens
  164. const token = new web3.eth.Contract(NFTImplementation.abi, NFTImplementation.address);
  165. await token.methods.mint(accounts[0], tokenId, "abcd").send({
  166. value: 0,
  167. from: accounts[0],
  168. gasLimit: 2000000
  169. });
  170. await token.methods.approve(NFTBridge.address, tokenId).send({
  171. value: 0,
  172. from: accounts[0],
  173. gasLimit: 2000000
  174. });
  175. // deposit tokens
  176. const initialized = new web3.eth.Contract(BridgeImplementationFullABI, NFTBridge.address);
  177. const ownerBefore = await token.methods.ownerOf(tokenId).call();
  178. assert.equal(ownerBefore, accounts[0]);
  179. await initialized.methods.transferNFT(
  180. NFTImplementation.address,
  181. tokenId,
  182. "10",
  183. "0x000000000000000000000000b7a2211e8165943192ad04f5dd21bedc29ff003e",
  184. "234"
  185. ).send({
  186. value: 0,
  187. from: accounts[0],
  188. gasLimit: 2000000
  189. });
  190. const ownerAfter = await token.methods.ownerOf(tokenId).call();
  191. assert.equal(ownerAfter, NFTBridge.address);
  192. // check transfer log
  193. const wormhole = new web3.eth.Contract(WormholeImplementationFullABI, Wormhole.address);
  194. const log = (await wormhole.getPastEvents('LogMessagePublished', {
  195. fromBlock: 'latest'
  196. }))[0].returnValues
  197. assert.equal(log.sender, NFTBridge.address)
  198. assert.equal(log.payload.length - 2, 340);
  199. // payload id
  200. assert.equal(log.payload.substr(2, 2), "01");
  201. // token
  202. assert.equal(log.payload.substr(4, 64), web3.eth.abi.encodeParameter("address", NFTImplementation.address).substring(2));
  203. // chain id
  204. assert.equal(log.payload.substr(68, 4), web3.eth.abi.encodeParameter("uint16", testChainId).substring(2 + 64 - 4))
  205. // symbol (TT)
  206. assert.equal(log.payload.substr(72, 64), "5454000000000000000000000000000000000000000000000000000000000000")
  207. // name (TestToken (Wormhole))
  208. assert.equal(log.payload.substr(136, 64), "54657374546f6b656e0000000000000000000000000000000000000000000000")
  209. // tokenID
  210. assert.equal(log.payload.substr(200, 64), web3.eth.abi.encodeParameter("uint256", new BigNumber(tokenId).toString()).substring(2));
  211. // url length
  212. assert.equal(log.payload.substr(264, 2), web3.eth.abi.encodeParameter("uint8", 4).substring(2 + 64 - 2))
  213. // url
  214. assert.equal(log.payload.substr(266, 8), "61626364")
  215. // to
  216. assert.equal(log.payload.substr(274, 64), "000000000000000000000000b7a2211e8165943192ad04f5dd21bedc29ff003e");
  217. // to chain id
  218. assert.equal(log.payload.substr(338, 4), web3.eth.abi.encodeParameter("uint16", 10).substring(2 + 64 - 4))
  219. })
  220. it("should transfer out locked assets for a valid transfer vm", async function () {
  221. const accounts = await web3.eth.getAccounts();
  222. const tokenId = "1000000000000000000";
  223. const token = new web3.eth.Contract(NFTImplementation.abi, NFTImplementation.address);
  224. const initialized = new web3.eth.Contract(BridgeImplementationFullABI, NFTBridge.address);
  225. const ownerBefore = await token.methods.ownerOf(tokenId).call();
  226. assert.equal(ownerBefore, NFTBridge.address);
  227. // PayloadID uint8 = 1
  228. // // Address of the NFT. Left-zero-padded if shorter than 32 bytes
  229. // NFTAddress [32]uint8
  230. // // Chain ID of the NFT
  231. // NFTChain uint16
  232. // // Name of the NFT
  233. // Name [32]uint8
  234. // // Symbol of the NFT
  235. // Symbol [10]uint8
  236. // // ID of the token (big-endian uint256)
  237. // TokenID [32]uint8
  238. // // URL of the NFT
  239. // URLLength u8
  240. // URL [n]uint8
  241. // // Address of the recipient. Left-zero-padded if shorter than 32 bytes
  242. // To [32]uint8
  243. // // Chain ID of the recipient
  244. // ToChain uint16
  245. const data = "0x" +
  246. "01" +
  247. // tokenaddress
  248. web3.eth.abi.encodeParameter("address", NFTImplementation.address).substr(2) +
  249. // tokenchain
  250. web3.eth.abi.encodeParameter("uint16", testChainId).substring(2 + (64 - 4)) +
  251. // symbol
  252. "0000000000000000000000000000000000000000000000000000000000000000" +
  253. // name
  254. "0000000000000000000000000000000000000000000000000000000000000000" +
  255. // tokenID
  256. web3.eth.abi.encodeParameter("uint256", new BigNumber(tokenId).toString()).substring(2) +
  257. // url length
  258. "00" +
  259. // no URL
  260. "" +
  261. // receiver
  262. web3.eth.abi.encodeParameter("address", accounts[0]).substr(2) +
  263. // receiving chain
  264. web3.eth.abi.encodeParameter("uint16", testChainId).substring(2 + (64 - 4));
  265. const vm = await signAndEncodeVM(
  266. 0,
  267. 0,
  268. testForeignChainId,
  269. testForeignBridgeContract,
  270. 0,
  271. data,
  272. [
  273. testSigner1PK
  274. ],
  275. 0,
  276. 0
  277. );
  278. await initialized.methods.completeTransfer("0x" + vm).send({
  279. value: 0,
  280. from: accounts[0],
  281. gasLimit: 2000000
  282. });
  283. const ownerAfter = await token.methods.ownerOf(tokenId).call();
  284. assert.equal(ownerAfter, accounts[0]);
  285. })
  286. it("should mint bridged assets wrappers on transfer from another chain and handle fees correctly", async function () {
  287. const accounts = await web3.eth.getAccounts();
  288. let tokenId = "1000000000000000001";
  289. const initialized = new web3.eth.Contract(BridgeImplementationFullABI, NFTBridge.address);
  290. // we are using the asset where we created a wrapper in the previous test
  291. let data = "0x" +
  292. "01" +
  293. // tokenaddress
  294. testBridgedAssetAddress +
  295. // tokenchain
  296. testBridgedAssetChain +
  297. // symbol
  298. "464f520000000000000000000000000000000000000000000000000000000000" +
  299. // name
  300. "466f726569676e20436861696e204e4654000000000000000000000000000000" +
  301. // tokenID
  302. web3.eth.abi.encodeParameter("uint256", new BigNumber(tokenId).toString()).substring(2) +
  303. // url length
  304. "00" +
  305. // no URL
  306. "" +
  307. // receiver
  308. web3.eth.abi.encodeParameter("address", accounts[0]).substr(2) +
  309. // receiving chain
  310. web3.eth.abi.encodeParameter("uint16", testChainId).substring(2 + (64 - 4));
  311. let vm = await signAndEncodeVM(
  312. 0,
  313. 0,
  314. testForeignChainId,
  315. testForeignBridgeContract,
  316. 0,
  317. data,
  318. [
  319. testSigner1PK
  320. ],
  321. 0,
  322. 0
  323. );
  324. await initialized.methods.completeTransfer("0x" + vm).send({
  325. value: 0,
  326. from: accounts[1],
  327. gasLimit: 2000000
  328. });
  329. const wrappedAddress = await initialized.methods.wrappedAsset("0x" + testBridgedAssetChain, "0x" + testBridgedAssetAddress).call();
  330. assert.ok(await initialized.methods.isWrappedAsset(wrappedAddress).call())
  331. const wrappedAsset = new web3.eth.Contract(NFTImplementation.abi, wrappedAddress);
  332. let ownerAfter = await wrappedAsset.methods.ownerOf(tokenId).call();
  333. assert.equal(ownerAfter, accounts[0]);
  334. const symbol = await wrappedAsset.methods.symbol().call();
  335. assert.equal(symbol, "FOR");
  336. const name = await wrappedAsset.methods.name().call();
  337. assert.equal(name, "Foreign Chain NFT");
  338. const chainId = await wrappedAsset.methods.chainId().call();
  339. assert.equal(chainId, Number(testBridgedAssetChain));
  340. const nativeContract = await wrappedAsset.methods.nativeContract().call();
  341. assert.equal(nativeContract, "0x000000000000000000000000b7a2211e8165943192ad04f5dd21bedc29ff003e");
  342. // Transfer another tokenID of the same token address
  343. tokenId = "1000000000000000002"
  344. data = "0x" +
  345. "01" +
  346. // tokenaddress
  347. testBridgedAssetAddress +
  348. // tokenchain
  349. testBridgedAssetChain +
  350. // symbol
  351. "464f520000000000000000000000000000000000000000000000000000000000" +
  352. // name
  353. "466f726569676e20436861696e204e4654000000000000000000000000000000" +
  354. // tokenID
  355. web3.eth.abi.encodeParameter("uint256", new BigNumber(tokenId + 1).toString()).substring(2) +
  356. // url length
  357. "00" +
  358. // no URL
  359. "" +
  360. // receiver
  361. web3.eth.abi.encodeParameter("address", accounts[0]).substr(2) +
  362. // receiving chain
  363. web3.eth.abi.encodeParameter("uint16", testChainId).substring(2 + (64 - 4));
  364. vm = await signAndEncodeVM(
  365. 0,
  366. 0,
  367. testForeignChainId,
  368. testForeignBridgeContract,
  369. 1,
  370. data,
  371. [
  372. testSigner1PK
  373. ],
  374. 0,
  375. 0
  376. );
  377. await initialized.methods.completeTransfer("0x" + vm).send({
  378. value: 0,
  379. from: accounts[1],
  380. gasLimit: 2000000
  381. });
  382. ownerAfter = await wrappedAsset.methods.ownerOf(tokenId + 1).call();
  383. assert.equal(ownerAfter, accounts[0]);
  384. })
  385. it("should mint bridged assets from solana under unified name, caching the original", async function () {
  386. const accounts = await web3.eth.getAccounts();
  387. let tokenId = "1000000000000000001";
  388. const initialized = new web3.eth.Contract(BridgeImplementationFullABI, NFTBridge.address);
  389. // we are using the asset where we created a wrapper in the previous test
  390. let data = "0x" +
  391. "01" +
  392. // tokenaddress
  393. testBridgedAssetAddress +
  394. // tokenchain
  395. "0001" +
  396. // symbol
  397. "464f520000000000000000000000000000000000000000000000000000000000" +
  398. // name
  399. "466f726569676e20436861696e204e4654000000000000000000000000000000" +
  400. // tokenID
  401. web3.eth.abi.encodeParameter("uint256", new BigNumber(tokenId).toString()).substring(2) +
  402. // url length
  403. "00" +
  404. // no URL
  405. "" +
  406. // receiver
  407. web3.eth.abi.encodeParameter("address", accounts[0]).substr(2) +
  408. // receiving chain
  409. web3.eth.abi.encodeParameter("uint16", testChainId).substring(2 + (64 - 4));
  410. let vm = await signAndEncodeVM(
  411. 0,
  412. 0,
  413. testForeignChainId,
  414. testForeignBridgeContract,
  415. 0,
  416. data,
  417. [
  418. testSigner1PK
  419. ],
  420. 0,
  421. 0
  422. );
  423. await initialized.methods.completeTransfer("0x" + vm).send({
  424. value: 0,
  425. from: accounts[1],
  426. gasLimit: 2000000
  427. });
  428. const cache = await initialized.methods.splCache(tokenId).call()
  429. assert.equal(cache.symbol, "0x464f520000000000000000000000000000000000000000000000000000000000");
  430. assert.equal(cache.name, "0x466f726569676e20436861696e204e4654000000000000000000000000000000");
  431. const wrappedAddress = await initialized.methods.wrappedAsset("0x0001", "0x" + testBridgedAssetAddress).call();
  432. const wrappedAsset = new web3.eth.Contract(NFTImplementation.abi, wrappedAddress);
  433. const symbol = await wrappedAsset.methods.symbol().call();
  434. assert.equal(symbol, "WORMSPLNFT");
  435. const name = await wrappedAsset.methods.name().call();
  436. assert.equal(name, "Wormhole Bridged Solana-NFT");
  437. })
  438. it("cached SPL names are loaded when transferring out, cache is cleared", async function () {
  439. const accounts = await web3.eth.getAccounts();
  440. let tokenId = "1000000000000000001";
  441. const initialized = new web3.eth.Contract(BridgeImplementationFullABI, NFTBridge.address);
  442. const wrappedAddress = await initialized.methods.wrappedAsset("0x0001", "0x" + testBridgedAssetAddress).call();
  443. const wrappedAsset = new web3.eth.Contract(NFTImplementation.abi, wrappedAddress);
  444. await wrappedAsset.methods.approve(NFTBridge.address, tokenId).send({
  445. value: 0,
  446. from: accounts[0],
  447. gasLimit: 2000000
  448. });
  449. const transfer = await initialized.methods.transferNFT(
  450. wrappedAddress,
  451. tokenId,
  452. "10",
  453. "0x000000000000000000000000b7a2211e8165943192ad04f5dd21bedc29ff003e",
  454. "2345"
  455. ).send({
  456. value: 0,
  457. from: accounts[0],
  458. gasLimit: 2000000
  459. });
  460. // symbol
  461. assert.ok(transfer.events[4].raw.data.includes('464f520000000000000000000000000000000000000000000000000000000000'))
  462. // name
  463. assert.ok(transfer.events[4].raw.data.includes('466f726569676e20436861696e204e4654000000000000000000000000000000'))
  464. // check if cache is cleared
  465. const cache = await initialized.methods.splCache(tokenId).call()
  466. assert.equal(cache.symbol, "0x0000000000000000000000000000000000000000000000000000000000000000");
  467. assert.equal(cache.name, "0x0000000000000000000000000000000000000000000000000000000000000000");
  468. })
  469. it("should should fail deposit unapproved NFTs", async function () {
  470. const accounts = await web3.eth.getAccounts();
  471. const initialized = new web3.eth.Contract(BridgeImplementationFullABI, NFTBridge.address);
  472. const tokenId = "1000000000000000001";
  473. const wrappedAddress = await initialized.methods.wrappedAsset("0x" + testBridgedAssetChain, "0x" + testBridgedAssetAddress).call();
  474. // deposit tokens
  475. let failed = false
  476. try {
  477. await initialized.methods.transferNFT(
  478. wrappedAddress,
  479. tokenId,
  480. "10",
  481. "0x000000000000000000000000b7a2211e8165943192ad04f5dd21bedc29ff003e",
  482. "234"
  483. ).send({
  484. value: 0,
  485. from: accounts[0],
  486. gasLimit: 2000000
  487. });
  488. } catch (e) {
  489. assert.equal(e.message, "Returned error: VM Exception while processing transaction: revert ERC721: transfer caller is not owner nor approved")
  490. failed = true
  491. }
  492. assert.ok(failed)
  493. })
  494. it("should refuse to burn wrappers not held by msg.sender", async function () {
  495. const accounts = await web3.eth.getAccounts();
  496. const initialized = new web3.eth.Contract(BridgeImplementationFullABI, NFTBridge.address);
  497. const tokenId = "1000000000000000001";
  498. const wrappedAddress = await initialized.methods.wrappedAsset("0x" + testBridgedAssetChain, "0x" + testBridgedAssetAddress).call();
  499. const wrappedAsset = new web3.eth.Contract(NFTImplementation.abi, wrappedAddress);
  500. // approve from 0
  501. await wrappedAsset.methods.approve(NFTBridge.address, tokenId).send({
  502. value: 0,
  503. from: accounts[0],
  504. gasLimit: 2000000
  505. });
  506. // deposit tokens from 1
  507. let failed = false
  508. try {
  509. await initialized.methods.transferNFT(
  510. wrappedAddress,
  511. tokenId,
  512. "10",
  513. "0x000000000000000000000000b7a2211e8165943192ad04f5dd21bedc29ff003e",
  514. "234"
  515. ).send({
  516. value: 0,
  517. from: accounts[1],
  518. gasLimit: 2000000
  519. });
  520. } catch (e) {
  521. assert.equal(e.message, "Returned error: VM Exception while processing transaction: revert ERC721: transfer of token that is not own")
  522. failed = true
  523. }
  524. assert.ok(failed)
  525. })
  526. it("should deposit and burn approved bridged assets wrappers on transfer to another chain", async function () {
  527. const accounts = await web3.eth.getAccounts();
  528. const initialized = new web3.eth.Contract(BridgeImplementationFullABI, NFTBridge.address);
  529. const tokenId = "1000000000000000001";
  530. const wrappedAddress = await initialized.methods.wrappedAsset("0x" + testBridgedAssetChain, "0x" + testBridgedAssetAddress).call();
  531. const wrappedAsset = new web3.eth.Contract(NFTImplementation.abi, wrappedAddress);
  532. await wrappedAsset.methods.approve(NFTBridge.address, tokenId).send({
  533. value: 0,
  534. from: accounts[0],
  535. gasLimit: 2000000
  536. });
  537. // deposit tokens
  538. const ownerBefore = await wrappedAsset.methods.ownerOf(tokenId).call();
  539. assert.equal(ownerBefore, accounts[0]);
  540. await initialized.methods.transferNFT(
  541. wrappedAddress,
  542. tokenId,
  543. "10",
  544. "0x000000000000000000000000b7a2211e8165943192ad04f5dd21bedc29ff003e",
  545. "234"
  546. ).send({
  547. value: 0,
  548. from: accounts[0],
  549. gasLimit: 2000000
  550. });
  551. try {
  552. await wrappedAsset.methods.ownerOf(tokenId).call();
  553. assert.fail("burned token still exists")
  554. } catch (e) {
  555. assert.equal(e.data[Object.keys(e.data)[0]].reason, "ERC721: owner query for nonexistent token")
  556. }
  557. })
  558. });
  559. const signAndEncodeVM = async function (
  560. timestamp,
  561. nonce,
  562. emitterChainId,
  563. emitterAddress,
  564. sequence,
  565. data,
  566. signers,
  567. guardianSetIndex,
  568. consistencyLevel
  569. ) {
  570. const body = [
  571. web3.eth.abi.encodeParameter("uint32", timestamp).substring(2 + (64 - 8)),
  572. web3.eth.abi.encodeParameter("uint32", nonce).substring(2 + (64 - 8)),
  573. web3.eth.abi.encodeParameter("uint16", emitterChainId).substring(2 + (64 - 4)),
  574. web3.eth.abi.encodeParameter("bytes32", emitterAddress).substring(2),
  575. web3.eth.abi.encodeParameter("uint64", sequence).substring(2 + (64 - 16)),
  576. web3.eth.abi.encodeParameter("uint8", consistencyLevel).substring(2 + (64 - 2)),
  577. data.substr(2)
  578. ]
  579. const hash = web3.utils.soliditySha3(web3.utils.soliditySha3("0x" + body.join("")))
  580. let signatures = "";
  581. for (let i in signers) {
  582. const ec = new elliptic.ec("secp256k1");
  583. const key = ec.keyFromPrivate(signers[i]);
  584. const signature = key.sign(hash.substr(2), {canonical: true});
  585. const packSig = [
  586. web3.eth.abi.encodeParameter("uint8", i).substring(2 + (64 - 2)),
  587. zeroPadBytes(signature.r.toString(16), 32),
  588. zeroPadBytes(signature.s.toString(16), 32),
  589. web3.eth.abi.encodeParameter("uint8", signature.recoveryParam).substr(2 + (64 - 2)),
  590. ]
  591. signatures += packSig.join("")
  592. }
  593. const vm = [
  594. web3.eth.abi.encodeParameter("uint8", 1).substring(2 + (64 - 2)),
  595. web3.eth.abi.encodeParameter("uint32", guardianSetIndex).substring(2 + (64 - 8)),
  596. web3.eth.abi.encodeParameter("uint8", signers.length).substring(2 + (64 - 2)),
  597. signatures,
  598. body.join("")
  599. ].join("");
  600. return vm
  601. }
  602. function zeroPadBytes(value, length) {
  603. while (value.length < 2 * length) {
  604. value = "0" + value;
  605. }
  606. return value;
  607. }