sdk/js: getOriginalAssetAptos NFT returns tokenId

This commit is contained in:
Kevin Peters 2023-01-31 14:59:00 +00:00 committed by Evan Gray
parent aa56dc5498
commit 7a8dea5158
3 changed files with 23 additions and 6 deletions

View File

@ -20,6 +20,7 @@ import {
CONTRACTS, CONTRACTS,
deriveCollectionHashFromTokenId, deriveCollectionHashFromTokenId,
deriveTokenHashFromTokenId, deriveTokenHashFromTokenId,
ensureHexPrefix,
generateSignAndSubmitEntryFunction, generateSignAndSubmitEntryFunction,
tryNativeToHexString, tryNativeToHexString,
tryNativeToUint8Array, tryNativeToUint8Array,
@ -160,6 +161,9 @@ describe("Aptos NFT SDK tests", () => {
isWrapped: true, isWrapped: true,
chainId: CHAIN_ID_ETH, chainId: CHAIN_ID_ETH,
assetAddress: Uint8Array.from(ethTransferVaaParsed.tokenAddress), assetAddress: Uint8Array.from(ethTransferVaaParsed.tokenAddress),
tokenId: ensureHexPrefix(
ethTransferVaaParsed.tokenId.toString(16).padStart(64, "0")
),
}); });
// transfer NFT from Aptos back to Ethereum // transfer NFT from Aptos back to Ethereum
@ -345,6 +349,9 @@ describe("Aptos NFT SDK tests", () => {
isWrapped: false, isWrapped: false,
chainId: CHAIN_ID_APTOS, chainId: CHAIN_ID_APTOS,
assetAddress: tokenAddressAptos, assetAddress: tokenAddressAptos,
tokenId: ensureHexPrefix(
ethTransferVaaParsed.tokenId.toString(16).padStart(64, "0")
),
}); });
}); });

View File

@ -22,6 +22,9 @@ import {
coalesceChainId, coalesceChainId,
deriveCollectionHashFromTokenId, deriveCollectionHashFromTokenId,
hex, hex,
deriveTokenHashFromTokenId,
ensureHexPrefix,
uint8ArrayToHex,
} from "../utils"; } from "../utils";
import { getIsWrappedAssetEth } from "./getIsWrappedAsset"; import { getIsWrappedAssetEth } from "./getIsWrappedAsset";
@ -198,7 +201,7 @@ export async function getOriginalAssetAptos(
client: AptosClient, client: AptosClient,
nftBridgeAddress: string, nftBridgeAddress: string,
tokenId: TokenTypes.TokenId tokenId: TokenTypes.TokenId
): Promise<WormholeWrappedInfo> { ): Promise<WormholeWrappedNFTInfo> {
try { try {
const originInfo = ( const originInfo = (
await client.getAccountResource( await client.getAccountResource(
@ -210,10 +213,12 @@ export async function getOriginalAssetAptos(
assertChain(chainId); assertChain(chainId);
return { return {
isWrapped: true, isWrapped: true,
chainId: chainId, chainId,
assetAddress: new Uint8Array( assetAddress:
hex(originInfo.token_address.external_address) chainId === CHAIN_ID_SOLANA
), ? arrayify(BigNumber.from(hex(tokenId.token_data_id.name)))
: new Uint8Array(hex(originInfo.token_address.external_address)),
tokenId: ensureHexPrefix(hex(tokenId.token_data_id.name).toString("hex")),
}; };
} catch (e: any) { } catch (e: any) {
if ( if (
@ -230,5 +235,8 @@ export async function getOriginalAssetAptos(
isWrapped: false, isWrapped: false,
chainId: CHAIN_ID_APTOS, chainId: CHAIN_ID_APTOS,
assetAddress: await deriveCollectionHashFromTokenId(tokenId), assetAddress: await deriveCollectionHashFromTokenId(tokenId),
tokenId: ensureHexPrefix(
uint8ArrayToHex(await deriveTokenHashFromTokenId(tokenId))
),
}; };
} }

View File

@ -315,7 +315,9 @@ export const deriveTokenHashFromTokenId = async (
const propertyVersion = Buffer.alloc(8); const propertyVersion = Buffer.alloc(8);
propertyVersion.writeBigUInt64BE(BigInt(tokenId.property_version)); propertyVersion.writeBigUInt64BE(BigInt(tokenId.property_version));
const inputs = Buffer.concat([ const inputs = Buffer.concat([
Buffer.from(tokenId.token_data_id.creator, "hex"), BCS.bcsToBytes(
TxnBuilderTypes.AccountAddress.fromHex(tokenId.token_data_id.creator)
),
Buffer.from(sha3_256(tokenId.token_data_id.collection), "hex"), Buffer.from(sha3_256(tokenId.token_data_id.collection), "hex"),
Buffer.from(sha3_256(tokenId.token_data_id.name), "hex"), Buffer.from(sha3_256(tokenId.token_data_id.name), "hex"),
propertyVersion, propertyVersion,