Correctly parse tokenID in SDK

Change-Id: I902b8fc3a8092ef1f7c38ba8eb3235e3d40543a8
This commit is contained in:
Hendrik Hofstadt 2021-09-21 15:07:48 +02:00
parent 3968b6eb7f
commit 4c9bf1162c
3 changed files with 44 additions and 9 deletions

View File

@ -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;
}

View File

@ -8,7 +8,8 @@
"strict": true,
"esModuleInterop": true,
"downlevelIteration": true,
"allowJs": true
"allowJs": true,
"lib": ["dom", "es5", "scripthost", "es2020.bigint"]
},
"include": ["src", "types"],
"exclude": ["node_modules", "**/__tests__/*"]

View File

@ -1,9 +1,6 @@
{
"extends": ["tslint:recommended", "tslint-config-prettier"],
"linterOptions": {
"exclude": [
"src/proto/**"
]
"exclude": ["src/proto/**"]
}
}