@mysten/signers 0.1.12 → 0.1.14
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 +20 -0
- package/dist/cjs/webcrypto/index.js +98 -0
- package/dist/cjs/webcrypto/index.js.map +7 -0
- package/dist/esm/webcrypto/index.d.ts +20 -0
- package/dist/esm/webcrypto/index.js +78 -0
- package/dist/esm/webcrypto/index.js.map +7 -0
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +7 -2
- package/src/webcrypto/index.ts +93 -0
- package/webcrypto/package.json +6 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mysten/signers",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.14",
|
|
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>",
|
|
@@ -13,6 +13,10 @@
|
|
|
13
13
|
"./gcp": {
|
|
14
14
|
"import": "./dist/esm/gcp/index.js",
|
|
15
15
|
"require": "./dist/cjs/gcp/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./webcrypto": {
|
|
18
|
+
"import": "./dist/esm/webcrypto/index.js",
|
|
19
|
+
"require": "./dist/cjs/webcrypto/index.js"
|
|
16
20
|
}
|
|
17
21
|
},
|
|
18
22
|
"sideEffects": false,
|
|
@@ -23,7 +27,8 @@
|
|
|
23
27
|
"aws",
|
|
24
28
|
"dist",
|
|
25
29
|
"gcp",
|
|
26
|
-
"src"
|
|
30
|
+
"src",
|
|
31
|
+
"webcrypto"
|
|
27
32
|
],
|
|
28
33
|
"repository": {
|
|
29
34
|
"type": "git",
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { SignatureScheme } from '@mysten/sui/cryptography';
|
|
5
|
+
import { Signer } from '@mysten/sui/cryptography';
|
|
6
|
+
import { Secp256r1PublicKey } from '@mysten/sui/keypairs/secp256r1';
|
|
7
|
+
import { secp256r1 } from '@noble/curves/p256';
|
|
8
|
+
|
|
9
|
+
// Convert from uncompressed (65 bytes) to compressed (33 bytes) format
|
|
10
|
+
function getCompressedPublicKey(publicKey: Uint8Array) {
|
|
11
|
+
const rawBytes = new Uint8Array(publicKey);
|
|
12
|
+
const x = rawBytes.slice(1, 33);
|
|
13
|
+
const y = rawBytes.slice(33, 65);
|
|
14
|
+
|
|
15
|
+
const prefix = (y[31] & 1) === 0 ? 0x02 : 0x03;
|
|
16
|
+
|
|
17
|
+
const compressed = new Uint8Array(Secp256r1PublicKey.SIZE);
|
|
18
|
+
compressed[0] = prefix;
|
|
19
|
+
compressed.set(x, 1);
|
|
20
|
+
|
|
21
|
+
return compressed;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface ExportedWebCryptoKeypair {
|
|
25
|
+
privateKey: CryptoKey;
|
|
26
|
+
publicKey: Uint8Array;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class WebCryptoSigner extends Signer {
|
|
30
|
+
privateKey: CryptoKey;
|
|
31
|
+
|
|
32
|
+
#publicKey: Secp256r1PublicKey;
|
|
33
|
+
|
|
34
|
+
static async generate({ extractable = false }: { extractable?: boolean } = {}) {
|
|
35
|
+
const keypair = await globalThis.crypto.subtle.generateKey(
|
|
36
|
+
{
|
|
37
|
+
name: 'ECDSA',
|
|
38
|
+
namedCurve: 'P-256',
|
|
39
|
+
},
|
|
40
|
+
extractable,
|
|
41
|
+
['sign', 'verify'],
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
const publicKey = await globalThis.crypto.subtle.exportKey('raw', keypair.publicKey);
|
|
45
|
+
|
|
46
|
+
return new WebCryptoSigner(
|
|
47
|
+
keypair.privateKey,
|
|
48
|
+
getCompressedPublicKey(new Uint8Array(publicKey)),
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
static import(data: ExportedWebCryptoKeypair) {
|
|
53
|
+
return new WebCryptoSigner(data.privateKey, data.publicKey);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
getKeyScheme(): SignatureScheme {
|
|
57
|
+
return 'Secp256r1';
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
constructor(privateKey: CryptoKey, publicKey: Uint8Array) {
|
|
61
|
+
super();
|
|
62
|
+
this.privateKey = privateKey;
|
|
63
|
+
this.#publicKey = new Secp256r1PublicKey(publicKey);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Exports the keypair to store in IndexedDB.
|
|
67
|
+
export(): ExportedWebCryptoKeypair {
|
|
68
|
+
// TODO: Should we add something like `toJSON` on this so that if you attempt to serialize it throws?
|
|
69
|
+
return {
|
|
70
|
+
privateKey: this.privateKey,
|
|
71
|
+
publicKey: this.#publicKey.toRawBytes(),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
getPublicKey() {
|
|
76
|
+
return this.#publicKey;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async sign(bytes: Uint8Array): Promise<Uint8Array> {
|
|
80
|
+
const rawSignature = await globalThis.crypto.subtle.sign(
|
|
81
|
+
{
|
|
82
|
+
name: 'ECDSA',
|
|
83
|
+
hash: 'SHA-256',
|
|
84
|
+
},
|
|
85
|
+
this.privateKey,
|
|
86
|
+
bytes,
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
const signature = secp256r1.Signature.fromCompact(new Uint8Array(rawSignature));
|
|
90
|
+
|
|
91
|
+
return signature.normalizeS().toCompactRawBytes();
|
|
92
|
+
}
|
|
93
|
+
}
|