@mysten/signers 0.1.13 → 0.1.15

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mysten/signers",
3
- "version": "0.1.13",
3
+ "version": "0.1.15",
4
4
  "description": "A collection of KMS signers for various cloud providers",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Mysten Labs <build@mystenlabs.com>",
@@ -4,6 +4,7 @@
4
4
  import type { SignatureScheme } from '@mysten/sui/cryptography';
5
5
  import { Signer } from '@mysten/sui/cryptography';
6
6
  import { Secp256r1PublicKey } from '@mysten/sui/keypairs/secp256r1';
7
+ import { secp256r1 } from '@noble/curves/p256';
7
8
 
8
9
  // Convert from uncompressed (65 bytes) to compressed (33 bytes) format
9
10
  function getCompressedPublicKey(publicKey: Uint8Array) {
@@ -20,6 +21,11 @@ function getCompressedPublicKey(publicKey: Uint8Array) {
20
21
  return compressed;
21
22
  }
22
23
 
24
+ export interface ExportedWebCryptoKeypair {
25
+ privateKey: CryptoKey;
26
+ publicKey: Uint8Array;
27
+ }
28
+
23
29
  export class WebCryptoSigner extends Signer {
24
30
  privateKey: CryptoKey;
25
31
 
@@ -37,7 +43,17 @@ export class WebCryptoSigner extends Signer {
37
43
 
38
44
  const publicKey = await globalThis.crypto.subtle.exportKey('raw', keypair.publicKey);
39
45
 
40
- return new WebCryptoSigner(keypair.privateKey, new Uint8Array(publicKey));
46
+ return new WebCryptoSigner(
47
+ keypair.privateKey,
48
+ getCompressedPublicKey(new Uint8Array(publicKey)),
49
+ );
50
+ }
51
+
52
+ /**
53
+ * Imports a keypair using the value returned by `export()`.
54
+ */
55
+ static import(data: ExportedWebCryptoKeypair) {
56
+ return new WebCryptoSigner(data.privateKey, data.publicKey);
41
57
  }
42
58
 
43
59
  getKeyScheme(): SignatureScheme {
@@ -47,7 +63,28 @@ export class WebCryptoSigner extends Signer {
47
63
  constructor(privateKey: CryptoKey, publicKey: Uint8Array) {
48
64
  super();
49
65
  this.privateKey = privateKey;
50
- this.#publicKey = new Secp256r1PublicKey(getCompressedPublicKey(publicKey));
66
+ this.#publicKey = new Secp256r1PublicKey(publicKey);
67
+ }
68
+
69
+ /**
70
+ * Exports the keypair so that it can be stored in IndexedDB.
71
+ */
72
+ export(): ExportedWebCryptoKeypair {
73
+ const exportedKeypair = {
74
+ privateKey: this.privateKey,
75
+ publicKey: this.#publicKey.toRawBytes(),
76
+ };
77
+
78
+ Object.defineProperty(exportedKeypair, 'toJSON', {
79
+ enumerable: false,
80
+ value: () => {
81
+ throw new Error(
82
+ 'The exported keypair must not be serialized. It must be stored in IndexedDB directly.',
83
+ );
84
+ },
85
+ });
86
+
87
+ return exportedKeypair;
51
88
  }
52
89
 
53
90
  getPublicKey() {
@@ -55,7 +92,7 @@ export class WebCryptoSigner extends Signer {
55
92
  }
56
93
 
57
94
  async sign(bytes: Uint8Array): Promise<Uint8Array> {
58
- const signature = await globalThis.crypto.subtle.sign(
95
+ const rawSignature = await globalThis.crypto.subtle.sign(
59
96
  {
60
97
  name: 'ECDSA',
61
98
  hash: 'SHA-256',
@@ -64,6 +101,8 @@ export class WebCryptoSigner extends Signer {
64
101
  bytes,
65
102
  );
66
103
 
67
- return new Uint8Array(signature);
104
+ const signature = secp256r1.Signature.fromCompact(new Uint8Array(rawSignature));
105
+
106
+ return signature.normalizeS().toCompactRawBytes();
68
107
  }
69
108
  }