@algorandfoundation/algokit-utils 10.0.0-alpha.2 → 10.0.0-alpha.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/algo25/index.d.ts +2 -0
- package/algo25/index.js +9 -0
- package/algo25/index.mjs +3 -0
- package/package.json +1 -1
- package/packages/abi/src/abi-method.d.ts +1 -1
- package/packages/abi/src/abi-method.js.map +1 -1
- package/packages/abi/src/abi-method.mjs.map +1 -1
- package/packages/algo25/src/index.d.ts +40 -0
- package/packages/algo25/src/index.js +44 -0
- package/packages/algo25/src/index.js.map +1 -1
- package/packages/algo25/src/index.mjs +39 -1
- package/packages/algo25/src/index.mjs.map +1 -1
- package/transactions/method-call.js +12 -12
- package/transactions/method-call.js.map +1 -1
- package/transactions/method-call.mjs +12 -12
- package/transactions/method-call.mjs.map +1 -1
- package/types/algorand-client-transaction-creator.d.ts +37 -37
- package/types/algorand-client-transaction-sender.d.ts +39 -39
- package/types/app-client.d.ts +64 -64
- package/types/app-factory.d.ts +27 -27
- package/types/app-manager.d.ts +1 -1
- package/types/app-manager.js +6 -1
- package/types/app-manager.js.map +1 -1
- package/types/app-manager.mjs +6 -1
- package/types/app-manager.mjs.map +1 -1
- package/types/composer.js +3 -6
- package/types/composer.js.map +1 -1
- package/types/composer.mjs +3 -6
- package/types/composer.mjs.map +1 -1
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { FAIL_TO_DECODE_MNEMONIC_ERROR_MSG, NOT_IN_WORDS_LIST_ERROR_MSG, masterDerivationKeyToMnemonic, mnemonicFromSeed, mnemonicToMasterDerivationKey, secretKeyToMnemonic, seedFromMnemonic } from "../packages/algo25/src/index.js";
|
|
2
|
+
export { FAIL_TO_DECODE_MNEMONIC_ERROR_MSG, NOT_IN_WORDS_LIST_ERROR_MSG, masterDerivationKeyToMnemonic, mnemonicFromSeed, mnemonicToMasterDerivationKey, secretKeyToMnemonic, seedFromMnemonic };
|
package/algo25/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
const require_index = require('../packages/algo25/src/index.js');
|
|
2
|
+
|
|
3
|
+
exports.FAIL_TO_DECODE_MNEMONIC_ERROR_MSG = require_index.FAIL_TO_DECODE_MNEMONIC_ERROR_MSG;
|
|
4
|
+
exports.NOT_IN_WORDS_LIST_ERROR_MSG = require_index.NOT_IN_WORDS_LIST_ERROR_MSG;
|
|
5
|
+
exports.masterDerivationKeyToMnemonic = require_index.masterDerivationKeyToMnemonic;
|
|
6
|
+
exports.mnemonicFromSeed = require_index.mnemonicFromSeed;
|
|
7
|
+
exports.mnemonicToMasterDerivationKey = require_index.mnemonicToMasterDerivationKey;
|
|
8
|
+
exports.secretKeyToMnemonic = require_index.secretKeyToMnemonic;
|
|
9
|
+
exports.seedFromMnemonic = require_index.seedFromMnemonic;
|
package/algo25/index.mjs
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { FAIL_TO_DECODE_MNEMONIC_ERROR_MSG, NOT_IN_WORDS_LIST_ERROR_MSG, masterDerivationKeyToMnemonic, mnemonicFromSeed, mnemonicToMasterDerivationKey, secretKeyToMnemonic, seedFromMnemonic } from "../packages/algo25/src/index.mjs";
|
|
2
|
+
|
|
3
|
+
export { FAIL_TO_DECODE_MNEMONIC_ERROR_MSG, NOT_IN_WORDS_LIST_ERROR_MSG, masterDerivationKeyToMnemonic, mnemonicFromSeed, mnemonicToMasterDerivationKey, secretKeyToMnemonic, seedFromMnemonic };
|
package/package.json
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
"**"
|
|
7
7
|
],
|
|
8
8
|
"name": "@algorandfoundation/algokit-utils",
|
|
9
|
-
"version": "10.0.0-alpha.
|
|
9
|
+
"version": "10.0.0-alpha.4",
|
|
10
10
|
"private": false,
|
|
11
11
|
"description": "A set of core Algorand utilities written in TypeScript and released via npm that make it easier to build solutions on Algorand.",
|
|
12
12
|
"author": "Algorand Foundation",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"abi-method.js","names":["sha512","parseTupleContent","ABIType","convertedDefaultValue: ABIDefaultValue | undefined","ABIStructType","ABIUintType","ABIAddressType"],"sources":["../../../../packages/abi/src/abi-method.ts"],"sourcesContent":["import sha512 from 'js-sha512'\nimport { ABIAddressType, ABIStructType, ABIType, ABIUintType, parseTupleContent } from './abi-type'\nimport { ABIValue } from './abi-value'\nimport { ARC28Event } from './arc28-event'\nimport { AVMType, Arc56Contract, Arc56Method } from './arc56-contract'\n\nexport enum ABITransactionType {\n Txn = 'txn',\n Payment = 'pay',\n KeyRegistration = 'keyreg',\n AssetConfig = 'acfg',\n AssetTransfer = 'axfer',\n AssetFreeze = 'afrz',\n AppCall = 'appl',\n}\nexport enum ABIReferenceType {\n Account = 'account',\n Application = 'application',\n Asset = 'asset',\n}\nexport type ABIMethodArgType = ABIType | ABITransactionType | ABIReferenceType\nexport type ABIMethodReturnType = ABIType | 'void'\n\nexport type ABIMethodArg = {\n type: ABIMethodArgType\n name?: string\n description?: string\n defaultValue?: ABIDefaultValue\n}\n\nexport type ABIMethodReturn = {\n type: ABIMethodReturnType\n description?: string\n}\n\nexport type ABIDefaultValue = {\n /** Base64 encoded bytes, base64 ARC4 encoded uint64, or UTF-8 method selector */\n data: string\n /** Where the default value is coming from */\n source: DefaultValueSource\n /** How the data is encoded. This is the encoding for the data provided here, not the arg type */\n type?: ABIType | AVMType\n}\n\nexport enum DefaultValueSource {\n Box = 'box',\n Global = 'global',\n Local = 'local',\n Literal = 'literal',\n Method = 'method',\n}\n\n/** Represents an ABI method return value with parsed data. */\nexport type ABIReturn =\n | {\n /** The method that was called. */\n method: ABIMethod\n /** The raw return value as bytes.\n *\n * This is the value from the last app call log with the first 4 bytes (the ABI return prefix) omitted.\n */\n rawReturnValue: Uint8Array\n /** The parsed ABI return value. */\n returnValue: ABIValue\n decodeError: undefined\n }\n | { rawReturnValue?: undefined; returnValue?: undefined; method: ABIMethod; decodeError: Error }\n\n/** Decoded ARC-56 struct as a struct rather than a tuple. */\nexport type ABIStruct = {\n [key: string]: ABIStruct | ABIValue\n}\n\nexport class ABIMethod {\n readonly name: string\n readonly description?: string\n readonly args: ABIMethodArg[]\n readonly returns: ABIMethodReturn\n readonly events?: ARC28Event[]\n readonly readonly?: boolean\n\n constructor(params: {\n name: string\n description?: string\n args: ABIMethodArg[]\n returns: ABIMethodReturn\n events?: ARC28Event[]\n readonly?: boolean\n }) {\n this.name = params.name\n this.description = params.description\n this.args = params.args\n this.returns = params.returns\n this.events = params.events\n this.readonly = params.readonly\n }\n\n /**\n * Returns the signature of this ABI method.\n * @returns The signature, e.g. `my_method(unit64,string)bytes`\n */\n getSignature(): string {\n const args = this.args\n .map((arg) => {\n if (argTypeIsTransaction(arg.type) || argTypeIsReference(arg.type)) return arg.type\n return arg.type.name\n })\n .join(',')\n const returns = this.returns.type === 'void' ? 'void' : this.returns.type.name\n return `${this.name}(${args})${returns}`\n }\n\n /**\n * Returns the method selector of this ABI method.\n * @returns The 4-byte method selector\n */\n getSelector(): Uint8Array {\n const hash = sha512.sha512_256.array(this.getSignature())\n return new Uint8Array(hash.slice(0, 4))\n }\n\n /**\n * Returns the ABI method object for a given method signature.\n * @param signature The method signature\n * e.g. `my_method(unit64,string)bytes`\n * @returns The `ABIMethod`\n */\n static fromSignature(signature: string): ABIMethod {\n const argsStart = signature.indexOf('(')\n if (argsStart === -1) {\n throw new Error(`Invalid method signature: ${signature}`)\n }\n\n let argsEnd = -1\n let depth = 0\n for (let i = argsStart; i < signature.length; i++) {\n const char = signature[i]\n\n if (char === '(') {\n depth += 1\n } else if (char === ')') {\n if (depth === 0) {\n // unpaired parenthesis\n break\n }\n\n depth -= 1\n if (depth === 0) {\n argsEnd = i\n break\n }\n }\n }\n\n if (argsEnd === -1) {\n throw new Error(`Invalid method signature: ${signature}`)\n }\n\n const name = signature.slice(0, argsStart)\n const args = parseTupleContent(signature.slice(argsStart + 1, argsEnd)).map((n: string) => {\n if (argTypeIsTransaction(n as ABIMethodArgType) || argTypeIsReference(n as ABIMethodArgType)) {\n return { type: n as ABIMethodArgType } satisfies ABIMethodArg\n }\n return { type: ABIType.from(n) } satisfies ABIMethodArg\n })\n const returnType = signature.slice(argsEnd + 1)\n const returns = { type: returnType === 'void' ? ('void' as const) : ABIType.from(returnType) } satisfies ABIMethodReturn\n\n return new ABIMethod({\n name,\n args,\n returns,\n })\n }\n}\n\n/**\n * Returns the ABI method object for a given method name or signature and ARC-56 app spec.\n * @param methodNameOrSignature The method name or method signature to call if an ABI call is being emitted.\n * e.g. `my_method` or `my_method(unit64,string)bytes`\n * @param appSpec The app spec for the app\n * @returns The `ABIMethod`\n */\nexport function getABIMethod(methodNameOrSignature: string, appSpec: Arc56Contract): ABIMethod {\n if (!methodNameOrSignature.includes('(')) {\n const methods = appSpec.methods.filter((m) => m.name === methodNameOrSignature)\n if (methods.length === 0) throw new Error(`Unable to find method ${methodNameOrSignature} in ${appSpec.name} app.`)\n if (methods.length > 1) {\n throw new Error(\n `Received a call to method ${methodNameOrSignature} in contract ${\n appSpec.name\n }, but this resolved to multiple methods; please pass in an ABI signature instead: ${appSpec.methods\n .map((m) => getArc56MethodSignature(m))\n .join(', ')}`,\n )\n }\n return arc56MethodToABIMethod(methods[0], appSpec)\n } else {\n const method = appSpec.methods.find((m) => getArc56MethodSignature(m) === methodNameOrSignature)\n if (!method) throw new Error(`Unable to find method ${methodNameOrSignature} in ${appSpec.name} app.`)\n return arc56MethodToABIMethod(method, appSpec)\n }\n}\n\nexport function arc56MethodToABIMethod(method: Arc56Method, appSpec: Arc56Contract): ABIMethod {\n if (typeof method.name !== 'string' || typeof method.returns !== 'object' || !Array.isArray(method.args)) {\n throw new Error('Invalid ABIMethod parameters')\n }\n\n const args = method.args.map(({ type, name, desc, struct, defaultValue }) => {\n const convertedDefaultValue: ABIDefaultValue | undefined = defaultValue\n ? {\n data: defaultValue.data,\n source: defaultValue.source as DefaultValueSource,\n type: defaultValue.type ? (isAVMType(defaultValue.type) ? defaultValue.type : ABIType.from(defaultValue.type)) : undefined,\n }\n : undefined\n\n if (argTypeIsTransaction(type as ABIMethodArgType) || argTypeIsReference(type as ABIMethodArgType)) {\n return {\n type: type as ABIMethodArgType,\n name,\n description: desc,\n defaultValue: convertedDefaultValue,\n } satisfies ABIMethodArg\n }\n\n if (struct) {\n return {\n type: ABIStructType.fromStruct(struct, appSpec.structs),\n name,\n description: desc,\n defaultValue: convertedDefaultValue,\n } satisfies ABIMethodArg\n }\n\n return {\n type: ABIType.from(type),\n name,\n description: desc,\n defaultValue: convertedDefaultValue,\n } satisfies ABIMethodArg\n })\n\n const returns = {\n type:\n method.returns.type === ('void' as const)\n ? ('void' as const)\n : method.returns.struct\n ? ABIStructType.fromStruct(method.returns.struct, appSpec.structs)\n : ABIType.from(method.returns.type),\n desc: method.returns.desc,\n }\n\n return new ABIMethod({\n name: method.name,\n description: method.desc,\n args,\n returns,\n events: method.events,\n readonly: method.readonly,\n })\n}\n\nexport function argTypeIsTransaction(type: ABIMethodArgType): type is ABITransactionType {\n return (\n typeof type === 'string' &&\n (type === ABITransactionType.Txn ||\n type === ABITransactionType.Payment ||\n type === ABITransactionType.KeyRegistration ||\n type === ABITransactionType.AssetConfig ||\n type === ABITransactionType.AssetTransfer ||\n type === ABITransactionType.AssetFreeze ||\n type === ABITransactionType.AppCall)\n )\n}\n\nexport function argTypeIsReference(type: ABIMethodArgType): type is ABIReferenceType {\n return (\n typeof type === 'string' &&\n (type === ABIReferenceType.Account || type === ABIReferenceType.Application || type === ABIReferenceType.Asset)\n )\n}\n\nexport function argTypeIsAbiType(type: ABIMethodArgType): type is ABIType {\n return !argTypeIsTransaction(type) && !argTypeIsReference(type)\n}\n\nfunction getArc56MethodSignature(method: Arc56Method): string {\n const args = method.args.map((arg) => arg.type).join(',')\n const returns = method.returns.type\n return `${method.name}(${args})${returns}`\n}\n\nexport function decodeAVMValue(avmType: AVMType, bytes: Uint8Array): ABIValue {\n switch (avmType) {\n case 'AVMString':\n return Buffer.from(bytes).toString('utf-8')\n case 'AVMBytes':\n return bytes\n case 'AVMUint64':\n return ABIType.from('uint64').decode(bytes)\n }\n}\n\nexport function encodeAVMValue(avmType: AVMType, value: ABIValue): Uint8Array {\n switch (avmType) {\n case 'AVMString':\n return ABIType.from('string').encode(value)\n case 'AVMBytes':\n if (typeof value === 'string') return Buffer.from(value, 'utf-8')\n if (typeof value !== 'object' || !(value instanceof Uint8Array))\n throw new Error(`Expected bytes value for AVMBytes, but got ${value}`)\n return value\n case 'AVMUint64':\n return ABIType.from('uint64').encode(value)\n }\n}\n\nexport function isAVMType(type: unknown): type is AVMType {\n return typeof type === 'string' && (type === 'AVMString' || type === 'AVMBytes' || type === 'AVMUint64')\n}\n\nexport function getABIDecodedValue(type: AVMType | ABIType | ABIReferenceType, bytes: Uint8Array): ABIValue {\n if (type === ABIReferenceType.Asset || type === ABIReferenceType.Application) {\n return new ABIUintType(64).decode(bytes)\n } else if (type === ABIReferenceType.Account) {\n return new ABIAddressType().decode(bytes)\n } else if (isAVMType(type)) {\n return decodeAVMValue(type, bytes)\n }\n return type.decode(bytes)\n}\n\nexport function getABIEncodedValue(type: AVMType | ABIType, value: ABIValue): Uint8Array {\n return isAVMType(type) ? encodeAVMValue(type, value) : type.encode(value)\n}\n"],"mappings":";;;;;;AAMA,IAAY,oEAAL;AACL;AACA;AACA;AACA;AACA;AACA;AACA;;;AAEF,IAAY,gEAAL;AACL;AACA;AACA;;;AAuDF,IAAa,YAAb,MAAa,UAAU;CACrB,AAAS;CACT,AAAS;CACT,AAAS;CACT,AAAS;CACT,AAAS;CACT,AAAS;CAET,YAAY,QAOT;AACD,OAAK,OAAO,OAAO;AACnB,OAAK,cAAc,OAAO;AAC1B,OAAK,OAAO,OAAO;AACnB,OAAK,UAAU,OAAO;AACtB,OAAK,SAAS,OAAO;AACrB,OAAK,WAAW,OAAO;;;;;;CAOzB,eAAuB;EACrB,MAAM,OAAO,KAAK,KACf,KAAK,QAAQ;AACZ,OAAI,qBAAqB,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H,QAAOA,yBAAQ,KAAK,SAAS,CAAC,OAAO,MAAM;EAC7C,KAAK;AACH,OAAI,OAAO,UAAU,SAAU,QAAO,OAAO,KAAK,OAAO,QAAQ;AACjE,OAAI,OAAO,UAAU,YAAY,EAAE,iBAAiB,YAClD,OAAM,IAAI,MAAM,8CAA8C,QAAQ;AACxE,UAAO;EACT,KAAK,YACH,QAAOA,yBAAQ,KAAK,SAAS,CAAC,OAAO,MAAM;;;AAIjD,SAAgB,UAAU,MAAgC;AACxD,QAAO,OAAO,SAAS,aAAa,SAAS,eAAe,SAAS,cAAc,SAAS;;AAG9F,SAAgB,mBAAmB,MAA4C,OAA6B;AAC1G,KAAI,SAAS,iBAAiB,SAAS,SAAS,iBAAiB,YAC/D,QAAO,IAAIG,6BAAY,GAAG,CAAC,OAAO,MAAM;UAC/B,SAAS,iBAAiB,QACnC,QAAO,IAAIC,iCAAgB,CAAC,OAAO,MAAM;UAChC,UAAU,KAAK,CACxB,QAAO,eAAe,MAAM,MAAM;AAEpC,QAAO,KAAK,OAAO,MAAM;;AAG3B,SAAgB,mBAAmB,MAAyB,OAA6B;AACvF,QAAO,UAAU,KAAK,GAAG,eAAe,MAAM,MAAM,GAAG,KAAK,OAAO,MAAM"}
|
|
1
|
+
{"version":3,"file":"abi-method.js","names":["sha512","parseTupleContent","ABIType","convertedDefaultValue: ABIDefaultValue | undefined","ABIStructType","ABIUintType","ABIAddressType"],"sources":["../../../../packages/abi/src/abi-method.ts"],"sourcesContent":["import sha512 from 'js-sha512'\nimport { ABIAddressType, ABIStructType, ABIType, ABIUintType, parseTupleContent } from './abi-type'\nimport { ABIValue } from './abi-value'\nimport { ARC28Event } from './arc28-event'\nimport { AVMType, Arc56Contract, Arc56Method } from './arc56-contract'\n\nexport enum ABITransactionType {\n Txn = 'txn',\n Payment = 'pay',\n KeyRegistration = 'keyreg',\n AssetConfig = 'acfg',\n AssetTransfer = 'axfer',\n AssetFreeze = 'afrz',\n AppCall = 'appl',\n}\nexport enum ABIReferenceType {\n Account = 'account',\n Application = 'application',\n Asset = 'asset',\n}\nexport type ABIMethodArgType = ABIType | ABITransactionType | ABIReferenceType\nexport type ABIMethodReturnType = ABIType | 'void'\n\nexport type ABIMethodArg = {\n type: ABIMethodArgType\n name?: string\n description?: string\n defaultValue?: ABIDefaultValue\n}\n\nexport type ABIMethodReturn = {\n type: ABIMethodReturnType\n description?: string\n}\n\nexport type ABIDefaultValue = {\n /** Base64 encoded bytes, base64 ARC4 encoded uint64, or UTF-8 method selector */\n data: string\n /** Where the default value is coming from */\n source: DefaultValueSource\n /** How the data is encoded. This is the encoding for the data provided here, not the arg type */\n type?: ABIType | AVMType\n}\n\nexport enum DefaultValueSource {\n Box = 'box',\n Global = 'global',\n Local = 'local',\n Literal = 'literal',\n Method = 'method',\n}\n\n/** Represents an ABI method return value with parsed data. */\nexport type ABIReturn =\n | {\n /** The method that was called. */\n method: ABIMethod\n /** The raw return value as bytes.\n *\n * This is the value from the last app call log with the first 4 bytes (the ABI return prefix) omitted.\n */\n rawReturnValue: Uint8Array\n /** The parsed ABI return value. */\n returnValue: ABIValue\n decodeError: undefined\n }\n | { rawReturnValue?: undefined; returnValue?: undefined; method: ABIMethod; decodeError?: Error }\n\n/** Decoded ARC-56 struct as a struct rather than a tuple. */\nexport type ABIStruct = {\n [key: string]: ABIStruct | ABIValue\n}\n\nexport class ABIMethod {\n readonly name: string\n readonly description?: string\n readonly args: ABIMethodArg[]\n readonly returns: ABIMethodReturn\n readonly events?: ARC28Event[]\n readonly readonly?: boolean\n\n constructor(params: {\n name: string\n description?: string\n args: ABIMethodArg[]\n returns: ABIMethodReturn\n events?: ARC28Event[]\n readonly?: boolean\n }) {\n this.name = params.name\n this.description = params.description\n this.args = params.args\n this.returns = params.returns\n this.events = params.events\n this.readonly = params.readonly\n }\n\n /**\n * Returns the signature of this ABI method.\n * @returns The signature, e.g. `my_method(unit64,string)bytes`\n */\n getSignature(): string {\n const args = this.args\n .map((arg) => {\n if (argTypeIsTransaction(arg.type) || argTypeIsReference(arg.type)) return arg.type\n return arg.type.name\n })\n .join(',')\n const returns = this.returns.type === 'void' ? 'void' : this.returns.type.name\n return `${this.name}(${args})${returns}`\n }\n\n /**\n * Returns the method selector of this ABI method.\n * @returns The 4-byte method selector\n */\n getSelector(): Uint8Array {\n const hash = sha512.sha512_256.array(this.getSignature())\n return new Uint8Array(hash.slice(0, 4))\n }\n\n /**\n * Returns the ABI method object for a given method signature.\n * @param signature The method signature\n * e.g. `my_method(unit64,string)bytes`\n * @returns The `ABIMethod`\n */\n static fromSignature(signature: string): ABIMethod {\n const argsStart = signature.indexOf('(')\n if (argsStart === -1) {\n throw new Error(`Invalid method signature: ${signature}`)\n }\n\n let argsEnd = -1\n let depth = 0\n for (let i = argsStart; i < signature.length; i++) {\n const char = signature[i]\n\n if (char === '(') {\n depth += 1\n } else if (char === ')') {\n if (depth === 0) {\n // unpaired parenthesis\n break\n }\n\n depth -= 1\n if (depth === 0) {\n argsEnd = i\n break\n }\n }\n }\n\n if (argsEnd === -1) {\n throw new Error(`Invalid method signature: ${signature}`)\n }\n\n const name = signature.slice(0, argsStart)\n const args = parseTupleContent(signature.slice(argsStart + 1, argsEnd)).map((n: string) => {\n if (argTypeIsTransaction(n as ABIMethodArgType) || argTypeIsReference(n as ABIMethodArgType)) {\n return { type: n as ABIMethodArgType } satisfies ABIMethodArg\n }\n return { type: ABIType.from(n) } satisfies ABIMethodArg\n })\n const returnType = signature.slice(argsEnd + 1)\n const returns = { type: returnType === 'void' ? ('void' as const) : ABIType.from(returnType) } satisfies ABIMethodReturn\n\n return new ABIMethod({\n name,\n args,\n returns,\n })\n }\n}\n\n/**\n * Returns the ABI method object for a given method name or signature and ARC-56 app spec.\n * @param methodNameOrSignature The method name or method signature to call if an ABI call is being emitted.\n * e.g. `my_method` or `my_method(unit64,string)bytes`\n * @param appSpec The app spec for the app\n * @returns The `ABIMethod`\n */\nexport function getABIMethod(methodNameOrSignature: string, appSpec: Arc56Contract): ABIMethod {\n if (!methodNameOrSignature.includes('(')) {\n const methods = appSpec.methods.filter((m) => m.name === methodNameOrSignature)\n if (methods.length === 0) throw new Error(`Unable to find method ${methodNameOrSignature} in ${appSpec.name} app.`)\n if (methods.length > 1) {\n throw new Error(\n `Received a call to method ${methodNameOrSignature} in contract ${\n appSpec.name\n }, but this resolved to multiple methods; please pass in an ABI signature instead: ${appSpec.methods\n .map((m) => getArc56MethodSignature(m))\n .join(', ')}`,\n )\n }\n return arc56MethodToABIMethod(methods[0], appSpec)\n } else {\n const method = appSpec.methods.find((m) => getArc56MethodSignature(m) === methodNameOrSignature)\n if (!method) throw new Error(`Unable to find method ${methodNameOrSignature} in ${appSpec.name} app.`)\n return arc56MethodToABIMethod(method, appSpec)\n }\n}\n\nexport function arc56MethodToABIMethod(method: Arc56Method, appSpec: Arc56Contract): ABIMethod {\n if (typeof method.name !== 'string' || typeof method.returns !== 'object' || !Array.isArray(method.args)) {\n throw new Error('Invalid ABIMethod parameters')\n }\n\n const args = method.args.map(({ type, name, desc, struct, defaultValue }) => {\n const convertedDefaultValue: ABIDefaultValue | undefined = defaultValue\n ? {\n data: defaultValue.data,\n source: defaultValue.source as DefaultValueSource,\n type: defaultValue.type ? (isAVMType(defaultValue.type) ? defaultValue.type : ABIType.from(defaultValue.type)) : undefined,\n }\n : undefined\n\n if (argTypeIsTransaction(type as ABIMethodArgType) || argTypeIsReference(type as ABIMethodArgType)) {\n return {\n type: type as ABIMethodArgType,\n name,\n description: desc,\n defaultValue: convertedDefaultValue,\n } satisfies ABIMethodArg\n }\n\n if (struct) {\n return {\n type: ABIStructType.fromStruct(struct, appSpec.structs),\n name,\n description: desc,\n defaultValue: convertedDefaultValue,\n } satisfies ABIMethodArg\n }\n\n return {\n type: ABIType.from(type),\n name,\n description: desc,\n defaultValue: convertedDefaultValue,\n } satisfies ABIMethodArg\n })\n\n const returns = {\n type:\n method.returns.type === ('void' as const)\n ? ('void' as const)\n : method.returns.struct\n ? ABIStructType.fromStruct(method.returns.struct, appSpec.structs)\n : ABIType.from(method.returns.type),\n desc: method.returns.desc,\n }\n\n return new ABIMethod({\n name: method.name,\n description: method.desc,\n args,\n returns,\n events: method.events,\n readonly: method.readonly,\n })\n}\n\nexport function argTypeIsTransaction(type: ABIMethodArgType): type is ABITransactionType {\n return (\n typeof type === 'string' &&\n (type === ABITransactionType.Txn ||\n type === ABITransactionType.Payment ||\n type === ABITransactionType.KeyRegistration ||\n type === ABITransactionType.AssetConfig ||\n type === ABITransactionType.AssetTransfer ||\n type === ABITransactionType.AssetFreeze ||\n type === ABITransactionType.AppCall)\n )\n}\n\nexport function argTypeIsReference(type: ABIMethodArgType): type is ABIReferenceType {\n return (\n typeof type === 'string' &&\n (type === ABIReferenceType.Account || type === ABIReferenceType.Application || type === ABIReferenceType.Asset)\n )\n}\n\nexport function argTypeIsAbiType(type: ABIMethodArgType): type is ABIType {\n return !argTypeIsTransaction(type) && !argTypeIsReference(type)\n}\n\nfunction getArc56MethodSignature(method: Arc56Method): string {\n const args = method.args.map((arg) => arg.type).join(',')\n const returns = method.returns.type\n return `${method.name}(${args})${returns}`\n}\n\nexport function decodeAVMValue(avmType: AVMType, bytes: Uint8Array): ABIValue {\n switch (avmType) {\n case 'AVMString':\n return Buffer.from(bytes).toString('utf-8')\n case 'AVMBytes':\n return bytes\n case 'AVMUint64':\n return ABIType.from('uint64').decode(bytes)\n }\n}\n\nexport function encodeAVMValue(avmType: AVMType, value: ABIValue): Uint8Array {\n switch (avmType) {\n case 'AVMString':\n return ABIType.from('string').encode(value)\n case 'AVMBytes':\n if (typeof value === 'string') return Buffer.from(value, 'utf-8')\n if (typeof value !== 'object' || !(value instanceof Uint8Array))\n throw new Error(`Expected bytes value for AVMBytes, but got ${value}`)\n return value\n case 'AVMUint64':\n return ABIType.from('uint64').encode(value)\n }\n}\n\nexport function isAVMType(type: unknown): type is AVMType {\n return typeof type === 'string' && (type === 'AVMString' || type === 'AVMBytes' || type === 'AVMUint64')\n}\n\nexport function getABIDecodedValue(type: AVMType | ABIType | ABIReferenceType, bytes: Uint8Array): ABIValue {\n if (type === ABIReferenceType.Asset || type === ABIReferenceType.Application) {\n return new ABIUintType(64).decode(bytes)\n } else if (type === ABIReferenceType.Account) {\n return new ABIAddressType().decode(bytes)\n } else if (isAVMType(type)) {\n return decodeAVMValue(type, bytes)\n }\n return type.decode(bytes)\n}\n\nexport function getABIEncodedValue(type: AVMType | ABIType, value: ABIValue): Uint8Array {\n return isAVMType(type) ? encodeAVMValue(type, value) : type.encode(value)\n}\n"],"mappings":";;;;;;AAMA,IAAY,oEAAL;AACL;AACA;AACA;AACA;AACA;AACA;AACA;;;AAEF,IAAY,gEAAL;AACL;AACA;AACA;;;AAuDF,IAAa,YAAb,MAAa,UAAU;CACrB,AAAS;CACT,AAAS;CACT,AAAS;CACT,AAAS;CACT,AAAS;CACT,AAAS;CAET,YAAY,QAOT;AACD,OAAK,OAAO,OAAO;AACnB,OAAK,cAAc,OAAO;AAC1B,OAAK,OAAO,OAAO;AACnB,OAAK,UAAU,OAAO;AACtB,OAAK,SAAS,OAAO;AACrB,OAAK,WAAW,OAAO;;;;;;CAOzB,eAAuB;EACrB,MAAM,OAAO,KAAK,KACf,KAAK,QAAQ;AACZ,OAAI,qBAAqB,IAAI,KAAK,IAAI,mBAAmB,IAAI,KAAK,CAAE,QAAO,IAAI;AAC/E,UAAO,IAAI,KAAK;IAChB,CACD,KAAK,IAAI;AAEZ,SAAO,GAAG,KAAK,KAAK,GAAG,KAAK,GADZ,KAAK,QAAQ,SAAS,SAAS,SAAS,KAAK,QAAQ,KAAK;;;;;;CAQ5E,cAA0B;EACxB,MAAM,OAAOA,kBAAO,WAAW,MAAM,KAAK,cAAc,CAAC;AACzD,SAAO,IAAI,WAAW,KAAK,MAAM,GAAG,EAAE,CAAC;;;;;;;;CASzC,OAAO,cAAc,WAA8B;EACjD,MAAM,YAAY,UAAU,QAAQ,IAAI;AACxC,MAAI,cAAc,GAChB,OAAM,IAAI,MAAM,6BAA6B,Y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/D,QAAO,IAAIG,6BAAY,GAAG,CAAC,OAAO,MAAM;UAC/B,SAAS,iBAAiB,QACnC,QAAO,IAAIC,iCAAgB,CAAC,OAAO,MAAM;UAChC,UAAU,KAAK,CACxB,QAAO,eAAe,MAAM,MAAM;AAEpC,QAAO,KAAK,OAAO,MAAM;;AAG3B,SAAgB,mBAAmB,MAAyB,OAA6B;AACvF,QAAO,UAAU,KAAK,GAAG,eAAe,MAAM,MAAM,GAAG,KAAK,OAAO,MAAM"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"abi-method.mjs","names":["convertedDefaultValue: ABIDefaultValue | undefined"],"sources":["../../../../packages/abi/src/abi-method.ts"],"sourcesContent":["import sha512 from 'js-sha512'\nimport { ABIAddressType, ABIStructType, ABIType, ABIUintType, parseTupleContent } from './abi-type'\nimport { ABIValue } from './abi-value'\nimport { ARC28Event } from './arc28-event'\nimport { AVMType, Arc56Contract, Arc56Method } from './arc56-contract'\n\nexport enum ABITransactionType {\n Txn = 'txn',\n Payment = 'pay',\n KeyRegistration = 'keyreg',\n AssetConfig = 'acfg',\n AssetTransfer = 'axfer',\n AssetFreeze = 'afrz',\n AppCall = 'appl',\n}\nexport enum ABIReferenceType {\n Account = 'account',\n Application = 'application',\n Asset = 'asset',\n}\nexport type ABIMethodArgType = ABIType | ABITransactionType | ABIReferenceType\nexport type ABIMethodReturnType = ABIType | 'void'\n\nexport type ABIMethodArg = {\n type: ABIMethodArgType\n name?: string\n description?: string\n defaultValue?: ABIDefaultValue\n}\n\nexport type ABIMethodReturn = {\n type: ABIMethodReturnType\n description?: string\n}\n\nexport type ABIDefaultValue = {\n /** Base64 encoded bytes, base64 ARC4 encoded uint64, or UTF-8 method selector */\n data: string\n /** Where the default value is coming from */\n source: DefaultValueSource\n /** How the data is encoded. This is the encoding for the data provided here, not the arg type */\n type?: ABIType | AVMType\n}\n\nexport enum DefaultValueSource {\n Box = 'box',\n Global = 'global',\n Local = 'local',\n Literal = 'literal',\n Method = 'method',\n}\n\n/** Represents an ABI method return value with parsed data. */\nexport type ABIReturn =\n | {\n /** The method that was called. */\n method: ABIMethod\n /** The raw return value as bytes.\n *\n * This is the value from the last app call log with the first 4 bytes (the ABI return prefix) omitted.\n */\n rawReturnValue: Uint8Array\n /** The parsed ABI return value. */\n returnValue: ABIValue\n decodeError: undefined\n }\n | { rawReturnValue?: undefined; returnValue?: undefined; method: ABIMethod; decodeError: Error }\n\n/** Decoded ARC-56 struct as a struct rather than a tuple. */\nexport type ABIStruct = {\n [key: string]: ABIStruct | ABIValue\n}\n\nexport class ABIMethod {\n readonly name: string\n readonly description?: string\n readonly args: ABIMethodArg[]\n readonly returns: ABIMethodReturn\n readonly events?: ARC28Event[]\n readonly readonly?: boolean\n\n constructor(params: {\n name: string\n description?: string\n args: ABIMethodArg[]\n returns: ABIMethodReturn\n events?: ARC28Event[]\n readonly?: boolean\n }) {\n this.name = params.name\n this.description = params.description\n this.args = params.args\n this.returns = params.returns\n this.events = params.events\n this.readonly = params.readonly\n }\n\n /**\n * Returns the signature of this ABI method.\n * @returns The signature, e.g. `my_method(unit64,string)bytes`\n */\n getSignature(): string {\n const args = this.args\n .map((arg) => {\n if (argTypeIsTransaction(arg.type) || argTypeIsReference(arg.type)) return arg.type\n return arg.type.name\n })\n .join(',')\n const returns = this.returns.type === 'void' ? 'void' : this.returns.type.name\n return `${this.name}(${args})${returns}`\n }\n\n /**\n * Returns the method selector of this ABI method.\n * @returns The 4-byte method selector\n */\n getSelector(): Uint8Array {\n const hash = sha512.sha512_256.array(this.getSignature())\n return new Uint8Array(hash.slice(0, 4))\n }\n\n /**\n * Returns the ABI method object for a given method signature.\n * @param signature The method signature\n * e.g. `my_method(unit64,string)bytes`\n * @returns The `ABIMethod`\n */\n static fromSignature(signature: string): ABIMethod {\n const argsStart = signature.indexOf('(')\n if (argsStart === -1) {\n throw new Error(`Invalid method signature: ${signature}`)\n }\n\n let argsEnd = -1\n let depth = 0\n for (let i = argsStart; i < signature.length; i++) {\n const char = signature[i]\n\n if (char === '(') {\n depth += 1\n } else if (char === ')') {\n if (depth === 0) {\n // unpaired parenthesis\n break\n }\n\n depth -= 1\n if (depth === 0) {\n argsEnd = i\n break\n }\n }\n }\n\n if (argsEnd === -1) {\n throw new Error(`Invalid method signature: ${signature}`)\n }\n\n const name = signature.slice(0, argsStart)\n const args = parseTupleContent(signature.slice(argsStart + 1, argsEnd)).map((n: string) => {\n if (argTypeIsTransaction(n as ABIMethodArgType) || argTypeIsReference(n as ABIMethodArgType)) {\n return { type: n as ABIMethodArgType } satisfies ABIMethodArg\n }\n return { type: ABIType.from(n) } satisfies ABIMethodArg\n })\n const returnType = signature.slice(argsEnd + 1)\n const returns = { type: returnType === 'void' ? ('void' as const) : ABIType.from(returnType) } satisfies ABIMethodReturn\n\n return new ABIMethod({\n name,\n args,\n returns,\n })\n }\n}\n\n/**\n * Returns the ABI method object for a given method name or signature and ARC-56 app spec.\n * @param methodNameOrSignature The method name or method signature to call if an ABI call is being emitted.\n * e.g. `my_method` or `my_method(unit64,string)bytes`\n * @param appSpec The app spec for the app\n * @returns The `ABIMethod`\n */\nexport function getABIMethod(methodNameOrSignature: string, appSpec: Arc56Contract): ABIMethod {\n if (!methodNameOrSignature.includes('(')) {\n const methods = appSpec.methods.filter((m) => m.name === methodNameOrSignature)\n if (methods.length === 0) throw new Error(`Unable to find method ${methodNameOrSignature} in ${appSpec.name} app.`)\n if (methods.length > 1) {\n throw new Error(\n `Received a call to method ${methodNameOrSignature} in contract ${\n appSpec.name\n }, but this resolved to multiple methods; please pass in an ABI signature instead: ${appSpec.methods\n .map((m) => getArc56MethodSignature(m))\n .join(', ')}`,\n )\n }\n return arc56MethodToABIMethod(methods[0], appSpec)\n } else {\n const method = appSpec.methods.find((m) => getArc56MethodSignature(m) === methodNameOrSignature)\n if (!method) throw new Error(`Unable to find method ${methodNameOrSignature} in ${appSpec.name} app.`)\n return arc56MethodToABIMethod(method, appSpec)\n }\n}\n\nexport function arc56MethodToABIMethod(method: Arc56Method, appSpec: Arc56Contract): ABIMethod {\n if (typeof method.name !== 'string' || typeof method.returns !== 'object' || !Array.isArray(method.args)) {\n throw new Error('Invalid ABIMethod parameters')\n }\n\n const args = method.args.map(({ type, name, desc, struct, defaultValue }) => {\n const convertedDefaultValue: ABIDefaultValue | undefined = defaultValue\n ? {\n data: defaultValue.data,\n source: defaultValue.source as DefaultValueSource,\n type: defaultValue.type ? (isAVMType(defaultValue.type) ? defaultValue.type : ABIType.from(defaultValue.type)) : undefined,\n }\n : undefined\n\n if (argTypeIsTransaction(type as ABIMethodArgType) || argTypeIsReference(type as ABIMethodArgType)) {\n return {\n type: type as ABIMethodArgType,\n name,\n description: desc,\n defaultValue: convertedDefaultValue,\n } satisfies ABIMethodArg\n }\n\n if (struct) {\n return {\n type: ABIStructType.fromStruct(struct, appSpec.structs),\n name,\n description: desc,\n defaultValue: convertedDefaultValue,\n } satisfies ABIMethodArg\n }\n\n return {\n type: ABIType.from(type),\n name,\n description: desc,\n defaultValue: convertedDefaultValue,\n } satisfies ABIMethodArg\n })\n\n const returns = {\n type:\n method.returns.type === ('void' as const)\n ? ('void' as const)\n : method.returns.struct\n ? ABIStructType.fromStruct(method.returns.struct, appSpec.structs)\n : ABIType.from(method.returns.type),\n desc: method.returns.desc,\n }\n\n return new ABIMethod({\n name: method.name,\n description: method.desc,\n args,\n returns,\n events: method.events,\n readonly: method.readonly,\n })\n}\n\nexport function argTypeIsTransaction(type: ABIMethodArgType): type is ABITransactionType {\n return (\n typeof type === 'string' &&\n (type === ABITransactionType.Txn ||\n type === ABITransactionType.Payment ||\n type === ABITransactionType.KeyRegistration ||\n type === ABITransactionType.AssetConfig ||\n type === ABITransactionType.AssetTransfer ||\n type === ABITransactionType.AssetFreeze ||\n type === ABITransactionType.AppCall)\n )\n}\n\nexport function argTypeIsReference(type: ABIMethodArgType): type is ABIReferenceType {\n return (\n typeof type === 'string' &&\n (type === ABIReferenceType.Account || type === ABIReferenceType.Application || type === ABIReferenceType.Asset)\n )\n}\n\nexport function argTypeIsAbiType(type: ABIMethodArgType): type is ABIType {\n return !argTypeIsTransaction(type) && !argTypeIsReference(type)\n}\n\nfunction getArc56MethodSignature(method: Arc56Method): string {\n const args = method.args.map((arg) => arg.type).join(',')\n const returns = method.returns.type\n return `${method.name}(${args})${returns}`\n}\n\nexport function decodeAVMValue(avmType: AVMType, bytes: Uint8Array): ABIValue {\n switch (avmType) {\n case 'AVMString':\n return Buffer.from(bytes).toString('utf-8')\n case 'AVMBytes':\n return bytes\n case 'AVMUint64':\n return ABIType.from('uint64').decode(bytes)\n }\n}\n\nexport function encodeAVMValue(avmType: AVMType, value: ABIValue): Uint8Array {\n switch (avmType) {\n case 'AVMString':\n return ABIType.from('string').encode(value)\n case 'AVMBytes':\n if (typeof value === 'string') return Buffer.from(value, 'utf-8')\n if (typeof value !== 'object' || !(value instanceof Uint8Array))\n throw new Error(`Expected bytes value for AVMBytes, but got ${value}`)\n return value\n case 'AVMUint64':\n return ABIType.from('uint64').encode(value)\n }\n}\n\nexport function isAVMType(type: unknown): type is AVMType {\n return typeof type === 'string' && (type === 'AVMString' || type === 'AVMBytes' || type === 'AVMUint64')\n}\n\nexport function getABIDecodedValue(type: AVMType | ABIType | ABIReferenceType, bytes: Uint8Array): ABIValue {\n if (type === ABIReferenceType.Asset || type === ABIReferenceType.Application) {\n return new ABIUintType(64).decode(bytes)\n } else if (type === ABIReferenceType.Account) {\n return new ABIAddressType().decode(bytes)\n } else if (isAVMType(type)) {\n return decodeAVMValue(type, bytes)\n }\n return type.decode(bytes)\n}\n\nexport function getABIEncodedValue(type: AVMType | ABIType, value: ABIValue): Uint8Array {\n return isAVMType(type) ? encodeAVMValue(type, value) : type.encode(value)\n}\n"],"mappings":";;;;AAMA,IAAY,oEAAL;AACL;AACA;AACA;AACA;AACA;AACA;AACA;;;AAEF,IAAY,gEAAL;AACL;AACA;AACA;;;AAuDF,IAAa,YAAb,MAAa,UAAU;CACrB,AAAS;CACT,AAAS;C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jC,SAAgB,eAAe,SAAkB,OAA6B;AAC5E,SAAQ,SAAR;EACE,KAAK,YACH,QAAO,OAAO,KAAK,MAAM,CAAC,SAAS,QAAQ;EAC7C,KAAK,WACH,QAAO;EACT,KAAK,YACH,QAAO,QAAQ,KAAK,SAAS,CAAC,OAAO,MAAM;;;AAIjD,SAAgB,eAAe,SAAkB,OAA6B;AAC5E,SAAQ,SAAR;EACE,KAAK,YACH,QAAO,QAAQ,KAAK,SAAS,CAAC,OAAO,MAAM;EAC7C,KAAK;AACH,OAAI,OAAO,UAAU,SAAU,QAAO,OAAO,KAAK,OAAO,QAAQ;AACjE,OAAI,OAAO,UAAU,YAAY,EAAE,iBAAiB,YAClD,OAAM,IAAI,MAAM,8CAA8C,QAAQ;AACxE,UAAO;EACT,KAAK,YACH,QAAO,QAAQ,KAAK,SAAS,CAAC,OAAO,MAAM;;;AAIjD,SAAgB,UAAU,MAAgC;AACxD,QAAO,OAAO,SAAS,aAAa,SAAS,eAAe,SAAS,cAAc,SAAS;;AAG9F,SAAgB,mBAAmB,MAA4C,OAA6B;AAC1G,KAAI,SAAS,iBAAiB,SAAS,SAAS,iBAAiB,YAC/D,QAAO,IAAI,YAAY,GAAG,CAAC,OAAO,MAAM;UAC/B,SAAS,iBAAiB,QACnC,QAAO,IAAI,gBAAgB,CAAC,OAAO,MAAM;UAChC,UAAU,KAAK,CACxB,QAAO,eAAe,MAAM,MAAM;AAEpC,QAAO,KAAK,OAAO,MAAM;;AAG3B,SAAgB,mBAAmB,MAAyB,OAA6B;AACvF,QAAO,UAAU,KAAK,GAAG,eAAe,MAAM,MAAM,GAAG,KAAK,OAAO,MAAM"}
|
|
1
|
+
{"version":3,"file":"abi-method.mjs","names":["convertedDefaultValue: ABIDefaultValue | undefined"],"sources":["../../../../packages/abi/src/abi-method.ts"],"sourcesContent":["import sha512 from 'js-sha512'\nimport { ABIAddressType, ABIStructType, ABIType, ABIUintType, parseTupleContent } from './abi-type'\nimport { ABIValue } from './abi-value'\nimport { ARC28Event } from './arc28-event'\nimport { AVMType, Arc56Contract, Arc56Method } from './arc56-contract'\n\nexport enum ABITransactionType {\n Txn = 'txn',\n Payment = 'pay',\n KeyRegistration = 'keyreg',\n AssetConfig = 'acfg',\n AssetTransfer = 'axfer',\n AssetFreeze = 'afrz',\n AppCall = 'appl',\n}\nexport enum ABIReferenceType {\n Account = 'account',\n Application = 'application',\n Asset = 'asset',\n}\nexport type ABIMethodArgType = ABIType | ABITransactionType | ABIReferenceType\nexport type ABIMethodReturnType = ABIType | 'void'\n\nexport type ABIMethodArg = {\n type: ABIMethodArgType\n name?: string\n description?: string\n defaultValue?: ABIDefaultValue\n}\n\nexport type ABIMethodReturn = {\n type: ABIMethodReturnType\n description?: string\n}\n\nexport type ABIDefaultValue = {\n /** Base64 encoded bytes, base64 ARC4 encoded uint64, or UTF-8 method selector */\n data: string\n /** Where the default value is coming from */\n source: DefaultValueSource\n /** How the data is encoded. This is the encoding for the data provided here, not the arg type */\n type?: ABIType | AVMType\n}\n\nexport enum DefaultValueSource {\n Box = 'box',\n Global = 'global',\n Local = 'local',\n Literal = 'literal',\n Method = 'method',\n}\n\n/** Represents an ABI method return value with parsed data. */\nexport type ABIReturn =\n | {\n /** The method that was called. */\n method: ABIMethod\n /** The raw return value as bytes.\n *\n * This is the value from the last app call log with the first 4 bytes (the ABI return prefix) omitted.\n */\n rawReturnValue: Uint8Array\n /** The parsed ABI return value. */\n returnValue: ABIValue\n decodeError: undefined\n }\n | { rawReturnValue?: undefined; returnValue?: undefined; method: ABIMethod; decodeError?: Error }\n\n/** Decoded ARC-56 struct as a struct rather than a tuple. */\nexport type ABIStruct = {\n [key: string]: ABIStruct | ABIValue\n}\n\nexport class ABIMethod {\n readonly name: string\n readonly description?: string\n readonly args: ABIMethodArg[]\n readonly returns: ABIMethodReturn\n readonly events?: ARC28Event[]\n readonly readonly?: boolean\n\n constructor(params: {\n name: string\n description?: string\n args: ABIMethodArg[]\n returns: ABIMethodReturn\n events?: ARC28Event[]\n readonly?: boolean\n }) {\n this.name = params.name\n this.description = params.description\n this.args = params.args\n this.returns = params.returns\n this.events = params.events\n this.readonly = params.readonly\n }\n\n /**\n * Returns the signature of this ABI method.\n * @returns The signature, e.g. `my_method(unit64,string)bytes`\n */\n getSignature(): string {\n const args = this.args\n .map((arg) => {\n if (argTypeIsTransaction(arg.type) || argTypeIsReference(arg.type)) return arg.type\n return arg.type.name\n })\n .join(',')\n const returns = this.returns.type === 'void' ? 'void' : this.returns.type.name\n return `${this.name}(${args})${returns}`\n }\n\n /**\n * Returns the method selector of this ABI method.\n * @returns The 4-byte method selector\n */\n getSelector(): Uint8Array {\n const hash = sha512.sha512_256.array(this.getSignature())\n return new Uint8Array(hash.slice(0, 4))\n }\n\n /**\n * Returns the ABI method object for a given method signature.\n * @param signature The method signature\n * e.g. `my_method(unit64,string)bytes`\n * @returns The `ABIMethod`\n */\n static fromSignature(signature: string): ABIMethod {\n const argsStart = signature.indexOf('(')\n if (argsStart === -1) {\n throw new Error(`Invalid method signature: ${signature}`)\n }\n\n let argsEnd = -1\n let depth = 0\n for (let i = argsStart; i < signature.length; i++) {\n const char = signature[i]\n\n if (char === '(') {\n depth += 1\n } else if (char === ')') {\n if (depth === 0) {\n // unpaired parenthesis\n break\n }\n\n depth -= 1\n if (depth === 0) {\n argsEnd = i\n break\n }\n }\n }\n\n if (argsEnd === -1) {\n throw new Error(`Invalid method signature: ${signature}`)\n }\n\n const name = signature.slice(0, argsStart)\n const args = parseTupleContent(signature.slice(argsStart + 1, argsEnd)).map((n: string) => {\n if (argTypeIsTransaction(n as ABIMethodArgType) || argTypeIsReference(n as ABIMethodArgType)) {\n return { type: n as ABIMethodArgType } satisfies ABIMethodArg\n }\n return { type: ABIType.from(n) } satisfies ABIMethodArg\n })\n const returnType = signature.slice(argsEnd + 1)\n const returns = { type: returnType === 'void' ? ('void' as const) : ABIType.from(returnType) } satisfies ABIMethodReturn\n\n return new ABIMethod({\n name,\n args,\n returns,\n })\n }\n}\n\n/**\n * Returns the ABI method object for a given method name or signature and ARC-56 app spec.\n * @param methodNameOrSignature The method name or method signature to call if an ABI call is being emitted.\n * e.g. `my_method` or `my_method(unit64,string)bytes`\n * @param appSpec The app spec for the app\n * @returns The `ABIMethod`\n */\nexport function getABIMethod(methodNameOrSignature: string, appSpec: Arc56Contract): ABIMethod {\n if (!methodNameOrSignature.includes('(')) {\n const methods = appSpec.methods.filter((m) => m.name === methodNameOrSignature)\n if (methods.length === 0) throw new Error(`Unable to find method ${methodNameOrSignature} in ${appSpec.name} app.`)\n if (methods.length > 1) {\n throw new Error(\n `Received a call to method ${methodNameOrSignature} in contract ${\n appSpec.name\n }, but this resolved to multiple methods; please pass in an ABI signature instead: ${appSpec.methods\n .map((m) => getArc56MethodSignature(m))\n .join(', ')}`,\n )\n }\n return arc56MethodToABIMethod(methods[0], appSpec)\n } else {\n const method = appSpec.methods.find((m) => getArc56MethodSignature(m) === methodNameOrSignature)\n if (!method) throw new Error(`Unable to find method ${methodNameOrSignature} in ${appSpec.name} app.`)\n return arc56MethodToABIMethod(method, appSpec)\n }\n}\n\nexport function arc56MethodToABIMethod(method: Arc56Method, appSpec: Arc56Contract): ABIMethod {\n if (typeof method.name !== 'string' || typeof method.returns !== 'object' || !Array.isArray(method.args)) {\n throw new Error('Invalid ABIMethod parameters')\n }\n\n const args = method.args.map(({ type, name, desc, struct, defaultValue }) => {\n const convertedDefaultValue: ABIDefaultValue | undefined = defaultValue\n ? {\n data: defaultValue.data,\n source: defaultValue.source as DefaultValueSource,\n type: defaultValue.type ? (isAVMType(defaultValue.type) ? defaultValue.type : ABIType.from(defaultValue.type)) : undefined,\n }\n : undefined\n\n if (argTypeIsTransaction(type as ABIMethodArgType) || argTypeIsReference(type as ABIMethodArgType)) {\n return {\n type: type as ABIMethodArgType,\n name,\n description: desc,\n defaultValue: convertedDefaultValue,\n } satisfies ABIMethodArg\n }\n\n if (struct) {\n return {\n type: ABIStructType.fromStruct(struct, appSpec.structs),\n name,\n description: desc,\n defaultValue: convertedDefaultValue,\n } satisfies ABIMethodArg\n }\n\n return {\n type: ABIType.from(type),\n name,\n description: desc,\n defaultValue: convertedDefaultValue,\n } satisfies ABIMethodArg\n })\n\n const returns = {\n type:\n method.returns.type === ('void' as const)\n ? ('void' as const)\n : method.returns.struct\n ? ABIStructType.fromStruct(method.returns.struct, appSpec.structs)\n : ABIType.from(method.returns.type),\n desc: method.returns.desc,\n }\n\n return new ABIMethod({\n name: method.name,\n description: method.desc,\n args,\n returns,\n events: method.events,\n readonly: method.readonly,\n })\n}\n\nexport function argTypeIsTransaction(type: ABIMethodArgType): type is ABITransactionType {\n return (\n typeof type === 'string' &&\n (type === ABITransactionType.Txn ||\n type === ABITransactionType.Payment ||\n type === ABITransactionType.KeyRegistration ||\n type === ABITransactionType.AssetConfig ||\n type === ABITransactionType.AssetTransfer ||\n type === ABITransactionType.AssetFreeze ||\n type === ABITransactionType.AppCall)\n )\n}\n\nexport function argTypeIsReference(type: ABIMethodArgType): type is ABIReferenceType {\n return (\n typeof type === 'string' &&\n (type === ABIReferenceType.Account || type === ABIReferenceType.Application || type === ABIReferenceType.Asset)\n )\n}\n\nexport function argTypeIsAbiType(type: ABIMethodArgType): type is ABIType {\n return !argTypeIsTransaction(type) && !argTypeIsReference(type)\n}\n\nfunction getArc56MethodSignature(method: Arc56Method): string {\n const args = method.args.map((arg) => arg.type).join(',')\n const returns = method.returns.type\n return `${method.name}(${args})${returns}`\n}\n\nexport function decodeAVMValue(avmType: AVMType, bytes: Uint8Array): ABIValue {\n switch (avmType) {\n case 'AVMString':\n return Buffer.from(bytes).toString('utf-8')\n case 'AVMBytes':\n return bytes\n case 'AVMUint64':\n return ABIType.from('uint64').decode(bytes)\n }\n}\n\nexport function encodeAVMValue(avmType: AVMType, value: ABIValue): Uint8Array {\n switch (avmType) {\n case 'AVMString':\n return ABIType.from('string').encode(value)\n case 'AVMBytes':\n if (typeof value === 'string') return Buffer.from(value, 'utf-8')\n if (typeof value !== 'object' || !(value instanceof Uint8Array))\n throw new Error(`Expected bytes value for AVMBytes, but got ${value}`)\n return value\n case 'AVMUint64':\n return ABIType.from('uint64').encode(value)\n }\n}\n\nexport function isAVMType(type: unknown): type is AVMType {\n return typeof type === 'string' && (type === 'AVMString' || type === 'AVMBytes' || type === 'AVMUint64')\n}\n\nexport function getABIDecodedValue(type: AVMType | ABIType | ABIReferenceType, bytes: Uint8Array): ABIValue {\n if (type === ABIReferenceType.Asset || type === ABIReferenceType.Application) {\n return new ABIUintType(64).decode(bytes)\n } else if (type === ABIReferenceType.Account) {\n return new ABIAddressType().decode(bytes)\n } else if (isAVMType(type)) {\n return decodeAVMValue(type, bytes)\n }\n return type.decode(bytes)\n}\n\nexport function getABIEncodedValue(type: AVMType | ABIType, value: ABIValue): Uint8Array {\n return isAVMType(type) ? encodeAVMValue(type, value) : type.encode(value)\n}\n"],"mappings":";;;;AAMA,IAAY,oEAAL;AACL;AACA;AACA;AACA;AACA;AACA;AACA;;;AAEF,IAAY,gEAAL;AACL;AACA;AACA;;;AAuDF,IAAa,YAAb,MAAa,UAAU;CACrB,AAAS;CACT,AAAS;CACT,AAAS;CACT,AAAS;CACT,AAAS;CACT,AAAS;CAET,YAAY,QAOT;AACD,OAAK,OAAO,OAAO;AACnB,OAAK,cAAc,OAAO;AAC1B,OAAK,OAAO,OAAO;AACnB,OAAK,UAAU,OAAO;AACtB,OAAK,SAAS,OAAO;AACrB,OAAK,WAAW,OAAO;;;;;;CAOzB,eAAuB;EACrB,MAAM,OAAO,KAAK,KACf,KAAK,QAAQ;AACZ,OAAI,qBAAqB,IAAI,KAAK,IAAI,mBAAmB,IAAI,KAAK,CAAE,QAAO,IAAI;AAC/E,UAAO,IAAI,KAAK;IAChB,CACD,KAAK,IAAI;AAEZ,SAAO,GAAG,KAAK,KAAK,GAAG,KAAK,GADZ,KAAK,QAAQ,SAAS,SAAS,SAAS,KAAK,QAAQ,KAAK;;;;;;CAQ5E,cAA0B;EACxB,MAAM,OAAO,OAAO,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M;;;AAIjD,SAAgB,UAAU,MAAgC;AACxD,QAAO,OAAO,SAAS,aAAa,SAAS,eAAe,SAAS,cAAc,SAAS;;AAG9F,SAAgB,mBAAmB,MAA4C,OAA6B;AAC1G,KAAI,SAAS,iBAAiB,SAAS,SAAS,iBAAiB,YAC/D,QAAO,IAAI,YAAY,GAAG,CAAC,OAAO,MAAM;UAC/B,SAAS,iBAAiB,QACnC,QAAO,IAAI,gBAAgB,CAAC,OAAO,MAAM;UAChC,UAAU,KAAK,CACxB,QAAO,eAAe,MAAM,MAAM;AAEpC,QAAO,KAAK,OAAO,MAAM;;AAG3B,SAAgB,mBAAmB,MAAyB,OAA6B;AACvF,QAAO,UAAU,KAAK,GAAG,eAAe,MAAM,MAAM,GAAG,KAAK,OAAO,MAAM"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
//#region packages/algo25/src/index.d.ts
|
|
2
|
+
declare const FAIL_TO_DECODE_MNEMONIC_ERROR_MSG = "failed to decode mnemonic";
|
|
3
|
+
declare const NOT_IN_WORDS_LIST_ERROR_MSG = "the mnemonic contains a word that is not in the wordlist";
|
|
4
|
+
/**
|
|
5
|
+
* mnemonicFromSeed converts a 32-byte key into a 25 word mnemonic. The generated mnemonic includes a checksum.
|
|
6
|
+
* Each word in the mnemonic represents 11 bits of data, and the last 11 bits are reserved for the checksum.
|
|
7
|
+
* @param seed - 32 bytes long seed
|
|
8
|
+
* @returns 25 words mnemonic
|
|
9
|
+
*/
|
|
10
|
+
declare function mnemonicFromSeed(seed: Uint8Array): string;
|
|
11
|
+
/**
|
|
12
|
+
* seedFromMnemonic converts a mnemonic generated using this library into the source key used to create it.
|
|
13
|
+
* It returns an error if the passed mnemonic has an incorrect checksum, if the number of words is unexpected, or if one
|
|
14
|
+
* of the passed words is not found in the words list.
|
|
15
|
+
* @param mnemonic - 25 words mnemonic
|
|
16
|
+
* @returns 32 bytes long seed
|
|
17
|
+
*/
|
|
18
|
+
declare function seedFromMnemonic(mnemonic: string): Uint8Array;
|
|
19
|
+
/**
|
|
20
|
+
* secretKeyToMnemonic takes an Algorand secret key and returns the corresponding mnemonic.
|
|
21
|
+
* @param sk - Algorand secret key
|
|
22
|
+
* @returns Secret key's associated mnemonic
|
|
23
|
+
*/
|
|
24
|
+
declare function secretKeyToMnemonic(sk: Uint8Array): string;
|
|
25
|
+
/**
|
|
26
|
+
* mnemonicToMasterDerivationKey takes a mnemonic string and returns the corresponding master derivation key.
|
|
27
|
+
* @param mn - 25 words Algorand mnemonic
|
|
28
|
+
* @returns Uint8Array
|
|
29
|
+
* @throws error if fails to decode the mnemonic
|
|
30
|
+
*/
|
|
31
|
+
declare function mnemonicToMasterDerivationKey(mn: string): Uint8Array;
|
|
32
|
+
/**
|
|
33
|
+
* masterDerivationKeyToMnemonic takes a master derivation key and returns the corresponding mnemonic.
|
|
34
|
+
* @param mdk - Uint8Array
|
|
35
|
+
* @returns string mnemonic
|
|
36
|
+
*/
|
|
37
|
+
declare function masterDerivationKeyToMnemonic(mdk: Uint8Array): string;
|
|
38
|
+
//#endregion
|
|
39
|
+
export { FAIL_TO_DECODE_MNEMONIC_ERROR_MSG, NOT_IN_WORDS_LIST_ERROR_MSG, masterDerivationKeyToMnemonic, mnemonicFromSeed, mnemonicToMasterDerivationKey, secretKeyToMnemonic, seedFromMnemonic };
|
|
40
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -4,6 +4,7 @@ const require_english = require('./english.js');
|
|
|
4
4
|
//#region packages/algo25/src/index.ts
|
|
5
5
|
const FAIL_TO_DECODE_MNEMONIC_ERROR_MSG = "failed to decode mnemonic";
|
|
6
6
|
const NOT_IN_WORDS_LIST_ERROR_MSG = "the mnemonic contains a word that is not in the wordlist";
|
|
7
|
+
const SEED_BTYES_LENGTH = 32;
|
|
7
8
|
function toUint11Array(buffer8) {
|
|
8
9
|
const buffer11 = [];
|
|
9
10
|
let acc = 0;
|
|
@@ -30,6 +31,18 @@ function applyWords(nums) {
|
|
|
30
31
|
function computeChecksum(seed) {
|
|
31
32
|
return applyWords(toUint11Array(require_crypto.hash(seed)))[0];
|
|
32
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* mnemonicFromSeed converts a 32-byte key into a 25 word mnemonic. The generated mnemonic includes a checksum.
|
|
36
|
+
* Each word in the mnemonic represents 11 bits of data, and the last 11 bits are reserved for the checksum.
|
|
37
|
+
* @param seed - 32 bytes long seed
|
|
38
|
+
* @returns 25 words mnemonic
|
|
39
|
+
*/
|
|
40
|
+
function mnemonicFromSeed(seed) {
|
|
41
|
+
if (seed.length !== SEED_BTYES_LENGTH) throw new RangeError(`Seed length must be ${SEED_BTYES_LENGTH}`);
|
|
42
|
+
const words = applyWords(toUint11Array(seed));
|
|
43
|
+
const checksumWord = computeChecksum(seed);
|
|
44
|
+
return `${words.join(" ")} ${checksumWord}`;
|
|
45
|
+
}
|
|
33
46
|
function toUint8Array(buffer11) {
|
|
34
47
|
const buffer8 = [];
|
|
35
48
|
let acc = 0;
|
|
@@ -69,7 +82,38 @@ function seedFromMnemonic(mnemonic) {
|
|
|
69
82
|
if (computeChecksum(uint8Array) === checksum) return uint8Array;
|
|
70
83
|
throw new Error(FAIL_TO_DECODE_MNEMONIC_ERROR_MSG);
|
|
71
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* secretKeyToMnemonic takes an Algorand secret key and returns the corresponding mnemonic.
|
|
87
|
+
* @param sk - Algorand secret key
|
|
88
|
+
* @returns Secret key's associated mnemonic
|
|
89
|
+
*/
|
|
90
|
+
function secretKeyToMnemonic(sk) {
|
|
91
|
+
return mnemonicFromSeed(sk.slice(0, SEED_BTYES_LENGTH));
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* mnemonicToMasterDerivationKey takes a mnemonic string and returns the corresponding master derivation key.
|
|
95
|
+
* @param mn - 25 words Algorand mnemonic
|
|
96
|
+
* @returns Uint8Array
|
|
97
|
+
* @throws error if fails to decode the mnemonic
|
|
98
|
+
*/
|
|
99
|
+
function mnemonicToMasterDerivationKey(mn) {
|
|
100
|
+
return seedFromMnemonic(mn);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* masterDerivationKeyToMnemonic takes a master derivation key and returns the corresponding mnemonic.
|
|
104
|
+
* @param mdk - Uint8Array
|
|
105
|
+
* @returns string mnemonic
|
|
106
|
+
*/
|
|
107
|
+
function masterDerivationKeyToMnemonic(mdk) {
|
|
108
|
+
return mnemonicFromSeed(mdk);
|
|
109
|
+
}
|
|
72
110
|
|
|
73
111
|
//#endregion
|
|
112
|
+
exports.FAIL_TO_DECODE_MNEMONIC_ERROR_MSG = FAIL_TO_DECODE_MNEMONIC_ERROR_MSG;
|
|
113
|
+
exports.NOT_IN_WORDS_LIST_ERROR_MSG = NOT_IN_WORDS_LIST_ERROR_MSG;
|
|
114
|
+
exports.masterDerivationKeyToMnemonic = masterDerivationKeyToMnemonic;
|
|
115
|
+
exports.mnemonicFromSeed = mnemonicFromSeed;
|
|
116
|
+
exports.mnemonicToMasterDerivationKey = mnemonicToMasterDerivationKey;
|
|
117
|
+
exports.secretKeyToMnemonic = secretKeyToMnemonic;
|
|
74
118
|
exports.seedFromMnemonic = seedFromMnemonic;
|
|
75
119
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["buffer11: number[]","english","hash","buffer8: number[]"],"sources":["../../../../packages/algo25/src/index.ts"],"sourcesContent":["import english from './english.js'\nimport { hash } from '@algorandfoundation/algokit-common'\nexport const FAIL_TO_DECODE_MNEMONIC_ERROR_MSG = 'failed to decode mnemonic'\nexport const NOT_IN_WORDS_LIST_ERROR_MSG = 'the mnemonic contains a word that is not in the wordlist'\n\nconst SEED_BTYES_LENGTH = 32\n\n// https://stackoverflow.com/a/51452614\nfunction toUint11Array(buffer8: Uint8Array | number[]): number[] {\n const buffer11: number[] = []\n let acc = 0\n let accBits = 0\n function add(octet: number) {\n acc |= octet << accBits\n accBits += 8\n if (accBits >= 11) {\n buffer11.push(acc & 0x7ff)\n acc >>= 11\n accBits -= 11\n }\n }\n function flush() {\n if (accBits) {\n buffer11.push(acc)\n }\n }\n\n buffer8.forEach(add)\n flush()\n return buffer11\n}\n\nfunction applyWords(nums: number[]): string[] {\n return nums.map((n) => english[n])\n}\n\nfunction computeChecksum(seed: Uint8Array): string {\n const hashBuffer = hash(seed)\n const uint11Hash = toUint11Array(hashBuffer)\n const words = applyWords(uint11Hash)\n\n return words[0]\n}\n\n/**\n * mnemonicFromSeed converts a 32-byte key into a 25 word mnemonic. The generated mnemonic includes a checksum.\n * Each word in the mnemonic represents 11 bits of data, and the last 11 bits are reserved for the checksum.\n * @param seed - 32 bytes long seed\n * @returns 25 words mnemonic\n */\nexport function mnemonicFromSeed(seed: Uint8Array) {\n // Sanity length check\n if (seed.length !== SEED_BTYES_LENGTH) {\n throw new RangeError(`Seed length must be ${SEED_BTYES_LENGTH}`)\n }\n\n const uint11Array = toUint11Array(seed)\n const words = applyWords(uint11Array)\n const checksumWord = computeChecksum(seed)\n\n return `${words.join(' ')} ${checksumWord}`\n}\n\n// from Uint11Array\n// https://stackoverflow.com/a/51452614\nfunction toUint8Array(buffer11: number[]): Uint8Array {\n const buffer8: number[] = []\n let acc = 0\n let accBits = 0\n function add(ui11: number) {\n acc |= ui11 << accBits\n accBits += 11\n while (accBits >= 8) {\n buffer8.push(acc & 0xff)\n acc >>= 8\n accBits -= 8\n }\n }\n function flush() {\n if (accBits) {\n buffer8.push(acc)\n }\n }\n\n buffer11.forEach(add)\n flush()\n return new Uint8Array(buffer8)\n}\n\n/**\n * seedFromMnemonic converts a mnemonic generated using this library into the source key used to create it.\n * It returns an error if the passed mnemonic has an incorrect checksum, if the number of words is unexpected, or if one\n * of the passed words is not found in the words list.\n * @param mnemonic - 25 words mnemonic\n * @returns 32 bytes long seed\n */\nexport function seedFromMnemonic(mnemonic: string) {\n const words = mnemonic.split(' ')\n const key = words.slice(0, 24)\n\n // Check that all words are in list\n for (const w of key) {\n if (english.indexOf(w) === -1) throw new Error(NOT_IN_WORDS_LIST_ERROR_MSG)\n }\n\n const checksum = words[words.length - 1]\n const uint11Array = key.map((word) => english.indexOf(word))\n\n // Convert the key to uint8Array\n let uint8Array = toUint8Array(uint11Array)\n\n // We need to chop the last byte -\n // the short explanation - Since 256 is not divisible by 11, we have an extra 0x0 byte.\n // The longer explanation - When splitting the 256 bits to chunks of 11, we get 23 words and a left over of 3 bits.\n // This left gets padded with another 8 bits to the create the 24th word.\n // While converting back to byte array, our new 264 bits array is divisible by 8 but the last byte is just the padding.\n\n // check that we have 33 bytes long array as expected\n if (uint8Array.length !== 33) throw new Error(FAIL_TO_DECODE_MNEMONIC_ERROR_MSG)\n\n // check that the last byte is actually 0x0\n if (uint8Array[uint8Array.length - 1] !== 0x0) throw new Error(FAIL_TO_DECODE_MNEMONIC_ERROR_MSG)\n\n // chop it !\n uint8Array = uint8Array.slice(0, uint8Array.length - 1)\n\n // compute checksum\n const cs = computeChecksum(uint8Array)\n\n // success!\n if (cs === checksum) return uint8Array\n\n throw new Error(FAIL_TO_DECODE_MNEMONIC_ERROR_MSG)\n}\n\n/**\n * secretKeyToMnemonic takes an Algorand secret key and returns the corresponding mnemonic.\n * @param sk - Algorand secret key\n * @returns Secret key's associated mnemonic\n */\nexport function secretKeyToMnemonic(sk: Uint8Array) {\n // get the seed from the sk\n const seed = sk.slice(0, SEED_BTYES_LENGTH)\n return mnemonicFromSeed(seed)\n}\n\n/**\n * mnemonicToMasterDerivationKey takes a mnemonic string and returns the corresponding master derivation key.\n * @param mn - 25 words Algorand mnemonic\n * @returns Uint8Array\n * @throws error if fails to decode the mnemonic\n */\nexport function mnemonicToMasterDerivationKey(mn: string) {\n return seedFromMnemonic(mn)\n}\n\n/**\n * masterDerivationKeyToMnemonic takes a master derivation key and returns the corresponding mnemonic.\n * @param mdk - Uint8Array\n * @returns string mnemonic\n */\nexport function masterDerivationKeyToMnemonic(mdk: Uint8Array) {\n return mnemonicFromSeed(mdk)\n}\n"],"mappings":";;;;AAEA,MAAa,oCAAoC;AACjD,MAAa,8BAA8B;
|
|
1
|
+
{"version":3,"file":"index.js","names":["buffer11: number[]","english","hash","buffer8: number[]"],"sources":["../../../../packages/algo25/src/index.ts"],"sourcesContent":["import english from './english.js'\nimport { hash } from '@algorandfoundation/algokit-common'\nexport const FAIL_TO_DECODE_MNEMONIC_ERROR_MSG = 'failed to decode mnemonic'\nexport const NOT_IN_WORDS_LIST_ERROR_MSG = 'the mnemonic contains a word that is not in the wordlist'\n\nconst SEED_BTYES_LENGTH = 32\n\n// https://stackoverflow.com/a/51452614\nfunction toUint11Array(buffer8: Uint8Array | number[]): number[] {\n const buffer11: number[] = []\n let acc = 0\n let accBits = 0\n function add(octet: number) {\n acc |= octet << accBits\n accBits += 8\n if (accBits >= 11) {\n buffer11.push(acc & 0x7ff)\n acc >>= 11\n accBits -= 11\n }\n }\n function flush() {\n if (accBits) {\n buffer11.push(acc)\n }\n }\n\n buffer8.forEach(add)\n flush()\n return buffer11\n}\n\nfunction applyWords(nums: number[]): string[] {\n return nums.map((n) => english[n])\n}\n\nfunction computeChecksum(seed: Uint8Array): string {\n const hashBuffer = hash(seed)\n const uint11Hash = toUint11Array(hashBuffer)\n const words = applyWords(uint11Hash)\n\n return words[0]\n}\n\n/**\n * mnemonicFromSeed converts a 32-byte key into a 25 word mnemonic. The generated mnemonic includes a checksum.\n * Each word in the mnemonic represents 11 bits of data, and the last 11 bits are reserved for the checksum.\n * @param seed - 32 bytes long seed\n * @returns 25 words mnemonic\n */\nexport function mnemonicFromSeed(seed: Uint8Array) {\n // Sanity length check\n if (seed.length !== SEED_BTYES_LENGTH) {\n throw new RangeError(`Seed length must be ${SEED_BTYES_LENGTH}`)\n }\n\n const uint11Array = toUint11Array(seed)\n const words = applyWords(uint11Array)\n const checksumWord = computeChecksum(seed)\n\n return `${words.join(' ')} ${checksumWord}`\n}\n\n// from Uint11Array\n// https://stackoverflow.com/a/51452614\nfunction toUint8Array(buffer11: number[]): Uint8Array {\n const buffer8: number[] = []\n let acc = 0\n let accBits = 0\n function add(ui11: number) {\n acc |= ui11 << accBits\n accBits += 11\n while (accBits >= 8) {\n buffer8.push(acc & 0xff)\n acc >>= 8\n accBits -= 8\n }\n }\n function flush() {\n if (accBits) {\n buffer8.push(acc)\n }\n }\n\n buffer11.forEach(add)\n flush()\n return new Uint8Array(buffer8)\n}\n\n/**\n * seedFromMnemonic converts a mnemonic generated using this library into the source key used to create it.\n * It returns an error if the passed mnemonic has an incorrect checksum, if the number of words is unexpected, or if one\n * of the passed words is not found in the words list.\n * @param mnemonic - 25 words mnemonic\n * @returns 32 bytes long seed\n */\nexport function seedFromMnemonic(mnemonic: string) {\n const words = mnemonic.split(' ')\n const key = words.slice(0, 24)\n\n // Check that all words are in list\n for (const w of key) {\n if (english.indexOf(w) === -1) throw new Error(NOT_IN_WORDS_LIST_ERROR_MSG)\n }\n\n const checksum = words[words.length - 1]\n const uint11Array = key.map((word) => english.indexOf(word))\n\n // Convert the key to uint8Array\n let uint8Array = toUint8Array(uint11Array)\n\n // We need to chop the last byte -\n // the short explanation - Since 256 is not divisible by 11, we have an extra 0x0 byte.\n // The longer explanation - When splitting the 256 bits to chunks of 11, we get 23 words and a left over of 3 bits.\n // This left gets padded with another 8 bits to the create the 24th word.\n // While converting back to byte array, our new 264 bits array is divisible by 8 but the last byte is just the padding.\n\n // check that we have 33 bytes long array as expected\n if (uint8Array.length !== 33) throw new Error(FAIL_TO_DECODE_MNEMONIC_ERROR_MSG)\n\n // check that the last byte is actually 0x0\n if (uint8Array[uint8Array.length - 1] !== 0x0) throw new Error(FAIL_TO_DECODE_MNEMONIC_ERROR_MSG)\n\n // chop it !\n uint8Array = uint8Array.slice(0, uint8Array.length - 1)\n\n // compute checksum\n const cs = computeChecksum(uint8Array)\n\n // success!\n if (cs === checksum) return uint8Array\n\n throw new Error(FAIL_TO_DECODE_MNEMONIC_ERROR_MSG)\n}\n\n/**\n * secretKeyToMnemonic takes an Algorand secret key and returns the corresponding mnemonic.\n * @param sk - Algorand secret key\n * @returns Secret key's associated mnemonic\n */\nexport function secretKeyToMnemonic(sk: Uint8Array) {\n // get the seed from the sk\n const seed = sk.slice(0, SEED_BTYES_LENGTH)\n return mnemonicFromSeed(seed)\n}\n\n/**\n * mnemonicToMasterDerivationKey takes a mnemonic string and returns the corresponding master derivation key.\n * @param mn - 25 words Algorand mnemonic\n * @returns Uint8Array\n * @throws error if fails to decode the mnemonic\n */\nexport function mnemonicToMasterDerivationKey(mn: string) {\n return seedFromMnemonic(mn)\n}\n\n/**\n * masterDerivationKeyToMnemonic takes a master derivation key and returns the corresponding mnemonic.\n * @param mdk - Uint8Array\n * @returns string mnemonic\n */\nexport function masterDerivationKeyToMnemonic(mdk: Uint8Array) {\n return mnemonicFromSeed(mdk)\n}\n"],"mappings":";;;;AAEA,MAAa,oCAAoC;AACjD,MAAa,8BAA8B;AAE3C,MAAM,oBAAoB;AAG1B,SAAS,cAAc,SAA0C;CAC/D,MAAMA,WAAqB,EAAE;CAC7B,IAAI,MAAM;CACV,IAAI,UAAU;CACd,SAAS,IAAI,OAAe;AAC1B,SAAO,SAAS;AAChB,aAAW;AACX,MAAI,WAAW,IAAI;AACjB,YAAS,KAAK,MAAM,KAAM;AAC1B,WAAQ;AACR,cAAW;;;CAGf,SAAS,QAAQ;AACf,MAAI,QACF,UAAS,KAAK,IAAI;;AAItB,SAAQ,QAAQ,IAAI;AACpB,QAAO;AACP,QAAO;;AAGT,SAAS,WAAW,MAA0B;AAC5C,QAAO,KAAK,KAAK,MAAMC,wBAAQ,GAAG;;AAGpC,SAAS,gBAAgB,MAA0B;AAKjD,QAFc,WADK,cADAC,oBAAK,KAAK,CACe,CACR,CAEvB;;;;;;;;AASf,SAAgB,iBAAiB,MAAkB;AAEjD,KAAI,KAAK,WAAW,kBAClB,OAAM,IAAI,WAAW,uBAAuB,oBAAoB;CAIlE,MAAM,QAAQ,WADM,cAAc,KAAK,CACF;CACrC,MAAM,eAAe,gBAAgB,KAAK;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_default from "./english.mjs";
|
|
|
4
4
|
//#region packages/algo25/src/index.ts
|
|
5
5
|
const FAIL_TO_DECODE_MNEMONIC_ERROR_MSG = "failed to decode mnemonic";
|
|
6
6
|
const NOT_IN_WORDS_LIST_ERROR_MSG = "the mnemonic contains a word that is not in the wordlist";
|
|
7
|
+
const SEED_BTYES_LENGTH = 32;
|
|
7
8
|
function toUint11Array(buffer8) {
|
|
8
9
|
const buffer11 = [];
|
|
9
10
|
let acc = 0;
|
|
@@ -30,6 +31,18 @@ function applyWords(nums) {
|
|
|
30
31
|
function computeChecksum(seed) {
|
|
31
32
|
return applyWords(toUint11Array(hash(seed)))[0];
|
|
32
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* mnemonicFromSeed converts a 32-byte key into a 25 word mnemonic. The generated mnemonic includes a checksum.
|
|
36
|
+
* Each word in the mnemonic represents 11 bits of data, and the last 11 bits are reserved for the checksum.
|
|
37
|
+
* @param seed - 32 bytes long seed
|
|
38
|
+
* @returns 25 words mnemonic
|
|
39
|
+
*/
|
|
40
|
+
function mnemonicFromSeed(seed) {
|
|
41
|
+
if (seed.length !== SEED_BTYES_LENGTH) throw new RangeError(`Seed length must be ${SEED_BTYES_LENGTH}`);
|
|
42
|
+
const words = applyWords(toUint11Array(seed));
|
|
43
|
+
const checksumWord = computeChecksum(seed);
|
|
44
|
+
return `${words.join(" ")} ${checksumWord}`;
|
|
45
|
+
}
|
|
33
46
|
function toUint8Array(buffer11) {
|
|
34
47
|
const buffer8 = [];
|
|
35
48
|
let acc = 0;
|
|
@@ -69,7 +82,32 @@ function seedFromMnemonic(mnemonic) {
|
|
|
69
82
|
if (computeChecksum(uint8Array) === checksum) return uint8Array;
|
|
70
83
|
throw new Error(FAIL_TO_DECODE_MNEMONIC_ERROR_MSG);
|
|
71
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* secretKeyToMnemonic takes an Algorand secret key and returns the corresponding mnemonic.
|
|
87
|
+
* @param sk - Algorand secret key
|
|
88
|
+
* @returns Secret key's associated mnemonic
|
|
89
|
+
*/
|
|
90
|
+
function secretKeyToMnemonic(sk) {
|
|
91
|
+
return mnemonicFromSeed(sk.slice(0, SEED_BTYES_LENGTH));
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* mnemonicToMasterDerivationKey takes a mnemonic string and returns the corresponding master derivation key.
|
|
95
|
+
* @param mn - 25 words Algorand mnemonic
|
|
96
|
+
* @returns Uint8Array
|
|
97
|
+
* @throws error if fails to decode the mnemonic
|
|
98
|
+
*/
|
|
99
|
+
function mnemonicToMasterDerivationKey(mn) {
|
|
100
|
+
return seedFromMnemonic(mn);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* masterDerivationKeyToMnemonic takes a master derivation key and returns the corresponding mnemonic.
|
|
104
|
+
* @param mdk - Uint8Array
|
|
105
|
+
* @returns string mnemonic
|
|
106
|
+
*/
|
|
107
|
+
function masterDerivationKeyToMnemonic(mdk) {
|
|
108
|
+
return mnemonicFromSeed(mdk);
|
|
109
|
+
}
|
|
72
110
|
|
|
73
111
|
//#endregion
|
|
74
|
-
export { seedFromMnemonic };
|
|
112
|
+
export { FAIL_TO_DECODE_MNEMONIC_ERROR_MSG, NOT_IN_WORDS_LIST_ERROR_MSG, masterDerivationKeyToMnemonic, mnemonicFromSeed, mnemonicToMasterDerivationKey, secretKeyToMnemonic, seedFromMnemonic };
|
|
75
113
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["buffer11: number[]","english","buffer8: number[]"],"sources":["../../../../packages/algo25/src/index.ts"],"sourcesContent":["import english from './english.js'\nimport { hash } from '@algorandfoundation/algokit-common'\nexport const FAIL_TO_DECODE_MNEMONIC_ERROR_MSG = 'failed to decode mnemonic'\nexport const NOT_IN_WORDS_LIST_ERROR_MSG = 'the mnemonic contains a word that is not in the wordlist'\n\nconst SEED_BTYES_LENGTH = 32\n\n// https://stackoverflow.com/a/51452614\nfunction toUint11Array(buffer8: Uint8Array | number[]): number[] {\n const buffer11: number[] = []\n let acc = 0\n let accBits = 0\n function add(octet: number) {\n acc |= octet << accBits\n accBits += 8\n if (accBits >= 11) {\n buffer11.push(acc & 0x7ff)\n acc >>= 11\n accBits -= 11\n }\n }\n function flush() {\n if (accBits) {\n buffer11.push(acc)\n }\n }\n\n buffer8.forEach(add)\n flush()\n return buffer11\n}\n\nfunction applyWords(nums: number[]): string[] {\n return nums.map((n) => english[n])\n}\n\nfunction computeChecksum(seed: Uint8Array): string {\n const hashBuffer = hash(seed)\n const uint11Hash = toUint11Array(hashBuffer)\n const words = applyWords(uint11Hash)\n\n return words[0]\n}\n\n/**\n * mnemonicFromSeed converts a 32-byte key into a 25 word mnemonic. The generated mnemonic includes a checksum.\n * Each word in the mnemonic represents 11 bits of data, and the last 11 bits are reserved for the checksum.\n * @param seed - 32 bytes long seed\n * @returns 25 words mnemonic\n */\nexport function mnemonicFromSeed(seed: Uint8Array) {\n // Sanity length check\n if (seed.length !== SEED_BTYES_LENGTH) {\n throw new RangeError(`Seed length must be ${SEED_BTYES_LENGTH}`)\n }\n\n const uint11Array = toUint11Array(seed)\n const words = applyWords(uint11Array)\n const checksumWord = computeChecksum(seed)\n\n return `${words.join(' ')} ${checksumWord}`\n}\n\n// from Uint11Array\n// https://stackoverflow.com/a/51452614\nfunction toUint8Array(buffer11: number[]): Uint8Array {\n const buffer8: number[] = []\n let acc = 0\n let accBits = 0\n function add(ui11: number) {\n acc |= ui11 << accBits\n accBits += 11\n while (accBits >= 8) {\n buffer8.push(acc & 0xff)\n acc >>= 8\n accBits -= 8\n }\n }\n function flush() {\n if (accBits) {\n buffer8.push(acc)\n }\n }\n\n buffer11.forEach(add)\n flush()\n return new Uint8Array(buffer8)\n}\n\n/**\n * seedFromMnemonic converts a mnemonic generated using this library into the source key used to create it.\n * It returns an error if the passed mnemonic has an incorrect checksum, if the number of words is unexpected, or if one\n * of the passed words is not found in the words list.\n * @param mnemonic - 25 words mnemonic\n * @returns 32 bytes long seed\n */\nexport function seedFromMnemonic(mnemonic: string) {\n const words = mnemonic.split(' ')\n const key = words.slice(0, 24)\n\n // Check that all words are in list\n for (const w of key) {\n if (english.indexOf(w) === -1) throw new Error(NOT_IN_WORDS_LIST_ERROR_MSG)\n }\n\n const checksum = words[words.length - 1]\n const uint11Array = key.map((word) => english.indexOf(word))\n\n // Convert the key to uint8Array\n let uint8Array = toUint8Array(uint11Array)\n\n // We need to chop the last byte -\n // the short explanation - Since 256 is not divisible by 11, we have an extra 0x0 byte.\n // The longer explanation - When splitting the 256 bits to chunks of 11, we get 23 words and a left over of 3 bits.\n // This left gets padded with another 8 bits to the create the 24th word.\n // While converting back to byte array, our new 264 bits array is divisible by 8 but the last byte is just the padding.\n\n // check that we have 33 bytes long array as expected\n if (uint8Array.length !== 33) throw new Error(FAIL_TO_DECODE_MNEMONIC_ERROR_MSG)\n\n // check that the last byte is actually 0x0\n if (uint8Array[uint8Array.length - 1] !== 0x0) throw new Error(FAIL_TO_DECODE_MNEMONIC_ERROR_MSG)\n\n // chop it !\n uint8Array = uint8Array.slice(0, uint8Array.length - 1)\n\n // compute checksum\n const cs = computeChecksum(uint8Array)\n\n // success!\n if (cs === checksum) return uint8Array\n\n throw new Error(FAIL_TO_DECODE_MNEMONIC_ERROR_MSG)\n}\n\n/**\n * secretKeyToMnemonic takes an Algorand secret key and returns the corresponding mnemonic.\n * @param sk - Algorand secret key\n * @returns Secret key's associated mnemonic\n */\nexport function secretKeyToMnemonic(sk: Uint8Array) {\n // get the seed from the sk\n const seed = sk.slice(0, SEED_BTYES_LENGTH)\n return mnemonicFromSeed(seed)\n}\n\n/**\n * mnemonicToMasterDerivationKey takes a mnemonic string and returns the corresponding master derivation key.\n * @param mn - 25 words Algorand mnemonic\n * @returns Uint8Array\n * @throws error if fails to decode the mnemonic\n */\nexport function mnemonicToMasterDerivationKey(mn: string) {\n return seedFromMnemonic(mn)\n}\n\n/**\n * masterDerivationKeyToMnemonic takes a master derivation key and returns the corresponding mnemonic.\n * @param mdk - Uint8Array\n * @returns string mnemonic\n */\nexport function masterDerivationKeyToMnemonic(mdk: Uint8Array) {\n return mnemonicFromSeed(mdk)\n}\n"],"mappings":";;;;AAEA,MAAa,oCAAoC;AACjD,MAAa,8BAA8B;
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["buffer11: number[]","english","buffer8: number[]"],"sources":["../../../../packages/algo25/src/index.ts"],"sourcesContent":["import english from './english.js'\nimport { hash } from '@algorandfoundation/algokit-common'\nexport const FAIL_TO_DECODE_MNEMONIC_ERROR_MSG = 'failed to decode mnemonic'\nexport const NOT_IN_WORDS_LIST_ERROR_MSG = 'the mnemonic contains a word that is not in the wordlist'\n\nconst SEED_BTYES_LENGTH = 32\n\n// https://stackoverflow.com/a/51452614\nfunction toUint11Array(buffer8: Uint8Array | number[]): number[] {\n const buffer11: number[] = []\n let acc = 0\n let accBits = 0\n function add(octet: number) {\n acc |= octet << accBits\n accBits += 8\n if (accBits >= 11) {\n buffer11.push(acc & 0x7ff)\n acc >>= 11\n accBits -= 11\n }\n }\n function flush() {\n if (accBits) {\n buffer11.push(acc)\n }\n }\n\n buffer8.forEach(add)\n flush()\n return buffer11\n}\n\nfunction applyWords(nums: number[]): string[] {\n return nums.map((n) => english[n])\n}\n\nfunction computeChecksum(seed: Uint8Array): string {\n const hashBuffer = hash(seed)\n const uint11Hash = toUint11Array(hashBuffer)\n const words = applyWords(uint11Hash)\n\n return words[0]\n}\n\n/**\n * mnemonicFromSeed converts a 32-byte key into a 25 word mnemonic. The generated mnemonic includes a checksum.\n * Each word in the mnemonic represents 11 bits of data, and the last 11 bits are reserved for the checksum.\n * @param seed - 32 bytes long seed\n * @returns 25 words mnemonic\n */\nexport function mnemonicFromSeed(seed: Uint8Array) {\n // Sanity length check\n if (seed.length !== SEED_BTYES_LENGTH) {\n throw new RangeError(`Seed length must be ${SEED_BTYES_LENGTH}`)\n }\n\n const uint11Array = toUint11Array(seed)\n const words = applyWords(uint11Array)\n const checksumWord = computeChecksum(seed)\n\n return `${words.join(' ')} ${checksumWord}`\n}\n\n// from Uint11Array\n// https://stackoverflow.com/a/51452614\nfunction toUint8Array(buffer11: number[]): Uint8Array {\n const buffer8: number[] = []\n let acc = 0\n let accBits = 0\n function add(ui11: number) {\n acc |= ui11 << accBits\n accBits += 11\n while (accBits >= 8) {\n buffer8.push(acc & 0xff)\n acc >>= 8\n accBits -= 8\n }\n }\n function flush() {\n if (accBits) {\n buffer8.push(acc)\n }\n }\n\n buffer11.forEach(add)\n flush()\n return new Uint8Array(buffer8)\n}\n\n/**\n * seedFromMnemonic converts a mnemonic generated using this library into the source key used to create it.\n * It returns an error if the passed mnemonic has an incorrect checksum, if the number of words is unexpected, or if one\n * of the passed words is not found in the words list.\n * @param mnemonic - 25 words mnemonic\n * @returns 32 bytes long seed\n */\nexport function seedFromMnemonic(mnemonic: string) {\n const words = mnemonic.split(' ')\n const key = words.slice(0, 24)\n\n // Check that all words are in list\n for (const w of key) {\n if (english.indexOf(w) === -1) throw new Error(NOT_IN_WORDS_LIST_ERROR_MSG)\n }\n\n const checksum = words[words.length - 1]\n const uint11Array = key.map((word) => english.indexOf(word))\n\n // Convert the key to uint8Array\n let uint8Array = toUint8Array(uint11Array)\n\n // We need to chop the last byte -\n // the short explanation - Since 256 is not divisible by 11, we have an extra 0x0 byte.\n // The longer explanation - When splitting the 256 bits to chunks of 11, we get 23 words and a left over of 3 bits.\n // This left gets padded with another 8 bits to the create the 24th word.\n // While converting back to byte array, our new 264 bits array is divisible by 8 but the last byte is just the padding.\n\n // check that we have 33 bytes long array as expected\n if (uint8Array.length !== 33) throw new Error(FAIL_TO_DECODE_MNEMONIC_ERROR_MSG)\n\n // check that the last byte is actually 0x0\n if (uint8Array[uint8Array.length - 1] !== 0x0) throw new Error(FAIL_TO_DECODE_MNEMONIC_ERROR_MSG)\n\n // chop it !\n uint8Array = uint8Array.slice(0, uint8Array.length - 1)\n\n // compute checksum\n const cs = computeChecksum(uint8Array)\n\n // success!\n if (cs === checksum) return uint8Array\n\n throw new Error(FAIL_TO_DECODE_MNEMONIC_ERROR_MSG)\n}\n\n/**\n * secretKeyToMnemonic takes an Algorand secret key and returns the corresponding mnemonic.\n * @param sk - Algorand secret key\n * @returns Secret key's associated mnemonic\n */\nexport function secretKeyToMnemonic(sk: Uint8Array) {\n // get the seed from the sk\n const seed = sk.slice(0, SEED_BTYES_LENGTH)\n return mnemonicFromSeed(seed)\n}\n\n/**\n * mnemonicToMasterDerivationKey takes a mnemonic string and returns the corresponding master derivation key.\n * @param mn - 25 words Algorand mnemonic\n * @returns Uint8Array\n * @throws error if fails to decode the mnemonic\n */\nexport function mnemonicToMasterDerivationKey(mn: string) {\n return seedFromMnemonic(mn)\n}\n\n/**\n * masterDerivationKeyToMnemonic takes a master derivation key and returns the corresponding mnemonic.\n * @param mdk - Uint8Array\n * @returns string mnemonic\n */\nexport function masterDerivationKeyToMnemonic(mdk: Uint8Array) {\n return mnemonicFromSeed(mdk)\n}\n"],"mappings":";;;;AAEA,MAAa,oCAAoC;AACjD,MAAa,8BAA8B;AAE3C,MAAM,oBAAoB;AAG1B,SAAS,cAAc,SAA0C;CAC/D,MAAMA,WAAqB,EAAE;CAC7B,IAAI,MAAM;CACV,IAAI,UAAU;CACd,SAAS,IAAI,OAAe;AAC1B,SAAO,SAAS;AAChB,aAAW;AACX,MAAI,WAAW,IAAI;AACjB,YAAS,KAAK,MAAM,KAAM;AAC1B,WAAQ;AACR,cAAW;;;CAGf,SAAS,QAAQ;AACf,MAAI,QACF,UAAS,KAAK,IAAI;;AAItB,SAAQ,QAAQ,IAAI;AACpB,QAAO;AACP,QAAO;;AAGT,SAAS,WAAW,MAA0B;AAC5C,QAAO,KAAK,KAAK,MAAMC,gBAAQ,GAAG;;AAGpC,SAAS,gBAAgB,MAA0B;AAKjD,QAFc,WADK,cADA,KAAK,KAAK,CACe,CACR,CAEvB;;;;;;;;AASf,SAAgB,iBAAiB,MAAkB;AAEjD,KAAI,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IAAY;AACxD,QAAO,iBAAiB,GAAG;;;;;;;AAQ7B,SAAgB,8BAA8B,KAAiB;AAC7D,QAAO,iBAAiB,IAAI"}
|
|
@@ -85,9 +85,9 @@ const isAbiValue = (x) => {
|
|
|
85
85
|
* Populate reference arrays from processed ABI method call arguments
|
|
86
86
|
*/
|
|
87
87
|
function populateMethodArgsIntoReferenceArrays(sender, appId, method, methodArgs, accountReferences, appReferences, assetReferences) {
|
|
88
|
-
const accounts = accountReferences ?? [];
|
|
89
|
-
const assets = assetReferences ?? [];
|
|
90
|
-
const apps = appReferences ?? [];
|
|
88
|
+
const accounts = [...accountReferences ?? []];
|
|
89
|
+
const assets = [...assetReferences ?? []];
|
|
90
|
+
const apps = [...appReferences ?? []];
|
|
91
91
|
methodArgs.forEach((arg, i) => {
|
|
92
92
|
const argType = method.args[i].type;
|
|
93
93
|
if (require_abi_method.argTypeIsReference(argType)) switch (argType) {
|
|
@@ -245,9 +245,9 @@ const buildAppCreateMethodCall = async (params, appManager, suggestedParams, def
|
|
|
245
245
|
extraProgramPages,
|
|
246
246
|
args: common.args,
|
|
247
247
|
...hasAccessReferences ? { accessReferences: params.accessReferences } : {
|
|
248
|
-
accountReferences:
|
|
249
|
-
appReferences:
|
|
250
|
-
assetReferences:
|
|
248
|
+
accountReferences: common.accountReferences,
|
|
249
|
+
appReferences: common.appReferences,
|
|
250
|
+
assetReferences: common.assetReferences,
|
|
251
251
|
boxReferences: params.boxReferences?.map(require_app_manager.AppManager.getBoxReference)
|
|
252
252
|
},
|
|
253
253
|
rejectVersion: params.rejectVersion
|
|
@@ -278,9 +278,9 @@ const buildAppUpdateMethodCall = async (params, appManager, suggestedParams, def
|
|
|
278
278
|
clearStateProgram,
|
|
279
279
|
args: common.args,
|
|
280
280
|
...hasAccessReferences ? { accessReferences: params.accessReferences } : {
|
|
281
|
-
accountReferences:
|
|
282
|
-
appReferences:
|
|
283
|
-
assetReferences:
|
|
281
|
+
accountReferences: common.accountReferences,
|
|
282
|
+
appReferences: common.appReferences,
|
|
283
|
+
assetReferences: common.assetReferences,
|
|
284
284
|
boxReferences: params.boxReferences?.map(require_app_manager.AppManager.getBoxReference)
|
|
285
285
|
},
|
|
286
286
|
rejectVersion: params.rejectVersion
|
|
@@ -307,9 +307,9 @@ const buildAppCallMethodCall = async (params, suggestedParams, defaultValidityWi
|
|
|
307
307
|
onComplete: params.onComplete ?? require_app_call.OnApplicationComplete.NoOp,
|
|
308
308
|
args: common.args,
|
|
309
309
|
...hasAccessReferences ? { accessReferences: params.accessReferences } : {
|
|
310
|
-
accountReferences:
|
|
311
|
-
appReferences:
|
|
312
|
-
assetReferences:
|
|
310
|
+
accountReferences: common.accountReferences,
|
|
311
|
+
appReferences: common.appReferences,
|
|
312
|
+
assetReferences: common.assetReferences,
|
|
313
313
|
boxReferences: params.boxReferences?.map(require_app_manager.AppManager.getBoxReference)
|
|
314
314
|
},
|
|
315
315
|
rejectVersion: params.rejectVersion
|