{"version":3,"file":"coin-reservation.mjs","names":[],"sources":["../../src/transaction-analyzer/coin-reservation.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { fromBase58, fromHex, toHex } from '@mysten/bcs';\nimport { normalizeStructTag, normalizeSuiAddress, parseStructTag } from '@mysten/sui/utils';\n\nconst COIN_RESERVATION_MAGIC = new Uint8Array([\n\t0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac,\n\t0xac, 0xac, 0xac, 0xac,\n]);\n\nconst SUI_FRAMEWORK = normalizeSuiAddress('0x2');\n\nexport function isCoinReservationDigest(digestBase58: string): boolean {\n\tconst digestBytes = fromBase58(digestBase58);\n\tconst last20Bytes = digestBytes.slice(12, 32);\n\treturn last20Bytes.every((byte, i) => byte === COIN_RESERVATION_MAGIC[i]);\n}\n\nexport function parseCoinReservationBalance(digestBase58: string): bigint {\n\tconst digestBytes = fromBase58(digestBase58);\n\tconst view = new DataView(digestBytes.buffer, digestBytes.byteOffset, digestBytes.byteLength);\n\treturn view.getBigUint64(0, true);\n}\n\n/**\n * XORs an object id with the chain identifier. Self-inverse — converts\n * between a reservation ref's masked id and the underlying accumulator-field\n * object id.\n */\nexport function xorCoinReservationObjectId(objectId: string, chainIdentifier: string): string {\n\tconst idBytes = fromHex(objectId);\n\tif (idBytes.length !== 32) {\n\t\tthrow new Error(`Invalid object id length: expected 32 bytes, got ${idBytes.length}`);\n\t}\n\tconst chainBytes = fromBase58(chainIdentifier);\n\tif (chainBytes.length !== 32) {\n\t\tthrow new Error(`Invalid chain identifier length: expected 32 bytes, got ${chainBytes.length}`);\n\t}\n\tconst xored = new Uint8Array(32);\n\tfor (let i = 0; i < 32; i++) {\n\t\txored[i] = idBytes[i] ^ chainBytes[i];\n\t}\n\treturn `0x${toHex(xored)}`;\n}\n\n/**\n * Given an accumulator-field object's struct tag\n * (`0x2::dynamic_field::Field<0x2::accumulator::Key<0x2::balance::Balance<T>>, 0x2::accumulator::U128>`),\n * returns the normalized coin type `T`. Returns `null` if the type tag\n * doesn't match the expected shape.\n */\nexport function parseAccumulatorFieldCoinType(objectType: string): string | null {\n\tlet field;\n\ttry {\n\t\tfield = parseStructTag(objectType);\n\t} catch {\n\t\treturn null;\n\t}\n\tif (\n\t\tfield.address !== SUI_FRAMEWORK ||\n\t\tfield.module !== 'dynamic_field' ||\n\t\tfield.name !== 'Field' ||\n\t\tfield.typeParams.length !== 2\n\t) {\n\t\treturn null;\n\t}\n\n\tconst [keyParam, valueParam] = field.typeParams;\n\tif (typeof keyParam === 'string' || typeof valueParam === 'string') {\n\t\treturn null;\n\t}\n\tif (\n\t\tvalueParam.address !== SUI_FRAMEWORK ||\n\t\tvalueParam.module !== 'accumulator' ||\n\t\tvalueParam.name !== 'U128'\n\t) {\n\t\treturn null;\n\t}\n\tif (\n\t\tkeyParam.address !== SUI_FRAMEWORK ||\n\t\tkeyParam.module !== 'accumulator' ||\n\t\tkeyParam.name !== 'Key' ||\n\t\tkeyParam.typeParams.length !== 1\n\t) {\n\t\treturn null;\n\t}\n\n\tconst balanceParam = keyParam.typeParams[0];\n\tif (typeof balanceParam === 'string') return null;\n\tif (\n\t\tbalanceParam.address !== SUI_FRAMEWORK ||\n\t\tbalanceParam.module !== 'balance' ||\n\t\tbalanceParam.name !== 'Balance' ||\n\t\tbalanceParam.typeParams.length !== 1\n\t) {\n\t\treturn null;\n\t}\n\n\tconst innerType = balanceParam.typeParams[0];\n\tif (typeof innerType === 'string') return null;\n\treturn normalizeStructTag(innerType);\n}\n"],"mappings":";;;;AAMA,MAAM,yBAAyB,IAAI,WAAW;CAC7C;CAAM;CAAM;CAAM;CAAM;CAAM;CAAM;CAAM;CAAM;CAAM;CAAM;CAAM;CAAM;CAAM;CAAM;CAAM;CAC1F;CAAM;CAAM;CAAM;CAClB,CAAC;AAEF,MAAM,gBAAgB,oBAAoB,MAAM;AAE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