@prosopo/keyring 2.9.60 → 2.9.62
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/.turbo/turbo-build$colon$cjs.log +21 -19
- package/.turbo/turbo-build$colon$tsc.log +11 -11
- package/.turbo/turbo-build.log +23 -20
- package/CHANGELOG.md +18 -0
- package/dist/_virtual/_rolldown/runtime.js +3 -0
- package/dist/accounts/getPair.js +28 -38
- package/dist/accounts/getPair.test.d.ts +2 -0
- package/dist/accounts/getPair.test.d.ts.map +1 -0
- package/dist/accounts/getPair.test.js +86 -0
- package/dist/accounts/getPair.test.js.map +1 -0
- package/dist/accounts/index.js +1 -7
- package/dist/accounts/mnemonic.js +17 -13
- package/dist/accounts/mnemonic.test.d.ts +2 -0
- package/dist/accounts/mnemonic.test.d.ts.map +1 -0
- package/dist/accounts/mnemonic.test.js +64 -0
- package/dist/accounts/mnemonic.test.js.map +1 -0
- package/dist/accounts/testAccounts.js +36 -42
- package/dist/accounts/testAccounts.test.d.ts +2 -0
- package/dist/accounts/testAccounts.test.d.ts.map +1 -0
- package/dist/accounts/testAccounts.test.js +66 -0
- package/dist/accounts/testAccounts.test.js.map +1 -0
- package/dist/cjs/accounts/getPair.cjs +30 -40
- package/dist/cjs/accounts/index.cjs +8 -10
- package/dist/cjs/accounts/mnemonic.cjs +19 -14
- package/dist/cjs/accounts/testAccounts.cjs +36 -41
- package/dist/cjs/index.cjs +15 -18
- package/dist/cjs/keyring/index.cjs +5 -7
- package/dist/cjs/keyring/keyring.cjs +257 -266
- package/dist/cjs/keyring/pairs.cjs +23 -27
- package/dist/cjs/pair/decode.cjs +31 -29
- package/dist/cjs/pair/defaults.cjs +28 -28
- package/dist/cjs/pair/encode.cjs +14 -15
- package/dist/cjs/pair/index.cjs +191 -167
- package/dist/cjs/pair/toJson.cjs +8 -11
- package/dist/index.js +4 -15
- package/dist/keyring/index.js +2 -6
- package/dist/keyring/keyring.js +254 -265
- package/dist/keyring/keyring.test.d.ts +2 -0
- package/dist/keyring/keyring.test.d.ts.map +1 -0
- package/dist/keyring/keyring.test.js +239 -0
- package/dist/keyring/keyring.test.js.map +1 -0
- package/dist/keyring/pairs.js +22 -26
- package/dist/keyring/pairs.test.d.ts +2 -0
- package/dist/keyring/pairs.test.d.ts.map +1 -0
- package/dist/keyring/pairs.test.js +84 -0
- package/dist/keyring/pairs.test.js.map +1 -0
- package/dist/keyring/testing.test.d.ts +2 -0
- package/dist/keyring/testing.test.d.ts.map +1 -0
- package/dist/keyring/testing.test.js +97 -0
- package/dist/keyring/testing.test.js.map +1 -0
- package/dist/keyring.test-d.d.ts +2 -0
- package/dist/keyring.test-d.d.ts.map +1 -0
- package/dist/keyring.test-d.js +66 -0
- package/dist/keyring.test-d.js.map +1 -0
- package/dist/pair/decode.js +29 -29
- package/dist/pair/decode.spec.js +1 -1
- package/dist/pair/decode.spec.js.map +1 -1
- package/dist/pair/defaults.js +29 -30
- package/dist/pair/encode.js +14 -15
- package/dist/pair/encode.spec.js +1 -1
- package/dist/pair/encode.spec.js.map +1 -1
- package/dist/pair/index.js +188 -165
- package/dist/pair/nobody.test.d.ts +2 -0
- package/dist/pair/nobody.test.d.ts.map +1 -0
- package/dist/pair/nobody.test.js +77 -0
- package/dist/pair/nobody.test.js.map +1 -0
- package/dist/pair/toJson.js +8 -11
- package/dist/pair/toJson.spec.js +1 -1
- package/dist/pair/toJson.spec.js.map +1 -1
- package/package.json +8 -8
- package/src/accounts/getPair.test.ts +142 -0
- package/src/accounts/mnemonic.test.ts +103 -0
- package/src/accounts/testAccounts.test.ts +100 -0
- package/src/keyring/keyring.test.ts +373 -0
- package/src/keyring/pairs.test.ts +133 -0
- package/src/keyring/testing.test.ts +146 -0
- package/src/keyring.test-d.ts +134 -0
- package/src/pair/decode.spec.ts +3 -1
- package/src/pair/encode.spec.ts +9 -3
- package/src/pair/nobody.test.ts +127 -0
- package/src/pair/toJson.spec.ts +18 -12
- package/tsconfig.tsbuildinfo +1 -1
package/dist/pair/decode.js
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PAIR_DIV, PAIR_HDR } from "./defaults.js";
|
|
2
2
|
import { jsonDecryptData } from "@prosopo/util-crypto";
|
|
3
|
-
import {
|
|
4
|
-
|
|
3
|
+
import { u8aEq } from "@polkadot/util";
|
|
4
|
+
//#region src/pair/decode.ts
|
|
5
|
+
var SEED_OFFSET = PAIR_HDR.length;
|
|
6
|
+
/**
|
|
7
|
+
* Decode a pair, taking into account the generation-specific formats and headers
|
|
8
|
+
*
|
|
9
|
+
* For divisor/headers, don't rely on the magic being static. These will
|
|
10
|
+
* change between generations, aka with the long-awaited 4th generation
|
|
11
|
+
* of the format. The external decode interface is the only way to use and decode these.
|
|
12
|
+
**/
|
|
5
13
|
function decodePair(passphrase, encrypted, _encType) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
const pubOffset = divOffset + PAIR_DIV.length;
|
|
24
|
-
const publicKey = decrypted.subarray(pubOffset, pubOffset + PUB_LENGTH);
|
|
25
|
-
return {
|
|
26
|
-
publicKey,
|
|
27
|
-
secretKey
|
|
28
|
-
};
|
|
14
|
+
const decrypted = jsonDecryptData(encrypted, passphrase, Array.isArray(_encType) || _encType === void 0 ? _encType : [_encType]);
|
|
15
|
+
if (!u8aEq(decrypted.subarray(0, PAIR_HDR.length), PAIR_HDR)) throw new Error("Invalid encoding header found in body");
|
|
16
|
+
let secretKey = decrypted.subarray(SEED_OFFSET, SEED_OFFSET + 64);
|
|
17
|
+
let divOffset = SEED_OFFSET + 64;
|
|
18
|
+
let divider = decrypted.subarray(divOffset, divOffset + PAIR_DIV.length);
|
|
19
|
+
if (!u8aEq(divider, PAIR_DIV)) {
|
|
20
|
+
divOffset = SEED_OFFSET + 32;
|
|
21
|
+
secretKey = decrypted.subarray(SEED_OFFSET, divOffset);
|
|
22
|
+
divider = decrypted.subarray(divOffset, divOffset + PAIR_DIV.length);
|
|
23
|
+
if (!u8aEq(divider, PAIR_DIV)) throw new Error("Invalid encoding divider found in body");
|
|
24
|
+
}
|
|
25
|
+
const pubOffset = divOffset + PAIR_DIV.length;
|
|
26
|
+
return {
|
|
27
|
+
publicKey: decrypted.subarray(pubOffset, pubOffset + 32),
|
|
28
|
+
secretKey
|
|
29
|
+
};
|
|
29
30
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
};
|
|
31
|
+
//#endregion
|
|
32
|
+
export { decodePair };
|
package/dist/pair/decode.spec.js
CHANGED
|
@@ -5,7 +5,7 @@ describe("decode", () => {
|
|
|
5
5
|
it("fails when no data provided", () => {
|
|
6
6
|
expect(() => keyring.alice.decodePkcs8()).toThrow(/(No encrypted data available|Password required)/);
|
|
7
7
|
});
|
|
8
|
-
it("returns correct publicKey from encoded", () => {
|
|
8
|
+
it("returns correct publicKey from encoded", { timeout: 60000 }, () => {
|
|
9
9
|
const PASS = "testing";
|
|
10
10
|
expect(() => keyring.alice.decodePkcs8(PASS, keyring.alice.encodePkcs8(PASS))).not.toThrow();
|
|
11
11
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decode.spec.js","sourceRoot":"","sources":["../../src/pair/decode.spec.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAE7D,MAAM,OAAO,GAAG,eAAe,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,KAAK,CAAC,CAAC;AAE5D,QAAQ,CAAC,QAAQ,EAAE,GAAS,EAAE;IAC7B,EAAE,CAAC,6BA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
|
-
3,
|
|
12
|
-
43,
|
|
13
|
-
101,
|
|
14
|
-
112,
|
|
15
|
-
4,
|
|
16
|
-
34,
|
|
17
|
-
4,
|
|
18
|
-
32
|
|
1
|
+
//#region src/pair/defaults.ts
|
|
2
|
+
/** public/secret section divider (generation 1-3, will change in 4, don't rely on value) */
|
|
3
|
+
var PAIR_DIV = new Uint8Array([
|
|
4
|
+
161,
|
|
5
|
+
35,
|
|
6
|
+
3,
|
|
7
|
+
33,
|
|
8
|
+
0
|
|
19
9
|
]);
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
10
|
+
/** public/secret start block (generation 1-3, will change in 4, don't rely on value) */
|
|
11
|
+
var PAIR_HDR = new Uint8Array([
|
|
12
|
+
48,
|
|
13
|
+
83,
|
|
14
|
+
2,
|
|
15
|
+
1,
|
|
16
|
+
1,
|
|
17
|
+
48,
|
|
18
|
+
5,
|
|
19
|
+
6,
|
|
20
|
+
3,
|
|
21
|
+
43,
|
|
22
|
+
101,
|
|
23
|
+
112,
|
|
24
|
+
4,
|
|
25
|
+
34,
|
|
26
|
+
4,
|
|
27
|
+
32
|
|
28
|
+
]);
|
|
29
|
+
//#endregion
|
|
30
|
+
export { PAIR_DIV, PAIR_HDR };
|
package/dist/pair/encode.js
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
|
+
import { PAIR_DIV, PAIR_HDR } from "./defaults.js";
|
|
2
|
+
import { naclEncrypt, scryptEncode, scryptToU8a } from "@prosopo/util-crypto";
|
|
1
3
|
import { u8aConcat } from "@polkadot/util";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
+
//#region src/pair/encode.ts
|
|
5
|
+
/**
|
|
6
|
+
* Encode a pair with the latest generation format (generation 3)
|
|
7
|
+
**/
|
|
4
8
|
function encodePair({ publicKey, secretKey }, passphrase) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
const { params, password, salt } = scryptEncode(passphrase);
|
|
13
|
-
const { encrypted, nonce } = naclEncrypt(encoded, password.subarray(0, 32));
|
|
14
|
-
return u8aConcat(scryptToU8a(salt, params), nonce, encrypted);
|
|
9
|
+
if (!secretKey) throw new Error("Expected a valid secretKey to be passed to encode");
|
|
10
|
+
const encoded = u8aConcat(PAIR_HDR, secretKey, PAIR_DIV, publicKey);
|
|
11
|
+
if (!passphrase) return encoded;
|
|
12
|
+
const { params, password, salt } = scryptEncode(passphrase);
|
|
13
|
+
const { encrypted, nonce } = naclEncrypt(encoded, password.subarray(0, 32));
|
|
14
|
+
return u8aConcat(scryptToU8a(salt, params), nonce, encrypted);
|
|
15
15
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
};
|
|
16
|
+
//#endregion
|
|
17
|
+
export { encodePair };
|
package/dist/pair/encode.spec.js
CHANGED
|
@@ -9,7 +9,7 @@ describe("encode", () => {
|
|
|
9
9
|
it("returns PKCS8 when no passphrase supplied", () => {
|
|
10
10
|
expect(keyring.alice.encodePkcs8()).toHaveLength(DECODED_LENGTH);
|
|
11
11
|
});
|
|
12
|
-
it("returns encoded PKCS8 when passphrase supplied", () => {
|
|
12
|
+
it("returns encoded PKCS8 when passphrase supplied", { timeout: 60000 }, () => {
|
|
13
13
|
expect(keyring.alice.encodePkcs8("testing")).toHaveLength(ENCODED_LENGTH);
|
|
14
14
|
});
|
|
15
15
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"encode.spec.js","sourceRoot":"","sources":["../../src/pair/encode.spec.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACnE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAE9C,O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C;AAE5D,QAAQ,CAAC,QAAQ,EAAE,GAAS,EAAE;IAC7B,EAAE,CAAC,2CAA2C,EAAE,GAAS,EAAE;QAC1D,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC,CAAC,YAAY,CAAC,cAAc,CAAC,CAAC;IAClE,CAAC,CAAC,CAAC;IAIH,EAAE,CACD,gDAAgD,EAChD,EAAE,OAAO,EAAE,KAAK,EAAE,EAClB,GAAS,EAAE;QACV,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC,CAAC,YAAY,CAAC,cAAc,CAAC,CAAC;IAC3E,CAAC,CACD,CAAC;AACH,CAAC,CAAC,CAAC"}
|
package/dist/pair/index.js
CHANGED
|
@@ -1,177 +1,200 @@
|
|
|
1
|
-
import { u8aEmpty, u8aConcat, u8aToU8a, u8aToHex, objectSpread } from "@polkadot/util";
|
|
2
|
-
import { sr25519FromSeed, sr25519Sign, sr25519jwtIssue, blake2AsU8a, sr25519VrfVerify, sr25519VrfSign, signatureVerify, secp256k1Compress, jwtVerify, keyExtractPath, keyFromPath } from "@prosopo/util-crypto";
|
|
3
1
|
import { decodePair } from "./decode.js";
|
|
4
2
|
import { encodePair } from "./encode.js";
|
|
5
3
|
import { pairToJson } from "./toJson.js";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
4
|
+
import { jwtVerify, keyExtractPath, keyFromPath, secp256k1Compress, signatureVerify, sr25519FromSeed, sr25519Sign, sr25519VrfSign, sr25519VrfVerify, sr25519jwtIssue } from "@prosopo/util-crypto";
|
|
5
|
+
import { objectSpread, u8aConcat, u8aEmpty, u8aToHex, u8aToU8a } from "@polkadot/util";
|
|
6
|
+
//#region src/pair/index.ts
|
|
7
|
+
var SIG_TYPE_NONE = new Uint8Array([]);
|
|
8
|
+
var TYPE_FROM_SEED = {
|
|
9
|
+
sr25519: sr25519FromSeed,
|
|
10
|
+
ed25519: () => {
|
|
11
|
+
throw new Error("Not Implemented");
|
|
12
|
+
},
|
|
13
|
+
ecdsa: () => {
|
|
14
|
+
throw new Error("Not Implemented");
|
|
15
|
+
},
|
|
16
|
+
ethereum: () => {
|
|
17
|
+
throw new Error("Not Implemented");
|
|
18
|
+
}
|
|
18
19
|
};
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
var TYPE_PREFIX = {
|
|
21
|
+
ecdsa: new Uint8Array([2]),
|
|
22
|
+
ed25519: new Uint8Array([0]),
|
|
23
|
+
ethereum: new Uint8Array([2]),
|
|
24
|
+
sr25519: new Uint8Array([1])
|
|
24
25
|
};
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
26
|
+
var TYPE_SIGNATURE = {
|
|
27
|
+
sr25519: sr25519Sign,
|
|
28
|
+
ed25519: () => {
|
|
29
|
+
throw new Error("Not Implemented");
|
|
30
|
+
},
|
|
31
|
+
ecdsa: () => {
|
|
32
|
+
throw new Error("Not Implemented");
|
|
33
|
+
},
|
|
34
|
+
ethereum: () => {
|
|
35
|
+
throw new Error("Not Implemented");
|
|
36
|
+
}
|
|
36
37
|
};
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
38
|
+
var TYPE_JWT_ISSUE = {
|
|
39
|
+
sr25519: sr25519jwtIssue,
|
|
40
|
+
ed25519: () => {
|
|
41
|
+
throw new Error("Not Implemented");
|
|
42
|
+
},
|
|
43
|
+
ecdsa: () => {
|
|
44
|
+
throw new Error("Not Implemented");
|
|
45
|
+
},
|
|
46
|
+
ethereum: () => {
|
|
47
|
+
throw new Error("Not Implemented");
|
|
48
|
+
}
|
|
48
49
|
};
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
50
|
+
var TYPE_ADDRESS = {
|
|
51
|
+
sr25519: (p) => p,
|
|
52
|
+
ed25519: () => {
|
|
53
|
+
throw new Error("Not Implemented");
|
|
54
|
+
},
|
|
55
|
+
ecdsa: () => {
|
|
56
|
+
throw new Error("Not Implemented");
|
|
57
|
+
},
|
|
58
|
+
ethereum: () => {
|
|
59
|
+
throw new Error("Not Implemented");
|
|
60
|
+
}
|
|
60
61
|
};
|
|
61
62
|
function isLocked(secretKey) {
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
function vrfHash(proof, context, extra) {
|
|
65
|
-
return blake2AsU8a(u8aConcat(context || "", extra || "", proof));
|
|
63
|
+
return !secretKey || u8aEmpty(secretKey);
|
|
66
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* @name createPair
|
|
67
|
+
* @summary Creates a keyring pair object
|
|
68
|
+
* @description Creates a keyring pair object with provided account public key, metadata, and encoded arguments.
|
|
69
|
+
* The keyring pair stores the account state including the encoded address and associated metadata.
|
|
70
|
+
*
|
|
71
|
+
* It has properties whose values are functions that may be called to perform account actions:
|
|
72
|
+
*
|
|
73
|
+
* - `address` function retrieves the address associated with the account.
|
|
74
|
+
* - `decodedPkcs8` function is called with the account passphrase and account encoded public key.
|
|
75
|
+
* It decodes the encoded public key using the passphrase provided to obtain the decoded account public key
|
|
76
|
+
* and associated secret key that are then available in memory, and changes the account address stored in the
|
|
77
|
+
* state of the pair to correspond to the address of the decoded public key.
|
|
78
|
+
* - `encodePkcs8` function when provided with the correct passphrase associated with the account pair
|
|
79
|
+
* and when the secret key is in memory (when the account pair is not locked) it returns an encoded
|
|
80
|
+
* public key of the account.
|
|
81
|
+
* - `meta` is the metadata that is stored in the state of the pair, either when it was originally
|
|
82
|
+
* created or set via `setMeta`.
|
|
83
|
+
* - `publicKey` returns the public key stored in memory for the pair.
|
|
84
|
+
* - `sign` may be used to return a signature by signing a provided message with the secret
|
|
85
|
+
* key (if it is in memory) using Nacl.
|
|
86
|
+
* - `toJson` calls another `toJson` function and provides the state of the pair,
|
|
87
|
+
* it generates arguments to be passed to the other `toJson` function including an encoded public key of the account
|
|
88
|
+
* that it generates using the secret key from memory (if it has been made available in memory)
|
|
89
|
+
* and the optionally provided passphrase argument. It passes a third boolean argument to `toJson`
|
|
90
|
+
* indicating whether the public key has been encoded or not (if a passphrase argument was provided then it is encoded).
|
|
91
|
+
* The `toJson` function that it calls returns a JSON object with properties including the `address`
|
|
92
|
+
* and `meta` that are assigned with the values stored in the corresponding state variables of the account pair,
|
|
93
|
+
* an `encoded` property that is assigned with the encoded public key in hex format, and an `encoding`
|
|
94
|
+
* property that indicates whether the public key value of the `encoded` property is encoded or not.
|
|
95
|
+
*/
|
|
67
96
|
function createPair({ toSS58, type }, { publicKey, secretKey }, meta = {}, encoded = null, encTypes) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
},
|
|
170
|
-
vrfVerify: (message, vrfResult, signerPublic, context, extra) => {
|
|
171
|
-
return sr25519VrfVerify(message, vrfResult, publicKey, context, extra);
|
|
172
|
-
}
|
|
173
|
-
};
|
|
97
|
+
const decodePkcs8 = (passphrase, userEncoded) => {
|
|
98
|
+
const decoded = decodePair(passphrase, userEncoded || encoded, encTypes);
|
|
99
|
+
if (decoded.secretKey.length === 64) {
|
|
100
|
+
publicKey = decoded.publicKey;
|
|
101
|
+
secretKey = decoded.secretKey;
|
|
102
|
+
} else {
|
|
103
|
+
const pair = TYPE_FROM_SEED[type](decoded.secretKey);
|
|
104
|
+
publicKey = pair.publicKey;
|
|
105
|
+
secretKey = pair.secretKey;
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
const recode = (passphrase) => {
|
|
109
|
+
isLocked(secretKey) && encoded && decodePkcs8(passphrase, encoded);
|
|
110
|
+
encoded = encodePair({
|
|
111
|
+
publicKey,
|
|
112
|
+
secretKey
|
|
113
|
+
}, passphrase);
|
|
114
|
+
encTypes = void 0;
|
|
115
|
+
return encoded;
|
|
116
|
+
};
|
|
117
|
+
const encodeAddress = () => {
|
|
118
|
+
return toSS58(TYPE_ADDRESS[type](publicKey));
|
|
119
|
+
};
|
|
120
|
+
return {
|
|
121
|
+
get address() {
|
|
122
|
+
return encodeAddress();
|
|
123
|
+
},
|
|
124
|
+
get addressRaw() {
|
|
125
|
+
return TYPE_ADDRESS[type](publicKey);
|
|
126
|
+
},
|
|
127
|
+
get isLocked() {
|
|
128
|
+
return isLocked(secretKey);
|
|
129
|
+
},
|
|
130
|
+
get meta() {
|
|
131
|
+
return meta;
|
|
132
|
+
},
|
|
133
|
+
get publicKey() {
|
|
134
|
+
return publicKey;
|
|
135
|
+
},
|
|
136
|
+
get type() {
|
|
137
|
+
return type;
|
|
138
|
+
},
|
|
139
|
+
decodePkcs8,
|
|
140
|
+
derive: (suri, meta) => {
|
|
141
|
+
if (isLocked(secretKey)) throw new Error("Cannot derive on a locked keypair");
|
|
142
|
+
const { path } = keyExtractPath(suri);
|
|
143
|
+
const derived = keyFromPath({
|
|
144
|
+
publicKey,
|
|
145
|
+
secretKey
|
|
146
|
+
}, path, type);
|
|
147
|
+
return createPair({
|
|
148
|
+
toSS58,
|
|
149
|
+
type
|
|
150
|
+
}, derived, meta, null);
|
|
151
|
+
},
|
|
152
|
+
encodePkcs8: (passphrase) => {
|
|
153
|
+
return recode(passphrase);
|
|
154
|
+
},
|
|
155
|
+
jwtIssue: (options, message) => {
|
|
156
|
+
if (isLocked(secretKey)) throw new Error("Cannot sign with a locked key pair");
|
|
157
|
+
return TYPE_JWT_ISSUE[type]({
|
|
158
|
+
publicKey,
|
|
159
|
+
secretKey
|
|
160
|
+
}, options, message);
|
|
161
|
+
},
|
|
162
|
+
jwtVerify: (jwt) => {
|
|
163
|
+
return jwtVerify(jwt, publicKey);
|
|
164
|
+
},
|
|
165
|
+
lock: () => {
|
|
166
|
+
secretKey = new Uint8Array([]);
|
|
167
|
+
},
|
|
168
|
+
setMeta: (additional) => {
|
|
169
|
+
meta = objectSpread({}, meta, additional);
|
|
170
|
+
},
|
|
171
|
+
sign: (message, options = {}) => {
|
|
172
|
+
if (isLocked(secretKey)) throw new Error("Cannot sign with a locked key pair");
|
|
173
|
+
return u8aConcat(options.withType ? TYPE_PREFIX[type] : SIG_TYPE_NONE, TYPE_SIGNATURE[type](u8aToU8a(message), {
|
|
174
|
+
publicKey,
|
|
175
|
+
secretKey
|
|
176
|
+
}));
|
|
177
|
+
},
|
|
178
|
+
toJson: (passphrase) => {
|
|
179
|
+
return pairToJson(type, {
|
|
180
|
+
address: ["ecdsa", "ethereum"].includes(type) ? publicKey.length === 20 ? u8aToHex(publicKey) : u8aToHex(secp256k1Compress(publicKey)) : encodeAddress(),
|
|
181
|
+
meta
|
|
182
|
+
}, recode(passphrase), !!passphrase);
|
|
183
|
+
},
|
|
184
|
+
unlock: (passphrase) => {
|
|
185
|
+
decodePkcs8(passphrase);
|
|
186
|
+
},
|
|
187
|
+
verify: (message, signature, signerPublic) => {
|
|
188
|
+
return signatureVerify(message, signature, TYPE_ADDRESS[type](u8aToU8a(signerPublic))).isValid;
|
|
189
|
+
},
|
|
190
|
+
vrfSign: (message, context, extra) => {
|
|
191
|
+
if (isLocked(secretKey)) throw new Error("Cannot sign with a locked key pair");
|
|
192
|
+
return sr25519VrfSign(message, { secretKey }, context, extra);
|
|
193
|
+
},
|
|
194
|
+
vrfVerify: (message, vrfResult, signerPublic, context, extra) => {
|
|
195
|
+
return sr25519VrfVerify(message, vrfResult, publicKey, context, extra);
|
|
196
|
+
}
|
|
197
|
+
};
|
|
174
198
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
};
|
|
199
|
+
//#endregion
|
|
200
|
+
export { createPair };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nobody.test.d.ts","sourceRoot":"","sources":["../../src/pair/nobody.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { encodeAddress } from "@prosopo/util-crypto";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import { PAIR_DIV, PAIR_HDR, PUB_LENGTH, SALT_LENGTH, SEC_LENGTH, SEED_LENGTH, } from "./defaults.js";
|
|
4
|
+
import { nobody } from "./nobody.js";
|
|
5
|
+
describe("nobody", () => {
|
|
6
|
+
it("is the all-zero public key, encoded as the well-known address", () => {
|
|
7
|
+
const pair = nobody();
|
|
8
|
+
expect(pair.publicKey).toEqual(new Uint8Array(32));
|
|
9
|
+
expect(pair.address).toBe("5C4hrfjw9DjXZTzV3MwzrrAr9P1MJhSrvWGWqi1eSuyUpnhM");
|
|
10
|
+
expect(pair.address).toBe(encodeAddress(pair.publicKey, 42));
|
|
11
|
+
});
|
|
12
|
+
it("is permanently locked", () => {
|
|
13
|
+
const pair = nobody();
|
|
14
|
+
expect(pair.isLocked).toBe(true);
|
|
15
|
+
pair.unlock("anything");
|
|
16
|
+
expect(pair.isLocked).toBe(true);
|
|
17
|
+
pair.lock();
|
|
18
|
+
expect(pair.isLocked).toBe(true);
|
|
19
|
+
});
|
|
20
|
+
it("never verifies a signature, whatever it is given", () => {
|
|
21
|
+
const pair = nobody();
|
|
22
|
+
expect(pair.verify(new Uint8Array(1), new Uint8Array(64), pair.publicKey)).toBe(false);
|
|
23
|
+
expect(pair.verify(new Uint8Array(0), pair.sign(new Uint8Array(1)), pair.publicKey)).toBe(false);
|
|
24
|
+
expect(pair.vrfVerify(new Uint8Array(1), new Uint8Array(96), pair.publicKey)).toBe(false);
|
|
25
|
+
});
|
|
26
|
+
it("never reports a JWT as valid", () => {
|
|
27
|
+
const result = nobody().jwtVerify(nobody().jwtIssue());
|
|
28
|
+
expect(result.isValid).toBe(false);
|
|
29
|
+
expect(result.error).toBe("JWT verification failed");
|
|
30
|
+
expect(result.publicKey).toEqual(new Uint8Array(32));
|
|
31
|
+
});
|
|
32
|
+
it("returns correctly sized but empty signatures", () => {
|
|
33
|
+
const pair = nobody();
|
|
34
|
+
expect(pair.sign(new Uint8Array(1))).toEqual(new Uint8Array(64));
|
|
35
|
+
expect(pair.vrfSign(new Uint8Array(1))).toEqual(new Uint8Array(96));
|
|
36
|
+
expect(pair.encodePkcs8()).toEqual(new Uint8Array(0));
|
|
37
|
+
});
|
|
38
|
+
it("derives to itself, so no chain of derivation can escape it", () => {
|
|
39
|
+
const pair = nobody();
|
|
40
|
+
expect(pair.derive("//anything")).toBe(pair);
|
|
41
|
+
expect(pair.derive("//a").derive("//b")).toBe(pair);
|
|
42
|
+
});
|
|
43
|
+
it("ignores attempts to change its meta", () => {
|
|
44
|
+
const pair = nobody();
|
|
45
|
+
pair.setMeta({ name: "somebody" });
|
|
46
|
+
expect(pair.meta.name).toBe("nobody");
|
|
47
|
+
expect(pair.meta.isTesting).toBe(true);
|
|
48
|
+
});
|
|
49
|
+
it("serialises to an unencrypted v0 json blob", () => {
|
|
50
|
+
const json = nobody().toJson();
|
|
51
|
+
expect(json.address).toBe(nobody().address);
|
|
52
|
+
expect(json.encoded).toBe("");
|
|
53
|
+
expect(json.encoding.version).toBe("0");
|
|
54
|
+
expect(json.encoding.type).toBe("none");
|
|
55
|
+
});
|
|
56
|
+
it("returns the same shared instance each call", () => {
|
|
57
|
+
expect(nobody()).toBe(nobody());
|
|
58
|
+
});
|
|
59
|
+
it("accepts pkcs8 decoding without throwing", () => {
|
|
60
|
+
expect(() => nobody().decodePkcs8("pass", new Uint8Array(1))).not.toThrow();
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
describe("pair defaults", () => {
|
|
64
|
+
it("declares the sr25519 key and seed lengths", () => {
|
|
65
|
+
expect(PUB_LENGTH).toBe(32);
|
|
66
|
+
expect(SEED_LENGTH).toBe(32);
|
|
67
|
+
expect(SALT_LENGTH).toBe(32);
|
|
68
|
+
expect(SEC_LENGTH).toBe(64);
|
|
69
|
+
});
|
|
70
|
+
it("declares the pkcs8 header and divider used by the encoder", () => {
|
|
71
|
+
expect(Array.from(PAIR_DIV)).toEqual([161, 35, 3, 33, 0]);
|
|
72
|
+
expect(Array.from(PAIR_HDR)).toEqual([
|
|
73
|
+
48, 83, 2, 1, 1, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32,
|
|
74
|
+
]);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
//# sourceMappingURL=nobody.test.js.map
|