From 4c9bf1162c66dcf1c5c5c04a9f7aae1b769ed9d7 Mon Sep 17 00:00:00 2001 From: Hendrik Hofstadt Date: Tue, 21 Sep 2021 15:07:48 +0200 Subject: [PATCH] Correctly parse tokenID in SDK Change-Id: I902b8fc3a8092ef1f7c38ba8eb3235e3d40543a8 --- sdk/js/src/nft_bridge/getOriginalAsset.ts | 41 +++++++++++++++++++++-- sdk/js/tsconfig.json | 5 +-- sdk/js/tslint.json | 7 ++-- 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/sdk/js/src/nft_bridge/getOriginalAsset.ts b/sdk/js/src/nft_bridge/getOriginalAsset.ts index e51b16f1..d1a4a341 100644 --- a/sdk/js/src/nft_bridge/getOriginalAsset.ts +++ b/sdk/js/src/nft_bridge/getOriginalAsset.ts @@ -40,7 +40,10 @@ export async function getOriginalAssetEth( return { isWrapped: true, chainId, - assetAddress: chainId === CHAIN_ID_SOLANA ? arrayify(BigNumber.from(tokenId)) : arrayify(assetAddress), + assetAddress: + chainId === CHAIN_ID_SOLANA + ? arrayify(BigNumber.from(tokenId)) + : arrayify(assetAddress), tokenId, // tokenIds are maintained across EVM chains }; } @@ -80,7 +83,11 @@ export async function getOriginalAssetSol( if (wrappedMetaAccountInfo) { const parsed = parse_wrapped_meta(wrappedMetaAccountInfo.data); const token_id_arr = parsed.token_id as BigUint64Array; - const token_id = BigNumber.from(token_id_arr.reverse()).toString(); + const token_id_bytes = []; + for (let elem of token_id_arr.reverse()) { + token_id_bytes.push(...bigToUint8Array(elem)); + } + const token_id = BigNumber.from(token_id_bytes).toString(); return { isWrapped: true, chainId: parsed.chain, @@ -102,3 +109,33 @@ export async function getOriginalAssetSol( assetAddress: new Uint8Array(32), }; } + +// Derived from https://www.jackieli.dev/posts/bigint-to-uint8array/ +const big0 = BigInt(0); +const big1 = BigInt(1); +const big8 = BigInt(8); + +function bigToUint8Array(big: bigint) { + if (big < big0) { + const bits: bigint = (BigInt(big.toString(2).length) / big8 + big1) * big8; + const prefix1: bigint = big1 << bits; + big += prefix1; + } + let hex = big.toString(16); + if (hex.length % 2) { + hex = "0" + hex; + } else if (hex[0] === "8") { + // maximum positive need to prepend 0 otherwise resuts in negative number + hex = "00" + hex; + } + const len = hex.length / 2; + const u8 = new Uint8Array(len); + var i = 0; + var j = 0; + while (i < len) { + u8[i] = parseInt(hex.slice(j, j + 2), 16); + i += 1; + j += 2; + } + return u8; +} diff --git a/sdk/js/tsconfig.json b/sdk/js/tsconfig.json index dc82fc55..b44fc04e 100644 --- a/sdk/js/tsconfig.json +++ b/sdk/js/tsconfig.json @@ -8,8 +8,9 @@ "strict": true, "esModuleInterop": true, "downlevelIteration": true, - "allowJs": true + "allowJs": true, + "lib": ["dom", "es5", "scripthost", "es2020.bigint"] }, "include": ["src", "types"], "exclude": ["node_modules", "**/__tests__/*"] -} \ No newline at end of file +} diff --git a/sdk/js/tslint.json b/sdk/js/tslint.json index 95772682..d2b69dde 100644 --- a/sdk/js/tslint.json +++ b/sdk/js/tslint.json @@ -1,9 +1,6 @@ - { "extends": ["tslint:recommended", "tslint-config-prettier"], "linterOptions": { - "exclude": [ - "src/proto/**" - ] + "exclude": ["src/proto/**"] } -} \ No newline at end of file +}