@keetanetwork/anchor 0.0.19 → 0.0.21
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/config.d.ts +2 -0
- package/config.d.ts.map +1 -1
- package/config.js +7 -3
- package/config.js.map +1 -1
- package/lib/certificates.d.ts.map +1 -1
- package/lib/certificates.js +16 -3
- package/lib/certificates.js.map +1 -1
- package/lib/encrypted-container.js +4 -4
- package/lib/encrypted-container.js.map +1 -1
- package/lib/error.d.ts +61 -4
- package/lib/error.d.ts.map +1 -1
- package/lib/error.js +196 -17
- package/lib/error.js.map +1 -1
- package/lib/resolver.d.ts +11 -3
- package/lib/resolver.d.ts.map +1 -1
- package/lib/resolver.js +166 -23
- package/lib/resolver.js.map +1 -1
- package/lib/utils/json.js +1 -1
- package/lib/utils/json.js.map +1 -1
- package/lib/utils/types.d.ts +4 -0
- package/lib/utils/types.d.ts.map +1 -0
- package/lib/utils/types.js +2 -0
- package/lib/utils/types.js.map +1 -0
- package/lib/utils/util.d.ts +7 -0
- package/lib/utils/util.d.ts.map +1 -0
- package/lib/utils/util.js +7 -0
- package/lib/utils/util.js.map +1 -0
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/services/kyc/common.d.ts +22 -3
- package/services/kyc/common.d.ts.map +1 -1
- package/services/kyc/common.js +60 -3
- package/services/kyc/common.js.map +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"encrypted-container.js","sourceRoot":"","sources":["../../src/lib/encrypted-container.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,mBAAmB,CAAC;AACvC,OAAO,EAAE,GAAG,IAAI,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAKnE,OAAO,EAAE,MAAM,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AACrF,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAE3C,MAAM,WAAW,GAAG,WAAW,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,CAAC;IACxC,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,gBAAgB;QACrB,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,eAAe,EAAE,CAAC;QAEhD,IAAI,UAAU,KAAK,SAAS,EAAE,CAAC;YAC9B,MAAK,CAAC,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAC,CAAC;QAC3D,CAAC;QAED;;;;WAIG;QACH,OAAM,CAAC,mBAAmB,CAAC,UAAU,CAAC,CAAC,CAAC;IACzC,CAAC;IAED;;;OAGG;IACH,IAAI,UAAU;QACb,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE,EAAE,CAAC;YAC1B,MAAK,CAAC,IAAI,KAAK,CAAC,gDAAgD,CAAC,CAAC,CAAC;QACpE,CAAC;QAED,OAAM,CAAC,IAAI,CAAC,cAAc,CAAC,UAAU,CAAC,CAAC;IACxC,CAAC;;AAGF,gBAAgB;AAChB,MAAM,CAAC,MAAM,QAAQ,GAAG;IACvB,SAAS,EAAE,SAAS;IACpB,SAAS,EAAE,SAAS;CACpB,CAAC;AAEF,eAAe,kBAAkB,CAAC","sourcesContent":["import crypto from './utils/crypto.js';\nimport { lib as KeetaNetLib } from '@keetanetwork/keetanet-client';\n\nimport type {\n\tASN1OID\n} from '@keetanetwork/keetanet-client/lib/utils/asn1.js';\nimport { Buffer, arrayBufferToBuffer, bufferToArrayBuffer } from './utils/buffer.js';\nimport { isArray } from './utils/array.js';\n\nconst zlibDeflate = KeetaNetLib.Utils.Buffer.ZlibDeflate; /* XXX:TODO: Change this to ZlibDeflateAsync when merged in */\nconst zlibInflate = KeetaNetLib.Utils.Buffer.ZlibInflate; /* XXX:TODO: Change this to ZlibInflateAsync when merged in */\nconst ASN1toJS = KeetaNetLib.Utils.ASN1.ASN1toJS;\nconst JStoASN1 = KeetaNetLib.Utils.ASN1.JStoASN1;\nconst Account: typeof KeetaNetLib.Account = KeetaNetLib.Account;\ntype Account = InstanceType<typeof KeetaNetLib.Account>;\n\n/*\n * ASN.1 Schema\n *\n * EncryptedContainer DEFINITIONS ::=\n * BEGIN\n * Version ::= INTEGER { v2(1) }\n *\n * KeyStore ::= SEQUENCE {\n * publicKey OCTET STRING,\n * encryptedSymmetricKey OCTET STRING,\n * ...\n * }\n *\n * EncryptedContainerBox ::= SEQUENCE {\n * keys SEQUENCE OF KeyStore,\n * encryptionAlgorithm OBJECT IDENTIFIER,\n * initializationVector OCTET STRING,\n * encryptedValue OCTET STRING,\n * ...\n * }\n *\n * PlaintextContainerBox ::= SEQUENCE {\n * plainValue OCTET STRING,\n * ...\n * }\n *\n * ContainerPackage ::= SEQUENCE {\n * version Version (v2),\n * encryptedContainer [0] EXPLICIT EncryptedContainerBox OPTIONAL,\n * plaintextContainer [1] EXPLICIT PlaintextContainerBox OPTIONAL,\n * ...\n * } (WITH COMPONENTS {\n * encryptedContainer PRESENT,\n * plaintextContainer ABSENT\n * } |\n * WITH COMPONENTS {\n * encryptedContainer ABSENT,\n * plaintextContainer PRESENT\n * })\n * END\n *\n */\n\ntype EncryptedContainerKeyStore = [\n\t/* publicKey */\n\tBuffer,\n\n\t/* encryptedSymmetricKey */\n\tBuffer\n];\n\ntype EncryptedContainerBoxEncrypted = [\n\t/* keys */\n\tEncryptedContainerKeyStore[],\n\n\t/* encryptionAlgorithm */\n\tASN1OID,\n\n\t/* initializationVector */\n\tBuffer,\n\n\t/* value */\n\tBuffer\n];\n\ntype EncryptedContainerBoxPlaintext = [\n\t/* value */\n\tBuffer\n];\n\ntype ContainerPackage = [\n\t/* version */\n\tnumber,\n\n\t{\n\t\ttype: 'context'\n\t\tvalue: 0,\n\t\tkind: 'explicit',\n\t\tcontains: EncryptedContainerBoxEncrypted\n\t} | {\n\t\ttype: 'context',\n\t\tvalue: 1,\n\t\tkind: 'explicit',\n\t\tcontains: EncryptedContainerBoxPlaintext\n\t}\n];\n\ntype CipherOptions = {\n\t/**\n\t * The symmetric cipher key (if any)\n\t */\n\tcipherKey: Buffer | undefined;\n\t/**\n\t * The symmetric cipher IV (if any)\n\t */\n\tcipherIV: Buffer | undefined;\n\t/**\n\t * The symmetric cipher algorithm\n\t */\n\tcipherAlgo: string;\n}\n\ntype ASN1Options = Required<CipherOptions> & {\n\t/**\n\t * The set of accounts to encrypt the formatted data\n\t */\n\tkeys: Account[];\n}\n\nconst oidDB = {\n\t'aes-256-cbc': '2.16.840.1.101.3.4.1.42'\n} as const;\n\n/**\n* Compiles the ASN.1 for the container\n*\n* @returns The ASN.1 DER data\n*/\nasync function buildASN1(plaintext: Buffer, encryptionOptions?: ASN1Options): Promise<Buffer> {\n\tconst compressedPlaintext = Buffer.from(zlibDeflate(plaintext));\n\n\tconst sequence: Partial<ContainerPackage> = [];\n\n\t/*\n\t * Version v2 (1)\n\t */\n\tsequence[0] = 1;\n\n\t/*\n\t * Encrypted container box\n\t */\n\tif (encryptionOptions) {\n\t\tconst { keys, cipherKey, cipherIV, cipherAlgo } = encryptionOptions;\n\n\t\tif (keys === undefined || keys.length === 0 || cipherKey === undefined || cipherIV === undefined || cipherAlgo === undefined) {\n\t\t\tthrow(new Error('internal error: Unsupported method invocation'));\n\t\t}\n\n\t\tif (!(cipherAlgo in oidDB)) {\n\t\t\tthrow(new Error(`Unsupported algorithm: ${cipherAlgo}`));\n\t\t}\n\n\t\t// eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n\t\tconst algorithmOID = oidDB[cipherAlgo as keyof typeof oidDB];\n\n\t\tconst cipher = crypto.createCipheriv(\n\t\t\tcipherAlgo,\n\t\t\tcipherKey,\n\t\t\tcipherIV\n\t\t);\n\n\t\tconst encryptedData = Buffer.concat([\n\t\t\tcipher.update(compressedPlaintext),\n\t\t\tcipher.final()\n\t\t]);\n\n\t\tconst cipherKeyArrayBuffer = bufferToArrayBuffer(cipherKey);\n\n\t\tconst encryptionKeysSequence = await Promise.all(keys.map(async function(key) {\n\t\t\tconst encryptedSymmetricKey = Buffer.from(await key.encrypt(cipherKeyArrayBuffer));\n\t\t\tconst retval: EncryptedContainerKeyStore = [\n\t\t\t\tkey.publicKeyAndType,\n\t\t\t\tencryptedSymmetricKey\n\t\t\t];\n\n\t\t\treturn(retval);\n\t\t}));\n\n\t\tsequence[1] = {\n\t\t\ttype: 'context',\n\t\t\tvalue: 0,\n\t\t\tkind: 'explicit',\n\t\t\tcontains: [\n\t\t\t\tencryptionKeysSequence,\n\t\t\t\t{ type: 'oid', oid: algorithmOID },\n\t\t\t\tcipherIV,\n\t\t\t\tencryptedData\n\t\t\t]\n\t\t};\n\t} else {\n\t\t/*\n\t\t * Otherwise we simply pass in the compressed data\n\t\t */\n\t\tsequence[1] = {\n\t\t\ttype: 'context',\n\t\t\tvalue: 1,\n\t\t\tkind: 'explicit',\n\t\t\tcontains: [compressedPlaintext]\n\t\t};\n\t}\n\n\tconst outputASN1 = JStoASN1(sequence);\n\tconst outputDER = Buffer.from(outputASN1.toBER(false));\n\n\treturn(outputDER);\n}\n\nfunction parseASN1Bare(input: Buffer, acceptableEncryptionAlgorithms = ['aes-256-cbc', 'null']) {\n\tconst inputSequence = ASN1toJS(bufferToArrayBuffer(input));\n\tif (!isArray(inputSequence, 2)) {\n\t\tthrow(new Error('Malformed data detected (incorrect base format)'));\n\t}\n\n\tconst version = inputSequence[0];\n\tif (typeof version !== 'bigint') {\n\t\tthrow(new Error('Malformed data detected (version expected at position 0)'));\n\t}\n\n\tif (version !== 1n) {\n\t\tthrow(new Error('Malformed data detected (unsupported version)'));\n\t}\n\n\tconst valueBox = inputSequence[1];\n\tif (typeof valueBox !== 'object' || valueBox === null) {\n\t\tthrow(new Error('Malformed data detected (data expected at position 1)'));\n\t}\n\n\tif (!('type' in valueBox) || typeof valueBox.type !== 'string') {\n\t\tthrow(new Error('Malformed data detected (expected type at position 1)'));\n\t}\n\n\tif (valueBox.type !== 'context') {\n\t\tthrow(new Error('Malformed data detected (expected context at position 1)'));\n\t}\n\n\tif (!('value' in valueBox) || typeof valueBox.value !== 'number') {\n\t\tthrow(new Error('Malformed data detected (expected context value at position 1)'));\n\t}\n\n\tif (valueBox.value !== 0 && valueBox.value !== 1) {\n\t\tthrow(new Error('Malformed data detected (expected context value of 0 or 1)'));\n\t}\n\n\tif (!('contains' in valueBox) || typeof valueBox.contains !== 'object' || valueBox.contains === null) {\n\t\tthrow(new Error('Malformed data detected (expected contents at position 1)'));\n\t}\n\n\tlet isEncrypted;\n\tif (valueBox.value === 0) {\n\t\tisEncrypted = true;\n\t} else {\n\t\tisEncrypted = false;\n\t}\n\n\tconst value = valueBox.contains;\n\tlet containedCompressed: Buffer;\n\tlet cipherInfo;\n\tif (isEncrypted) {\n\t\tif (!isArray(value, 4)) {\n\t\t\tthrow(new Error('Malformed data (incorrect number of elements within position 1 -- expected 4)'));\n\t\t}\n\n\t\tconst keyInfoUnchecked = value[0];\n\t\tif (!isArray(keyInfoUnchecked)) {\n\t\t\tthrow(new Error('Malformed data (expected sequence at position 2.0)'));\n\t\t}\n\n\t\tconst keyInfo = keyInfoUnchecked.map(function(checkKeyInfo) {\n\t\t\tif (!isArray(checkKeyInfo, 2)) {\n\t\t\t\tthrow(new Error('Malformed key information (expected sequence of 2 at position 1.0.x)'));\n\t\t\t}\n\n\t\t\tconst publicKeyBuffer = checkKeyInfo[0];\n\t\t\tif (!Buffer.isBuffer(publicKeyBuffer)) {\n\t\t\t\tthrow(new Error('Malformed key information (expected octet string for public key at position 1.0.x)'));\n\t\t\t}\n\t\t\tconst publicKey = Account.fromPublicKeyAndType(publicKeyBuffer);\n\n\t\t\tconst encryptedSymmetricKey = checkKeyInfo[1];\n\t\t\tif (!Buffer.isBuffer(encryptedSymmetricKey)) {\n\t\t\t\tthrow(new Error('Malformed key information (expected octet string for cipher key at position 1.0.x)'));\n\t\t\t}\n\n\t\t\treturn({\n\t\t\t\tpublicKey,\n\t\t\t\tencryptedSymmetricKey\n\t\t\t});\n\t\t});\n\n\t\t// eslint-disable-next-line @typescript-eslint/no-unused-vars\n\t\tconst encryptionAlgorithmOID = value[1];\n\t\t/* XXX:TODO: Lookup the encryption algorithm from the OID */\n\t\tconst encryptionAlgorithm = 'aes-256-cbc';\n\n\t\tconst cipherIV = value[2];\n\t\tif (!Buffer.isBuffer(cipherIV)) {\n\t\t\tthrow(new Error('Malformed data (cipher IV expected at position 1.2)'));\n\t\t}\n\n\t\tconst encryptedCompressedValue = value[3];\n\t\tif (!Buffer.isBuffer(encryptedCompressedValue)) {\n\t\t\tthrow(new Error('Malformed data (encrypted compressed buffer expected at position 1.3)'));\n\t\t}\n\n\t\tcipherInfo = {\n\t\t\tkeys: keyInfo,\n\t\t\tcipherIV: cipherIV,\n\t\t\tencryptedData: encryptedCompressedValue,\n\t\t\tencryptionAlgorithm: encryptionAlgorithm\n\t\t};\n\n\t\tcontainedCompressed = encryptedCompressedValue;\n\t} else {\n\t\tif (!isArray(value, 1)) {\n\t\t\tthrow(new Error('Malformed data (incorrect number of elements within position 1 -- expected 1)'));\n\t\t}\n\n\t\tconst containedCompressedUnchecked = value[0];\n\t\tif (!Buffer.isBuffer(containedCompressedUnchecked)) {\n\t\t\tthrow(new Error('Malformed data (compressed buffer expected at position 1.0)'));\n\t\t}\n\n\t\tif (!acceptableEncryptionAlgorithms.includes('null')) {\n\t\t\tthrow(new Error('Malformed data (plaintext found but the null encryption algorithm is not acceptable)'));\n\t\t}\n\n\t\tcontainedCompressed = containedCompressedUnchecked;\n\t}\n\n\treturn({\n\t\tversion: version,\n\t\tisEncrypted: isEncrypted,\n\t\tinnerValue: containedCompressed,\n\t\t...cipherInfo\n\t});\n}\n\nasync function parseASN1Decrypt(inputInfo: ReturnType<typeof parseASN1Bare>, keys?: Account[]) {\n\tlet containedCompressed: Buffer;\n\tlet cipherInfo;\n\tif (inputInfo.isEncrypted) {\n\t\tif (keys === undefined || keys.length === 0) {\n\t\t\tthrow(new Error('Encrypted Container found with encryption but no keys for decryption supplied'));\n\t\t}\n\n\t\tconst algorithm = inputInfo.encryptionAlgorithm;\n\t\tif (algorithm === undefined) {\n\t\t\tthrow(new Error('Encrypted Container found with encryption but no algorithm supplied'));\n\t\t}\n\n\t\tconst keyInfo = inputInfo.keys;\n\t\tif (keyInfo === undefined) {\n\t\t\tthrow(new Error('internal error: Encrypted container found with missing keys'));\n\t\t}\n\n\t\tlet decryptionKeyInfo;\n\t\tfor (const checkKeyInfo of keyInfo) {\n\t\t\tfor (const key of keys) {\n\t\t\t\tif (!key.hasPrivateKey) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tif (key.comparePublicKey(checkKeyInfo.publicKey)) {\n\t\t\t\t\tdecryptionKeyInfo = {\n\t\t\t\t\t\t...checkKeyInfo,\n\t\t\t\t\t\tprivateKey: key\n\t\t\t\t\t};\n\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (decryptionKeyInfo === undefined) {\n\t\t\tthrow(new Error('No keys found which can perform decryption on the supplied encryption box'));\n\t\t}\n\n\t\tconst cipherKey = Buffer.from(await decryptionKeyInfo.privateKey.decrypt(bufferToArrayBuffer(decryptionKeyInfo.encryptedSymmetricKey)));\n\n\t\tconst cipherIV = inputInfo.cipherIV;\n\t\tif (cipherIV === undefined) {\n\t\t\tthrow(new Error('internal error: No Cipher IV found'));\n\t\t}\n\n\t\tconst encryptedCompressedValue = inputInfo.innerValue;\n\t\tconst decipher = crypto.createDecipheriv(algorithm, cipherKey, cipherIV);\n\t\tcontainedCompressed = Buffer.concat([\n\t\t\tdecipher.update(encryptedCompressedValue),\n\t\t\tdecipher.final()\n\t\t]);\n\n\t\tcipherInfo = {\n\t\t\tisEncrypted: true,\n\t\t\tkeys: keyInfo,\n\t\t\tcipherIV: cipherIV,\n\t\t\tcipherKey: cipherKey,\n\t\t\tencryptedData: encryptedCompressedValue\n\t\t};\n\t} else {\n\t\tcontainedCompressed = inputInfo.innerValue;\n\t\tcipherInfo = {\n\t\t\tisEncrypted: false\n\t\t};\n\t}\n\n\tconst plaintext = Buffer.from(zlibInflate(containedCompressed));\n\n\treturn({\n\t\tversion: inputInfo.version,\n\t\tplaintext: plaintext,\n\t\t...cipherInfo\n\t});\n\n}\n\nasync function parseASN1(input: Buffer, keys?: Account[]) {\n\tconst inputInfo = parseASN1Bare(input);\n\tconst retval = await parseASN1Decrypt(inputInfo, keys);\n\n\treturn(retval);\n}\n\nfunction inputToBuffer(data: string | ArrayBuffer | Buffer): Buffer {\n\tif (typeof data === 'string') {\n\t\treturn(Buffer.from(data, 'utf-8'));\n\t} else if (Buffer.isBuffer(data)) {\n\t\treturn(data);\n\t} else {\n\t\treturn(arrayBufferToBuffer(data));\n\t}\n}\n\ntype EncryptedContainerInfo = Pick<CipherOptions, 'cipherAlgo'> & {\n\t/**\n\t * Set of accounts which can access the data\n\t */\n\tprincipals: Account[];\n}\n\ntype UnencryptedContainerInfo = {\n\t/**\n\t * Unencrypted container should not have any principals\n\t */\n\tprincipals: null;\n}\n\nexport class EncryptedContainer {\n\tprivate static readonly algorithm = 'aes-256-cbc';\n\n\t/**\n\t * Flag indicating whether we support exporting the plaintext\n\t */\n\t#mayAccessPlaintext = true;\n\n\t/**\n\t * Encryption details\n\t */\n\tprotected _internalState: EncryptedContainerInfo | UnencryptedContainerInfo;\n\n\t/**\n\t * The plaintext or encoded (and possibly encrypted) data\n\t */\n\t#data:\n\t\t{ plaintext: Buffer } |\n\t\t{ encoded: Buffer } |\n\t\t{ plaintext: Buffer, encoded: Buffer };\n\n\tconstructor(principals: Account[] | null) {\n\t\tif (principals === null) {\n\t\t\tthis._internalState = {\n\t\t\t\tprincipals: null\n\t\t\t};\n\t\t} else {\n\t\t\tthis._internalState = {\n\t\t\t\tprincipals: principals,\n\t\t\t\tcipherAlgo: EncryptedContainer.algorithm\n\t\t\t}\n\t\t};\n\t\tthis.#data = { plaintext: Buffer.alloc(0) };\n\t}\n\n\tget encrypted(): boolean {\n\t\treturn(this._internalState.principals !== null);\n\t}\n\n\t#isEncrypted(): this is { _internalState: EncryptedContainerInfo } {\n\t\treturn(this.encrypted);\n\t}\n\n\t/**\n\t * Create an instance of the EncryptedContainer from an encrypted blob,\n\t * it will need to be decryptable with one of the specified principals\n\t *\n\t * After decryption happens, the list of principals with access to the\n\t * resource will be reset to what is contained within the encrypted\n\t * container\n\t */\n\tstatic fromEncryptedBuffer(data: ArrayBuffer | Buffer, principals: Account[]): EncryptedContainer {\n\t\tconst retval = new EncryptedContainer(principals);\n\n\t\tretval.#setEncodedBuffer(data);\n\t\tretval.#computeAndSetKeyInfo(true);\n\n\t\treturn(retval);\n\t}\n\n\tstatic fromEncodedBuffer(data: ArrayBuffer | Buffer, principals: Account[] | null): EncryptedContainer {\n\t\tconst retval = new EncryptedContainer(principals);\n\n\t\tretval.#setEncodedBuffer(data);\n\t\tretval.#computeAndSetKeyInfo(false);\n\n\t\treturn(retval);\n\t}\n\n\t/**\n\t * Create an instance of the EncryptedContainer from a plaintext.\n\t *\n\t * It will be decryptable by any one of the specified principals\n\t *\n\t * @param data The plaintext data to encrypt or encode\n\t * @param principals The list of principals who can access the data if it is null then the data is not encrypted\n\t * @param locked If true, the plaintext data will not be accessible from this instance; otherwise it will be -- default depends on principals\n\t * @returns The EncryptedContainer instance with the plaintext data and principals set\n\t */\n\tstatic fromPlaintext(data: string | ArrayBuffer | Buffer, principals: Account[] | null, locked?: boolean): EncryptedContainer {\n\t\tconst retval = new EncryptedContainer(principals);\n\n\t\tif (locked === undefined) {\n\t\t\tlocked = true;\n\t\t\tif (principals === null) {\n\t\t\t\tlocked = false;\n\t\t\t}\n\t\t}\n\n\t\tif (locked) {\n\t\t\tretval.disablePlaintext();\n\t\t}\n\n\t\tretval.setPlaintext(data);\n\n\t\treturn(retval);\n\t}\n\n\t/**\n\t * Set the plaintext buffer to the specified value\n\t */\n\tsetPlaintext(data: string | ArrayBuffer | Buffer): void {\n\t\tthis.#data = { plaintext: inputToBuffer(data) };\n\t}\n\n\t/**\n\t * Set the encoded blob to the specified value\n\t */\n\t#setEncodedBuffer(data: ArrayBuffer | Buffer): void {\n\t\tthis.#data = { encoded: inputToBuffer(data) };\n\t}\n\n\tprivate get _encoded(): Buffer | undefined {\n\t\tif ('encoded' in this.#data && this.#data.encoded !== undefined) {\n\t\t\treturn(this.#data.encoded);\n\t\t}\n\n\t\treturn(undefined);\n\t}\n\n\tprivate get _plaintext(): Buffer | undefined {\n\t\tif ('plaintext' in this.#data && this.#data.plaintext !== undefined) {\n\t\t\treturn(this.#data.plaintext);\n\t\t}\n\n\t\treturn(undefined);\n\t}\n\n\t/*\n\t * Return the decoded data from the encoded blob\n\t * and populate the symmetric key parameters from the encoded blob if it is encrypted\n\t */\n\t#computeAndSetKeyInfo(mustBeEncrypted: boolean) {\n\t\tif (this._encoded === undefined) {\n\t\t\tthrow(new Error('No encoded data available'));\n\t\t}\n\n\t\tconst plaintextWrapper = parseASN1Bare(this._encoded);\n\n\t\tif (mustBeEncrypted && !plaintextWrapper.isEncrypted) {\n\t\t\tthrow(new Error('Unable to set key information from plaintext -- it is not encrypted but that was required'));\n\t\t}\n\n\t\tif (plaintextWrapper.isEncrypted) {\n\t\t\tconst principals = this._internalState.principals;\n\t\t\tif (principals === null) {\n\t\t\t\tthrow(new Error('May not encrypt data with a null set of principals'));\n\t\t\t}\n\n\t\t\t/*\n\t\t\t * Compute the new accounts by merging the input from the\n\t\t\t * data and the existing list of principals\n\t\t\t */\n\t\t\t/**\n\t\t\t * The existing principals from the blob, with existing\n\t\t\t * principals substituted in where appropriate\n\t\t\t */\n\t\t\tconst blobPrincipals = (plaintextWrapper.keys ?? []).map((keyInfo) => {\n\t\t\t\tconst currentPublicKey = keyInfo.publicKey;\n\t\t\t\tif (!currentPublicKey.isAccount()) {\n\t\t\t\t\tthrow(new Error('internal error: Non-account found within the encryption key list'));\n\t\t\t\t}\n\n\t\t\t\tfor (const checkExistingKey of principals) {\n\t\t\t\t\tif (checkExistingKey.comparePublicKey(currentPublicKey)) {\n\t\t\t\t\t\treturn(checkExistingKey);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\treturn(currentPublicKey);\n\t\t\t});\n\n\t\t\tthis._internalState.principals = blobPrincipals;\n\n\t\t\t// Confirm updated principals are populated correctly which sets container to encrypted\n\t\t\tif (!this.encrypted) {\n\t\t\t\tthrow(new Error('internal error: Encrypted data found but not marked as encrypted'));\n\t\t\t}\n\t\t} else {\n\t\t\tthis._internalState.principals = null;\n\n\t\t\tif (this.encrypted) {\n\t\t\t\tthrow(new Error('internal error: Plaintext data found but marked as encrypted'));\n\t\t\t}\n\t\t}\n\n\t\treturn(plaintextWrapper);\n\t}\n\n\t/**\n\t * Populate the plaintext (as well as symmetric key parameters) from\n\t * the encoded blob\n\t */\n\tasync #computePlaintext() {\n\t\tif (this._plaintext) {\n\t\t\treturn(this._plaintext);\n\t\t}\n\n\t\tif (this._encoded === undefined) {\n\t\t\tthrow(new Error('No plaintext or encoded data available'));\n\t\t}\n\n\n\t\tconst info = this.#computeAndSetKeyInfo(this.encrypted);\n\n\t\tlet principals = this._internalState.principals;\n\t\tif (info.isEncrypted) {\n\t\t\tif (principals === null) {\n\t\t\t\tthrow(new Error('May not decrypt data with a null set of principals'));\n\t\t\t}\n\t\t} else {\n\t\t\tprincipals = [];\n\t\t}\n\n\t\tconst plaintextWrapper = await parseASN1Decrypt(info, principals);\n\t\tconst plaintext = plaintextWrapper.plaintext;\n\n\t\tthis.#data = { ...this.#data, plaintext };\n\n\t\treturn(plaintext);\n\t}\n\n\t/**\n\t * Compute the encoded version of the plaintext data\n\t */\n\tasync #computePlaintextEncoded() {\n\t\tif (this._plaintext === undefined) {\n\t\t\tthrow(new Error('No plaintext data available'));\n\t\t}\n\n\t\tconst structuredData = await buildASN1(\n\t\t\tthis._plaintext\n\t\t);\n\n\t\treturn(structuredData);\n\t}\n\n\t/**\n\t * Populate the encrypted blob from the plaintext and symmetric key\n\t * parameters. If the symmetric key parameters have not been\n\t * initialized they will be initialized at this time.\n\t */\n\tasync #computeEncryptedEncoded() {\n\t\tif (this._plaintext === undefined) {\n\t\t\tthrow(new Error('No encrypted nor plaintext data available'));\n\t\t}\n\n\t\tif (!this.#isEncrypted()) {\n\t\t\tthrow(new Error('internal error: Asked to encrypt a plaintext buffer'));\n\t\t}\n\n\t\t/**\n\t\t * structured data is the ASN.1 encoded structure\n\t\t */\n\t\tconst structuredData = await buildASN1(\n\t\t\tthis._plaintext,\n\t\t\t{\n\t\t\t\tkeys: this._internalState.principals,\n\t\t\t\tcipherKey: crypto.randomBytes(32),\n\t\t\t\tcipherIV: crypto.randomBytes(16),\n\t\t\t\tcipherAlgo: this._internalState.cipherAlgo\n\t\t\t}\n\t\t);\n\n\t\treturn(structuredData);\n\t}\n\n\tasync #computeEncoded() {\n\t\tif (this._encoded !== undefined) {\n\t\t\treturn(this._encoded);\n\t\t}\n\n\t\tlet computed: Buffer;\n\t\tif (!this.encrypted) {\n\t\t\tcomputed = await this.#computePlaintextEncoded();\n\t\t} else {\n\t\t\tcomputed = await this.#computeEncryptedEncoded();\n\t\t}\n\n\t\tthis.#data = { ...this.#data, encoded: computed };\n\n\t\treturn(computed);\n\t}\n\n\t/**\n\t * Grant access to the secret for account(s) synchronously. This\n\t * assumes the plaintext has already been computed and will fail\n\t * if it is not\n\t */\n\tgrantAccessSync(accounts: Account[] | Account): this {\n\t\tif (this._plaintext === undefined) {\n\t\t\tthrow(new Error('Unable to grant access, plaintext not available'));\n\t\t}\n\n\t\tif (!this.#isEncrypted()) {\n\t\t\tthrow(new Error('May not manage access to a plaintext container'));\n\t\t}\n\n\t\tif (!Array.isArray(accounts)) {\n\t\t\taccounts = [accounts];\n\t\t}\n\n\t\t// Encoded data is invalidated with the new permissions so set only the plaintext data\n\t\tthis.setPlaintext(this._plaintext);\n\n\t\tthis._internalState.principals.push(...accounts);\n\n\t\treturn(this);\n\t}\n\n\t/**\n\t * Grant access to the secret for account(s).\n\t */\n\tasync grantAccess(accounts: Account[] | Account): Promise<this> {\n\t\tawait this.#computePlaintext();\n\n\t\tthis.grantAccessSync(accounts);\n\n\t\treturn(this);\n\t}\n\n\t/**\n\t * Revoke access to the secret for an account synchronously. This\n\t * assumes the plaintext has already been computed and will fail\n\t * if it is not\n\t */\n\trevokeAccessSync(account: Account): this {\n\t\tif (this._plaintext === undefined) {\n\t\t\tthrow(new Error('Unable to revoke access, plaintext not available'));\n\t\t}\n\n\t\tif (!this.#isEncrypted()) {\n\t\t\tthrow(new Error('May not manage access to a plaintext container'));\n\t\t}\n\n\t\t// Encoded data is invalidated with the new permissions so set only the plaintext data\n\t\tthis.setPlaintext(this._plaintext);\n\n\t\tthis._internalState.principals = this._internalState.principals.filter(function(checkAccount) {\n\t\t\treturn(!checkAccount.comparePublicKey(account));\n\t\t});\n\n\t\treturn(this);\n\t}\n\n\t/**\n\t * Revoke access to the secret for an account\n\t */\n\tasync revokeAccess(account: Account): Promise<this> {\n\t\tawait this.#computePlaintext();\n\n\t\tthis.revokeAccessSync(account);\n\n\t\treturn(this);\n\t}\n\n\t/**\n\t * Disable access to the plaintext from this instance\n\t */\n\tdisablePlaintext(): this {\n\t\tthis.#mayAccessPlaintext = false;\n\n\t\treturn(this);\n\t}\n\n\t/**\n\t * Get the plaintext for this instance\n\t */\n\tasync getPlaintext(): Promise<ArrayBuffer> {\n\t\tif (!this.#mayAccessPlaintext) {\n\t\t\tthrow(new Error('May not access plaintext'));\n\t\t}\n\n\t\tconst plaintext = await this.#computePlaintext();\n\n\t\tif (plaintext === undefined) {\n\t\t\tthrow(new Error('internal error: Plaintext could not be decoded'));\n\t\t}\n\n\t\t/*\n\t\t * Make a copy of our internal buffer so that any changes made\n\t\t * to either our internal buffer or by our caller do not\n\t\t * interfere\n\t\t */\n\t\treturn(bufferToArrayBuffer(plaintext));\n\t}\n\n\t/**\n\t * Get the serializable buffer which can be stored and reconstructed\n\t */\n\tasync getEncodedBuffer(): Promise<ArrayBuffer> {\n\t\tconst serialized = await this.#computeEncoded();\n\n\t\tif (serialized === undefined) {\n\t\t\tthrow(new Error('internal error: Could not encode data'));\n\t\t}\n\n\t\t/*\n\t\t * Make a copy of our internal buffer so that any changes made\n\t\t * to either our internal buffer or by our caller do not\n\t\t * interfere\n\t\t */\n\t\treturn(bufferToArrayBuffer(serialized));\n\t}\n\n\t/**\n\t * Get the list of accounts which have access to read the plaintext of\n\t * this container\n\t */\n\tget principals(): Account[] {\n\t\tif (!this.#isEncrypted()) {\n\t\t\tthrow(new Error('May not manage access to a plaintext container'));\n\t\t}\n\n\t\treturn(this._internalState.principals);\n\t}\n}\n\n/** @internal */\nexport const _Testing = {\n\tbuildASN1: buildASN1,\n\tparseASN1: parseASN1\n};\n\nexport default EncryptedContainer;\n"]}
|
|
1
|
+
{"version":3,"file":"encrypted-container.js","sourceRoot":"","sources":["../../src/lib/encrypted-container.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,mBAAmB,CAAC;AACvC,OAAO,EAAE,GAAG,IAAI,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAKnE,OAAO,EAAE,MAAM,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AACrF,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAE3C,MAAM,gBAAgB,GAAG,WAAW,CAAC,KAAK,CAAC,MAAM,CAAC,gBAAgB,CAAC;AACnE,MAAM,gBAAgB,GAAG,WAAW,CAAC,KAAK,CAAC,MAAM,CAAC,gBAAgB,CAAC;AACnE,MAAM,QAAQ,GAAG,WAAW,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC;AACjD,MAAM,QAAQ,GAAG,WAAW,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC;AACjD,MAAM,OAAO,GAA+B,WAAW,CAAC,OAAO,CAAC;AAgHhE,MAAM,KAAK,GAAG;IACb,aAAa,EAAE,yBAAyB;CAC/B,CAAC;AAEX;;;;EAIE;AACF,KAAK,UAAU,SAAS,CAAC,SAAiB,EAAE,iBAA+B;IAC1E,MAAM,mBAAmB,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,gBAAgB,CAAC,SAA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AAC;IACxC,CAAC;;AAGF,gBAAgB;AAChB,MAAM,CAAC,MAAM,QAAQ,GAAG;IACvB,SAAS,EAAE,SAAS;IACpB,SAAS,EAAE,SAAS;CACpB,CAAC;AAEF,eAAe,kBAAkB,CAAC","sourcesContent":["import crypto from './utils/crypto.js';\nimport { lib as KeetaNetLib } from '@keetanetwork/keetanet-client';\n\nimport type {\n\tASN1OID\n} from '@keetanetwork/keetanet-client/lib/utils/asn1.js';\nimport { Buffer, arrayBufferToBuffer, bufferToArrayBuffer } from './utils/buffer.js';\nimport { isArray } from './utils/array.js';\n\nconst zlibDeflateAsync = KeetaNetLib.Utils.Buffer.ZlibDeflateAsync;\nconst zlibInflateAsync = KeetaNetLib.Utils.Buffer.ZlibInflateAsync;\nconst ASN1toJS = KeetaNetLib.Utils.ASN1.ASN1toJS;\nconst JStoASN1 = KeetaNetLib.Utils.ASN1.JStoASN1;\nconst Account: typeof KeetaNetLib.Account = KeetaNetLib.Account;\ntype Account = InstanceType<typeof KeetaNetLib.Account>;\n\n/*\n * ASN.1 Schema\n *\n * EncryptedContainer DEFINITIONS ::=\n * BEGIN\n * Version ::= INTEGER { v2(1) }\n *\n * KeyStore ::= SEQUENCE {\n * publicKey OCTET STRING,\n * encryptedSymmetricKey OCTET STRING,\n * ...\n * }\n *\n * EncryptedContainerBox ::= SEQUENCE {\n * keys SEQUENCE OF KeyStore,\n * encryptionAlgorithm OBJECT IDENTIFIER,\n * initializationVector OCTET STRING,\n * encryptedValue OCTET STRING,\n * ...\n * }\n *\n * PlaintextContainerBox ::= SEQUENCE {\n * plainValue OCTET STRING,\n * ...\n * }\n *\n * ContainerPackage ::= SEQUENCE {\n * version Version (v2),\n * encryptedContainer [0] EXPLICIT EncryptedContainerBox OPTIONAL,\n * plaintextContainer [1] EXPLICIT PlaintextContainerBox OPTIONAL,\n * ...\n * } (WITH COMPONENTS {\n * encryptedContainer PRESENT,\n * plaintextContainer ABSENT\n * } |\n * WITH COMPONENTS {\n * encryptedContainer ABSENT,\n * plaintextContainer PRESENT\n * })\n * END\n *\n */\n\ntype EncryptedContainerKeyStore = [\n\t/* publicKey */\n\tBuffer,\n\n\t/* encryptedSymmetricKey */\n\tBuffer\n];\n\ntype EncryptedContainerBoxEncrypted = [\n\t/* keys */\n\tEncryptedContainerKeyStore[],\n\n\t/* encryptionAlgorithm */\n\tASN1OID,\n\n\t/* initializationVector */\n\tBuffer,\n\n\t/* value */\n\tBuffer\n];\n\ntype EncryptedContainerBoxPlaintext = [\n\t/* value */\n\tBuffer\n];\n\ntype ContainerPackage = [\n\t/* version */\n\tnumber,\n\n\t{\n\t\ttype: 'context'\n\t\tvalue: 0,\n\t\tkind: 'explicit',\n\t\tcontains: EncryptedContainerBoxEncrypted\n\t} | {\n\t\ttype: 'context',\n\t\tvalue: 1,\n\t\tkind: 'explicit',\n\t\tcontains: EncryptedContainerBoxPlaintext\n\t}\n];\n\ntype CipherOptions = {\n\t/**\n\t * The symmetric cipher key (if any)\n\t */\n\tcipherKey: Buffer | undefined;\n\t/**\n\t * The symmetric cipher IV (if any)\n\t */\n\tcipherIV: Buffer | undefined;\n\t/**\n\t * The symmetric cipher algorithm\n\t */\n\tcipherAlgo: string;\n}\n\ntype ASN1Options = Required<CipherOptions> & {\n\t/**\n\t * The set of accounts to encrypt the formatted data\n\t */\n\tkeys: Account[];\n}\n\nconst oidDB = {\n\t'aes-256-cbc': '2.16.840.1.101.3.4.1.42'\n} as const;\n\n/**\n* Compiles the ASN.1 for the container\n*\n* @returns The ASN.1 DER data\n*/\nasync function buildASN1(plaintext: Buffer, encryptionOptions?: ASN1Options): Promise<Buffer> {\n\tconst compressedPlaintext = Buffer.from(await zlibDeflateAsync(plaintext));\n\n\tconst sequence: Partial<ContainerPackage> = [];\n\n\t/*\n\t * Version v2 (1)\n\t */\n\tsequence[0] = 1;\n\n\t/*\n\t * Encrypted container box\n\t */\n\tif (encryptionOptions) {\n\t\tconst { keys, cipherKey, cipherIV, cipherAlgo } = encryptionOptions;\n\n\t\tif (keys === undefined || keys.length === 0 || cipherKey === undefined || cipherIV === undefined || cipherAlgo === undefined) {\n\t\t\tthrow(new Error('internal error: Unsupported method invocation'));\n\t\t}\n\n\t\tif (!(cipherAlgo in oidDB)) {\n\t\t\tthrow(new Error(`Unsupported algorithm: ${cipherAlgo}`));\n\t\t}\n\n\t\t// eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n\t\tconst algorithmOID = oidDB[cipherAlgo as keyof typeof oidDB];\n\n\t\tconst cipher = crypto.createCipheriv(\n\t\t\tcipherAlgo,\n\t\t\tcipherKey,\n\t\t\tcipherIV\n\t\t);\n\n\t\tconst encryptedData = Buffer.concat([\n\t\t\tcipher.update(compressedPlaintext),\n\t\t\tcipher.final()\n\t\t]);\n\n\t\tconst cipherKeyArrayBuffer = bufferToArrayBuffer(cipherKey);\n\n\t\tconst encryptionKeysSequence = await Promise.all(keys.map(async function(key) {\n\t\t\tconst encryptedSymmetricKey = Buffer.from(await key.encrypt(cipherKeyArrayBuffer));\n\t\t\tconst retval: EncryptedContainerKeyStore = [\n\t\t\t\tkey.publicKeyAndType,\n\t\t\t\tencryptedSymmetricKey\n\t\t\t];\n\n\t\t\treturn(retval);\n\t\t}));\n\n\t\tsequence[1] = {\n\t\t\ttype: 'context',\n\t\t\tvalue: 0,\n\t\t\tkind: 'explicit',\n\t\t\tcontains: [\n\t\t\t\tencryptionKeysSequence,\n\t\t\t\t{ type: 'oid', oid: algorithmOID },\n\t\t\t\tcipherIV,\n\t\t\t\tencryptedData\n\t\t\t]\n\t\t};\n\t} else {\n\t\t/*\n\t\t * Otherwise we simply pass in the compressed data\n\t\t */\n\t\tsequence[1] = {\n\t\t\ttype: 'context',\n\t\t\tvalue: 1,\n\t\t\tkind: 'explicit',\n\t\t\tcontains: [compressedPlaintext]\n\t\t};\n\t}\n\n\tconst outputASN1 = JStoASN1(sequence);\n\tconst outputDER = Buffer.from(outputASN1.toBER(false));\n\n\treturn(outputDER);\n}\n\nfunction parseASN1Bare(input: Buffer, acceptableEncryptionAlgorithms = ['aes-256-cbc', 'null']) {\n\tconst inputSequence = ASN1toJS(bufferToArrayBuffer(input));\n\tif (!isArray(inputSequence, 2)) {\n\t\tthrow(new Error('Malformed data detected (incorrect base format)'));\n\t}\n\n\tconst version = inputSequence[0];\n\tif (typeof version !== 'bigint') {\n\t\tthrow(new Error('Malformed data detected (version expected at position 0)'));\n\t}\n\n\tif (version !== 1n) {\n\t\tthrow(new Error('Malformed data detected (unsupported version)'));\n\t}\n\n\tconst valueBox = inputSequence[1];\n\tif (typeof valueBox !== 'object' || valueBox === null) {\n\t\tthrow(new Error('Malformed data detected (data expected at position 1)'));\n\t}\n\n\tif (!('type' in valueBox) || typeof valueBox.type !== 'string') {\n\t\tthrow(new Error('Malformed data detected (expected type at position 1)'));\n\t}\n\n\tif (valueBox.type !== 'context') {\n\t\tthrow(new Error('Malformed data detected (expected context at position 1)'));\n\t}\n\n\tif (!('value' in valueBox) || typeof valueBox.value !== 'number') {\n\t\tthrow(new Error('Malformed data detected (expected context value at position 1)'));\n\t}\n\n\tif (valueBox.value !== 0 && valueBox.value !== 1) {\n\t\tthrow(new Error('Malformed data detected (expected context value of 0 or 1)'));\n\t}\n\n\tif (!('contains' in valueBox) || typeof valueBox.contains !== 'object' || valueBox.contains === null) {\n\t\tthrow(new Error('Malformed data detected (expected contents at position 1)'));\n\t}\n\n\tlet isEncrypted;\n\tif (valueBox.value === 0) {\n\t\tisEncrypted = true;\n\t} else {\n\t\tisEncrypted = false;\n\t}\n\n\tconst value = valueBox.contains;\n\tlet containedCompressed: Buffer;\n\tlet cipherInfo;\n\tif (isEncrypted) {\n\t\tif (!isArray(value, 4)) {\n\t\t\tthrow(new Error('Malformed data (incorrect number of elements within position 1 -- expected 4)'));\n\t\t}\n\n\t\tconst keyInfoUnchecked = value[0];\n\t\tif (!isArray(keyInfoUnchecked)) {\n\t\t\tthrow(new Error('Malformed data (expected sequence at position 2.0)'));\n\t\t}\n\n\t\tconst keyInfo = keyInfoUnchecked.map(function(checkKeyInfo) {\n\t\t\tif (!isArray(checkKeyInfo, 2)) {\n\t\t\t\tthrow(new Error('Malformed key information (expected sequence of 2 at position 1.0.x)'));\n\t\t\t}\n\n\t\t\tconst publicKeyBuffer = checkKeyInfo[0];\n\t\t\tif (!Buffer.isBuffer(publicKeyBuffer)) {\n\t\t\t\tthrow(new Error('Malformed key information (expected octet string for public key at position 1.0.x)'));\n\t\t\t}\n\t\t\tconst publicKey = Account.fromPublicKeyAndType(publicKeyBuffer);\n\n\t\t\tconst encryptedSymmetricKey = checkKeyInfo[1];\n\t\t\tif (!Buffer.isBuffer(encryptedSymmetricKey)) {\n\t\t\t\tthrow(new Error('Malformed key information (expected octet string for cipher key at position 1.0.x)'));\n\t\t\t}\n\n\t\t\treturn({\n\t\t\t\tpublicKey,\n\t\t\t\tencryptedSymmetricKey\n\t\t\t});\n\t\t});\n\n\t\t// eslint-disable-next-line @typescript-eslint/no-unused-vars\n\t\tconst encryptionAlgorithmOID = value[1];\n\t\t/* XXX:TODO: Lookup the encryption algorithm from the OID */\n\t\tconst encryptionAlgorithm = 'aes-256-cbc';\n\n\t\tconst cipherIV = value[2];\n\t\tif (!Buffer.isBuffer(cipherIV)) {\n\t\t\tthrow(new Error('Malformed data (cipher IV expected at position 1.2)'));\n\t\t}\n\n\t\tconst encryptedCompressedValue = value[3];\n\t\tif (!Buffer.isBuffer(encryptedCompressedValue)) {\n\t\t\tthrow(new Error('Malformed data (encrypted compressed buffer expected at position 1.3)'));\n\t\t}\n\n\t\tcipherInfo = {\n\t\t\tkeys: keyInfo,\n\t\t\tcipherIV: cipherIV,\n\t\t\tencryptedData: encryptedCompressedValue,\n\t\t\tencryptionAlgorithm: encryptionAlgorithm\n\t\t};\n\n\t\tcontainedCompressed = encryptedCompressedValue;\n\t} else {\n\t\tif (!isArray(value, 1)) {\n\t\t\tthrow(new Error('Malformed data (incorrect number of elements within position 1 -- expected 1)'));\n\t\t}\n\n\t\tconst containedCompressedUnchecked = value[0];\n\t\tif (!Buffer.isBuffer(containedCompressedUnchecked)) {\n\t\t\tthrow(new Error('Malformed data (compressed buffer expected at position 1.0)'));\n\t\t}\n\n\t\tif (!acceptableEncryptionAlgorithms.includes('null')) {\n\t\t\tthrow(new Error('Malformed data (plaintext found but the null encryption algorithm is not acceptable)'));\n\t\t}\n\n\t\tcontainedCompressed = containedCompressedUnchecked;\n\t}\n\n\treturn({\n\t\tversion: version,\n\t\tisEncrypted: isEncrypted,\n\t\tinnerValue: containedCompressed,\n\t\t...cipherInfo\n\t});\n}\n\nasync function parseASN1Decrypt(inputInfo: ReturnType<typeof parseASN1Bare>, keys?: Account[]) {\n\tlet containedCompressed: Buffer;\n\tlet cipherInfo;\n\tif (inputInfo.isEncrypted) {\n\t\tif (keys === undefined || keys.length === 0) {\n\t\t\tthrow(new Error('Encrypted Container found with encryption but no keys for decryption supplied'));\n\t\t}\n\n\t\tconst algorithm = inputInfo.encryptionAlgorithm;\n\t\tif (algorithm === undefined) {\n\t\t\tthrow(new Error('Encrypted Container found with encryption but no algorithm supplied'));\n\t\t}\n\n\t\tconst keyInfo = inputInfo.keys;\n\t\tif (keyInfo === undefined) {\n\t\t\tthrow(new Error('internal error: Encrypted container found with missing keys'));\n\t\t}\n\n\t\tlet decryptionKeyInfo;\n\t\tfor (const checkKeyInfo of keyInfo) {\n\t\t\tfor (const key of keys) {\n\t\t\t\tif (!key.hasPrivateKey) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tif (key.comparePublicKey(checkKeyInfo.publicKey)) {\n\t\t\t\t\tdecryptionKeyInfo = {\n\t\t\t\t\t\t...checkKeyInfo,\n\t\t\t\t\t\tprivateKey: key\n\t\t\t\t\t};\n\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (decryptionKeyInfo === undefined) {\n\t\t\tthrow(new Error('No keys found which can perform decryption on the supplied encryption box'));\n\t\t}\n\n\t\tconst cipherKey = Buffer.from(await decryptionKeyInfo.privateKey.decrypt(bufferToArrayBuffer(decryptionKeyInfo.encryptedSymmetricKey)));\n\n\t\tconst cipherIV = inputInfo.cipherIV;\n\t\tif (cipherIV === undefined) {\n\t\t\tthrow(new Error('internal error: No Cipher IV found'));\n\t\t}\n\n\t\tconst encryptedCompressedValue = inputInfo.innerValue;\n\t\tconst decipher = crypto.createDecipheriv(algorithm, cipherKey, cipherIV);\n\t\tcontainedCompressed = Buffer.concat([\n\t\t\tdecipher.update(encryptedCompressedValue),\n\t\t\tdecipher.final()\n\t\t]);\n\n\t\tcipherInfo = {\n\t\t\tisEncrypted: true,\n\t\t\tkeys: keyInfo,\n\t\t\tcipherIV: cipherIV,\n\t\t\tcipherKey: cipherKey,\n\t\t\tencryptedData: encryptedCompressedValue\n\t\t};\n\t} else {\n\t\tcontainedCompressed = inputInfo.innerValue;\n\t\tcipherInfo = {\n\t\t\tisEncrypted: false\n\t\t};\n\t}\n\n\tconst plaintext = Buffer.from(await zlibInflateAsync(containedCompressed));\n\n\treturn({\n\t\tversion: inputInfo.version,\n\t\tplaintext: plaintext,\n\t\t...cipherInfo\n\t});\n\n}\n\nasync function parseASN1(input: Buffer, keys?: Account[]) {\n\tconst inputInfo = parseASN1Bare(input);\n\tconst retval = await parseASN1Decrypt(inputInfo, keys);\n\n\treturn(retval);\n}\n\nfunction inputToBuffer(data: string | ArrayBuffer | Buffer): Buffer {\n\tif (typeof data === 'string') {\n\t\treturn(Buffer.from(data, 'utf-8'));\n\t} else if (Buffer.isBuffer(data)) {\n\t\treturn(data);\n\t} else {\n\t\treturn(arrayBufferToBuffer(data));\n\t}\n}\n\ntype EncryptedContainerInfo = Pick<CipherOptions, 'cipherAlgo'> & {\n\t/**\n\t * Set of accounts which can access the data\n\t */\n\tprincipals: Account[];\n}\n\ntype UnencryptedContainerInfo = {\n\t/**\n\t * Unencrypted container should not have any principals\n\t */\n\tprincipals: null;\n}\n\nexport class EncryptedContainer {\n\tprivate static readonly algorithm = 'aes-256-cbc';\n\n\t/**\n\t * Flag indicating whether we support exporting the plaintext\n\t */\n\t#mayAccessPlaintext = true;\n\n\t/**\n\t * Encryption details\n\t */\n\tprotected _internalState: EncryptedContainerInfo | UnencryptedContainerInfo;\n\n\t/**\n\t * The plaintext or encoded (and possibly encrypted) data\n\t */\n\t#data:\n\t\t{ plaintext: Buffer } |\n\t\t{ encoded: Buffer } |\n\t\t{ plaintext: Buffer, encoded: Buffer };\n\n\tconstructor(principals: Account[] | null) {\n\t\tif (principals === null) {\n\t\t\tthis._internalState = {\n\t\t\t\tprincipals: null\n\t\t\t};\n\t\t} else {\n\t\t\tthis._internalState = {\n\t\t\t\tprincipals: principals,\n\t\t\t\tcipherAlgo: EncryptedContainer.algorithm\n\t\t\t}\n\t\t};\n\t\tthis.#data = { plaintext: Buffer.alloc(0) };\n\t}\n\n\tget encrypted(): boolean {\n\t\treturn(this._internalState.principals !== null);\n\t}\n\n\t#isEncrypted(): this is { _internalState: EncryptedContainerInfo } {\n\t\treturn(this.encrypted);\n\t}\n\n\t/**\n\t * Create an instance of the EncryptedContainer from an encrypted blob,\n\t * it will need to be decryptable with one of the specified principals\n\t *\n\t * After decryption happens, the list of principals with access to the\n\t * resource will be reset to what is contained within the encrypted\n\t * container\n\t */\n\tstatic fromEncryptedBuffer(data: ArrayBuffer | Buffer, principals: Account[]): EncryptedContainer {\n\t\tconst retval = new EncryptedContainer(principals);\n\n\t\tretval.#setEncodedBuffer(data);\n\t\tretval.#computeAndSetKeyInfo(true);\n\n\t\treturn(retval);\n\t}\n\n\tstatic fromEncodedBuffer(data: ArrayBuffer | Buffer, principals: Account[] | null): EncryptedContainer {\n\t\tconst retval = new EncryptedContainer(principals);\n\n\t\tretval.#setEncodedBuffer(data);\n\t\tretval.#computeAndSetKeyInfo(false);\n\n\t\treturn(retval);\n\t}\n\n\t/**\n\t * Create an instance of the EncryptedContainer from a plaintext.\n\t *\n\t * It will be decryptable by any one of the specified principals\n\t *\n\t * @param data The plaintext data to encrypt or encode\n\t * @param principals The list of principals who can access the data if it is null then the data is not encrypted\n\t * @param locked If true, the plaintext data will not be accessible from this instance; otherwise it will be -- default depends on principals\n\t * @returns The EncryptedContainer instance with the plaintext data and principals set\n\t */\n\tstatic fromPlaintext(data: string | ArrayBuffer | Buffer, principals: Account[] | null, locked?: boolean): EncryptedContainer {\n\t\tconst retval = new EncryptedContainer(principals);\n\n\t\tif (locked === undefined) {\n\t\t\tlocked = true;\n\t\t\tif (principals === null) {\n\t\t\t\tlocked = false;\n\t\t\t}\n\t\t}\n\n\t\tif (locked) {\n\t\t\tretval.disablePlaintext();\n\t\t}\n\n\t\tretval.setPlaintext(data);\n\n\t\treturn(retval);\n\t}\n\n\t/**\n\t * Set the plaintext buffer to the specified value\n\t */\n\tsetPlaintext(data: string | ArrayBuffer | Buffer): void {\n\t\tthis.#data = { plaintext: inputToBuffer(data) };\n\t}\n\n\t/**\n\t * Set the encoded blob to the specified value\n\t */\n\t#setEncodedBuffer(data: ArrayBuffer | Buffer): void {\n\t\tthis.#data = { encoded: inputToBuffer(data) };\n\t}\n\n\tprivate get _encoded(): Buffer | undefined {\n\t\tif ('encoded' in this.#data && this.#data.encoded !== undefined) {\n\t\t\treturn(this.#data.encoded);\n\t\t}\n\n\t\treturn(undefined);\n\t}\n\n\tprivate get _plaintext(): Buffer | undefined {\n\t\tif ('plaintext' in this.#data && this.#data.plaintext !== undefined) {\n\t\t\treturn(this.#data.plaintext);\n\t\t}\n\n\t\treturn(undefined);\n\t}\n\n\t/*\n\t * Return the decoded data from the encoded blob\n\t * and populate the symmetric key parameters from the encoded blob if it is encrypted\n\t */\n\t#computeAndSetKeyInfo(mustBeEncrypted: boolean) {\n\t\tif (this._encoded === undefined) {\n\t\t\tthrow(new Error('No encoded data available'));\n\t\t}\n\n\t\tconst plaintextWrapper = parseASN1Bare(this._encoded);\n\n\t\tif (mustBeEncrypted && !plaintextWrapper.isEncrypted) {\n\t\t\tthrow(new Error('Unable to set key information from plaintext -- it is not encrypted but that was required'));\n\t\t}\n\n\t\tif (plaintextWrapper.isEncrypted) {\n\t\t\tconst principals = this._internalState.principals;\n\t\t\tif (principals === null) {\n\t\t\t\tthrow(new Error('May not encrypt data with a null set of principals'));\n\t\t\t}\n\n\t\t\t/*\n\t\t\t * Compute the new accounts by merging the input from the\n\t\t\t * data and the existing list of principals\n\t\t\t */\n\t\t\t/**\n\t\t\t * The existing principals from the blob, with existing\n\t\t\t * principals substituted in where appropriate\n\t\t\t */\n\t\t\tconst blobPrincipals = (plaintextWrapper.keys ?? []).map((keyInfo) => {\n\t\t\t\tconst currentPublicKey = keyInfo.publicKey;\n\t\t\t\tif (!currentPublicKey.isAccount()) {\n\t\t\t\t\tthrow(new Error('internal error: Non-account found within the encryption key list'));\n\t\t\t\t}\n\n\t\t\t\tfor (const checkExistingKey of principals) {\n\t\t\t\t\tif (checkExistingKey.comparePublicKey(currentPublicKey)) {\n\t\t\t\t\t\treturn(checkExistingKey);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\treturn(currentPublicKey);\n\t\t\t});\n\n\t\t\tthis._internalState.principals = blobPrincipals;\n\n\t\t\t// Confirm updated principals are populated correctly which sets container to encrypted\n\t\t\tif (!this.encrypted) {\n\t\t\t\tthrow(new Error('internal error: Encrypted data found but not marked as encrypted'));\n\t\t\t}\n\t\t} else {\n\t\t\tthis._internalState.principals = null;\n\n\t\t\tif (this.encrypted) {\n\t\t\t\tthrow(new Error('internal error: Plaintext data found but marked as encrypted'));\n\t\t\t}\n\t\t}\n\n\t\treturn(plaintextWrapper);\n\t}\n\n\t/**\n\t * Populate the plaintext (as well as symmetric key parameters) from\n\t * the encoded blob\n\t */\n\tasync #computePlaintext() {\n\t\tif (this._plaintext) {\n\t\t\treturn(this._plaintext);\n\t\t}\n\n\t\tif (this._encoded === undefined) {\n\t\t\tthrow(new Error('No plaintext or encoded data available'));\n\t\t}\n\n\n\t\tconst info = this.#computeAndSetKeyInfo(this.encrypted);\n\n\t\tlet principals = this._internalState.principals;\n\t\tif (info.isEncrypted) {\n\t\t\tif (principals === null) {\n\t\t\t\tthrow(new Error('May not decrypt data with a null set of principals'));\n\t\t\t}\n\t\t} else {\n\t\t\tprincipals = [];\n\t\t}\n\n\t\tconst plaintextWrapper = await parseASN1Decrypt(info, principals);\n\t\tconst plaintext = plaintextWrapper.plaintext;\n\n\t\tthis.#data = { ...this.#data, plaintext };\n\n\t\treturn(plaintext);\n\t}\n\n\t/**\n\t * Compute the encoded version of the plaintext data\n\t */\n\tasync #computePlaintextEncoded() {\n\t\tif (this._plaintext === undefined) {\n\t\t\tthrow(new Error('No plaintext data available'));\n\t\t}\n\n\t\tconst structuredData = await buildASN1(\n\t\t\tthis._plaintext\n\t\t);\n\n\t\treturn(structuredData);\n\t}\n\n\t/**\n\t * Populate the encrypted blob from the plaintext and symmetric key\n\t * parameters. If the symmetric key parameters have not been\n\t * initialized they will be initialized at this time.\n\t */\n\tasync #computeEncryptedEncoded() {\n\t\tif (this._plaintext === undefined) {\n\t\t\tthrow(new Error('No encrypted nor plaintext data available'));\n\t\t}\n\n\t\tif (!this.#isEncrypted()) {\n\t\t\tthrow(new Error('internal error: Asked to encrypt a plaintext buffer'));\n\t\t}\n\n\t\t/**\n\t\t * structured data is the ASN.1 encoded structure\n\t\t */\n\t\tconst structuredData = await buildASN1(\n\t\t\tthis._plaintext,\n\t\t\t{\n\t\t\t\tkeys: this._internalState.principals,\n\t\t\t\tcipherKey: crypto.randomBytes(32),\n\t\t\t\tcipherIV: crypto.randomBytes(16),\n\t\t\t\tcipherAlgo: this._internalState.cipherAlgo\n\t\t\t}\n\t\t);\n\n\t\treturn(structuredData);\n\t}\n\n\tasync #computeEncoded() {\n\t\tif (this._encoded !== undefined) {\n\t\t\treturn(this._encoded);\n\t\t}\n\n\t\tlet computed: Buffer;\n\t\tif (!this.encrypted) {\n\t\t\tcomputed = await this.#computePlaintextEncoded();\n\t\t} else {\n\t\t\tcomputed = await this.#computeEncryptedEncoded();\n\t\t}\n\n\t\tthis.#data = { ...this.#data, encoded: computed };\n\n\t\treturn(computed);\n\t}\n\n\t/**\n\t * Grant access to the secret for account(s) synchronously. This\n\t * assumes the plaintext has already been computed and will fail\n\t * if it is not\n\t */\n\tgrantAccessSync(accounts: Account[] | Account): this {\n\t\tif (this._plaintext === undefined) {\n\t\t\tthrow(new Error('Unable to grant access, plaintext not available'));\n\t\t}\n\n\t\tif (!this.#isEncrypted()) {\n\t\t\tthrow(new Error('May not manage access to a plaintext container'));\n\t\t}\n\n\t\tif (!Array.isArray(accounts)) {\n\t\t\taccounts = [accounts];\n\t\t}\n\n\t\t// Encoded data is invalidated with the new permissions so set only the plaintext data\n\t\tthis.setPlaintext(this._plaintext);\n\n\t\tthis._internalState.principals.push(...accounts);\n\n\t\treturn(this);\n\t}\n\n\t/**\n\t * Grant access to the secret for account(s).\n\t */\n\tasync grantAccess(accounts: Account[] | Account): Promise<this> {\n\t\tawait this.#computePlaintext();\n\n\t\tthis.grantAccessSync(accounts);\n\n\t\treturn(this);\n\t}\n\n\t/**\n\t * Revoke access to the secret for an account synchronously. This\n\t * assumes the plaintext has already been computed and will fail\n\t * if it is not\n\t */\n\trevokeAccessSync(account: Account): this {\n\t\tif (this._plaintext === undefined) {\n\t\t\tthrow(new Error('Unable to revoke access, plaintext not available'));\n\t\t}\n\n\t\tif (!this.#isEncrypted()) {\n\t\t\tthrow(new Error('May not manage access to a plaintext container'));\n\t\t}\n\n\t\t// Encoded data is invalidated with the new permissions so set only the plaintext data\n\t\tthis.setPlaintext(this._plaintext);\n\n\t\tthis._internalState.principals = this._internalState.principals.filter(function(checkAccount) {\n\t\t\treturn(!checkAccount.comparePublicKey(account));\n\t\t});\n\n\t\treturn(this);\n\t}\n\n\t/**\n\t * Revoke access to the secret for an account\n\t */\n\tasync revokeAccess(account: Account): Promise<this> {\n\t\tawait this.#computePlaintext();\n\n\t\tthis.revokeAccessSync(account);\n\n\t\treturn(this);\n\t}\n\n\t/**\n\t * Disable access to the plaintext from this instance\n\t */\n\tdisablePlaintext(): this {\n\t\tthis.#mayAccessPlaintext = false;\n\n\t\treturn(this);\n\t}\n\n\t/**\n\t * Get the plaintext for this instance\n\t */\n\tasync getPlaintext(): Promise<ArrayBuffer> {\n\t\tif (!this.#mayAccessPlaintext) {\n\t\t\tthrow(new Error('May not access plaintext'));\n\t\t}\n\n\t\tconst plaintext = await this.#computePlaintext();\n\n\t\tif (plaintext === undefined) {\n\t\t\tthrow(new Error('internal error: Plaintext could not be decoded'));\n\t\t}\n\n\t\t/*\n\t\t * Make a copy of our internal buffer so that any changes made\n\t\t * to either our internal buffer or by our caller do not\n\t\t * interfere\n\t\t */\n\t\treturn(bufferToArrayBuffer(plaintext));\n\t}\n\n\t/**\n\t * Get the serializable buffer which can be stored and reconstructed\n\t */\n\tasync getEncodedBuffer(): Promise<ArrayBuffer> {\n\t\tconst serialized = await this.#computeEncoded();\n\n\t\tif (serialized === undefined) {\n\t\t\tthrow(new Error('internal error: Could not encode data'));\n\t\t}\n\n\t\t/*\n\t\t * Make a copy of our internal buffer so that any changes made\n\t\t * to either our internal buffer or by our caller do not\n\t\t * interfere\n\t\t */\n\t\treturn(bufferToArrayBuffer(serialized));\n\t}\n\n\t/**\n\t * Get the list of accounts which have access to read the plaintext of\n\t * this container\n\t */\n\tget principals(): Account[] {\n\t\tif (!this.#isEncrypted()) {\n\t\t\tthrow(new Error('May not manage access to a plaintext container'));\n\t\t}\n\n\t\treturn(this._internalState.principals);\n\t}\n}\n\n/** @internal */\nexport const _Testing = {\n\tbuildASN1: buildASN1,\n\tparseASN1: parseASN1\n};\n\nexport default EncryptedContainer;\n"]}
|
package/lib/error.d.ts
CHANGED
|
@@ -1,8 +1,64 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Base error class for all Keeta Anchor errors
|
|
3
|
+
*/
|
|
4
|
+
export declare class KeetaAnchorError extends Error {
|
|
5
|
+
#private;
|
|
6
|
+
static readonly name: string;
|
|
7
|
+
private readonly keetaAnchorErrorObjectTypeID;
|
|
8
|
+
private static readonly keetaAnchorErrorObjectTypeID;
|
|
9
|
+
protected userError: boolean;
|
|
10
|
+
get name(): string;
|
|
11
|
+
protected set name(value: string);
|
|
12
|
+
get statusCode(): number;
|
|
13
|
+
protected set statusCode(value: number);
|
|
14
|
+
get retryable(): boolean;
|
|
15
|
+
protected set retryable(value: boolean);
|
|
16
|
+
constructor(message: string);
|
|
17
|
+
/**
|
|
18
|
+
* Protected method to restore error properties from JSON
|
|
19
|
+
* This allows subclasses to properly restore properties without using any
|
|
20
|
+
*/
|
|
21
|
+
protected restoreFromJSON(other: {
|
|
22
|
+
[key: string]: unknown;
|
|
23
|
+
}): void;
|
|
24
|
+
static isInstance(input: unknown): input is KeetaAnchorError;
|
|
25
|
+
asErrorResponse(contentType: 'text/plain' | 'application/json', message?: string): {
|
|
26
|
+
error: string;
|
|
27
|
+
statusCode: number;
|
|
28
|
+
contentType: string;
|
|
29
|
+
};
|
|
30
|
+
toJSON(): {
|
|
31
|
+
ok: false;
|
|
32
|
+
retryable: boolean;
|
|
33
|
+
error: string;
|
|
34
|
+
name: string;
|
|
35
|
+
statusCode: number;
|
|
36
|
+
};
|
|
37
|
+
protected static hasPropWithValue<PROP extends string, VALUE extends string | number | boolean>(input: unknown, prop: PROP, value: VALUE): input is {
|
|
38
|
+
[key in PROP]: VALUE;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Extract common error properties from JSON input
|
|
42
|
+
* This validates the structure and extracts properties needed for construction
|
|
43
|
+
*/
|
|
44
|
+
protected static extractErrorProperties(input: unknown, expectedClass?: {
|
|
45
|
+
name: string;
|
|
46
|
+
}): {
|
|
47
|
+
message: string;
|
|
48
|
+
other: {
|
|
49
|
+
[key: string]: unknown;
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
static fromJSON(input: unknown): Promise<KeetaAnchorError>;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* User-facing error class that extends KeetaAnchorError
|
|
56
|
+
*/
|
|
57
|
+
export declare class KeetaAnchorUserError extends KeetaAnchorError {
|
|
58
|
+
static readonly name: string;
|
|
59
|
+
private readonly keetaAnchorUserErrorObjectTypeID;
|
|
5
60
|
private static readonly keetaAnchorUserErrorObjectTypeID;
|
|
61
|
+
protected userError: boolean;
|
|
6
62
|
static isInstance(input: unknown): input is KeetaAnchorUserError;
|
|
7
63
|
constructor(message: string);
|
|
8
64
|
asErrorResponse(contentType: 'text/plain' | 'application/json'): {
|
|
@@ -10,5 +66,6 @@ export declare class KeetaAnchorUserError extends Error {
|
|
|
10
66
|
statusCode: number;
|
|
11
67
|
contentType: string;
|
|
12
68
|
};
|
|
69
|
+
static fromJSON(input: unknown): Promise<InstanceType<typeof this>>;
|
|
13
70
|
}
|
|
14
71
|
//# sourceMappingURL=error.d.ts.map
|
package/lib/error.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../src/lib/error.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../src/lib/error.ts"],"names":[],"mappings":"AA4CA;;GAEG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;;IAC1C,gBAAyB,IAAI,EAAE,MAAM,CAAsB;IAI3D,OAAO,CAAC,QAAQ,CAAC,4BAA4B,CAAU;IACvD,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,4BAA4B,CAA0C;IAC9F,SAAS,CAAC,SAAS,UAAS;IAE5B,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED,SAAS,KAAK,IAAI,CAAC,KAAK,EAAE,MAAM,EAE/B;IAED,IAAI,UAAU,IAAI,MAAM,CAEvB;IAED,SAAS,KAAK,UAAU,CAAC,KAAK,EAAE,MAAM,EAErC;IAED,IAAI,SAAS,IAAI,OAAO,CAEvB;IAED,SAAS,KAAK,SAAS,CAAC,KAAK,E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B;IACzD,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAA0B;IACtD,OAAO,CAAC,QAAQ,CAAC,gCAAgC,CAAU;IAC3D,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,gCAAgC,CAA0C;IAClG,UAAmB,SAAS,UAAQ;IAEpC,MAAM,CAAC,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,oBAAoB;gBAIpD,OAAO,EAAE,MAAM;IAS3B,eAAe,CAAC,WAAW,EAAE,YAAY,GAAG,kBAAkB,GAAG;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,MAAM,CAAA;KAAE;WAI9G,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,YAAY,CAAC,OAAO,IAAI,CAAC,CAAC;CAMzE"}
|
package/lib/error.js
CHANGED
|
@@ -1,38 +1,217 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Lazy-loaded error classes to avoid circular dependencies
|
|
3
|
+
* The classes are loaded on first use
|
|
4
|
+
*/
|
|
5
|
+
let ERROR_CLASS_MAPPING = null;
|
|
6
|
+
async function getErrorClassMapping() {
|
|
7
|
+
if (ERROR_CLASS_MAPPING) {
|
|
8
|
+
return (ERROR_CLASS_MAPPING);
|
|
9
|
+
}
|
|
10
|
+
// Dynamically import KYC errors to avoid circular dependencies
|
|
11
|
+
const kycModule = await import('../services/kyc/common.js');
|
|
12
|
+
const KYCErrors = kycModule.Errors;
|
|
13
|
+
const ERROR_CLASSES = [
|
|
14
|
+
/*
|
|
15
|
+
* We purposefully leave out KeetaAnchorError here since it
|
|
16
|
+
* is the base error class and could cause circular resolution
|
|
17
|
+
*/
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
19
|
+
KeetaAnchorUserError,
|
|
20
|
+
KYCErrors.VerificationNotFound,
|
|
21
|
+
KYCErrors.CertificateNotFound,
|
|
22
|
+
KYCErrors.PaymentRequired
|
|
23
|
+
];
|
|
24
|
+
const mapping = {};
|
|
25
|
+
for (const errorClass of ERROR_CLASSES) {
|
|
26
|
+
mapping[errorClass.name] = errorClass.fromJSON.bind(errorClass);
|
|
27
|
+
}
|
|
28
|
+
ERROR_CLASS_MAPPING = mapping;
|
|
29
|
+
return (mapping);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Base error class for all Keeta Anchor errors
|
|
33
|
+
*/
|
|
34
|
+
export class KeetaAnchorError extends Error {
|
|
35
|
+
static name = 'KeetaAnchorError';
|
|
36
|
+
#name;
|
|
37
|
+
#statusCode = 400;
|
|
38
|
+
#retryable = false;
|
|
39
|
+
keetaAnchorErrorObjectTypeID;
|
|
40
|
+
static keetaAnchorErrorObjectTypeID = '5d7f1578-e887-4104-bab0-4115ae33b08f';
|
|
41
|
+
userError = false;
|
|
42
|
+
get name() {
|
|
43
|
+
return (this.#name);
|
|
44
|
+
}
|
|
45
|
+
set name(value) {
|
|
46
|
+
this.#name = value;
|
|
47
|
+
}
|
|
48
|
+
get statusCode() {
|
|
49
|
+
return (this.#statusCode);
|
|
50
|
+
}
|
|
51
|
+
set statusCode(value) {
|
|
52
|
+
this.#statusCode = value;
|
|
53
|
+
}
|
|
54
|
+
get retryable() {
|
|
55
|
+
return (this.#retryable);
|
|
56
|
+
}
|
|
57
|
+
set retryable(value) {
|
|
58
|
+
this.#retryable = value;
|
|
59
|
+
}
|
|
60
|
+
constructor(message) {
|
|
61
|
+
super(message);
|
|
62
|
+
// Need to cast to access the static name property from the constructor
|
|
63
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
64
|
+
this.#name = this.constructor.name;
|
|
65
|
+
Object.defineProperty(this, 'keetaAnchorErrorObjectTypeID', {
|
|
66
|
+
value: KeetaAnchorError.keetaAnchorErrorObjectTypeID,
|
|
67
|
+
enumerable: false
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Protected method to restore error properties from JSON
|
|
72
|
+
* This allows subclasses to properly restore properties without using any
|
|
73
|
+
*/
|
|
74
|
+
restoreFromJSON(other) {
|
|
75
|
+
// Restore statusCode if present
|
|
76
|
+
if ('statusCode' in other) {
|
|
77
|
+
if (typeof other.statusCode !== 'number') {
|
|
78
|
+
throw (new TypeError('Invalid statusCode: expected number'));
|
|
79
|
+
}
|
|
80
|
+
this.statusCode = other.statusCode;
|
|
81
|
+
}
|
|
82
|
+
// Restore retryable if present
|
|
83
|
+
if ('retryable' in other) {
|
|
84
|
+
if (typeof other.retryable !== 'boolean') {
|
|
85
|
+
throw (new TypeError('Invalid retryable: expected boolean'));
|
|
86
|
+
}
|
|
87
|
+
this.retryable = other.retryable;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
6
90
|
static isInstance(input) {
|
|
91
|
+
return (this.hasPropWithValue(input, 'keetaAnchorErrorObjectTypeID', KeetaAnchorError.keetaAnchorErrorObjectTypeID));
|
|
92
|
+
}
|
|
93
|
+
asErrorResponse(contentType, message) {
|
|
94
|
+
message ??= 'Internal error';
|
|
95
|
+
if (contentType === 'application/json') {
|
|
96
|
+
message = JSON.stringify({
|
|
97
|
+
...this.toJSON(),
|
|
98
|
+
statusCode: undefined
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
return ({
|
|
102
|
+
error: message,
|
|
103
|
+
statusCode: this.statusCode,
|
|
104
|
+
contentType: contentType
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
toJSON() {
|
|
108
|
+
let message = 'Internal error';
|
|
109
|
+
if (this.userError) {
|
|
110
|
+
message = this.message;
|
|
111
|
+
}
|
|
112
|
+
return ({
|
|
113
|
+
ok: false,
|
|
114
|
+
retryable: this.retryable,
|
|
115
|
+
error: message,
|
|
116
|
+
name: this.#name,
|
|
117
|
+
statusCode: this.statusCode
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
static hasPropWithValue(input, prop, value) {
|
|
7
121
|
if (typeof input !== 'object' || input === null) {
|
|
8
122
|
return (false);
|
|
9
123
|
}
|
|
10
|
-
if (!(
|
|
124
|
+
if (!(prop in input)) {
|
|
11
125
|
return (false);
|
|
12
126
|
}
|
|
13
|
-
|
|
127
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
128
|
+
const inputValue = input[prop];
|
|
129
|
+
if (inputValue !== value) {
|
|
14
130
|
return (false);
|
|
15
131
|
}
|
|
16
132
|
return (true);
|
|
17
133
|
}
|
|
134
|
+
/**
|
|
135
|
+
* Extract common error properties from JSON input
|
|
136
|
+
* This validates the structure and extracts properties needed for construction
|
|
137
|
+
*/
|
|
138
|
+
static extractErrorProperties(input, expectedClass) {
|
|
139
|
+
if (!this.hasPropWithValue(input, 'ok', false)) {
|
|
140
|
+
throw (new Error('Invalid error JSON object'));
|
|
141
|
+
}
|
|
142
|
+
if (typeof input !== 'object' || input === null) {
|
|
143
|
+
throw (new Error('Invalid error JSON object'));
|
|
144
|
+
}
|
|
145
|
+
// Verify the name matches if an expected class is provided
|
|
146
|
+
if (expectedClass && 'name' in input && input.name !== expectedClass.name) {
|
|
147
|
+
throw (new Error(`Error name mismatch: expected ${expectedClass.name}, got ${input.name}`));
|
|
148
|
+
}
|
|
149
|
+
// Extract error message
|
|
150
|
+
let message = 'Internal error';
|
|
151
|
+
if ('error' in input && typeof input.error === 'string') {
|
|
152
|
+
message = input.error;
|
|
153
|
+
}
|
|
154
|
+
// Extract other properties
|
|
155
|
+
const other = {};
|
|
156
|
+
for (const key in input) {
|
|
157
|
+
if (key !== 'error' && key !== 'ok') {
|
|
158
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
159
|
+
other[key] = input[key];
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return ({ message, other });
|
|
163
|
+
}
|
|
164
|
+
static async fromJSON(input) {
|
|
165
|
+
// Try to use the deserializer mapping if available for subclasses
|
|
166
|
+
if (typeof input !== 'object' || input === null) {
|
|
167
|
+
throw (new Error('Invalid error JSON object'));
|
|
168
|
+
}
|
|
169
|
+
if (!('ok' in input) || input.ok !== false) {
|
|
170
|
+
throw (new Error('Invalid error JSON object: expected ok: false'));
|
|
171
|
+
}
|
|
172
|
+
// Check if there's a specific error class name
|
|
173
|
+
if ('name' in input && typeof input.name === 'string') {
|
|
174
|
+
if (input.name === 'KeetaAnchorError') {
|
|
175
|
+
const { message, other } = KeetaAnchorError.extractErrorProperties(input, this);
|
|
176
|
+
const error = new this(message);
|
|
177
|
+
error.restoreFromJSON(other);
|
|
178
|
+
return (error);
|
|
179
|
+
}
|
|
180
|
+
const mapping = await getErrorClassMapping();
|
|
181
|
+
const deserializer = mapping[input.name];
|
|
182
|
+
if (deserializer) {
|
|
183
|
+
return (await deserializer(input));
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
throw (new Error('Invalid error JSON object: unknown error class'));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* User-facing error class that extends KeetaAnchorError
|
|
191
|
+
*/
|
|
192
|
+
export class KeetaAnchorUserError extends KeetaAnchorError {
|
|
193
|
+
static name = 'KeetaAnchorUserError';
|
|
194
|
+
keetaAnchorUserErrorObjectTypeID;
|
|
195
|
+
static keetaAnchorUserErrorObjectTypeID = 'a1e64819-14b6-45ac-a1ec-b9c0bdd51e7b';
|
|
196
|
+
userError = true;
|
|
197
|
+
static isInstance(input) {
|
|
198
|
+
return (this.hasPropWithValue(input, 'keetaAnchorUserErrorObjectTypeID', KeetaAnchorUserError.keetaAnchorUserErrorObjectTypeID));
|
|
199
|
+
}
|
|
18
200
|
constructor(message) {
|
|
19
201
|
super(message);
|
|
20
|
-
this.name = 'KeetaAnchorUserError';
|
|
21
202
|
Object.defineProperty(this, 'keetaAnchorUserErrorObjectTypeID', {
|
|
22
203
|
value: KeetaAnchorUserError.keetaAnchorUserErrorObjectTypeID,
|
|
23
204
|
enumerable: false
|
|
24
205
|
});
|
|
25
206
|
}
|
|
26
207
|
asErrorResponse(contentType) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
contentType: contentType
|
|
35
|
-
});
|
|
208
|
+
return (super.asErrorResponse(contentType, this.message));
|
|
209
|
+
}
|
|
210
|
+
static async fromJSON(input) {
|
|
211
|
+
const { message, other } = this.extractErrorProperties(input, this);
|
|
212
|
+
const error = new this(message);
|
|
213
|
+
error.restoreFromJSON(other);
|
|
214
|
+
return (error);
|
|
36
215
|
}
|
|
37
216
|
}
|
|
38
217
|
//# sourceMappingURL=error.js.map
|
package/lib/error.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error.js","sourceRoot":"","sources":["../../src/lib/error.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,oBAAqB,SAAQ,KAAK;IACrC,IAAI,CAAS;IACZ,UAAU,GAAG,GAAG,CAAC;IACjB,gCAAgC,CAAU;IAC5C,MAAM,CAAU,gCAAgC,GAAG,sCAAsC,CAAC;IAElG,MAAM,CAAC,UAAU,CAAC,KAAc;QAC/B,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;YACjD,OAAM,CAAC,KAAK,CAAC,CAAC;QACf,CAAC;QAED,IAAI,CAAC,CAAC,kCAAkC,IAAI,KAAK,CAAC,EAAE,CAAC;YACpD,OAAM,CAAC,KAAK,CAAC,CAAC;QACf,CAAC;QAED,IAAI,KAAK,CAAC,gCAAgC,KAAK,oBAAoB,CAAC,gCAAgC,EAAE,CAAC;YACtG,OAAM,CAAC,KAAK,CAAC,CAAC;QACf,CAAC;QAED,OAAM,CAAC,IAAI,CAAC,CAAC;IACd,CAAC;IAED,YAAY,OAAe;QAC1B,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;QAEnC,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,kCAAkC,EAAE;YAC/D,KAAK,EAAE,oBAAoB,CAAC,gCAAgC;YAC5D,UAAU,EAAE,KAAK;SACjB,CAAC,CAAC;IACJ,CAAC;IAED,eAAe,CAAC,WAA8C;QAC7D,IAAI,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC;QAC3B,IAAI,WAAW,KAAK,kBAAkB,EAAE,CAAC;YACxC,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC;QAC9D,CAAC;QAED,OAAM,CAAC;YACN,KAAK,EAAE,OAAO;YACd,UAAU,EAAE,IAAI,CAAC,UAAU;YAC3B,WAAW,EAAE,WAAW;SACxB,CAAC,CAAC;IACJ,CAAC","sourcesContent":["export class KeetaAnchorUserError extends Error {\n\treadonly name: string;\n\tprotected statusCode = 400;\n\tprotected keetaAnchorUserErrorObjectTypeID!: string;\n\tprivate static readonly keetaAnchorUserErrorObjectTypeID = 'a1e64819-14b6-45ac-a1ec-b9c0bdd51e7b';\n\n\tstatic isInstance(input: unknown): input is KeetaAnchorUserError {\n\t\tif (typeof input !== 'object' || input === null) {\n\t\t\treturn(false);\n\t\t}\n\n\t\tif (!('keetaAnchorUserErrorObjectTypeID' in input)) {\n\t\t\treturn(false);\n\t\t}\n\n\t\tif (input.keetaAnchorUserErrorObjectTypeID !== KeetaAnchorUserError.keetaAnchorUserErrorObjectTypeID) {\n\t\t\treturn(false);\n\t\t}\n\n\t\treturn(true);\n\t}\n\n\tconstructor(message: string) {\n\t\tsuper(message);\n\t\tthis.name = 'KeetaAnchorUserError';\n\n\t\tObject.defineProperty(this, 'keetaAnchorUserErrorObjectTypeID', {\n\t\t\tvalue: KeetaAnchorUserError.keetaAnchorUserErrorObjectTypeID,\n\t\t\tenumerable: false\n\t\t});\n\t}\n\n\tasErrorResponse(contentType: 'text/plain' | 'application/json'): { error: string; statusCode: number; contentType: string } {\n\t\tlet message = this.message;\n\t\tif (contentType === 'application/json') {\n\t\t\tmessage = JSON.stringify({ ok: false, error: this.message });\n\t\t}\n\n\t\treturn({\n\t\t\terror: message,\n\t\t\tstatusCode: this.statusCode,\n\t\t\tcontentType: contentType\n\t\t});\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"error.js","sourceRoot":"","sources":["../../src/lib/error.ts"],"names":[],"mappings":"AAQA;;;GAGG;AACH,IAAI,mBAAmB,GAA4E,IAAI,CAAC;AAExG,KAAK,UAAU,oBAAoB;IAClC,IAAI,mBAAmB,EAAE,CAAC;QACzB,OAAM,CAAC,mBAAmB,CAAC,CAAC;IAC7B,CAAC;IAED,+DAA+D;IAC/D,MAAM,SAAS,GAAG,MAAM,MAAM,CAAC,2BAA2B,CAAC,CAAC;IAC5D,MAAM,SAAS,GAAG,SAAS,CAAC,MAAM,CAAC;IAEnC,MAAM,aAAa,GAA+B;QACjD;;;WAGG;QACH,mEAAmE;QACnE,oBAAoB;QACpB,SAAS,CAAC,oBAAoB;QAC9B,SAAS,CAAC,mBAAmB;QAC7B,SAAS,CAAC,eAAe;KACzB,CAAC;IAEF,MAAM,OAAO,GAAqE,EAAE,CAAC;IACrF,KAAK,MAAM,UAAU,IAAI,aAAa,EAAE,CAAC;QACxC,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,UAAU,CAAC,QAAQ,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IACjE,CAAC;IAED,mBAAmB,GAAG,OAAO,CAAC;IAC9B,OAAM,CAAC,OAAO,CAAC,CAAC;AACjB,CAAC;AAED;;GAEG;AACH,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IAC1C,MAAM,CAAmB,IAAI,GAAW,kBAAkB,CAAC;IAC3D,KAAK,CAAS;IACd,WAAW,GAAG,GAAG,CAAC;IAClB,UAAU,GAAG,KAAK,CAAC;IACF,4BAA4B,CAAU;IAC/C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string;\n\tfromJSON: (input: unknown) => Promise<KeetaAnchorError>;\n}\n\n/**\n * Lazy-loaded error classes to avoid circular dependencies\n * The classes are loaded on first use\n */\nlet ERROR_CLASS_MAPPING: { [key: string]: (input: unknown) => Promise<KeetaAnchorError> } | null = null;\n\nasync function getErrorClassMapping(): Promise<{ [key: string]: (input: unknown) => Promise<KeetaAnchorError> }> {\n\tif (ERROR_CLASS_MAPPING) {\n\t\treturn(ERROR_CLASS_MAPPING);\n\t}\n\n\t// Dynamically import KYC errors to avoid circular dependencies\n\tconst kycModule = await import('../services/kyc/common.js');\n\tconst KYCErrors = kycModule.Errors;\n\n\tconst ERROR_CLASSES: DeserializableErrorClass[] = [\n\t\t/*\n\t\t * We purposefully leave out KeetaAnchorError here since it\n\t\t * is the base error class and could cause circular resolution\n\t\t */\n\t\t// eslint-disable-next-line @typescript-eslint/no-use-before-define\n\t\tKeetaAnchorUserError,\n\t\tKYCErrors.VerificationNotFound,\n\t\tKYCErrors.CertificateNotFound,\n\t\tKYCErrors.PaymentRequired\n\t];\n\n\tconst mapping: { [key: string]: (input: unknown) => Promise<KeetaAnchorError> } = {};\n\tfor (const errorClass of ERROR_CLASSES) {\n\t\tmapping[errorClass.name] = errorClass.fromJSON.bind(errorClass);\n\t}\n\n\tERROR_CLASS_MAPPING = mapping;\n\treturn(mapping);\n}\n\n/**\n * Base error class for all Keeta Anchor errors\n */\nexport class KeetaAnchorError extends Error {\n\tstatic override readonly name: string = 'KeetaAnchorError';\n\t#name: string;\n\t#statusCode = 400;\n\t#retryable = false;\n\tprivate readonly keetaAnchorErrorObjectTypeID!: string;\n\tprivate static readonly keetaAnchorErrorObjectTypeID = '5d7f1578-e887-4104-bab0-4115ae33b08f';\n\tprotected userError = false;\n\n\tget name(): string {\n\t\treturn(this.#name);\n\t}\n\n\tprotected set name(value: string) {\n\t\tthis.#name = value;\n\t}\n\n\tget statusCode(): number {\n\t\treturn(this.#statusCode);\n\t}\n\n\tprotected set statusCode(value: number) {\n\t\tthis.#statusCode = value;\n\t}\n\n\tget retryable(): boolean {\n\t\treturn(this.#retryable);\n\t}\n\n\tprotected set retryable(value: boolean) {\n\t\tthis.#retryable = value;\n\t}\n\n\tconstructor(message: string) {\n\t\tsuper(message);\n\n\t\t// Need to cast to access the static name property from the constructor\n\t\t// eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n\t\tthis.#name = (this.constructor as typeof KeetaAnchorError).name;\n\n\t\tObject.defineProperty(this, 'keetaAnchorErrorObjectTypeID', {\n\t\t\tvalue: KeetaAnchorError.keetaAnchorErrorObjectTypeID,\n\t\t\tenumerable: false\n\t\t});\n\t}\n\n\t/**\n\t * Protected method to restore error properties from JSON\n\t * This allows subclasses to properly restore properties without using any\n\t */\n\tprotected restoreFromJSON(other: { [key: string]: unknown }): void {\n\t\t// Restore statusCode if present\n\t\tif ('statusCode' in other) {\n\t\t\tif (typeof other.statusCode !== 'number') {\n\t\t\t\tthrow(new TypeError('Invalid statusCode: expected number'));\n\t\t\t}\n\t\t\tthis.statusCode = other.statusCode;\n\t\t}\n\n\t\t// Restore retryable if present\n\t\tif ('retryable' in other) {\n\t\t\tif (typeof other.retryable !== 'boolean') {\n\t\t\t\tthrow(new TypeError('Invalid retryable: expected boolean'));\n\t\t\t}\n\t\t\tthis.retryable = other.retryable;\n\t\t}\n\t}\n\n\tstatic isInstance(input: unknown): input is KeetaAnchorError {\n\t\treturn(this.hasPropWithValue(input, 'keetaAnchorErrorObjectTypeID', KeetaAnchorError.keetaAnchorErrorObjectTypeID));\n\t}\n\n\tasErrorResponse(contentType: 'text/plain' | 'application/json', message?: string): { error: string; statusCode: number; contentType: string } {\n\t\tmessage ??= 'Internal error';\n\t\tif (contentType === 'application/json') {\n\t\t\tmessage = JSON.stringify({\n\t\t\t\t...this.toJSON(),\n\t\t\t\tstatusCode: undefined\n\t\t\t});\n\t\t}\n\n\t\treturn({\n\t\t\terror: message,\n\t\t\tstatusCode: this.statusCode,\n\t\t\tcontentType: contentType\n\t\t});\n\t}\n\n\ttoJSON(): { ok: false; retryable: boolean; error: string; name: string; statusCode: number } {\n\t\tlet message = 'Internal error';\n\t\tif (this.userError) {\n\t\t\tmessage = this.message;\n\t\t}\n\n\t\treturn({\n\t\t\tok: false,\n\t\t\tretryable: this.retryable,\n\t\t\terror: message,\n\t\t\tname: this.#name,\n\t\t\tstatusCode: this.statusCode\n\t\t});\n\t}\n\n\tprotected static hasPropWithValue<PROP extends string, VALUE extends string | number | boolean>(input: unknown, prop: PROP, value: VALUE): input is { [key in PROP]: VALUE } {\n\t\tif (typeof input !== 'object' || input === null) {\n\t\t\treturn(false);\n\t\t}\n\n\t\tif (!(prop in input)) {\n\t\t\treturn(false);\n\t\t}\n\n\t\t// eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n\t\tconst inputValue = input[prop as keyof typeof input] as unknown;\n\t\tif (inputValue !== value) {\n\t\t\treturn(false);\n\t\t}\n\n\t\treturn(true);\n\t}\n\n\t/**\n\t * Extract common error properties from JSON input\n\t * This validates the structure and extracts properties needed for construction\n\t */\n\tprotected static extractErrorProperties(input: unknown, expectedClass?: { name: string }): { message: string; other: { [key: string]: unknown }} {\n\t\tif (!this.hasPropWithValue(input, 'ok', false)) {\n\t\t\tthrow(new Error('Invalid error JSON object'));\n\t\t}\n\n\t\tif (typeof input !== 'object' || input === null) {\n\t\t\tthrow(new Error('Invalid error JSON object'));\n\t\t}\n\n\t\t// Verify the name matches if an expected class is provided\n\t\tif (expectedClass && 'name' in input && input.name !== expectedClass.name) {\n\t\t\tthrow(new Error(`Error name mismatch: expected ${expectedClass.name}, got ${input.name}`));\n\t\t}\n\n\t\t// Extract error message\n\t\tlet message = 'Internal error';\n\t\tif ('error' in input && typeof input.error === 'string') {\n\t\t\tmessage = input.error;\n\t\t}\n\n\t\t// Extract other properties\n\t\tconst other: { [key: string]: unknown } = {};\n\t\tfor (const key in input) {\n\t\t\tif (key !== 'error' && key !== 'ok') {\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n\t\t\t\tother[key] = input[key as keyof typeof input];\n\t\t\t}\n\t\t}\n\n\t\treturn({ message, other });\n\t}\n\n\tstatic async fromJSON(input: unknown): Promise<KeetaAnchorError> {\n\t\t// Try to use the deserializer mapping if available for subclasses\n\t\tif (typeof input !== 'object' || input === null) {\n\t\t\tthrow(new Error('Invalid error JSON object'));\n\t\t}\n\n\t\tif (!('ok' in input) || input.ok !== false) {\n\t\t\tthrow(new Error('Invalid error JSON object: expected ok: false'));\n\t\t}\n\n\t\t// Check if there's a specific error class name\n\t\tif ('name' in input && typeof input.name === 'string') {\n\t\t\tif (input.name === 'KeetaAnchorError') {\n\t\t\t\tconst { message, other } = KeetaAnchorError.extractErrorProperties(input, this);\n\t\t\t\tconst error = new this(message);\n\t\t\t\terror.restoreFromJSON(other);\n\t\t\t\treturn(error);\n\t\t\t}\n\t\t\tconst mapping = await getErrorClassMapping();\n\t\t\tconst deserializer = mapping[input.name];\n\t\t\tif (deserializer) {\n\t\t\t\treturn(await deserializer(input));\n\t\t\t}\n\t\t}\n\n\t\tthrow(new Error('Invalid error JSON object: unknown error class'));\n\t}\n}\n\n/**\n * User-facing error class that extends KeetaAnchorError\n */\nexport class KeetaAnchorUserError extends KeetaAnchorError {\n\tstatic readonly name: string = 'KeetaAnchorUserError';\n\tprivate readonly keetaAnchorUserErrorObjectTypeID!: string;\n\tprivate static readonly keetaAnchorUserErrorObjectTypeID = 'a1e64819-14b6-45ac-a1ec-b9c0bdd51e7b';\n\tprotected override userError = true;\n\n\tstatic isInstance(input: unknown): input is KeetaAnchorUserError {\n\t\treturn(this.hasPropWithValue(input, 'keetaAnchorUserErrorObjectTypeID', KeetaAnchorUserError.keetaAnchorUserErrorObjectTypeID));\n\t}\n\n\tconstructor(message: string) {\n\t\tsuper(message);\n\n\t\tObject.defineProperty(this, 'keetaAnchorUserErrorObjectTypeID', {\n\t\t\tvalue: KeetaAnchorUserError.keetaAnchorUserErrorObjectTypeID,\n\t\t\tenumerable: false\n\t\t});\n\t}\n\n\tasErrorResponse(contentType: 'text/plain' | 'application/json'): { error: string; statusCode: number; contentType: string } {\n\t\treturn(super.asErrorResponse(contentType, this.message));\n\t}\n\n\tstatic async fromJSON(input: unknown): Promise<InstanceType<typeof this>> {\n\t\tconst { message, other } = this.extractErrorProperties(input, this);\n\t\tconst error = new this(message);\n\t\terror.restoreFromJSON(other);\n\t\treturn(error);\n\t}\n}\n"]}
|
package/lib/resolver.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type { GenericAccount as KeetaNetGenericAccount } from '@keetanetwork/kee
|
|
|
3
3
|
import * as CurrencyInfo from '@keetanetwork/currency-info';
|
|
4
4
|
import type { Logger } from './log/index.ts';
|
|
5
5
|
import type { JSONSerializable } from './utils/json.ts';
|
|
6
|
+
import type { DeepPartial } from './utils/types.ts';
|
|
6
7
|
import { type MovableAssetSearchInput, type AssetLocationString, type AssetWithRailsMetadata, type Rail, type SupportedAssets } from '../services/asset-movement/common.js';
|
|
7
8
|
type ExternalURL = {
|
|
8
9
|
external: '2b828e33-2692-46e9-817e-9b93d63f28fd';
|
|
@@ -313,11 +314,18 @@ type URLCacheObject = Map<string, {
|
|
|
313
314
|
}>;
|
|
314
315
|
type ResolverConfig = {
|
|
315
316
|
/**
|
|
316
|
-
* The "root" account to use as the basis for all lookups. It should
|
|
317
|
+
* The "root" account(s) to use as the basis for all lookups. It should
|
|
317
318
|
* contain the authoritative information for resolving in its
|
|
318
319
|
* Metadata.
|
|
320
|
+
*
|
|
321
|
+
* Can be either:
|
|
322
|
+
* - A single KeetaNetGenericAccount
|
|
323
|
+
* - An array of KeetaNetGenericAccount in priority order (highest priority first)
|
|
324
|
+
*
|
|
325
|
+
* When an array is provided, the resolver will merge results from all roots,
|
|
326
|
+
* with entries from higher priority roots taking precedence over lower priority ones.
|
|
319
327
|
*/
|
|
320
|
-
root: KeetaNetGenericAccount;
|
|
328
|
+
root: KeetaNetGenericAccount | KeetaNetGenericAccount[];
|
|
321
329
|
/**
|
|
322
330
|
* A KeetaNet Client to access the network using.
|
|
323
331
|
*/
|
|
@@ -436,7 +444,7 @@ declare class Resolver {
|
|
|
436
444
|
private lookupFXServices;
|
|
437
445
|
filterSupportedAssets(assetService: ValuizableObject, criteria?: ServiceSearchCriteria<'assetMovement'>): Promise<SupportedAssets[]>;
|
|
438
446
|
private lookupAssetMovementServices;
|
|
439
|
-
getRootMetadata(): Promise<
|
|
447
|
+
getRootMetadata(): Promise<ToValuizableObject<Pick<ServiceMetadata, 'version'> & DeepPartial<Omit<ServiceMetadata, 'version'>>>>;
|
|
440
448
|
listTransferableAssets(): Promise<KeetaNetAccountTokenPublicKeyString[]>;
|
|
441
449
|
listTokens(): Promise<{
|
|
442
450
|
token: KeetaNetAccountTokenPublicKeyString;
|