@haneullabs/signers 0.1.0

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.
Files changed (88) hide show
  1. package/CHANGELOG.md +555 -0
  2. package/README.md +200 -0
  3. package/aws/package.json +6 -0
  4. package/dist/cjs/aws/aws-client.d.ts +43 -0
  5. package/dist/cjs/aws/aws-client.js +79 -0
  6. package/dist/cjs/aws/aws-client.js.map +7 -0
  7. package/dist/cjs/aws/aws-kms-signer.d.ts +61 -0
  8. package/dist/cjs/aws/aws-kms-signer.js +114 -0
  9. package/dist/cjs/aws/aws-kms-signer.js.map +7 -0
  10. package/dist/cjs/aws/aws4fetch.d.ts +125 -0
  11. package/dist/cjs/aws/aws4fetch.js +382 -0
  12. package/dist/cjs/aws/aws4fetch.js.map +7 -0
  13. package/dist/cjs/aws/index.d.ts +5 -0
  14. package/dist/cjs/aws/index.js +25 -0
  15. package/dist/cjs/aws/index.js.map +7 -0
  16. package/dist/cjs/gcp/gcp-kms-client.d.ts +68 -0
  17. package/dist/cjs/gcp/gcp-kms-client.js +147 -0
  18. package/dist/cjs/gcp/gcp-kms-client.js.map +7 -0
  19. package/dist/cjs/gcp/index.d.ts +4 -0
  20. package/dist/cjs/gcp/index.js +25 -0
  21. package/dist/cjs/gcp/index.js.map +7 -0
  22. package/dist/cjs/ledger/bcs.d.ts +14 -0
  23. package/dist/cjs/ledger/bcs.js +85 -0
  24. package/dist/cjs/ledger/bcs.js.map +7 -0
  25. package/dist/cjs/ledger/index.d.ts +66 -0
  26. package/dist/cjs/ledger/index.js +158 -0
  27. package/dist/cjs/ledger/index.js.map +7 -0
  28. package/dist/cjs/ledger/objects.d.ts +5 -0
  29. package/dist/cjs/ledger/objects.js +60 -0
  30. package/dist/cjs/ledger/objects.js.map +7 -0
  31. package/dist/cjs/package.json +5 -0
  32. package/dist/cjs/utils/utils.d.ts +18 -0
  33. package/dist/cjs/utils/utils.js +85 -0
  34. package/dist/cjs/utils/utils.js.map +7 -0
  35. package/dist/cjs/webcrypto/index.d.ts +26 -0
  36. package/dist/cjs/webcrypto/index.js +112 -0
  37. package/dist/cjs/webcrypto/index.js.map +7 -0
  38. package/dist/esm/aws/aws-client.d.ts +43 -0
  39. package/dist/esm/aws/aws-client.js +59 -0
  40. package/dist/esm/aws/aws-client.js.map +7 -0
  41. package/dist/esm/aws/aws-kms-signer.d.ts +61 -0
  42. package/dist/esm/aws/aws-kms-signer.js +94 -0
  43. package/dist/esm/aws/aws-kms-signer.js.map +7 -0
  44. package/dist/esm/aws/aws4fetch.d.ts +125 -0
  45. package/dist/esm/aws/aws4fetch.js +362 -0
  46. package/dist/esm/aws/aws4fetch.js.map +7 -0
  47. package/dist/esm/aws/index.d.ts +5 -0
  48. package/dist/esm/aws/index.js +5 -0
  49. package/dist/esm/aws/index.js.map +7 -0
  50. package/dist/esm/gcp/gcp-kms-client.d.ts +68 -0
  51. package/dist/esm/gcp/gcp-kms-client.js +127 -0
  52. package/dist/esm/gcp/gcp-kms-client.js.map +7 -0
  53. package/dist/esm/gcp/index.d.ts +4 -0
  54. package/dist/esm/gcp/index.js +5 -0
  55. package/dist/esm/gcp/index.js.map +7 -0
  56. package/dist/esm/ledger/bcs.d.ts +14 -0
  57. package/dist/esm/ledger/bcs.js +70 -0
  58. package/dist/esm/ledger/bcs.js.map +7 -0
  59. package/dist/esm/ledger/index.d.ts +66 -0
  60. package/dist/esm/ledger/index.js +138 -0
  61. package/dist/esm/ledger/index.js.map +7 -0
  62. package/dist/esm/ledger/objects.d.ts +5 -0
  63. package/dist/esm/ledger/objects.js +40 -0
  64. package/dist/esm/ledger/objects.js.map +7 -0
  65. package/dist/esm/package.json +5 -0
  66. package/dist/esm/utils/utils.d.ts +18 -0
  67. package/dist/esm/utils/utils.js +65 -0
  68. package/dist/esm/utils/utils.js.map +7 -0
  69. package/dist/esm/webcrypto/index.d.ts +26 -0
  70. package/dist/esm/webcrypto/index.js +92 -0
  71. package/dist/esm/webcrypto/index.js.map +7 -0
  72. package/dist/tsconfig.esm.tsbuildinfo +1 -0
  73. package/dist/tsconfig.tsbuildinfo +1 -0
  74. package/gcp/package.json +6 -0
  75. package/ledger/package.json +6 -0
  76. package/package.json +76 -0
  77. package/src/aws/aws-client.ts +107 -0
  78. package/src/aws/aws-kms-signer.ts +111 -0
  79. package/src/aws/aws4fetch.ts +502 -0
  80. package/src/aws/index.ts +9 -0
  81. package/src/gcp/gcp-kms-client.ts +165 -0
  82. package/src/gcp/index.ts +9 -0
  83. package/src/ledger/bcs.ts +87 -0
  84. package/src/ledger/index.ts +164 -0
  85. package/src/ledger/objects.ts +56 -0
  86. package/src/utils/utils.ts +119 -0
  87. package/src/webcrypto/index.ts +108 -0
  88. package/webcrypto/package.json +6 -0
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/ledger/objects.ts"],
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { Transaction } from '@haneullabs/haneul/transactions';\nimport type { HaneulClient } from '@haneullabs/haneul/client';\nimport { HaneulMoveObject } from './bcs.js';\n\nexport const getInputObjects = async (transaction: Transaction, client: HaneulClient) => {\n\tconst data = transaction.getData();\n\n\tconst gasObjectIds = data.gasData.payment?.map((object) => object.objectId) ?? [];\n\tconst inputObjectIds = data.inputs\n\t\t.map((input) => {\n\t\t\treturn input.$kind === 'Object' && input.Object.$kind === 'ImmOrOwnedObject'\n\t\t\t\t? input.Object.ImmOrOwnedObject.objectId\n\t\t\t\t: null;\n\t\t})\n\t\t.filter((objectId): objectId is string => !!objectId);\n\n\tconst objects = await client.multiGetObjects({\n\t\tids: [...gasObjectIds, ...inputObjectIds],\n\t\toptions: {\n\t\t\tshowBcs: true,\n\t\t\tshowPreviousTransaction: true,\n\t\t\tshowStorageRebate: true,\n\t\t\tshowOwner: true,\n\t\t},\n\t});\n\n\t// NOTE: We should probably get rid of this manual serialization logic in favor of using the\n\t// already serialized object bytes from the GraphQL API once there is more mainstream support\n\t// for it + we can enforce the transport type on the Haneul client.\n\tconst bcsObjects = objects\n\t\t.map((object) => {\n\t\t\tif (object.error || !object.data || object.data.bcs?.dataType !== 'moveObject') {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\treturn HaneulMoveObject.serialize({\n\t\t\t\tdata: {\n\t\t\t\t\tMoveObject: {\n\t\t\t\t\t\ttype: object.data.bcs.type,\n\t\t\t\t\t\thasPublicTransfer: object.data.bcs.hasPublicTransfer,\n\t\t\t\t\t\tversion: object.data.bcs.version,\n\t\t\t\t\t\tcontents: object.data.bcs.bcsBytes,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\towner: object.data.owner!,\n\t\t\t\tpreviousTransaction: object.data.previousTransaction!,\n\t\t\t\tstorageRebate: object.data.storageRebate!,\n\t\t\t}).toBytes();\n\t\t})\n\t\t.filter((bcsBytes): bcsBytes is Uint8Array<ArrayBuffer> => !!bcsBytes);\n\n\treturn { bcsObjects };\n};\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAKA,iBAAiC;AAE1B,MAAM,kBAAkB,OAAO,aAA0B,WAAyB;AACxF,QAAM,OAAO,YAAY,QAAQ;AAEjC,QAAM,eAAe,KAAK,QAAQ,SAAS,IAAI,CAAC,WAAW,OAAO,QAAQ,KAAK,CAAC;AAChF,QAAM,iBAAiB,KAAK,OAC1B,IAAI,CAAC,UAAU;AACf,WAAO,MAAM,UAAU,YAAY,MAAM,OAAO,UAAU,qBACvD,MAAM,OAAO,iBAAiB,WAC9B;AAAA,EACJ,CAAC,EACA,OAAO,CAAC,aAAiC,CAAC,CAAC,QAAQ;AAErD,QAAM,UAAU,MAAM,OAAO,gBAAgB;AAAA,IAC5C,KAAK,CAAC,GAAG,cAAc,GAAG,cAAc;AAAA,IACxC,SAAS;AAAA,MACR,SAAS;AAAA,MACT,yBAAyB;AAAA,MACzB,mBAAmB;AAAA,MACnB,WAAW;AAAA,IACZ;AAAA,EACD,CAAC;AAKD,QAAM,aAAa,QACjB,IAAI,CAAC,WAAW;AAChB,QAAI,OAAO,SAAS,CAAC,OAAO,QAAQ,OAAO,KAAK,KAAK,aAAa,cAAc;AAC/E,aAAO;AAAA,IACR;AAEA,WAAO,4BAAiB,UAAU;AAAA,MACjC,MAAM;AAAA,QACL,YAAY;AAAA,UACX,MAAM,OAAO,KAAK,IAAI;AAAA,UACtB,mBAAmB,OAAO,KAAK,IAAI;AAAA,UACnC,SAAS,OAAO,KAAK,IAAI;AAAA,UACzB,UAAU,OAAO,KAAK,IAAI;AAAA,QAC3B;AAAA,MACD;AAAA,MACA,OAAO,OAAO,KAAK;AAAA,MACnB,qBAAqB,OAAO,KAAK;AAAA,MACjC,eAAe,OAAO,KAAK;AAAA,IAC5B,CAAC,EAAE,QAAQ;AAAA,EACZ,CAAC,EACA,OAAO,CAAC,aAAkD,CAAC,CAAC,QAAQ;AAEtE,SAAO,EAAE,WAAW;AACrB;",
6
+ "names": []
7
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "private": true,
3
+ "type": "commonjs",
4
+ "sideEffects": false
5
+ }
@@ -0,0 +1,18 @@
1
+ /** The total number of bits in the DER bit string for the uncompressed public key. */
2
+ export declare const DER_BIT_STRING_LENGTH = 520;
3
+ /** The total number of bytes corresponding to the DER bit string length. */
4
+ export declare const DER_BYTES_LENGTH: number;
5
+ export declare function publicKeyFromDER(derBytes: Uint8Array): Uint8Array<ArrayBufferLike>;
6
+ export declare function getConcatenatedSignature(signature: Uint8Array, keyScheme: string): Uint8Array<ArrayBuffer>;
7
+ /**
8
+ * Compresses an uncompressed public key into its compressed form.
9
+ *
10
+ * The uncompressed key must follow the DER bit string format as specified in [RFC 5480](https://datatracker.ietf.org/doc/html/rfc5480#section-2.2)
11
+ * and [SEC 1: Elliptic Curve Cryptography](https://www.secg.org/sec1-v2.pdf).
12
+ *
13
+ * @param uncompressedKey - A `Uint8ClampedArray` representing the uncompressed public key bits.
14
+ * @returns A `Uint8Array` containing the compressed public key.
15
+ *
16
+ * @throws {Error} If the uncompressed key has an unexpected length or does not start with the expected prefix.
17
+ */
18
+ export declare function compressPublicKeyClamped(uncompressedKey: Uint8ClampedArray): Uint8Array;
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var utils_exports = {};
20
+ __export(utils_exports, {
21
+ DER_BIT_STRING_LENGTH: () => DER_BIT_STRING_LENGTH,
22
+ DER_BYTES_LENGTH: () => DER_BYTES_LENGTH,
23
+ compressPublicKeyClamped: () => compressPublicKeyClamped,
24
+ getConcatenatedSignature: () => getConcatenatedSignature,
25
+ publicKeyFromDER: () => publicKeyFromDER
26
+ });
27
+ module.exports = __toCommonJS(utils_exports);
28
+ var import_p256 = require("@noble/curves/p256");
29
+ var import_secp256k1 = require("@noble/curves/secp256k1");
30
+ var import_asn1_ts = require("asn1-ts");
31
+ const DER_BIT_STRING_LENGTH = 520;
32
+ const DER_BYTES_LENGTH = DER_BIT_STRING_LENGTH / 8;
33
+ function bitsToBytes(bitsArray) {
34
+ const bytes = new Uint8Array(DER_BYTES_LENGTH);
35
+ for (let i = 0; i < DER_BIT_STRING_LENGTH; i++) {
36
+ if (bitsArray[i] === 1) {
37
+ bytes[Math.floor(i / 8)] |= 1 << 7 - i % 8;
38
+ }
39
+ }
40
+ return bytes;
41
+ }
42
+ function publicKeyFromDER(derBytes) {
43
+ const encodedData = derBytes;
44
+ const derElement = new import_asn1_ts.DERElement();
45
+ derElement.fromBytes(encodedData);
46
+ if (!(derElement.tagClass === import_asn1_ts.ASN1TagClass.universal && derElement.construction === import_asn1_ts.ASN1Construction.constructed)) {
47
+ throw new Error("Unexpected ASN.1 structure");
48
+ }
49
+ const components = derElement.components;
50
+ const publicKeyElement = components[1];
51
+ if (!publicKeyElement) {
52
+ throw new Error("Public Key not found in the DER structure");
53
+ }
54
+ return compressPublicKeyClamped(publicKeyElement.bitString);
55
+ }
56
+ function getConcatenatedSignature(signature, keyScheme) {
57
+ if (!signature || signature.length === 0) {
58
+ throw new Error("Invalid signature");
59
+ }
60
+ const derElement = new import_asn1_ts.DERElement();
61
+ derElement.fromBytes(signature);
62
+ const [r, s] = derElement.toJSON();
63
+ switch (keyScheme) {
64
+ case "Secp256k1":
65
+ return new import_secp256k1.secp256k1.Signature(BigInt(r), BigInt(s)).normalizeS().toCompactRawBytes();
66
+ case "Secp256r1":
67
+ return new import_p256.secp256r1.Signature(BigInt(r), BigInt(s)).normalizeS().toCompactRawBytes();
68
+ default:
69
+ throw new Error("Unsupported key scheme");
70
+ }
71
+ }
72
+ function compressPublicKeyClamped(uncompressedKey) {
73
+ if (uncompressedKey.length !== DER_BIT_STRING_LENGTH) {
74
+ throw new Error("Unexpected length for an uncompressed public key");
75
+ }
76
+ const uncompressedBytes = bitsToBytes(uncompressedKey);
77
+ if (uncompressedBytes[0] !== 4) {
78
+ throw new Error("Public key does not start with 0x04");
79
+ }
80
+ const xCoord = uncompressedBytes.slice(1, 33);
81
+ const yCoordLastByte = uncompressedBytes[64];
82
+ const parityByte = yCoordLastByte % 2 === 0 ? 2 : 3;
83
+ return new Uint8Array([parityByte, ...xCoord]);
84
+ }
85
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/utils/utils.ts"],
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { secp256r1 } from '@noble/curves/p256';\nimport { secp256k1 } from '@noble/curves/secp256k1';\nimport { ASN1Construction, ASN1TagClass, DERElement } from 'asn1-ts';\n\n/** The total number of bits in the DER bit string for the uncompressed public key. */\nexport const DER_BIT_STRING_LENGTH = 520;\n\n/** The total number of bytes corresponding to the DER bit string length. */\nexport const DER_BYTES_LENGTH = DER_BIT_STRING_LENGTH / 8;\n\n// Reference Specifications:\n// https://datatracker.ietf.org/doc/html/rfc5480#section-2.2\n// https://www.secg.org/sec1-v2.pdf\n\n/**\n * Converts an array of bits into a byte array.\n *\n * @param bitsArray - A `Uint8ClampedArray` representing the bits to convert.\n * @returns A `Uint8Array` containing the corresponding bytes.\n *\n * @throws {Error} If the input array does not have the expected length.\n */\nfunction bitsToBytes(bitsArray: Uint8ClampedArray): Uint8Array {\n\tconst bytes = new Uint8Array(DER_BYTES_LENGTH);\n\tfor (let i = 0; i < DER_BIT_STRING_LENGTH; i++) {\n\t\tif (bitsArray[i] === 1) {\n\t\t\tbytes[Math.floor(i / 8)] |= 1 << (7 - (i % 8));\n\t\t}\n\t}\n\treturn bytes;\n}\n\nexport function publicKeyFromDER(derBytes: Uint8Array) {\n\tconst encodedData: Uint8Array = derBytes;\n\tconst derElement = new DERElement();\n\tderElement.fromBytes(encodedData);\n\n\t// Validate the ASN.1 structure of the public key\n\tif (\n\t\t!(\n\t\t\tderElement.tagClass === ASN1TagClass.universal &&\n\t\t\tderElement.construction === ASN1Construction.constructed\n\t\t)\n\t) {\n\t\tthrow new Error('Unexpected ASN.1 structure');\n\t}\n\n\tconst components = derElement.components;\n\tconst publicKeyElement = components[1];\n\n\tif (!publicKeyElement) {\n\t\tthrow new Error('Public Key not found in the DER structure');\n\t}\n\n\treturn compressPublicKeyClamped(publicKeyElement.bitString);\n}\n\nexport function getConcatenatedSignature(signature: Uint8Array, keyScheme: string) {\n\tif (!signature || signature.length === 0) {\n\t\tthrow new Error('Invalid signature');\n\t}\n\n\t// Initialize a DERElement to parse the DER-encoded signature\n\tconst derElement = new DERElement();\n\tderElement.fromBytes(signature);\n\n\tconst [r, s] = derElement.toJSON() as [string, string];\n\n\tswitch (keyScheme) {\n\t\tcase 'Secp256k1':\n\t\t\treturn new secp256k1.Signature(BigInt(r), BigInt(s))\n\t\t\t\t.normalizeS()\n\t\t\t\t.toCompactRawBytes() as Uint8Array<ArrayBuffer>;\n\t\tcase 'Secp256r1':\n\t\t\treturn new secp256r1.Signature(BigInt(r), BigInt(s))\n\t\t\t\t.normalizeS()\n\t\t\t\t.toCompactRawBytes() as Uint8Array<ArrayBuffer>;\n\t\tdefault:\n\t\t\tthrow new Error('Unsupported key scheme');\n\t}\n}\n\n/**\n * Compresses an uncompressed public key into its compressed form.\n *\n * The uncompressed key must follow the DER bit string format as specified in [RFC 5480](https://datatracker.ietf.org/doc/html/rfc5480#section-2.2)\n * and [SEC 1: Elliptic Curve Cryptography](https://www.secg.org/sec1-v2.pdf).\n *\n * @param uncompressedKey - A `Uint8ClampedArray` representing the uncompressed public key bits.\n * @returns A `Uint8Array` containing the compressed public key.\n *\n * @throws {Error} If the uncompressed key has an unexpected length or does not start with the expected prefix.\n */\nexport function compressPublicKeyClamped(uncompressedKey: Uint8ClampedArray): Uint8Array {\n\tif (uncompressedKey.length !== DER_BIT_STRING_LENGTH) {\n\t\tthrow new Error('Unexpected length for an uncompressed public key');\n\t}\n\n\t// Convert bits to bytes\n\tconst uncompressedBytes = bitsToBytes(uncompressedKey);\n\n\t// Ensure the public key starts with the standard uncompressed prefix 0x04\n\tif (uncompressedBytes[0] !== 0x04) {\n\t\tthrow new Error('Public key does not start with 0x04');\n\t}\n\n\t// Extract X-Coordinate (skip the first byte, which is the prefix 0x04)\n\tconst xCoord = uncompressedBytes.slice(1, 33);\n\n\t// Determine parity byte for Y coordinate based on the last byte\n\tconst yCoordLastByte = uncompressedBytes[64];\n\tconst parityByte = yCoordLastByte % 2 === 0 ? 0x02 : 0x03;\n\n\t// Return the compressed public key consisting of the parity byte and X-coordinate\n\treturn new Uint8Array([parityByte, ...xCoord]);\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAA0B;AAC1B,uBAA0B;AAC1B,qBAA2D;AAGpD,MAAM,wBAAwB;AAG9B,MAAM,mBAAmB,wBAAwB;AAcxD,SAAS,YAAY,WAA0C;AAC9D,QAAM,QAAQ,IAAI,WAAW,gBAAgB;AAC7C,WAAS,IAAI,GAAG,IAAI,uBAAuB,KAAK;AAC/C,QAAI,UAAU,CAAC,MAAM,GAAG;AACvB,YAAM,KAAK,MAAM,IAAI,CAAC,CAAC,KAAK,KAAM,IAAK,IAAI;AAAA,IAC5C;AAAA,EACD;AACA,SAAO;AACR;AAEO,SAAS,iBAAiB,UAAsB;AACtD,QAAM,cAA0B;AAChC,QAAM,aAAa,IAAI,0BAAW;AAClC,aAAW,UAAU,WAAW;AAGhC,MACC,EACC,WAAW,aAAa,4BAAa,aACrC,WAAW,iBAAiB,gCAAiB,cAE7C;AACD,UAAM,IAAI,MAAM,4BAA4B;AAAA,EAC7C;AAEA,QAAM,aAAa,WAAW;AAC9B,QAAM,mBAAmB,WAAW,CAAC;AAErC,MAAI,CAAC,kBAAkB;AACtB,UAAM,IAAI,MAAM,2CAA2C;AAAA,EAC5D;AAEA,SAAO,yBAAyB,iBAAiB,SAAS;AAC3D;AAEO,SAAS,yBAAyB,WAAuB,WAAmB;AAClF,MAAI,CAAC,aAAa,UAAU,WAAW,GAAG;AACzC,UAAM,IAAI,MAAM,mBAAmB;AAAA,EACpC;AAGA,QAAM,aAAa,IAAI,0BAAW;AAClC,aAAW,UAAU,SAAS;AAE9B,QAAM,CAAC,GAAG,CAAC,IAAI,WAAW,OAAO;AAEjC,UAAQ,WAAW;AAAA,IAClB,KAAK;AACJ,aAAO,IAAI,2BAAU,UAAU,OAAO,CAAC,GAAG,OAAO,CAAC,CAAC,EACjD,WAAW,EACX,kBAAkB;AAAA,IACrB,KAAK;AACJ,aAAO,IAAI,sBAAU,UAAU,OAAO,CAAC,GAAG,OAAO,CAAC,CAAC,EACjD,WAAW,EACX,kBAAkB;AAAA,IACrB;AACC,YAAM,IAAI,MAAM,wBAAwB;AAAA,EAC1C;AACD;AAaO,SAAS,yBAAyB,iBAAgD;AACxF,MAAI,gBAAgB,WAAW,uBAAuB;AACrD,UAAM,IAAI,MAAM,kDAAkD;AAAA,EACnE;AAGA,QAAM,oBAAoB,YAAY,eAAe;AAGrD,MAAI,kBAAkB,CAAC,MAAM,GAAM;AAClC,UAAM,IAAI,MAAM,qCAAqC;AAAA,EACtD;AAGA,QAAM,SAAS,kBAAkB,MAAM,GAAG,EAAE;AAG5C,QAAM,iBAAiB,kBAAkB,EAAE;AAC3C,QAAM,aAAa,iBAAiB,MAAM,IAAI,IAAO;AAGrD,SAAO,IAAI,WAAW,CAAC,YAAY,GAAG,MAAM,CAAC;AAC9C;",
6
+ "names": []
7
+ }
@@ -0,0 +1,26 @@
1
+ import type { SignatureScheme } from '@haneullabs/haneul/cryptography';
2
+ import { Signer } from '@haneullabs/haneul/cryptography';
3
+ import { Secp256r1PublicKey } from '@haneullabs/haneul/keypairs/secp256r1';
4
+ export interface ExportedWebCryptoKeypair {
5
+ privateKey: CryptoKey;
6
+ publicKey: Uint8Array<ArrayBuffer>;
7
+ }
8
+ export declare class WebCryptoSigner extends Signer {
9
+ #private;
10
+ privateKey: CryptoKey;
11
+ static generate({ extractable }?: {
12
+ extractable?: boolean;
13
+ }): Promise<WebCryptoSigner>;
14
+ /**
15
+ * Imports a keypair using the value returned by `export()`.
16
+ */
17
+ static import(data: ExportedWebCryptoKeypair): WebCryptoSigner;
18
+ getKeyScheme(): SignatureScheme;
19
+ constructor(privateKey: CryptoKey, publicKey: Uint8Array);
20
+ /**
21
+ * Exports the keypair so that it can be stored in IndexedDB.
22
+ */
23
+ export(): ExportedWebCryptoKeypair;
24
+ getPublicKey(): Secp256r1PublicKey;
25
+ sign(bytes: Uint8Array): Promise<Uint8Array<ArrayBuffer>>;
26
+ }
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __typeError = (msg) => {
7
+ throw TypeError(msg);
8
+ };
9
+ var __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
23
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
24
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
25
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
26
+ var webcrypto_exports = {};
27
+ __export(webcrypto_exports, {
28
+ WebCryptoSigner: () => WebCryptoSigner
29
+ });
30
+ module.exports = __toCommonJS(webcrypto_exports);
31
+ var import_cryptography = require("@haneullabs/haneul/cryptography");
32
+ var import_secp256r1 = require("@haneullabs/haneul/keypairs/secp256r1");
33
+ var import_p256 = require("@noble/curves/p256");
34
+ var _publicKey;
35
+ function getCompressedPublicKey(publicKey) {
36
+ const rawBytes = new Uint8Array(publicKey);
37
+ const x = rawBytes.slice(1, 33);
38
+ const y = rawBytes.slice(33, 65);
39
+ const prefix = (y[31] & 1) === 0 ? 2 : 3;
40
+ const compressed = new Uint8Array(import_secp256r1.Secp256r1PublicKey.SIZE);
41
+ compressed[0] = prefix;
42
+ compressed.set(x, 1);
43
+ return compressed;
44
+ }
45
+ const _WebCryptoSigner = class _WebCryptoSigner extends import_cryptography.Signer {
46
+ constructor(privateKey, publicKey) {
47
+ super();
48
+ __privateAdd(this, _publicKey);
49
+ this.privateKey = privateKey;
50
+ __privateSet(this, _publicKey, new import_secp256r1.Secp256r1PublicKey(publicKey));
51
+ }
52
+ static async generate({ extractable = false } = {}) {
53
+ const keypair = await globalThis.crypto.subtle.generateKey(
54
+ {
55
+ name: "ECDSA",
56
+ namedCurve: "P-256"
57
+ },
58
+ extractable,
59
+ ["sign", "verify"]
60
+ );
61
+ const publicKey = await globalThis.crypto.subtle.exportKey("raw", keypair.publicKey);
62
+ return new _WebCryptoSigner(
63
+ keypair.privateKey,
64
+ getCompressedPublicKey(new Uint8Array(publicKey))
65
+ );
66
+ }
67
+ /**
68
+ * Imports a keypair using the value returned by `export()`.
69
+ */
70
+ static import(data) {
71
+ return new _WebCryptoSigner(data.privateKey, data.publicKey);
72
+ }
73
+ getKeyScheme() {
74
+ return "Secp256r1";
75
+ }
76
+ /**
77
+ * Exports the keypair so that it can be stored in IndexedDB.
78
+ */
79
+ export() {
80
+ const exportedKeypair = {
81
+ privateKey: this.privateKey,
82
+ publicKey: __privateGet(this, _publicKey).toRawBytes()
83
+ };
84
+ Object.defineProperty(exportedKeypair, "toJSON", {
85
+ enumerable: false,
86
+ value: () => {
87
+ throw new Error(
88
+ "The exported keypair must not be serialized. It must be stored in IndexedDB directly."
89
+ );
90
+ }
91
+ });
92
+ return exportedKeypair;
93
+ }
94
+ getPublicKey() {
95
+ return __privateGet(this, _publicKey);
96
+ }
97
+ async sign(bytes) {
98
+ const rawSignature = await globalThis.crypto.subtle.sign(
99
+ {
100
+ name: "ECDSA",
101
+ hash: "SHA-256"
102
+ },
103
+ this.privateKey,
104
+ bytes
105
+ );
106
+ const signature = import_p256.secp256r1.Signature.fromCompact(new Uint8Array(rawSignature));
107
+ return signature.normalizeS().toCompactRawBytes();
108
+ }
109
+ };
110
+ _publicKey = new WeakMap();
111
+ let WebCryptoSigner = _WebCryptoSigner;
112
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/webcrypto/index.ts"],
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { SignatureScheme } from '@haneullabs/haneul/cryptography';\nimport { Signer } from '@haneullabs/haneul/cryptography';\nimport { Secp256r1PublicKey } from '@haneullabs/haneul/keypairs/secp256r1';\nimport { secp256r1 } from '@noble/curves/p256';\n\n// Convert from uncompressed (65 bytes) to compressed (33 bytes) format\nfunction getCompressedPublicKey(publicKey: Uint8Array) {\n\tconst rawBytes = new Uint8Array(publicKey);\n\tconst x = rawBytes.slice(1, 33);\n\tconst y = rawBytes.slice(33, 65);\n\n\tconst prefix = (y[31] & 1) === 0 ? 0x02 : 0x03;\n\n\tconst compressed = new Uint8Array(Secp256r1PublicKey.SIZE);\n\tcompressed[0] = prefix;\n\tcompressed.set(x, 1);\n\n\treturn compressed;\n}\n\nexport interface ExportedWebCryptoKeypair {\n\tprivateKey: CryptoKey;\n\tpublicKey: Uint8Array<ArrayBuffer>;\n}\n\nexport class WebCryptoSigner extends Signer {\n\tprivateKey: CryptoKey;\n\n\t#publicKey: Secp256r1PublicKey;\n\n\tstatic async generate({ extractable = false }: { extractable?: boolean } = {}) {\n\t\tconst keypair = await globalThis.crypto.subtle.generateKey(\n\t\t\t{\n\t\t\t\tname: 'ECDSA',\n\t\t\t\tnamedCurve: 'P-256',\n\t\t\t},\n\t\t\textractable,\n\t\t\t['sign', 'verify'],\n\t\t);\n\n\t\tconst publicKey = await globalThis.crypto.subtle.exportKey('raw', keypair.publicKey);\n\n\t\treturn new WebCryptoSigner(\n\t\t\tkeypair.privateKey,\n\t\t\tgetCompressedPublicKey(new Uint8Array(publicKey)),\n\t\t);\n\t}\n\n\t/**\n\t * Imports a keypair using the value returned by `export()`.\n\t */\n\tstatic import(data: ExportedWebCryptoKeypair) {\n\t\treturn new WebCryptoSigner(data.privateKey, data.publicKey);\n\t}\n\n\tgetKeyScheme(): SignatureScheme {\n\t\treturn 'Secp256r1';\n\t}\n\n\tconstructor(privateKey: CryptoKey, publicKey: Uint8Array) {\n\t\tsuper();\n\t\tthis.privateKey = privateKey;\n\t\tthis.#publicKey = new Secp256r1PublicKey(publicKey);\n\t}\n\n\t/**\n\t * Exports the keypair so that it can be stored in IndexedDB.\n\t */\n\texport(): ExportedWebCryptoKeypair {\n\t\tconst exportedKeypair = {\n\t\t\tprivateKey: this.privateKey,\n\t\t\tpublicKey: this.#publicKey.toRawBytes(),\n\t\t};\n\n\t\tObject.defineProperty(exportedKeypair, 'toJSON', {\n\t\t\tenumerable: false,\n\t\t\tvalue: () => {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t'The exported keypair must not be serialized. It must be stored in IndexedDB directly.',\n\t\t\t\t);\n\t\t\t},\n\t\t});\n\n\t\treturn exportedKeypair;\n\t}\n\n\tgetPublicKey() {\n\t\treturn this.#publicKey;\n\t}\n\n\tasync sign(bytes: Uint8Array): Promise<Uint8Array<ArrayBuffer>> {\n\t\tconst rawSignature = await globalThis.crypto.subtle.sign(\n\t\t\t{\n\t\t\t\tname: 'ECDSA',\n\t\t\t\thash: 'SHA-256',\n\t\t\t},\n\t\t\tthis.privateKey,\n\t\t\tbytes as BufferSource,\n\t\t);\n\n\t\tconst signature = secp256r1.Signature.fromCompact(new Uint8Array(rawSignature));\n\n\t\treturn signature.normalizeS().toCompactRawBytes() as Uint8Array<ArrayBuffer>;\n\t}\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,0BAAuB;AACvB,uBAAmC;AACnC,kBAA0B;AAN1B;AASA,SAAS,uBAAuB,WAAuB;AACtD,QAAM,WAAW,IAAI,WAAW,SAAS;AACzC,QAAM,IAAI,SAAS,MAAM,GAAG,EAAE;AAC9B,QAAM,IAAI,SAAS,MAAM,IAAI,EAAE;AAE/B,QAAM,UAAU,EAAE,EAAE,IAAI,OAAO,IAAI,IAAO;AAE1C,QAAM,aAAa,IAAI,WAAW,oCAAmB,IAAI;AACzD,aAAW,CAAC,IAAI;AAChB,aAAW,IAAI,GAAG,CAAC;AAEnB,SAAO;AACR;AAOO,MAAM,mBAAN,MAAM,yBAAwB,2BAAO;AAAA,EAkC3C,YAAY,YAAuB,WAAuB;AACzD,UAAM;AAhCP;AAiCC,SAAK,aAAa;AAClB,uBAAK,YAAa,IAAI,oCAAmB,SAAS;AAAA,EACnD;AAAA,EAjCA,aAAa,SAAS,EAAE,cAAc,MAAM,IAA+B,CAAC,GAAG;AAC9E,UAAM,UAAU,MAAM,WAAW,OAAO,OAAO;AAAA,MAC9C;AAAA,QACC,MAAM;AAAA,QACN,YAAY;AAAA,MACb;AAAA,MACA;AAAA,MACA,CAAC,QAAQ,QAAQ;AAAA,IAClB;AAEA,UAAM,YAAY,MAAM,WAAW,OAAO,OAAO,UAAU,OAAO,QAAQ,SAAS;AAEnF,WAAO,IAAI;AAAA,MACV,QAAQ;AAAA,MACR,uBAAuB,IAAI,WAAW,SAAS,CAAC;AAAA,IACjD;AAAA,EACD;AAAA;AAAA;AAAA;AAAA,EAKA,OAAO,OAAO,MAAgC;AAC7C,WAAO,IAAI,iBAAgB,KAAK,YAAY,KAAK,SAAS;AAAA,EAC3D;AAAA,EAEA,eAAgC;AAC/B,WAAO;AAAA,EACR;AAAA;AAAA;AAAA;AAAA,EAWA,SAAmC;AAClC,UAAM,kBAAkB;AAAA,MACvB,YAAY,KAAK;AAAA,MACjB,WAAW,mBAAK,YAAW,WAAW;AAAA,IACvC;AAEA,WAAO,eAAe,iBAAiB,UAAU;AAAA,MAChD,YAAY;AAAA,MACZ,OAAO,MAAM;AACZ,cAAM,IAAI;AAAA,UACT;AAAA,QACD;AAAA,MACD;AAAA,IACD,CAAC;AAED,WAAO;AAAA,EACR;AAAA,EAEA,eAAe;AACd,WAAO,mBAAK;AAAA,EACb;AAAA,EAEA,MAAM,KAAK,OAAqD;AAC/D,UAAM,eAAe,MAAM,WAAW,OAAO,OAAO;AAAA,MACnD;AAAA,QACC,MAAM;AAAA,QACN,MAAM;AAAA,MACP;AAAA,MACA,KAAK;AAAA,MACL;AAAA,IACD;AAEA,UAAM,YAAY,sBAAU,UAAU,YAAY,IAAI,WAAW,YAAY,CAAC;AAE9E,WAAO,UAAU,WAAW,EAAE,kBAAkB;AAAA,EACjD;AACD;AA5EC;AAHM,IAAM,kBAAN;",
6
+ "names": []
7
+ }
@@ -0,0 +1,43 @@
1
+ import { Secp256k1PublicKey } from '@haneullabs/haneul/keypairs/secp256k1';
2
+ import { Secp256r1PublicKey } from '@haneullabs/haneul/keypairs/secp256r1';
3
+ import { AwsClient } from './aws4fetch.js';
4
+ interface KmsCommands {
5
+ Sign: {
6
+ request: {
7
+ KeyId: string;
8
+ Message: string;
9
+ MessageType: 'RAW' | 'DIGEST';
10
+ SigningAlgorithm: 'ECDSA_SHA_256';
11
+ };
12
+ response: {
13
+ KeyId: string;
14
+ KeyOrigin: string;
15
+ Signature: string;
16
+ SigningAlgorithm: string;
17
+ };
18
+ };
19
+ GetPublicKey: {
20
+ request: {
21
+ KeyId: string;
22
+ };
23
+ response: {
24
+ CustomerMasterKeySpec: string;
25
+ KeyId: string;
26
+ KeyOrigin: string;
27
+ KeySpec: string;
28
+ KeyUsage: string;
29
+ PublicKey: string;
30
+ SigningAlgorithms: string[];
31
+ };
32
+ };
33
+ }
34
+ export interface AwsClientOptions extends Partial<ConstructorParameters<typeof AwsClient>[0]> {
35
+ }
36
+ export declare class AwsKmsClient extends AwsClient {
37
+ constructor(options?: AwsClientOptions);
38
+ getPublicKey(keyId: string): Promise<Secp256r1PublicKey | Secp256k1PublicKey>;
39
+ runCommand<T extends keyof KmsCommands>(command: T, body: KmsCommands[T]['request'], { region, }?: {
40
+ region?: string;
41
+ }): Promise<KmsCommands[T]['response']>;
42
+ }
43
+ export {};
@@ -0,0 +1,59 @@
1
+ import { Secp256k1PublicKey } from "@haneullabs/haneul/keypairs/secp256k1";
2
+ import { Secp256r1PublicKey } from "@haneullabs/haneul/keypairs/secp256r1";
3
+ import { fromBase64 } from "@haneullabs/haneul/utils";
4
+ import { publicKeyFromDER } from "../utils/utils.js";
5
+ import { AwsClient } from "./aws4fetch.js";
6
+ class AwsKmsClient extends AwsClient {
7
+ constructor(options = {}) {
8
+ if (!options.accessKeyId || !options.secretAccessKey) {
9
+ throw new Error("AWS Access Key ID and Secret Access Key are required");
10
+ }
11
+ if (!options.region) {
12
+ throw new Error("Region is required");
13
+ }
14
+ super({
15
+ region: options.region,
16
+ accessKeyId: options.accessKeyId,
17
+ secretAccessKey: options.secretAccessKey,
18
+ service: "kms",
19
+ ...options
20
+ });
21
+ }
22
+ async getPublicKey(keyId) {
23
+ const publicKeyResponse = await this.runCommand("GetPublicKey", { KeyId: keyId });
24
+ if (!publicKeyResponse.PublicKey) {
25
+ throw new Error("Public Key not found for the supplied `keyId`");
26
+ }
27
+ const compressedKey = publicKeyFromDER(fromBase64(publicKeyResponse.PublicKey));
28
+ switch (publicKeyResponse.KeySpec) {
29
+ case "ECC_NIST_P256":
30
+ return new Secp256r1PublicKey(compressedKey);
31
+ case "ECC_SECG_P256K1":
32
+ return new Secp256k1PublicKey(compressedKey);
33
+ default:
34
+ throw new Error("Unsupported key spec: " + publicKeyResponse.KeySpec);
35
+ }
36
+ }
37
+ async runCommand(command, body, {
38
+ region = this.region
39
+ } = {}) {
40
+ if (!region) {
41
+ throw new Error("Region is required");
42
+ }
43
+ const res = await this.fetch(`https://kms.${region}.amazonaws.com/`, {
44
+ headers: {
45
+ "Content-Type": "application/x-amz-json-1.1",
46
+ "X-Amz-Target": `TrentService.${command}`
47
+ },
48
+ body: JSON.stringify(body)
49
+ });
50
+ if (!res.ok) {
51
+ throw new Error(await res.text());
52
+ }
53
+ return res.json();
54
+ }
55
+ }
56
+ export {
57
+ AwsKmsClient
58
+ };
59
+ //# sourceMappingURL=aws-client.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/aws/aws-client.ts"],
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { Secp256k1PublicKey } from '@haneullabs/haneul/keypairs/secp256k1';\nimport { Secp256r1PublicKey } from '@haneullabs/haneul/keypairs/secp256r1';\nimport { fromBase64 } from '@haneullabs/haneul/utils';\n\nimport { publicKeyFromDER } from '../utils/utils.js';\nimport { AwsClient } from './aws4fetch.js';\n\ninterface KmsCommands {\n\tSign: {\n\t\trequest: {\n\t\t\tKeyId: string;\n\t\t\tMessage: string;\n\t\t\tMessageType: 'RAW' | 'DIGEST';\n\t\t\tSigningAlgorithm: 'ECDSA_SHA_256';\n\t\t};\n\t\tresponse: {\n\t\t\tKeyId: string;\n\t\t\tKeyOrigin: string;\n\t\t\tSignature: string;\n\t\t\tSigningAlgorithm: string;\n\t\t};\n\t};\n\tGetPublicKey: {\n\t\trequest: { KeyId: string };\n\t\tresponse: {\n\t\t\tCustomerMasterKeySpec: string;\n\t\t\tKeyId: string;\n\t\t\tKeyOrigin: string;\n\t\t\tKeySpec: string;\n\t\t\tKeyUsage: string;\n\t\t\tPublicKey: string;\n\t\t\tSigningAlgorithms: string[];\n\t\t};\n\t};\n}\n\nexport interface AwsClientOptions extends Partial<ConstructorParameters<typeof AwsClient>[0]> {}\n\nexport class AwsKmsClient extends AwsClient {\n\tconstructor(options: AwsClientOptions = {}) {\n\t\tif (!options.accessKeyId || !options.secretAccessKey) {\n\t\t\tthrow new Error('AWS Access Key ID and Secret Access Key are required');\n\t\t}\n\n\t\tif (!options.region) {\n\t\t\tthrow new Error('Region is required');\n\t\t}\n\n\t\tsuper({\n\t\t\tregion: options.region,\n\t\t\taccessKeyId: options.accessKeyId,\n\t\t\tsecretAccessKey: options.secretAccessKey,\n\t\t\tservice: 'kms',\n\t\t\t...options,\n\t\t});\n\t}\n\n\tasync getPublicKey(keyId: string) {\n\t\tconst publicKeyResponse = await this.runCommand('GetPublicKey', { KeyId: keyId });\n\n\t\tif (!publicKeyResponse.PublicKey) {\n\t\t\tthrow new Error('Public Key not found for the supplied `keyId`');\n\t\t}\n\n\t\tconst compressedKey = publicKeyFromDER(fromBase64(publicKeyResponse.PublicKey));\n\n\t\tswitch (publicKeyResponse.KeySpec) {\n\t\t\tcase 'ECC_NIST_P256':\n\t\t\t\treturn new Secp256r1PublicKey(compressedKey);\n\t\t\tcase 'ECC_SECG_P256K1':\n\t\t\t\treturn new Secp256k1PublicKey(compressedKey);\n\t\t\tdefault:\n\t\t\t\tthrow new Error('Unsupported key spec: ' + publicKeyResponse.KeySpec);\n\t\t}\n\t}\n\n\tasync runCommand<T extends keyof KmsCommands>(\n\t\tcommand: T,\n\t\tbody: KmsCommands[T]['request'],\n\t\t{\n\t\t\tregion = this.region!,\n\t\t}: {\n\t\t\tregion?: string;\n\t\t} = {},\n\t): Promise<KmsCommands[T]['response']> {\n\t\tif (!region) {\n\t\t\tthrow new Error('Region is required');\n\t\t}\n\n\t\tconst res = await this.fetch(`https://kms.${region}.amazonaws.com/`, {\n\t\t\theaders: {\n\t\t\t\t'Content-Type': 'application/x-amz-json-1.1',\n\t\t\t\t'X-Amz-Target': `TrentService.${command}`,\n\t\t\t},\n\t\t\tbody: JSON.stringify(body),\n\t\t});\n\n\t\tif (!res.ok) {\n\t\t\tthrow new Error(await res.text());\n\t\t}\n\n\t\treturn res.json();\n\t}\n}\n"],
5
+ "mappings": "AAGA,SAAS,0BAA0B;AACnC,SAAS,0BAA0B;AACnC,SAAS,kBAAkB;AAE3B,SAAS,wBAAwB;AACjC,SAAS,iBAAiB;AAiCnB,MAAM,qBAAqB,UAAU;AAAA,EAC3C,YAAY,UAA4B,CAAC,GAAG;AAC3C,QAAI,CAAC,QAAQ,eAAe,CAAC,QAAQ,iBAAiB;AACrD,YAAM,IAAI,MAAM,sDAAsD;AAAA,IACvE;AAEA,QAAI,CAAC,QAAQ,QAAQ;AACpB,YAAM,IAAI,MAAM,oBAAoB;AAAA,IACrC;AAEA,UAAM;AAAA,MACL,QAAQ,QAAQ;AAAA,MAChB,aAAa,QAAQ;AAAA,MACrB,iBAAiB,QAAQ;AAAA,MACzB,SAAS;AAAA,MACT,GAAG;AAAA,IACJ,CAAC;AAAA,EACF;AAAA,EAEA,MAAM,aAAa,OAAe;AACjC,UAAM,oBAAoB,MAAM,KAAK,WAAW,gBAAgB,EAAE,OAAO,MAAM,CAAC;AAEhF,QAAI,CAAC,kBAAkB,WAAW;AACjC,YAAM,IAAI,MAAM,+CAA+C;AAAA,IAChE;AAEA,UAAM,gBAAgB,iBAAiB,WAAW,kBAAkB,SAAS,CAAC;AAE9E,YAAQ,kBAAkB,SAAS;AAAA,MAClC,KAAK;AACJ,eAAO,IAAI,mBAAmB,aAAa;AAAA,MAC5C,KAAK;AACJ,eAAO,IAAI,mBAAmB,aAAa;AAAA,MAC5C;AACC,cAAM,IAAI,MAAM,2BAA2B,kBAAkB,OAAO;AAAA,IACtE;AAAA,EACD;AAAA,EAEA,MAAM,WACL,SACA,MACA;AAAA,IACC,SAAS,KAAK;AAAA,EACf,IAEI,CAAC,GACiC;AACtC,QAAI,CAAC,QAAQ;AACZ,YAAM,IAAI,MAAM,oBAAoB;AAAA,IACrC;AAEA,UAAM,MAAM,MAAM,KAAK,MAAM,eAAe,MAAM,mBAAmB;AAAA,MACpE,SAAS;AAAA,QACR,gBAAgB;AAAA,QAChB,gBAAgB,gBAAgB,OAAO;AAAA,MACxC;AAAA,MACA,MAAM,KAAK,UAAU,IAAI;AAAA,IAC1B,CAAC;AAED,QAAI,CAAC,IAAI,IAAI;AACZ,YAAM,IAAI,MAAM,MAAM,IAAI,KAAK,CAAC;AAAA,IACjC;AAEA,WAAO,IAAI,KAAK;AAAA,EACjB;AACD;",
6
+ "names": []
7
+ }
@@ -0,0 +1,61 @@
1
+ import type { PublicKey } from '@haneullabs/haneul/cryptography';
2
+ import { Signer } from '@haneullabs/haneul/cryptography';
3
+ import type { AwsClientOptions } from './aws-client.js';
4
+ import { AwsKmsClient } from './aws-client.js';
5
+ /**
6
+ * Configuration options for initializing the AwsKmsSigner.
7
+ */
8
+ export interface AwsKmsSignerOptions {
9
+ /** AWS KMS Key ID used for signing */
10
+ kmsKeyId: string;
11
+ /** Options for setting up the AWS KMS client */
12
+ client: AwsKmsClient;
13
+ /** Public key */
14
+ publicKey: PublicKey;
15
+ }
16
+ /**
17
+ * Aws KMS Signer integrates AWS Key Management Service (KMS) with the Haneul blockchain
18
+ * to provide signing capabilities using AWS-managed cryptographic keys.
19
+ */
20
+ export declare class AwsKmsSigner extends Signer {
21
+ #private;
22
+ /**
23
+ * Creates an instance of AwsKmsSigner. It's expected to call the static `fromKeyId` method to create an instance.
24
+ * For example:
25
+ * ```
26
+ * const signer = await AwsKmsSigner.fromKeyId(keyId, options);
27
+ * ```
28
+ * @throws Will throw an error if required AWS credentials or region are not provided.
29
+ */
30
+ constructor({ kmsKeyId, client, publicKey }: AwsKmsSignerOptions);
31
+ /**
32
+ * Retrieves the key scheme used by this signer.
33
+ * @returns AWS supports only Secp256k1 and Secp256r1 schemes.
34
+ */
35
+ getKeyScheme(): "Secp256k1" | "Secp256r1" | "ED25519" | "MultiSig" | "ZkLogin" | "Passkey";
36
+ /**
37
+ * Retrieves the public key associated with this signer.
38
+ * @returns The Secp256k1PublicKey instance.
39
+ * @throws Will throw an error if the public key has not been initialized.
40
+ */
41
+ getPublicKey(): PublicKey;
42
+ /**
43
+ * Signs the given data using AWS KMS.
44
+ * @param bytes - The data to be signed as a Uint8Array.
45
+ * @returns A promise that resolves to the signature as a Uint8Array.
46
+ * @throws Will throw an error if the public key is not initialized or if signing fails.
47
+ */
48
+ sign(bytes: Uint8Array): Promise<Uint8Array<ArrayBuffer>>;
49
+ /**
50
+ * Synchronous signing is not supported by AWS KMS.
51
+ * @throws Always throws an error indicating synchronous signing is unsupported.
52
+ * @deprecated use `sign` instead
53
+ */
54
+ signData(): never;
55
+ /**
56
+ * Prepares the signer by fetching and setting the public key from AWS KMS.
57
+ * It is recommended to initialize an `AwsKmsSigner` instance using this function.
58
+ * @returns A promise that resolves once a `AwsKmsSigner` instance is prepared (public key is set).
59
+ */
60
+ static fromKeyId(keyId: string, options: AwsClientOptions): Promise<AwsKmsSigner>;
61
+ }
@@ -0,0 +1,94 @@
1
+ var __typeError = (msg) => {
2
+ throw TypeError(msg);
3
+ };
4
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
5
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
6
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
7
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
8
+ var _publicKey, _client, _kmsKeyId;
9
+ import { SIGNATURE_FLAG_TO_SCHEME, Signer } from "@haneullabs/haneul/cryptography";
10
+ import { fromBase64, toBase64 } from "@haneullabs/haneul/utils";
11
+ import { getConcatenatedSignature } from "../utils/utils.js";
12
+ import { AwsKmsClient } from "./aws-client.js";
13
+ const _AwsKmsSigner = class _AwsKmsSigner extends Signer {
14
+ /**
15
+ * Creates an instance of AwsKmsSigner. It's expected to call the static `fromKeyId` method to create an instance.
16
+ * For example:
17
+ * ```
18
+ * const signer = await AwsKmsSigner.fromKeyId(keyId, options);
19
+ * ```
20
+ * @throws Will throw an error if required AWS credentials or region are not provided.
21
+ */
22
+ constructor({ kmsKeyId, client, publicKey }) {
23
+ super();
24
+ __privateAdd(this, _publicKey);
25
+ /** AWS KMS client instance */
26
+ __privateAdd(this, _client);
27
+ /** AWS KMS Key ID used for signing */
28
+ __privateAdd(this, _kmsKeyId);
29
+ if (!kmsKeyId) throw new Error("KMS Key ID is required");
30
+ __privateSet(this, _client, client);
31
+ __privateSet(this, _kmsKeyId, kmsKeyId);
32
+ __privateSet(this, _publicKey, publicKey);
33
+ }
34
+ /**
35
+ * Retrieves the key scheme used by this signer.
36
+ * @returns AWS supports only Secp256k1 and Secp256r1 schemes.
37
+ */
38
+ getKeyScheme() {
39
+ return SIGNATURE_FLAG_TO_SCHEME[__privateGet(this, _publicKey).flag()];
40
+ }
41
+ /**
42
+ * Retrieves the public key associated with this signer.
43
+ * @returns The Secp256k1PublicKey instance.
44
+ * @throws Will throw an error if the public key has not been initialized.
45
+ */
46
+ getPublicKey() {
47
+ return __privateGet(this, _publicKey);
48
+ }
49
+ /**
50
+ * Signs the given data using AWS KMS.
51
+ * @param bytes - The data to be signed as a Uint8Array.
52
+ * @returns A promise that resolves to the signature as a Uint8Array.
53
+ * @throws Will throw an error if the public key is not initialized or if signing fails.
54
+ */
55
+ async sign(bytes) {
56
+ const signResponse = await __privateGet(this, _client).runCommand("Sign", {
57
+ KeyId: __privateGet(this, _kmsKeyId),
58
+ Message: toBase64(bytes),
59
+ MessageType: "RAW",
60
+ SigningAlgorithm: "ECDSA_SHA_256"
61
+ });
62
+ return getConcatenatedSignature(fromBase64(signResponse.Signature), this.getKeyScheme());
63
+ }
64
+ /**
65
+ * Synchronous signing is not supported by AWS KMS.
66
+ * @throws Always throws an error indicating synchronous signing is unsupported.
67
+ * @deprecated use `sign` instead
68
+ */
69
+ signData() {
70
+ throw new Error("KMS Signer does not support sync signing");
71
+ }
72
+ /**
73
+ * Prepares the signer by fetching and setting the public key from AWS KMS.
74
+ * It is recommended to initialize an `AwsKmsSigner` instance using this function.
75
+ * @returns A promise that resolves once a `AwsKmsSigner` instance is prepared (public key is set).
76
+ */
77
+ static async fromKeyId(keyId, options) {
78
+ const client = new AwsKmsClient(options);
79
+ const pubKey = await client.getPublicKey(keyId);
80
+ return new _AwsKmsSigner({
81
+ kmsKeyId: keyId,
82
+ client,
83
+ publicKey: pubKey
84
+ });
85
+ }
86
+ };
87
+ _publicKey = new WeakMap();
88
+ _client = new WeakMap();
89
+ _kmsKeyId = new WeakMap();
90
+ let AwsKmsSigner = _AwsKmsSigner;
91
+ export {
92
+ AwsKmsSigner
93
+ };
94
+ //# sourceMappingURL=aws-kms-signer.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/aws/aws-kms-signer.ts"],
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\nimport type { PublicKey, SignatureFlag } from '@haneullabs/haneul/cryptography';\nimport { SIGNATURE_FLAG_TO_SCHEME, Signer } from '@haneullabs/haneul/cryptography';\nimport { fromBase64, toBase64 } from '@haneullabs/haneul/utils';\n\nimport { getConcatenatedSignature } from '../utils/utils.js';\nimport type { AwsClientOptions } from './aws-client.js';\nimport { AwsKmsClient } from './aws-client.js';\n\n/**\n * Configuration options for initializing the AwsKmsSigner.\n */\nexport interface AwsKmsSignerOptions {\n\t/** AWS KMS Key ID used for signing */\n\tkmsKeyId: string;\n\t/** Options for setting up the AWS KMS client */\n\tclient: AwsKmsClient;\n\t/** Public key */\n\tpublicKey: PublicKey;\n}\n\n/**\n * Aws KMS Signer integrates AWS Key Management Service (KMS) with the Haneul blockchain\n * to provide signing capabilities using AWS-managed cryptographic keys.\n */\nexport class AwsKmsSigner extends Signer {\n\t#publicKey: PublicKey;\n\t/** AWS KMS client instance */\n\t#client: AwsKmsClient;\n\t/** AWS KMS Key ID used for signing */\n\t#kmsKeyId: string;\n\n\t/**\n\t * Creates an instance of AwsKmsSigner. It's expected to call the static `fromKeyId` method to create an instance.\n\t * For example:\n\t * ```\n\t * const signer = await AwsKmsSigner.fromKeyId(keyId, options);\n\t * ```\n\t * @throws Will throw an error if required AWS credentials or region are not provided.\n\t */\n\tconstructor({ kmsKeyId, client, publicKey }: AwsKmsSignerOptions) {\n\t\tsuper();\n\t\tif (!kmsKeyId) throw new Error('KMS Key ID is required');\n\n\t\tthis.#client = client;\n\t\tthis.#kmsKeyId = kmsKeyId;\n\t\tthis.#publicKey = publicKey;\n\t}\n\n\t/**\n\t * Retrieves the key scheme used by this signer.\n\t * @returns AWS supports only Secp256k1 and Secp256r1 schemes.\n\t */\n\tgetKeyScheme() {\n\t\treturn SIGNATURE_FLAG_TO_SCHEME[this.#publicKey.flag() as SignatureFlag];\n\t}\n\n\t/**\n\t * Retrieves the public key associated with this signer.\n\t * @returns The Secp256k1PublicKey instance.\n\t * @throws Will throw an error if the public key has not been initialized.\n\t */\n\tgetPublicKey() {\n\t\treturn this.#publicKey;\n\t}\n\n\t/**\n\t * Signs the given data using AWS KMS.\n\t * @param bytes - The data to be signed as a Uint8Array.\n\t * @returns A promise that resolves to the signature as a Uint8Array.\n\t * @throws Will throw an error if the public key is not initialized or if signing fails.\n\t */\n\tasync sign(bytes: Uint8Array): Promise<Uint8Array<ArrayBuffer>> {\n\t\tconst signResponse = await this.#client.runCommand('Sign', {\n\t\t\tKeyId: this.#kmsKeyId,\n\t\t\tMessage: toBase64(bytes),\n\t\t\tMessageType: 'RAW',\n\t\t\tSigningAlgorithm: 'ECDSA_SHA_256',\n\t\t});\n\n\t\t// Concatenate the signature components into a compact form\n\t\treturn getConcatenatedSignature(fromBase64(signResponse.Signature), this.getKeyScheme());\n\t}\n\n\t/**\n\t * Synchronous signing is not supported by AWS KMS.\n\t * @throws Always throws an error indicating synchronous signing is unsupported.\n\t * @deprecated use `sign` instead\n\t */\n\tsignData(): never {\n\t\tthrow new Error('KMS Signer does not support sync signing');\n\t}\n\n\t/**\n\t * Prepares the signer by fetching and setting the public key from AWS KMS.\n\t * It is recommended to initialize an `AwsKmsSigner` instance using this function.\n\t * @returns A promise that resolves once a `AwsKmsSigner` instance is prepared (public key is set).\n\t */\n\tstatic async fromKeyId(keyId: string, options: AwsClientOptions) {\n\t\tconst client = new AwsKmsClient(options);\n\n\t\tconst pubKey = await client.getPublicKey(keyId);\n\n\t\treturn new AwsKmsSigner({\n\t\t\tkmsKeyId: keyId,\n\t\t\tclient,\n\t\t\tpublicKey: pubKey,\n\t\t});\n\t}\n}\n"],
5
+ "mappings": ";;;;;;;AAAA;AAGA,SAAS,0BAA0B,cAAc;AACjD,SAAS,YAAY,gBAAgB;AAErC,SAAS,gCAAgC;AAEzC,SAAS,oBAAoB;AAkBtB,MAAM,gBAAN,MAAM,sBAAqB,OAAO;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAexC,YAAY,EAAE,UAAU,QAAQ,UAAU,GAAwB;AACjE,UAAM;AAfP;AAEA;AAAA;AAEA;AAAA;AAYC,QAAI,CAAC,SAAU,OAAM,IAAI,MAAM,wBAAwB;AAEvD,uBAAK,SAAU;AACf,uBAAK,WAAY;AACjB,uBAAK,YAAa;AAAA,EACnB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,eAAe;AACd,WAAO,yBAAyB,mBAAK,YAAW,KAAK,CAAkB;AAAA,EACxE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,eAAe;AACd,WAAO,mBAAK;AAAA,EACb;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,KAAK,OAAqD;AAC/D,UAAM,eAAe,MAAM,mBAAK,SAAQ,WAAW,QAAQ;AAAA,MAC1D,OAAO,mBAAK;AAAA,MACZ,SAAS,SAAS,KAAK;AAAA,MACvB,aAAa;AAAA,MACb,kBAAkB;AAAA,IACnB,CAAC;AAGD,WAAO,yBAAyB,WAAW,aAAa,SAAS,GAAG,KAAK,aAAa,CAAC;AAAA,EACxF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,WAAkB;AACjB,UAAM,IAAI,MAAM,0CAA0C;AAAA,EAC3D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,aAAa,UAAU,OAAe,SAA2B;AAChE,UAAM,SAAS,IAAI,aAAa,OAAO;AAEvC,UAAM,SAAS,MAAM,OAAO,aAAa,KAAK;AAE9C,WAAO,IAAI,cAAa;AAAA,MACvB,UAAU;AAAA,MACV;AAAA,MACA,WAAW;AAAA,IACZ,CAAC;AAAA,EACF;AACD;AAnFC;AAEA;AAEA;AALM,IAAM,eAAN;",
6
+ "names": []
7
+ }