@libp2p/crypto 5.1.4-6a3ae02f5 → 5.1.4-8e87be9e6
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/dist/index.min.js +1 -1
- package/dist/index.min.js.map +4 -4
- package/dist/src/keys/ed25519/ed25519.d.ts +2 -2
- package/dist/src/keys/ed25519/ed25519.d.ts.map +1 -1
- package/dist/src/keys/ed25519/ed25519.js +18 -2
- package/dist/src/keys/ed25519/ed25519.js.map +1 -1
- package/dist/src/keys/ed25519/index.browser.d.ts +2 -5
- package/dist/src/keys/ed25519/index.browser.d.ts.map +1 -1
- package/dist/src/keys/ed25519/index.browser.js +61 -5
- package/dist/src/keys/ed25519/index.browser.js.map +1 -1
- package/package.json +2 -2
- package/src/keys/ed25519/ed25519.ts +24 -4
- package/src/keys/ed25519/index.browser.ts +72 -5
|
@@ -10,7 +10,7 @@ export declare class Ed25519PublicKey implements Ed25519PublicKeyInterface {
|
|
|
10
10
|
toCID(): CID<unknown, 114, 0x0, 1>;
|
|
11
11
|
toString(): string;
|
|
12
12
|
equals(key?: any): boolean;
|
|
13
|
-
verify(data: Uint8Array | Uint8ArrayList, sig: Uint8Array, options?: AbortOptions): boolean
|
|
13
|
+
verify(data: Uint8Array | Uint8ArrayList, sig: Uint8Array, options?: AbortOptions): boolean | Promise<boolean>;
|
|
14
14
|
}
|
|
15
15
|
export declare class Ed25519PrivateKey implements Ed25519PrivateKeyInterface {
|
|
16
16
|
readonly type = "Ed25519";
|
|
@@ -18,6 +18,6 @@ export declare class Ed25519PrivateKey implements Ed25519PrivateKeyInterface {
|
|
|
18
18
|
readonly publicKey: Ed25519PublicKey;
|
|
19
19
|
constructor(key: Uint8Array, publicKey: Uint8Array);
|
|
20
20
|
equals(key?: any): boolean;
|
|
21
|
-
sign(message: Uint8Array | Uint8ArrayList, options?: AbortOptions): Uint8Array
|
|
21
|
+
sign(message: Uint8Array | Uint8ArrayList, options?: AbortOptions): Uint8Array | Promise<Uint8Array>;
|
|
22
22
|
}
|
|
23
23
|
//# sourceMappingURL=ed25519.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ed25519.d.ts","sourceRoot":"","sources":["../../../../src/keys/ed25519/ed25519.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;
|
|
1
|
+
{"version":3,"file":"ed25519.d.ts","sourceRoot":"","sources":["../../../../src/keys/ed25519/ed25519.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAOtC,OAAO,KAAK,EAAE,gBAAgB,IAAI,yBAAyB,EAAE,iBAAiB,IAAI,0BAA0B,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AACrJ,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAA;AACxD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEpD,qBAAa,gBAAiB,YAAW,yBAAyB;IAChE,SAAgB,IAAI,aAAY;IAChC,SAAgB,GAAG,EAAE,UAAU,CAAA;gBAElB,GAAG,EAAE,UAAU;IAI5B,WAAW,IAAK,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC;IAInC,KAAK,IAAK,GAAG,CAAC,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC;IAInC,QAAQ,IAAK,MAAM;IAInB,MAAM,CAAE,GAAG,CAAC,EAAE,GAAG,GAAG,OAAO;IAQ3B,MAAM,CAAE,IAAI,EAAE,UAAU,GAAG,cAAc,EAAE,GAAG,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;CAahH;AAED,qBAAa,iBAAkB,YAAW,0BAA0B;IAClE,SAAgB,IAAI,aAAY;IAChC,SAAgB,GAAG,EAAE,UAAU,CAAA;IAC/B,SAAgB,SAAS,EAAE,gBAAgB,CAAA;gBAI9B,GAAG,EAAE,UAAU,EAAE,SAAS,EAAE,UAAU;IAKnD,MAAM,CAAE,GAAG,CAAC,EAAE,GAAG,GAAG,OAAO;IAQ3B,IAAI,CAAE,OAAO,EAAE,UAAU,GAAG,cAAc,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;CActG"}
|
|
@@ -2,6 +2,7 @@ import { base58btc } from 'multiformats/bases/base58';
|
|
|
2
2
|
import { CID } from 'multiformats/cid';
|
|
3
3
|
import { identity } from 'multiformats/hashes/identity';
|
|
4
4
|
import { equals as uint8ArrayEquals } from 'uint8arrays/equals';
|
|
5
|
+
import { isPromise } from "../../util.js";
|
|
5
6
|
import { publicKeyToProtobuf } from '../index.js';
|
|
6
7
|
import { ensureEd25519Key } from './utils.js';
|
|
7
8
|
import * as crypto from './index.js';
|
|
@@ -28,7 +29,14 @@ export class Ed25519PublicKey {
|
|
|
28
29
|
}
|
|
29
30
|
verify(data, sig, options) {
|
|
30
31
|
options?.signal?.throwIfAborted();
|
|
31
|
-
|
|
32
|
+
const result = crypto.hashAndVerify(this.raw, sig, data);
|
|
33
|
+
if (isPromise(result)) {
|
|
34
|
+
return result.then(res => {
|
|
35
|
+
options?.signal?.throwIfAborted();
|
|
36
|
+
return res;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
return result;
|
|
32
40
|
}
|
|
33
41
|
}
|
|
34
42
|
export class Ed25519PrivateKey {
|
|
@@ -49,7 +57,15 @@ export class Ed25519PrivateKey {
|
|
|
49
57
|
}
|
|
50
58
|
sign(message, options) {
|
|
51
59
|
options?.signal?.throwIfAborted();
|
|
52
|
-
|
|
60
|
+
const sig = crypto.hashAndSign(this.raw, message);
|
|
61
|
+
if (isPromise(sig)) {
|
|
62
|
+
return sig.then(res => {
|
|
63
|
+
options?.signal?.throwIfAborted();
|
|
64
|
+
return res;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
options?.signal?.throwIfAborted();
|
|
68
|
+
return sig;
|
|
53
69
|
}
|
|
54
70
|
}
|
|
55
71
|
//# sourceMappingURL=ed25519.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ed25519.js","sourceRoot":"","sources":["../../../../src/keys/ed25519/ed25519.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAA;AACrD,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AACtC,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAA;AACvD,OAAO,EAAE,MAAM,IAAI,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAC/D,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAA;AACjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAC7C,OAAO,KAAK,MAAM,MAAM,YAAY,CAAA;AAKpC,MAAM,OAAO,gBAAgB;IACX,IAAI,GAAG,SAAS,CAAA;IAChB,GAAG,CAAY;IAE/B,YAAa,GAAe;QAC1B,IAAI,CAAC,GAAG,GAAG,gBAAgB,CAAC,GAAG,EAAE,MAAM,CAAC,eAAe,CAAC,CAAA;IAC1D,CAAC;IAED,WAAW;QACT,OAAO,QAAQ,CAAC,MAAM,CAAC,mBAAmB,CAAC,IAAI,CAAC,CAAC,CAAA;IACnD,CAAC;IAED,KAAK;QACH,OAAO,GAAG,CAAC,QAAQ,CAAC,GAAG,EAAE,IAAI,CAAC,WAAW,EAAE,CAAC,CAAA;IAC9C,CAAC;IAED,QAAQ;QACN,OAAO,SAAS,CAAC,MAAM,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,KAAK,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAA;IAChE,CAAC;IAED,MAAM,CAAE,GAAS;QACf,IAAI,GAAG,IAAI,IAAI,IAAI,CAAC,CAAC,GAAG,CAAC,GAAG,YAAY,UAAU,CAAC,EAAE,CAAC;YACpD,OA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I,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,CAAC,CAAA;IAC5C,CAAC;IAED,IAAI,CAAE,OAAoC,EAAE,OAAsB;QAChE,OAAO,EAAE,MAAM,EAAE,cAAc,EAAE,CAAA;QACjC,MAAM,GAAG,GAAG,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;QAEjD,IAAI,SAAS,CAAa,GAAG,CAAC,EAAE,CAAC;YAC/B,OAAO,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;gBACpB,OAAO,EAAE,MAAM,EAAE,cAAc,EAAE,CAAA;gBACjC,OAAO,GAAG,CAAA;YACZ,CAAC,CAAC,CAAA;QACJ,CAAC;QAED,OAAO,EAAE,MAAM,EAAE,cAAc,EAAE,CAAA;QACjC,OAAO,GAAG,CAAA;IACZ,CAAC;CACF"}
|
|
@@ -5,10 +5,7 @@ declare const PRIVATE_KEY_BYTE_LENGTH = 64;
|
|
|
5
5
|
export { PUBLIC_KEY_BYTE_LENGTH as publicKeyLength };
|
|
6
6
|
export { PRIVATE_KEY_BYTE_LENGTH as privateKeyLength };
|
|
7
7
|
export declare function generateKey(): Uint8ArrayKeyPair;
|
|
8
|
-
/**
|
|
9
|
-
* Generate keypair from a 32 byte uint8array
|
|
10
|
-
*/
|
|
11
8
|
export declare function generateKeyFromSeed(seed: Uint8Array): Uint8ArrayKeyPair;
|
|
12
|
-
export declare function hashAndSign(privateKey: Uint8Array, msg: Uint8Array | Uint8ArrayList): Uint8Array
|
|
13
|
-
export declare function hashAndVerify(publicKey: Uint8Array, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): boolean
|
|
9
|
+
export declare function hashAndSign(privateKey: Uint8Array, msg: Uint8Array | Uint8ArrayList): Promise<Uint8Array>;
|
|
10
|
+
export declare function hashAndVerify(publicKey: Uint8Array, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): Promise<boolean>;
|
|
14
11
|
//# sourceMappingURL=index.browser.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.browser.d.ts","sourceRoot":"","sources":["../../../../src/keys/ed25519/index.browser.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.browser.d.ts","sourceRoot":"","sources":["../../../../src/keys/ed25519/index.browser.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAA;AACxD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEpD,QAAA,MAAM,sBAAsB,KAAK,CAAA;AACjC,QAAA,MAAM,uBAAuB,KAAK,CAAA;AAGlC,OAAO,EAAE,sBAAsB,IAAI,eAAe,EAAE,CAAA;AACpD,OAAO,EAAE,uBAAuB,IAAI,gBAAgB,EAAE,CAAA;AAatD,wBAAgB,WAAW,IAAK,iBAAiB,CAYhD;AAED,wBAAgB,mBAAmB,CAAE,IAAI,EAAE,UAAU,GAAG,iBAAiB,CAiBxE;AA+BD,wBAAsB,WAAW,CAAE,UAAU,EAAE,UAAU,EAAE,GAAG,EAAE,UAAU,GAAG,cAAc,GAAG,OAAO,CAAC,UAAU,CAAC,CAUhH;AAgBD,wBAAsB,aAAa,CAAE,SAAS,EAAE,UAAU,EAAE,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,UAAU,GAAG,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC,CAU/H"}
|
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
import { ed25519 as ed } from '@noble/curves/ed25519';
|
|
2
|
+
import { toString as uint8arrayToString } from 'uint8arrays/to-string';
|
|
3
|
+
import crypto from '../../webcrypto/index.js';
|
|
2
4
|
const PUBLIC_KEY_BYTE_LENGTH = 32;
|
|
3
5
|
const PRIVATE_KEY_BYTE_LENGTH = 64; // private key is actually 32 bytes but for historical reasons we concat private and public keys
|
|
4
6
|
const KEYS_BYTE_LENGTH = 32;
|
|
5
7
|
export { PUBLIC_KEY_BYTE_LENGTH as publicKeyLength };
|
|
6
8
|
export { PRIVATE_KEY_BYTE_LENGTH as privateKeyLength };
|
|
9
|
+
// memoize support result to skip additional awaits every time we use an ed key
|
|
10
|
+
let ed25519Supported;
|
|
11
|
+
const webCryptoEd25519SupportedPromise = (async () => {
|
|
12
|
+
try {
|
|
13
|
+
await crypto.get().subtle.generateKey({ name: 'Ed25519' }, true, ['sign', 'verify']);
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
})();
|
|
7
20
|
export function generateKey() {
|
|
8
21
|
// the actual private key (32 bytes)
|
|
9
22
|
const privateKeyRaw = ed.utils.randomPrivateKey();
|
|
@@ -15,9 +28,6 @@ export function generateKey() {
|
|
|
15
28
|
publicKey
|
|
16
29
|
};
|
|
17
30
|
}
|
|
18
|
-
/**
|
|
19
|
-
* Generate keypair from a 32 byte uint8array
|
|
20
|
-
*/
|
|
21
31
|
export function generateKeyFromSeed(seed) {
|
|
22
32
|
if (seed.length !== KEYS_BYTE_LENGTH) {
|
|
23
33
|
throw new TypeError('"seed" must be 32 bytes in length.');
|
|
@@ -34,13 +44,59 @@ export function generateKeyFromSeed(seed) {
|
|
|
34
44
|
publicKey
|
|
35
45
|
};
|
|
36
46
|
}
|
|
37
|
-
|
|
47
|
+
async function hashAndSignWebCrypto(privateKey, msg) {
|
|
48
|
+
let privateKeyRaw;
|
|
49
|
+
if (privateKey.length === PRIVATE_KEY_BYTE_LENGTH) {
|
|
50
|
+
privateKeyRaw = privateKey.subarray(0, 32);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
privateKeyRaw = privateKey;
|
|
54
|
+
}
|
|
55
|
+
const jwk = {
|
|
56
|
+
crv: 'Ed25519',
|
|
57
|
+
kty: 'OKP',
|
|
58
|
+
x: uint8arrayToString(privateKey.subarray(32), 'base64url'),
|
|
59
|
+
d: uint8arrayToString(privateKeyRaw, 'base64url'),
|
|
60
|
+
ext: true,
|
|
61
|
+
key_ops: ['sign']
|
|
62
|
+
};
|
|
63
|
+
const key = await crypto.get().subtle.importKey('jwk', jwk, { name: 'Ed25519' }, true, ['sign']);
|
|
64
|
+
const sig = await crypto.get().subtle.sign({ name: 'Ed25519' }, key, msg instanceof Uint8Array ? msg : msg.subarray());
|
|
65
|
+
return new Uint8Array(sig, 0, sig.byteLength);
|
|
66
|
+
}
|
|
67
|
+
function hashAndSignNoble(privateKey, msg) {
|
|
38
68
|
const privateKeyRaw = privateKey.subarray(0, KEYS_BYTE_LENGTH);
|
|
39
69
|
return ed.sign(msg instanceof Uint8Array ? msg : msg.subarray(), privateKeyRaw);
|
|
40
70
|
}
|
|
41
|
-
export function
|
|
71
|
+
export async function hashAndSign(privateKey, msg) {
|
|
72
|
+
if (ed25519Supported == null) {
|
|
73
|
+
ed25519Supported = await webCryptoEd25519SupportedPromise;
|
|
74
|
+
}
|
|
75
|
+
if (ed25519Supported) {
|
|
76
|
+
return hashAndSignWebCrypto(privateKey, msg);
|
|
77
|
+
}
|
|
78
|
+
return hashAndSignNoble(privateKey, msg);
|
|
79
|
+
}
|
|
80
|
+
async function hashAndVerifyWebCrypto(publicKey, sig, msg) {
|
|
81
|
+
if (publicKey.buffer instanceof ArrayBuffer) {
|
|
82
|
+
const key = await crypto.get().subtle.importKey('raw', publicKey.buffer, { name: 'Ed25519' }, false, ['verify']);
|
|
83
|
+
const isValid = await crypto.get().subtle.verify({ name: 'Ed25519' }, key, sig, msg instanceof Uint8Array ? msg : msg.subarray());
|
|
84
|
+
return isValid;
|
|
85
|
+
}
|
|
86
|
+
throw new TypeError('WebCrypto does not support SharedArrayBuffer for Ed25519 keys');
|
|
87
|
+
}
|
|
88
|
+
function hashAndVerifyNoble(publicKey, sig, msg) {
|
|
42
89
|
return ed.verify(sig, msg instanceof Uint8Array ? msg : msg.subarray(), publicKey);
|
|
43
90
|
}
|
|
91
|
+
export async function hashAndVerify(publicKey, sig, msg) {
|
|
92
|
+
if (ed25519Supported == null) {
|
|
93
|
+
ed25519Supported = await webCryptoEd25519SupportedPromise;
|
|
94
|
+
}
|
|
95
|
+
if (ed25519Supported) {
|
|
96
|
+
return hashAndVerifyWebCrypto(publicKey, sig, msg);
|
|
97
|
+
}
|
|
98
|
+
return hashAndVerifyNoble(publicKey, sig, msg);
|
|
99
|
+
}
|
|
44
100
|
function concatKeys(privateKeyRaw, publicKey) {
|
|
45
101
|
const privateKey = new Uint8Array(PRIVATE_KEY_BYTE_LENGTH);
|
|
46
102
|
for (let i = 0; i < KEYS_BYTE_LENGTH; i++) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.browser.js","sourceRoot":"","sources":["../../../../src/keys/ed25519/index.browser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,EAAE,EAAE,MAAM,uBAAuB,CAAA;
|
|
1
|
+
{"version":3,"file":"index.browser.js","sourceRoot":"","sources":["../../../../src/keys/ed25519/index.browser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,EAAE,EAAE,MAAM,uBAAuB,CAAA;AACrD,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AACtE,OAAO,MAAM,MAAM,0BAA0B,CAAA;AAI7C,MAAM,sBAAsB,GAAG,EAAE,CAAA;AACjC,MAAM,uBAAuB,GAAG,EAAE,CAAA,CAAC,gGAAgG;AACnI,MAAM,gBAAgB,GAAG,EAAE,CAAA;AAE3B,OAAO,EAAE,sBAAsB,IAAI,eAAe,EAAE,CAAA;AACpD,OAAO,EAAE,uBAAuB,IAAI,gBAAgB,EAAE,CAAA;AAEtD,+EAA+E;AAC/E,IAAI,gBAAqC,CAAA;AACzC,MAAM,gCAAgC,GAAG,CAAC,KAAK,IAAI,EAAE;IACnD,IAAI,CAAC;QACH,MAAM,MAAM,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,WAAW,CAAC,EAAE,IAAI,EAAE,SAAS,EAA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IAAI,EAAE,CAAC;QAC7B,gBAAgB,GAAG,MAAM,gCAAgC,CAAA;IAC3D,CAAC;IAED,IAAI,gBAAgB,EAAE,CAAC;QACrB,OAAO,sBAAsB,CAAC,SAAS,EAAE,GAAG,EAAE,GAAG,CAAC,CAAA;IACpD,CAAC;IAED,OAAO,kBAAkB,CAAC,SAAS,EAAE,GAAG,EAAE,GAAG,CAAC,CAAA;AAChD,CAAC;AAED,SAAS,UAAU,CAAE,aAAyB,EAAE,SAAqB;IACnE,MAAM,UAAU,GAAG,IAAI,UAAU,CAAC,uBAAuB,CAAC,CAAA;IAC1D,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,gBAAgB,EAAE,CAAC,EAAE,EAAE,CAAC;QAC1C,UAAU,CAAC,CAAC,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC,CAAA;QAChC,UAAU,CAAC,gBAAgB,GAAG,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAAA;IACjD,CAAC;IACD,OAAO,UAAU,CAAA;AACnB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/crypto",
|
|
3
|
-
"version": "5.1.4-
|
|
3
|
+
"version": "5.1.4-8e87be9e6",
|
|
4
4
|
"description": "Crypto primitives for libp2p",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/crypto#readme",
|
|
@@ -85,7 +85,7 @@
|
|
|
85
85
|
"generate": "protons ./src/keys/keys.proto"
|
|
86
86
|
},
|
|
87
87
|
"dependencies": {
|
|
88
|
-
"@libp2p/interface": "2.10.2-
|
|
88
|
+
"@libp2p/interface": "2.10.2-8e87be9e6",
|
|
89
89
|
"@noble/curves": "^1.9.1",
|
|
90
90
|
"@noble/hashes": "^1.8.0",
|
|
91
91
|
"multiformats": "^13.3.6",
|
|
@@ -2,6 +2,7 @@ import { base58btc } from 'multiformats/bases/base58'
|
|
|
2
2
|
import { CID } from 'multiformats/cid'
|
|
3
3
|
import { identity } from 'multiformats/hashes/identity'
|
|
4
4
|
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
5
|
+
import { isPromise } from '../../util.ts'
|
|
5
6
|
import { publicKeyToProtobuf } from '../index.js'
|
|
6
7
|
import { ensureEd25519Key } from './utils.js'
|
|
7
8
|
import * as crypto from './index.js'
|
|
@@ -37,9 +38,18 @@ export class Ed25519PublicKey implements Ed25519PublicKeyInterface {
|
|
|
37
38
|
return uint8ArrayEquals(this.raw, key.raw)
|
|
38
39
|
}
|
|
39
40
|
|
|
40
|
-
verify (data: Uint8Array | Uint8ArrayList, sig: Uint8Array, options?: AbortOptions): boolean {
|
|
41
|
+
verify (data: Uint8Array | Uint8ArrayList, sig: Uint8Array, options?: AbortOptions): boolean | Promise<boolean> {
|
|
41
42
|
options?.signal?.throwIfAborted()
|
|
42
|
-
|
|
43
|
+
const result = crypto.hashAndVerify(this.raw, sig, data)
|
|
44
|
+
|
|
45
|
+
if (isPromise<boolean>(result)) {
|
|
46
|
+
return result.then(res => {
|
|
47
|
+
options?.signal?.throwIfAborted()
|
|
48
|
+
return res
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return result
|
|
43
53
|
}
|
|
44
54
|
}
|
|
45
55
|
|
|
@@ -63,8 +73,18 @@ export class Ed25519PrivateKey implements Ed25519PrivateKeyInterface {
|
|
|
63
73
|
return uint8ArrayEquals(this.raw, key.raw)
|
|
64
74
|
}
|
|
65
75
|
|
|
66
|
-
sign (message: Uint8Array | Uint8ArrayList, options?: AbortOptions): Uint8Array {
|
|
76
|
+
sign (message: Uint8Array | Uint8ArrayList, options?: AbortOptions): Uint8Array | Promise<Uint8Array> {
|
|
77
|
+
options?.signal?.throwIfAborted()
|
|
78
|
+
const sig = crypto.hashAndSign(this.raw, message)
|
|
79
|
+
|
|
80
|
+
if (isPromise<Uint8Array>(sig)) {
|
|
81
|
+
return sig.then(res => {
|
|
82
|
+
options?.signal?.throwIfAborted()
|
|
83
|
+
return res
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
|
|
67
87
|
options?.signal?.throwIfAborted()
|
|
68
|
-
return
|
|
88
|
+
return sig
|
|
69
89
|
}
|
|
70
90
|
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { ed25519 as ed } from '@noble/curves/ed25519'
|
|
2
|
+
import { toString as uint8arrayToString } from 'uint8arrays/to-string'
|
|
3
|
+
import crypto from '../../webcrypto/index.js'
|
|
2
4
|
import type { Uint8ArrayKeyPair } from '../interface.js'
|
|
3
5
|
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
4
6
|
|
|
@@ -9,6 +11,17 @@ const KEYS_BYTE_LENGTH = 32
|
|
|
9
11
|
export { PUBLIC_KEY_BYTE_LENGTH as publicKeyLength }
|
|
10
12
|
export { PRIVATE_KEY_BYTE_LENGTH as privateKeyLength }
|
|
11
13
|
|
|
14
|
+
// memoize support result to skip additional awaits every time we use an ed key
|
|
15
|
+
let ed25519Supported: boolean | undefined
|
|
16
|
+
const webCryptoEd25519SupportedPromise = (async () => {
|
|
17
|
+
try {
|
|
18
|
+
await crypto.get().subtle.generateKey({ name: 'Ed25519' }, true, ['sign', 'verify'])
|
|
19
|
+
return true
|
|
20
|
+
} catch {
|
|
21
|
+
return false
|
|
22
|
+
}
|
|
23
|
+
})()
|
|
24
|
+
|
|
12
25
|
export function generateKey (): Uint8ArrayKeyPair {
|
|
13
26
|
// the actual private key (32 bytes)
|
|
14
27
|
const privateKeyRaw = ed.utils.randomPrivateKey()
|
|
@@ -23,9 +36,6 @@ export function generateKey (): Uint8ArrayKeyPair {
|
|
|
23
36
|
}
|
|
24
37
|
}
|
|
25
38
|
|
|
26
|
-
/**
|
|
27
|
-
* Generate keypair from a 32 byte uint8array
|
|
28
|
-
*/
|
|
29
39
|
export function generateKeyFromSeed (seed: Uint8Array): Uint8ArrayKeyPair {
|
|
30
40
|
if (seed.length !== KEYS_BYTE_LENGTH) {
|
|
31
41
|
throw new TypeError('"seed" must be 32 bytes in length.')
|
|
@@ -45,16 +55,73 @@ export function generateKeyFromSeed (seed: Uint8Array): Uint8ArrayKeyPair {
|
|
|
45
55
|
}
|
|
46
56
|
}
|
|
47
57
|
|
|
48
|
-
|
|
58
|
+
async function hashAndSignWebCrypto (privateKey: Uint8Array, msg: Uint8Array | Uint8ArrayList): Promise<Uint8Array> {
|
|
59
|
+
let privateKeyRaw: Uint8Array
|
|
60
|
+
if (privateKey.length === PRIVATE_KEY_BYTE_LENGTH) {
|
|
61
|
+
privateKeyRaw = privateKey.subarray(0, 32)
|
|
62
|
+
} else {
|
|
63
|
+
privateKeyRaw = privateKey
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const jwk: JsonWebKey = {
|
|
67
|
+
crv: 'Ed25519',
|
|
68
|
+
kty: 'OKP',
|
|
69
|
+
x: uint8arrayToString(privateKey.subarray(32), 'base64url'),
|
|
70
|
+
d: uint8arrayToString(privateKeyRaw, 'base64url'),
|
|
71
|
+
ext: true,
|
|
72
|
+
key_ops: ['sign']
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const key = await crypto.get().subtle.importKey('jwk', jwk, { name: 'Ed25519' }, true, ['sign'])
|
|
76
|
+
const sig = await crypto.get().subtle.sign({ name: 'Ed25519' }, key, msg instanceof Uint8Array ? msg : msg.subarray())
|
|
77
|
+
|
|
78
|
+
return new Uint8Array(sig, 0, sig.byteLength)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function hashAndSignNoble (privateKey: Uint8Array, msg: Uint8Array | Uint8ArrayList): Uint8Array {
|
|
49
82
|
const privateKeyRaw = privateKey.subarray(0, KEYS_BYTE_LENGTH)
|
|
50
83
|
|
|
51
84
|
return ed.sign(msg instanceof Uint8Array ? msg : msg.subarray(), privateKeyRaw)
|
|
52
85
|
}
|
|
53
86
|
|
|
54
|
-
export function
|
|
87
|
+
export async function hashAndSign (privateKey: Uint8Array, msg: Uint8Array | Uint8ArrayList): Promise<Uint8Array> {
|
|
88
|
+
if (ed25519Supported == null) {
|
|
89
|
+
ed25519Supported = await webCryptoEd25519SupportedPromise
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (ed25519Supported) {
|
|
93
|
+
return hashAndSignWebCrypto(privateKey, msg)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return hashAndSignNoble(privateKey, msg)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function hashAndVerifyWebCrypto (publicKey: Uint8Array, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): Promise<boolean> {
|
|
100
|
+
if (publicKey.buffer instanceof ArrayBuffer) {
|
|
101
|
+
const key = await crypto.get().subtle.importKey('raw', publicKey.buffer, { name: 'Ed25519' }, false, ['verify'])
|
|
102
|
+
const isValid = await crypto.get().subtle.verify({ name: 'Ed25519' }, key, sig, msg instanceof Uint8Array ? msg : msg.subarray())
|
|
103
|
+
return isValid
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
throw new TypeError('WebCrypto does not support SharedArrayBuffer for Ed25519 keys')
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function hashAndVerifyNoble (publicKey: Uint8Array, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): boolean {
|
|
55
110
|
return ed.verify(sig, msg instanceof Uint8Array ? msg : msg.subarray(), publicKey)
|
|
56
111
|
}
|
|
57
112
|
|
|
113
|
+
export async function hashAndVerify (publicKey: Uint8Array, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): Promise<boolean> {
|
|
114
|
+
if (ed25519Supported == null) {
|
|
115
|
+
ed25519Supported = await webCryptoEd25519SupportedPromise
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (ed25519Supported) {
|
|
119
|
+
return hashAndVerifyWebCrypto(publicKey, sig, msg)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return hashAndVerifyNoble(publicKey, sig, msg)
|
|
123
|
+
}
|
|
124
|
+
|
|
58
125
|
function concatKeys (privateKeyRaw: Uint8Array, publicKey: Uint8Array): Uint8Array {
|
|
59
126
|
const privateKey = new Uint8Array(PRIVATE_KEY_BYTE_LENGTH)
|
|
60
127
|
for (let i = 0; i < KEYS_BYTE_LENGTH; i++) {
|