@mysten/signers 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/README.md +1 -1
  3. package/dist/aws/index.d.mts +1 -3
  4. package/dist/aws/index.mjs +2 -2
  5. package/dist/gcp/index.d.mts +1 -2
  6. package/dist/gcp/index.mjs +2 -2
  7. package/dist/ledger/index.d.mts +1 -74
  8. package/dist/ledger/index.mjs +2 -109
  9. package/dist/webcrypto/index.d.mts +1 -32
  10. package/dist/webcrypto/index.mjs +2 -69
  11. package/package.json +9 -19
  12. package/src/aws/index.ts +1 -6
  13. package/src/gcp/index.ts +1 -6
  14. package/src/ledger/index.ts +1 -160
  15. package/src/webcrypto/index.ts +1 -108
  16. package/dist/aws/aws-client.d.mts +0 -48
  17. package/dist/aws/aws-client.d.mts.map +0 -1
  18. package/dist/aws/aws-client.mjs +0 -46
  19. package/dist/aws/aws-client.mjs.map +0 -1
  20. package/dist/aws/aws-kms-signer.d.mts +0 -63
  21. package/dist/aws/aws-kms-signer.d.mts.map +0 -1
  22. package/dist/aws/aws-kms-signer.mjs +0 -78
  23. package/dist/aws/aws-kms-signer.mjs.map +0 -1
  24. package/dist/aws/aws4fetch.d.mts +0 -62
  25. package/dist/aws/aws4fetch.d.mts.map +0 -1
  26. package/dist/aws/aws4fetch.mjs +0 -313
  27. package/dist/aws/aws4fetch.mjs.map +0 -1
  28. package/dist/gcp/gcp-kms-client.d.mts +0 -71
  29. package/dist/gcp/gcp-kms-client.d.mts.map +0 -1
  30. package/dist/gcp/gcp-kms-client.mjs +0 -104
  31. package/dist/gcp/gcp-kms-client.mjs.map +0 -1
  32. package/dist/ledger/index.d.mts.map +0 -1
  33. package/dist/ledger/index.mjs.map +0 -1
  34. package/dist/ledger/objects.d.mts +0 -10
  35. package/dist/ledger/objects.d.mts.map +0 -1
  36. package/dist/ledger/objects.mjs +0 -16
  37. package/dist/ledger/objects.mjs.map +0 -1
  38. package/dist/utils/utils.mjs +0 -71
  39. package/dist/utils/utils.mjs.map +0 -1
  40. package/dist/webcrypto/index.d.mts.map +0 -1
  41. package/dist/webcrypto/index.mjs.map +0 -1
  42. package/src/aws/aws-client.ts +0 -107
  43. package/src/aws/aws-kms-signer.ts +0 -102
  44. package/src/aws/aws4fetch.ts +0 -502
  45. package/src/gcp/gcp-kms-client.ts +0 -156
  46. package/src/ledger/objects.ts +0 -32
  47. package/src/utils/utils.ts +0 -127
@@ -1,127 +0,0 @@
1
- // Copyright (c) Mysten Labs, Inc.
2
- // SPDX-License-Identifier: Apache-2.0
3
-
4
- import { p256 as secp256r1 } from '@noble/curves/nist.js';
5
- import { secp256k1 } from '@noble/curves/secp256k1.js';
6
- import { ASN1Construction, ASN1TagClass, DERElement } from 'asn1-ts';
7
-
8
- /** The total number of bits in the DER bit string for the uncompressed public key. */
9
- export const DER_BIT_STRING_LENGTH = 520;
10
-
11
- /** The total number of bytes corresponding to the DER bit string length. */
12
- export const DER_BYTES_LENGTH = DER_BIT_STRING_LENGTH / 8;
13
-
14
- // Reference Specifications:
15
- // https://datatracker.ietf.org/doc/html/rfc5480#section-2.2
16
- // https://www.secg.org/sec1-v2.pdf
17
-
18
- /**
19
- * Converts an array of bits into a byte array.
20
- *
21
- * @param bitsArray - A `Uint8ClampedArray` representing the bits to convert.
22
- * @returns A `Uint8Array` containing the corresponding bytes.
23
- *
24
- * @throws {Error} If the input array does not have the expected length.
25
- */
26
- function bitsToBytes(bitsArray: Uint8ClampedArray): Uint8Array {
27
- const bytes = new Uint8Array(DER_BYTES_LENGTH);
28
- for (let i = 0; i < DER_BIT_STRING_LENGTH; i++) {
29
- if (bitsArray[i] === 1) {
30
- bytes[Math.floor(i / 8)] |= 1 << (7 - (i % 8));
31
- }
32
- }
33
- return bytes;
34
- }
35
-
36
- export function publicKeyFromDER(derBytes: Uint8Array) {
37
- const encodedData: Uint8Array = derBytes;
38
- const derElement = new DERElement();
39
- derElement.fromBytes(encodedData);
40
-
41
- // Validate the ASN.1 structure of the public key
42
- if (
43
- !(
44
- derElement.tagClass === ASN1TagClass.universal &&
45
- derElement.construction === ASN1Construction.constructed
46
- )
47
- ) {
48
- throw new Error('Unexpected ASN.1 structure');
49
- }
50
-
51
- const components = derElement.components;
52
- const publicKeyElement = components[1];
53
-
54
- if (!publicKeyElement) {
55
- throw new Error('Public Key not found in the DER structure');
56
- }
57
-
58
- return compressPublicKeyClamped(publicKeyElement.bitString);
59
- }
60
-
61
- export function getConcatenatedSignature(signature: Uint8Array, keyScheme: string) {
62
- if (!signature || signature.length === 0) {
63
- throw new Error('Invalid signature');
64
- }
65
-
66
- // Initialize a DERElement to parse the DER-encoded signature
67
- const derElement = new DERElement();
68
- derElement.fromBytes(signature);
69
-
70
- const [r, s] = derElement.toJSON() as [string, string];
71
-
72
- switch (keyScheme) {
73
- case 'Secp256k1': {
74
- const sig = new secp256k1.Signature(BigInt(r), BigInt(s));
75
- const normalized = sig.hasHighS()
76
- ? new secp256k1.Signature(sig.r, secp256k1.Point.Fn.neg(sig.s))
77
- : sig;
78
-
79
- return normalized.toBytes('compact') as Uint8Array<ArrayBuffer>;
80
- }
81
- case 'Secp256r1': {
82
- const sig = new secp256r1.Signature(BigInt(r), BigInt(s));
83
- const normalized = sig.hasHighS()
84
- ? new secp256r1.Signature(sig.r, secp256r1.Point.Fn.neg(sig.s))
85
- : sig;
86
-
87
- return normalized.toBytes('compact') as Uint8Array<ArrayBuffer>;
88
- }
89
- default:
90
- throw new Error('Unsupported key scheme');
91
- }
92
- }
93
-
94
- /**
95
- * Compresses an uncompressed public key into its compressed form.
96
- *
97
- * 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)
98
- * and [SEC 1: Elliptic Curve Cryptography](https://www.secg.org/sec1-v2.pdf).
99
- *
100
- * @param uncompressedKey - A `Uint8ClampedArray` representing the uncompressed public key bits.
101
- * @returns A `Uint8Array` containing the compressed public key.
102
- *
103
- * @throws {Error} If the uncompressed key has an unexpected length or does not start with the expected prefix.
104
- */
105
- export function compressPublicKeyClamped(uncompressedKey: Uint8ClampedArray): Uint8Array {
106
- if (uncompressedKey.length !== DER_BIT_STRING_LENGTH) {
107
- throw new Error('Unexpected length for an uncompressed public key');
108
- }
109
-
110
- // Convert bits to bytes
111
- const uncompressedBytes = bitsToBytes(uncompressedKey);
112
-
113
- // Ensure the public key starts with the standard uncompressed prefix 0x04
114
- if (uncompressedBytes[0] !== 0x04) {
115
- throw new Error('Public key does not start with 0x04');
116
- }
117
-
118
- // Extract X-Coordinate (skip the first byte, which is the prefix 0x04)
119
- const xCoord = uncompressedBytes.slice(1, 33);
120
-
121
- // Determine parity byte for Y coordinate based on the last byte
122
- const yCoordLastByte = uncompressedBytes[64];
123
- const parityByte = yCoordLastByte % 2 === 0 ? 0x02 : 0x03;
124
-
125
- // Return the compressed public key consisting of the parity byte and X-coordinate
126
- return new Uint8Array([parityByte, ...xCoord]);
127
- }