@prosopo/keyring 2.9.61 → 2.9.63

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.
Files changed (55) hide show
  1. package/.turbo/turbo-build$colon$cjs.log +2 -2
  2. package/.turbo/turbo-build$colon$tsc.log +11 -11
  3. package/.turbo/turbo-build.log +3 -3
  4. package/CHANGELOG.md +14 -0
  5. package/dist/accounts/getPair.test.d.ts +2 -0
  6. package/dist/accounts/getPair.test.d.ts.map +1 -0
  7. package/dist/accounts/getPair.test.js +86 -0
  8. package/dist/accounts/getPair.test.js.map +1 -0
  9. package/dist/accounts/mnemonic.test.d.ts +2 -0
  10. package/dist/accounts/mnemonic.test.d.ts.map +1 -0
  11. package/dist/accounts/mnemonic.test.js +64 -0
  12. package/dist/accounts/mnemonic.test.js.map +1 -0
  13. package/dist/accounts/testAccounts.test.d.ts +2 -0
  14. package/dist/accounts/testAccounts.test.d.ts.map +1 -0
  15. package/dist/accounts/testAccounts.test.js +66 -0
  16. package/dist/accounts/testAccounts.test.js.map +1 -0
  17. package/dist/keyring/keyring.test.d.ts +2 -0
  18. package/dist/keyring/keyring.test.d.ts.map +1 -0
  19. package/dist/keyring/keyring.test.js +239 -0
  20. package/dist/keyring/keyring.test.js.map +1 -0
  21. package/dist/keyring/pairs.test.d.ts +2 -0
  22. package/dist/keyring/pairs.test.d.ts.map +1 -0
  23. package/dist/keyring/pairs.test.js +84 -0
  24. package/dist/keyring/pairs.test.js.map +1 -0
  25. package/dist/keyring/testing.test.d.ts +2 -0
  26. package/dist/keyring/testing.test.d.ts.map +1 -0
  27. package/dist/keyring/testing.test.js +97 -0
  28. package/dist/keyring/testing.test.js.map +1 -0
  29. package/dist/keyring.test-d.d.ts +2 -0
  30. package/dist/keyring.test-d.d.ts.map +1 -0
  31. package/dist/keyring.test-d.js +66 -0
  32. package/dist/keyring.test-d.js.map +1 -0
  33. package/dist/pair/decode.spec.js +1 -1
  34. package/dist/pair/decode.spec.js.map +1 -1
  35. package/dist/pair/encode.spec.js +1 -1
  36. package/dist/pair/encode.spec.js.map +1 -1
  37. package/dist/pair/nobody.test.d.ts +2 -0
  38. package/dist/pair/nobody.test.d.ts.map +1 -0
  39. package/dist/pair/nobody.test.js +77 -0
  40. package/dist/pair/nobody.test.js.map +1 -0
  41. package/dist/pair/toJson.spec.js +1 -1
  42. package/dist/pair/toJson.spec.js.map +1 -1
  43. package/package.json +3 -3
  44. package/src/accounts/getPair.test.ts +142 -0
  45. package/src/accounts/mnemonic.test.ts +103 -0
  46. package/src/accounts/testAccounts.test.ts +100 -0
  47. package/src/keyring/keyring.test.ts +373 -0
  48. package/src/keyring/pairs.test.ts +133 -0
  49. package/src/keyring/testing.test.ts +146 -0
  50. package/src/keyring.test-d.ts +134 -0
  51. package/src/pair/decode.spec.ts +3 -1
  52. package/src/pair/encode.spec.ts +9 -3
  53. package/src/pair/nobody.test.ts +127 -0
  54. package/src/pair/toJson.spec.ts +18 -12
  55. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,134 @@
1
+ // Copyright 2021-2026 Prosopo (UK) Ltd.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ import type {
16
+ IProviderAccount,
17
+ ISite,
18
+ KeyringInstance,
19
+ KeyringPair,
20
+ KeyringPair$Json,
21
+ } from "@prosopo/types";
22
+ import { assertType, describe, expectTypeOf, it } from "vitest";
23
+ import {
24
+ Keyring,
25
+ generateMiniSecret,
26
+ generateMnemonic,
27
+ getDefaultProviders,
28
+ getDefaultSiteKeys,
29
+ getPair,
30
+ } from "./index.js";
31
+ // The testing helpers and `nobody` are deliberately not re-exported from the
32
+ // package barrel, so they are imported from their modules directly.
33
+ import type {
34
+ DetectMap,
35
+ DetectPairType,
36
+ TestKeyringMapEthereum,
37
+ TestKeyringMapSubstrate,
38
+ } from "./keyring/testingPairs.js";
39
+ import { nobody } from "./pair/nobody.js";
40
+
41
+ describe("Keyring types", () => {
42
+ it("implements the shared KeyringInstance contract", () => {
43
+ expectTypeOf<Keyring>().toExtend<KeyringInstance>();
44
+ });
45
+
46
+ it("returns KeyringPair from every add and create method", () => {
47
+ const keyring = new Keyring();
48
+ expectTypeOf(keyring.addFromUri("//Alice")).toEqualTypeOf<KeyringPair>();
49
+ expectTypeOf(keyring.addFromMnemonic("x")).toEqualTypeOf<KeyringPair>();
50
+ expectTypeOf(
51
+ keyring.addFromSeed(new Uint8Array()),
52
+ ).toEqualTypeOf<KeyringPair>();
53
+ expectTypeOf(keyring.addFromAddress("x")).toEqualTypeOf<KeyringPair>();
54
+ expectTypeOf(keyring.getPair("x")).toEqualTypeOf<KeyringPair>();
55
+ expectTypeOf(keyring.getPairs()).toEqualTypeOf<KeyringPair[]>();
56
+ expectTypeOf(keyring.publicKeys).toEqualTypeOf<Uint8Array[]>();
57
+ expectTypeOf(keyring.toJson("x")).toEqualTypeOf<KeyringPair$Json>();
58
+ });
59
+
60
+ it("accepts an address as either a string or raw bytes", () => {
61
+ const keyring = new Keyring();
62
+ assertType(keyring.getPair("5abc"));
63
+ assertType(keyring.getPair(new Uint8Array(32)));
64
+ // @ts-expect-error a number is not an address
65
+ assertType(keyring.getPair(1));
66
+ });
67
+
68
+ it("rejects a keyring option object with an unknown key", () => {
69
+ // @ts-expect-error `curve` is not an option
70
+ new Keyring({ curve: "sr25519" });
71
+ });
72
+
73
+ it("has a readonly type", () => {
74
+ expectTypeOf<Keyring["type"]>().toEqualTypeOf<
75
+ import("@prosopo/util-crypto").KeypairType
76
+ >();
77
+ });
78
+ });
79
+
80
+ describe("account helper types", () => {
81
+ it("returns a pair from getPair with every argument optional", () => {
82
+ expectTypeOf(getPair).returns.toEqualTypeOf<KeyringPair>();
83
+ assertType(getPair());
84
+ assertType(getPair("secret", "account", "sr25519", 42));
85
+ });
86
+
87
+ it("returns the mnemonic and address as a tuple", () => {
88
+ expectTypeOf(generateMnemonic).returns.resolves.toEqualTypeOf<
89
+ [string, string]
90
+ >();
91
+ });
92
+
93
+ it("returns the mini secret as bytes paired with an address", () => {
94
+ expectTypeOf(generateMiniSecret).returns.resolves.toEqualTypeOf<
95
+ [Uint8Array, string]
96
+ >();
97
+ });
98
+
99
+ it("describes the dev fixtures with the shared account types", () => {
100
+ expectTypeOf(getDefaultSiteKeys()).toEqualTypeOf<ISite[]>();
101
+ expectTypeOf(getDefaultProviders()).toEqualTypeOf<IProviderAccount[]>();
102
+ });
103
+
104
+ it("returns a real KeyringPair for nobody", () => {
105
+ expectTypeOf(nobody()).toEqualTypeOf<KeyringPair>();
106
+ });
107
+ });
108
+
109
+ describe("test keyring map inference", () => {
110
+ it("defaults to the substrate map when no options are given", () => {
111
+ expectTypeOf<DetectPairType<undefined>>().toEqualTypeOf<"sr25519">();
112
+ expectTypeOf<
113
+ DetectMap<undefined>
114
+ >().toEqualTypeOf<TestKeyringMapSubstrate>();
115
+ });
116
+
117
+ it("selects the ethereum map only for an ethereum keyring", () => {
118
+ expectTypeOf<
119
+ DetectMap<{ type: "ethereum" }>
120
+ >().toEqualTypeOf<TestKeyringMapEthereum>();
121
+ expectTypeOf<
122
+ DetectMap<{ type: "sr25519" }>
123
+ >().toEqualTypeOf<TestKeyringMapSubstrate>();
124
+ });
125
+
126
+ it("indexes the map by arbitrary names, always yielding a pair", () => {
127
+ expectTypeOf<
128
+ TestKeyringMapSubstrate["alice"]
129
+ >().toEqualTypeOf<KeyringPair>();
130
+ expectTypeOf<
131
+ TestKeyringMapSubstrate["anything-else"]
132
+ >().toEqualTypeOf<KeyringPair>();
133
+ });
134
+ });
@@ -13,7 +13,9 @@ describe("decode", (): void => {
13
13
  );
14
14
  });
15
15
 
16
- it("returns correct publicKey from encoded", (): void => {
16
+ // scrypt with the production parameters takes several seconds, which
17
+ // overruns the 10s default on a loaded CI runner.
18
+ it("returns correct publicKey from encoded", { timeout: 60000 }, (): void => {
17
19
  const PASS = "testing";
18
20
 
19
21
  expect((): void =>
@@ -18,7 +18,13 @@ describe("encode", (): void => {
18
18
  expect(keyring.alice.encodePkcs8()).toHaveLength(DECODED_LENGTH);
19
19
  });
20
20
 
21
- it("returns encoded PKCS8 when passphrase supplied", (): void => {
22
- expect(keyring.alice.encodePkcs8("testing")).toHaveLength(ENCODED_LENGTH);
23
- });
21
+ // scrypt with the production parameters takes several seconds, which
22
+ // overruns the 10s default on a loaded CI runner.
23
+ it(
24
+ "returns encoded PKCS8 when passphrase supplied",
25
+ { timeout: 60000 },
26
+ (): void => {
27
+ expect(keyring.alice.encodePkcs8("testing")).toHaveLength(ENCODED_LENGTH);
28
+ },
29
+ );
24
30
  });
@@ -0,0 +1,127 @@
1
+ // Copyright 2021-2026 Prosopo (UK) Ltd.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ import { encodeAddress } from "@prosopo/util-crypto";
16
+ import { describe, expect, it } from "vitest";
17
+ import {
18
+ PAIR_DIV,
19
+ PAIR_HDR,
20
+ PUB_LENGTH,
21
+ SALT_LENGTH,
22
+ SEC_LENGTH,
23
+ SEED_LENGTH,
24
+ } from "./defaults.js";
25
+ import { nobody } from "./nobody.js";
26
+
27
+ describe("nobody", () => {
28
+ it("is the all-zero public key, encoded as the well-known address", () => {
29
+ const pair = nobody();
30
+ expect(pair.publicKey).toEqual(new Uint8Array(32));
31
+ expect(pair.address).toBe(
32
+ "5C4hrfjw9DjXZTzV3MwzrrAr9P1MJhSrvWGWqi1eSuyUpnhM",
33
+ );
34
+ // The address is a pre-computed constant in the source; if the encoding
35
+ // ever changed, the constant would silently be wrong.
36
+ expect(pair.address).toBe(encodeAddress(pair.publicKey, 42));
37
+ });
38
+
39
+ it("is permanently locked", () => {
40
+ const pair = nobody();
41
+ expect(pair.isLocked).toBe(true);
42
+ pair.unlock("anything");
43
+ expect(pair.isLocked).toBe(true);
44
+ pair.lock();
45
+ expect(pair.isLocked).toBe(true);
46
+ });
47
+
48
+ it("never verifies a signature, whatever it is given", () => {
49
+ // This is the whole point of the pair: it stands in for "no account"
50
+ // and must never authenticate anyone.
51
+ const pair = nobody();
52
+ expect(
53
+ pair.verify(new Uint8Array(1), new Uint8Array(64), pair.publicKey),
54
+ ).toBe(false);
55
+ expect(
56
+ pair.verify(
57
+ new Uint8Array(0),
58
+ pair.sign(new Uint8Array(1)),
59
+ pair.publicKey,
60
+ ),
61
+ ).toBe(false);
62
+ expect(
63
+ pair.vrfVerify(new Uint8Array(1), new Uint8Array(96), pair.publicKey),
64
+ ).toBe(false);
65
+ });
66
+
67
+ it("never reports a JWT as valid", () => {
68
+ const result = nobody().jwtVerify(nobody().jwtIssue());
69
+ expect(result.isValid).toBe(false);
70
+ expect(result.error).toBe("JWT verification failed");
71
+ expect(result.publicKey).toEqual(new Uint8Array(32));
72
+ });
73
+
74
+ it("returns correctly sized but empty signatures", () => {
75
+ const pair = nobody();
76
+ expect(pair.sign(new Uint8Array(1))).toEqual(new Uint8Array(64));
77
+ expect(pair.vrfSign(new Uint8Array(1))).toEqual(new Uint8Array(96));
78
+ expect(pair.encodePkcs8()).toEqual(new Uint8Array(0));
79
+ });
80
+
81
+ it("derives to itself, so no chain of derivation can escape it", () => {
82
+ const pair = nobody();
83
+ expect(pair.derive("//anything")).toBe(pair);
84
+ expect(pair.derive("//a").derive("//b")).toBe(pair);
85
+ });
86
+
87
+ it("ignores attempts to change its meta", () => {
88
+ const pair = nobody();
89
+ pair.setMeta({ name: "somebody" });
90
+ expect(pair.meta.name).toBe("nobody");
91
+ expect(pair.meta.isTesting).toBe(true);
92
+ });
93
+
94
+ it("serialises to an unencrypted v0 json blob", () => {
95
+ const json = nobody().toJson();
96
+ expect(json.address).toBe(nobody().address);
97
+ expect(json.encoded).toBe("");
98
+ expect(json.encoding.version).toBe("0");
99
+ expect(json.encoding.type).toBe("none");
100
+ });
101
+
102
+ it("returns the same shared instance each call", () => {
103
+ expect(nobody()).toBe(nobody());
104
+ });
105
+
106
+ it("accepts pkcs8 decoding without throwing", () => {
107
+ expect(() => nobody().decodePkcs8("pass", new Uint8Array(1))).not.toThrow();
108
+ });
109
+ });
110
+
111
+ describe("pair defaults", () => {
112
+ it("declares the sr25519 key and seed lengths", () => {
113
+ expect(PUB_LENGTH).toBe(32);
114
+ expect(SEED_LENGTH).toBe(32);
115
+ expect(SALT_LENGTH).toBe(32);
116
+ expect(SEC_LENGTH).toBe(64);
117
+ });
118
+
119
+ it("declares the pkcs8 header and divider used by the encoder", () => {
120
+ // These framing bytes are what makes an exported key readable by
121
+ // polkadot-js; a change here silently breaks every existing backup.
122
+ expect(Array.from(PAIR_DIV)).toEqual([161, 35, 3, 33, 0]);
123
+ expect(Array.from(PAIR_HDR)).toEqual([
124
+ 48, 83, 2, 1, 1, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32,
125
+ ]);
126
+ });
127
+ });
@@ -24,17 +24,23 @@ describe("toJson", (): void => {
24
24
  });
25
25
  });
26
26
 
27
- it("creates an encoded output with passphrase", (): void => {
28
- const json = keyring.alice.toJson("testing");
27
+ // scrypt with the production parameters takes several seconds, which
28
+ // overruns the 10s default on a loaded CI runner.
29
+ it(
30
+ "creates an encoded output with passphrase",
31
+ { timeout: 60000 },
32
+ (): void => {
33
+ const json = keyring.alice.toJson("testing");
29
34
 
30
- expect(json.encoded).toHaveLength(268);
31
- expect(json).toMatchObject({
32
- address: "5Engs9f8Gk6JqvVWz3kFyJ8Kqkgx7pLi8C1UTcr7EZ855qBQ",
33
- encoding: {
34
- content: ["pkcs8", "sr25519"],
35
- type: ["scrypt", "xsalsa20-poly1305"],
36
- version: "3",
37
- },
38
- });
39
- });
35
+ expect(json.encoded).toHaveLength(268);
36
+ expect(json).toMatchObject({
37
+ address: "5Engs9f8Gk6JqvVWz3kFyJ8Kqkgx7pLi8C1UTcr7EZ855qBQ",
38
+ encoding: {
39
+ content: ["pkcs8", "sr25519"],
40
+ type: ["scrypt", "xsalsa20-poly1305"],
41
+ version: "3",
42
+ },
43
+ });
44
+ },
45
+ );
40
46
  });