@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/CHANGELOG.md +14 -0
- package/dist/cjs/webcrypto/index.d.ts +12 -0
- package/dist/cjs/webcrypto/index.js +33 -4
- package/dist/cjs/webcrypto/index.js.map +2 -2
- package/dist/esm/webcrypto/index.d.ts +12 -0
- package/dist/esm/webcrypto/index.js +33 -4
- package/dist/esm/webcrypto/index.js.map +2 -2
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/webcrypto/index.ts +43 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @mysten/signers
|
|
2
2
|
|
|
3
|
+
## 0.1.15
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 850369a: Prevent exported webcrypto keys from being serialized.
|
|
8
|
+
|
|
9
|
+
## 0.1.14
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 5217cab: Fix signature not having lowS enforced
|
|
14
|
+
- a08ead6: Add webcrypto to the files allowlist
|
|
15
|
+
- b34f523: Fix re-constructing signers from exported bytes, and introduce import / export methods
|
|
16
|
+
|
|
3
17
|
## 0.1.13
|
|
4
18
|
|
|
5
19
|
### Patch Changes
|
|
@@ -1,14 +1,26 @@
|
|
|
1
1
|
import type { SignatureScheme } from '@mysten/sui/cryptography';
|
|
2
2
|
import { Signer } from '@mysten/sui/cryptography';
|
|
3
3
|
import { Secp256r1PublicKey } from '@mysten/sui/keypairs/secp256r1';
|
|
4
|
+
export interface ExportedWebCryptoKeypair {
|
|
5
|
+
privateKey: CryptoKey;
|
|
6
|
+
publicKey: Uint8Array;
|
|
7
|
+
}
|
|
4
8
|
export declare class WebCryptoSigner extends Signer {
|
|
5
9
|
#private;
|
|
6
10
|
privateKey: CryptoKey;
|
|
7
11
|
static generate({ extractable }?: {
|
|
8
12
|
extractable?: boolean;
|
|
9
13
|
}): Promise<WebCryptoSigner>;
|
|
14
|
+
/**
|
|
15
|
+
* Imports a keypair using the value returned by `export()`.
|
|
16
|
+
*/
|
|
17
|
+
static import(data: ExportedWebCryptoKeypair): WebCryptoSigner;
|
|
10
18
|
getKeyScheme(): SignatureScheme;
|
|
11
19
|
constructor(privateKey: CryptoKey, publicKey: Uint8Array);
|
|
20
|
+
/**
|
|
21
|
+
* Exports the keypair so that it can be stored in IndexedDB.
|
|
22
|
+
*/
|
|
23
|
+
export(): ExportedWebCryptoKeypair;
|
|
12
24
|
getPublicKey(): Secp256r1PublicKey;
|
|
13
25
|
sign(bytes: Uint8Array): Promise<Uint8Array>;
|
|
14
26
|
}
|
|
@@ -30,6 +30,7 @@ __export(webcrypto_exports, {
|
|
|
30
30
|
module.exports = __toCommonJS(webcrypto_exports);
|
|
31
31
|
var import_cryptography = require("@mysten/sui/cryptography");
|
|
32
32
|
var import_secp256r1 = require("@mysten/sui/keypairs/secp256r1");
|
|
33
|
+
var import_p256 = require("@noble/curves/p256");
|
|
33
34
|
var _publicKey;
|
|
34
35
|
function getCompressedPublicKey(publicKey) {
|
|
35
36
|
const rawBytes = new Uint8Array(publicKey);
|
|
@@ -46,7 +47,7 @@ const _WebCryptoSigner = class _WebCryptoSigner extends import_cryptography.Sign
|
|
|
46
47
|
super();
|
|
47
48
|
__privateAdd(this, _publicKey);
|
|
48
49
|
this.privateKey = privateKey;
|
|
49
|
-
__privateSet(this, _publicKey, new import_secp256r1.Secp256r1PublicKey(
|
|
50
|
+
__privateSet(this, _publicKey, new import_secp256r1.Secp256r1PublicKey(publicKey));
|
|
50
51
|
}
|
|
51
52
|
static async generate({ extractable = false } = {}) {
|
|
52
53
|
const keypair = await globalThis.crypto.subtle.generateKey(
|
|
@@ -58,16 +59,43 @@ const _WebCryptoSigner = class _WebCryptoSigner extends import_cryptography.Sign
|
|
|
58
59
|
["sign", "verify"]
|
|
59
60
|
);
|
|
60
61
|
const publicKey = await globalThis.crypto.subtle.exportKey("raw", keypair.publicKey);
|
|
61
|
-
return new _WebCryptoSigner(
|
|
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);
|
|
62
72
|
}
|
|
63
73
|
getKeyScheme() {
|
|
64
74
|
return "Secp256r1";
|
|
65
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
|
+
}
|
|
66
94
|
getPublicKey() {
|
|
67
95
|
return __privateGet(this, _publicKey);
|
|
68
96
|
}
|
|
69
97
|
async sign(bytes) {
|
|
70
|
-
const
|
|
98
|
+
const rawSignature = await globalThis.crypto.subtle.sign(
|
|
71
99
|
{
|
|
72
100
|
name: "ECDSA",
|
|
73
101
|
hash: "SHA-256"
|
|
@@ -75,7 +103,8 @@ const _WebCryptoSigner = class _WebCryptoSigner extends import_cryptography.Sign
|
|
|
75
103
|
this.privateKey,
|
|
76
104
|
bytes
|
|
77
105
|
);
|
|
78
|
-
|
|
106
|
+
const signature = import_p256.secp256r1.Signature.fromCompact(new Uint8Array(rawSignature));
|
|
107
|
+
return signature.normalizeS().toCompactRawBytes();
|
|
79
108
|
}
|
|
80
109
|
};
|
|
81
110
|
_publicKey = new WeakMap();
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
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 '@mysten/sui/cryptography';\nimport { Signer } from '@mysten/sui/cryptography';\nimport { Secp256r1PublicKey } from '@mysten/sui/keypairs/secp256r1';\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 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(
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,0BAAuB;AACvB,uBAAmC;
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { SignatureScheme } from '@mysten/sui/cryptography';\nimport { Signer } from '@mysten/sui/cryptography';\nimport { Secp256r1PublicKey } from '@mysten/sui/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;\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> {\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,\n\t\t);\n\n\t\tconst signature = secp256r1.Signature.fromCompact(new Uint8Array(rawSignature));\n\n\t\treturn signature.normalizeS().toCompactRawBytes();\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;AA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YAAY,sBAAU,UAAU,YAAY,IAAI,WAAW,YAAY,CAAC;AAE9E,WAAO,UAAU,WAAW,EAAE,kBAAkB;AAAA,EACjD;AACD;AA5EC;AAHM,IAAM,kBAAN;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,14 +1,26 @@
|
|
|
1
1
|
import type { SignatureScheme } from '@mysten/sui/cryptography';
|
|
2
2
|
import { Signer } from '@mysten/sui/cryptography';
|
|
3
3
|
import { Secp256r1PublicKey } from '@mysten/sui/keypairs/secp256r1';
|
|
4
|
+
export interface ExportedWebCryptoKeypair {
|
|
5
|
+
privateKey: CryptoKey;
|
|
6
|
+
publicKey: Uint8Array;
|
|
7
|
+
}
|
|
4
8
|
export declare class WebCryptoSigner extends Signer {
|
|
5
9
|
#private;
|
|
6
10
|
privateKey: CryptoKey;
|
|
7
11
|
static generate({ extractable }?: {
|
|
8
12
|
extractable?: boolean;
|
|
9
13
|
}): Promise<WebCryptoSigner>;
|
|
14
|
+
/**
|
|
15
|
+
* Imports a keypair using the value returned by `export()`.
|
|
16
|
+
*/
|
|
17
|
+
static import(data: ExportedWebCryptoKeypair): WebCryptoSigner;
|
|
10
18
|
getKeyScheme(): SignatureScheme;
|
|
11
19
|
constructor(privateKey: CryptoKey, publicKey: Uint8Array);
|
|
20
|
+
/**
|
|
21
|
+
* Exports the keypair so that it can be stored in IndexedDB.
|
|
22
|
+
*/
|
|
23
|
+
export(): ExportedWebCryptoKeypair;
|
|
12
24
|
getPublicKey(): Secp256r1PublicKey;
|
|
13
25
|
sign(bytes: Uint8Array): Promise<Uint8Array>;
|
|
14
26
|
}
|
|
@@ -8,6 +8,7 @@ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "
|
|
|
8
8
|
var _publicKey;
|
|
9
9
|
import { Signer } from "@mysten/sui/cryptography";
|
|
10
10
|
import { Secp256r1PublicKey } from "@mysten/sui/keypairs/secp256r1";
|
|
11
|
+
import { secp256r1 } from "@noble/curves/p256";
|
|
11
12
|
function getCompressedPublicKey(publicKey) {
|
|
12
13
|
const rawBytes = new Uint8Array(publicKey);
|
|
13
14
|
const x = rawBytes.slice(1, 33);
|
|
@@ -23,7 +24,7 @@ const _WebCryptoSigner = class _WebCryptoSigner extends Signer {
|
|
|
23
24
|
super();
|
|
24
25
|
__privateAdd(this, _publicKey);
|
|
25
26
|
this.privateKey = privateKey;
|
|
26
|
-
__privateSet(this, _publicKey, new Secp256r1PublicKey(
|
|
27
|
+
__privateSet(this, _publicKey, new Secp256r1PublicKey(publicKey));
|
|
27
28
|
}
|
|
28
29
|
static async generate({ extractable = false } = {}) {
|
|
29
30
|
const keypair = await globalThis.crypto.subtle.generateKey(
|
|
@@ -35,16 +36,43 @@ const _WebCryptoSigner = class _WebCryptoSigner extends Signer {
|
|
|
35
36
|
["sign", "verify"]
|
|
36
37
|
);
|
|
37
38
|
const publicKey = await globalThis.crypto.subtle.exportKey("raw", keypair.publicKey);
|
|
38
|
-
return new _WebCryptoSigner(
|
|
39
|
+
return new _WebCryptoSigner(
|
|
40
|
+
keypair.privateKey,
|
|
41
|
+
getCompressedPublicKey(new Uint8Array(publicKey))
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Imports a keypair using the value returned by `export()`.
|
|
46
|
+
*/
|
|
47
|
+
static import(data) {
|
|
48
|
+
return new _WebCryptoSigner(data.privateKey, data.publicKey);
|
|
39
49
|
}
|
|
40
50
|
getKeyScheme() {
|
|
41
51
|
return "Secp256r1";
|
|
42
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Exports the keypair so that it can be stored in IndexedDB.
|
|
55
|
+
*/
|
|
56
|
+
export() {
|
|
57
|
+
const exportedKeypair = {
|
|
58
|
+
privateKey: this.privateKey,
|
|
59
|
+
publicKey: __privateGet(this, _publicKey).toRawBytes()
|
|
60
|
+
};
|
|
61
|
+
Object.defineProperty(exportedKeypair, "toJSON", {
|
|
62
|
+
enumerable: false,
|
|
63
|
+
value: () => {
|
|
64
|
+
throw new Error(
|
|
65
|
+
"The exported keypair must not be serialized. It must be stored in IndexedDB directly."
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
return exportedKeypair;
|
|
70
|
+
}
|
|
43
71
|
getPublicKey() {
|
|
44
72
|
return __privateGet(this, _publicKey);
|
|
45
73
|
}
|
|
46
74
|
async sign(bytes) {
|
|
47
|
-
const
|
|
75
|
+
const rawSignature = await globalThis.crypto.subtle.sign(
|
|
48
76
|
{
|
|
49
77
|
name: "ECDSA",
|
|
50
78
|
hash: "SHA-256"
|
|
@@ -52,7 +80,8 @@ const _WebCryptoSigner = class _WebCryptoSigner extends Signer {
|
|
|
52
80
|
this.privateKey,
|
|
53
81
|
bytes
|
|
54
82
|
);
|
|
55
|
-
|
|
83
|
+
const signature = secp256r1.Signature.fromCompact(new Uint8Array(rawSignature));
|
|
84
|
+
return signature.normalizeS().toCompactRawBytes();
|
|
56
85
|
}
|
|
57
86
|
};
|
|
58
87
|
_publicKey = new WeakMap();
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
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 '@mysten/sui/cryptography';\nimport { Signer } from '@mysten/sui/cryptography';\nimport { Secp256r1PublicKey } from '@mysten/sui/keypairs/secp256r1';\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 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(
|
|
5
|
-
"mappings": ";;;;;;;AAAA;AAIA,SAAS,cAAc;AACvB,SAAS,0BAA0B;
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { SignatureScheme } from '@mysten/sui/cryptography';\nimport { Signer } from '@mysten/sui/cryptography';\nimport { Secp256r1PublicKey } from '@mysten/sui/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;\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> {\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,\n\t\t);\n\n\t\tconst signature = secp256r1.Signature.fromCompact(new Uint8Array(rawSignature));\n\n\t\treturn signature.normalizeS().toCompactRawBytes();\n\t}\n}\n"],
|
|
5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