@prosopo/keyring 2.9.59 → 2.9.61

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.
@@ -1,31 +1,30 @@
1
- const PAIR_DIV = new Uint8Array([161, 35, 3, 33, 0]);
2
- const PAIR_HDR = new Uint8Array([
3
- 48,
4
- 83,
5
- 2,
6
- 1,
7
- 1,
8
- 48,
9
- 5,
10
- 6,
11
- 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
- const PUB_LENGTH = 32;
21
- const SALT_LENGTH = 32;
22
- const SEC_LENGTH = 64;
23
- const SEED_LENGTH = 32;
24
- export {
25
- PAIR_DIV,
26
- PAIR_HDR,
27
- PUB_LENGTH,
28
- SALT_LENGTH,
29
- SEC_LENGTH,
30
- SEED_LENGTH
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 };
@@ -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
- import { scryptEncode, naclEncrypt, scryptToU8a } from "@prosopo/util-crypto";
3
- import { PAIR_HDR, PAIR_DIV } from "./defaults.js";
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
- if (!secretKey) {
6
- throw new Error("Expected a valid secretKey to be passed to encode");
7
- }
8
- const encoded = u8aConcat(PAIR_HDR, secretKey, PAIR_DIV, publicKey);
9
- if (!passphrase) {
10
- return encoded;
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
- export {
17
- encodePair
18
- };
16
+ //#endregion
17
+ export { encodePair };
@@ -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
- const SIG_TYPE_NONE = new Uint8Array([]);
7
- const TYPE_FROM_SEED = {
8
- sr25519: sr25519FromSeed,
9
- ed25519: () => {
10
- throw new Error("Not Implemented");
11
- },
12
- ecdsa: () => {
13
- throw new Error("Not Implemented");
14
- },
15
- ethereum: () => {
16
- throw new Error("Not Implemented");
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
- const TYPE_PREFIX = {
20
- ecdsa: new Uint8Array([2]),
21
- ed25519: new Uint8Array([0]),
22
- ethereum: new Uint8Array([2]),
23
- sr25519: new Uint8Array([1])
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
- const TYPE_SIGNATURE = {
26
- sr25519: sr25519Sign,
27
- ed25519: () => {
28
- throw new Error("Not Implemented");
29
- },
30
- ecdsa: () => {
31
- throw new Error("Not Implemented");
32
- },
33
- ethereum: () => {
34
- throw new Error("Not Implemented");
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
- const TYPE_JWT_ISSUE = {
38
- sr25519: sr25519jwtIssue,
39
- ed25519: () => {
40
- throw new Error("Not Implemented");
41
- },
42
- ecdsa: () => {
43
- throw new Error("Not Implemented");
44
- },
45
- ethereum: () => {
46
- throw new Error("Not Implemented");
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
- const TYPE_ADDRESS = {
50
- sr25519: (p) => p,
51
- ed25519: () => {
52
- throw new Error("Not Implemented");
53
- },
54
- ecdsa: () => {
55
- throw new Error("Not Implemented");
56
- },
57
- ethereum: () => {
58
- throw new Error("Not Implemented");
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
- return !secretKey || u8aEmpty(secretKey);
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
- const decodePkcs8 = (passphrase, userEncoded) => {
69
- const decoded = decodePair(passphrase, userEncoded || encoded, encTypes);
70
- if (decoded.secretKey.length === 64) {
71
- publicKey = decoded.publicKey;
72
- secretKey = decoded.secretKey;
73
- } else {
74
- const pair = TYPE_FROM_SEED[type](decoded.secretKey);
75
- publicKey = pair.publicKey;
76
- secretKey = pair.secretKey;
77
- }
78
- };
79
- const recode = (passphrase) => {
80
- isLocked(secretKey) && encoded && decodePkcs8(passphrase, encoded);
81
- encoded = encodePair({ publicKey, secretKey }, passphrase);
82
- encTypes = void 0;
83
- return encoded;
84
- };
85
- const encodeAddress = () => {
86
- const raw = TYPE_ADDRESS[type](publicKey);
87
- return toSS58(raw);
88
- };
89
- return {
90
- get address() {
91
- return encodeAddress();
92
- },
93
- get addressRaw() {
94
- return TYPE_ADDRESS[type](publicKey);
95
- },
96
- get isLocked() {
97
- return isLocked(secretKey);
98
- },
99
- get meta() {
100
- return meta;
101
- },
102
- get publicKey() {
103
- return publicKey;
104
- },
105
- get type() {
106
- return type;
107
- },
108
- // eslint-disable-next-line sort-keys
109
- decodePkcs8,
110
- derive: (suri, meta2) => {
111
- if (isLocked(secretKey)) {
112
- throw new Error("Cannot derive on a locked keypair");
113
- }
114
- const { path } = keyExtractPath(suri);
115
- const derived = keyFromPath({ publicKey, secretKey }, path, type);
116
- return createPair({ toSS58, type }, derived, meta2, null);
117
- },
118
- encodePkcs8: (passphrase) => {
119
- return recode(passphrase);
120
- },
121
- jwtIssue: (options, message) => {
122
- if (isLocked(secretKey)) {
123
- throw new Error("Cannot sign with a locked key pair");
124
- }
125
- return TYPE_JWT_ISSUE[type]({ publicKey, secretKey }, options, message);
126
- },
127
- jwtVerify: (jwt) => {
128
- return jwtVerify(jwt, publicKey);
129
- },
130
- lock: () => {
131
- secretKey = new Uint8Array([]);
132
- },
133
- setMeta: (additional) => {
134
- meta = objectSpread({}, meta, additional);
135
- },
136
- sign: (message, options = {}) => {
137
- if (isLocked(secretKey)) {
138
- throw new Error("Cannot sign with a locked key pair");
139
- }
140
- return u8aConcat(
141
- options.withType ? TYPE_PREFIX[type] : SIG_TYPE_NONE,
142
- TYPE_SIGNATURE[type](u8aToU8a(message), { publicKey, secretKey })
143
- );
144
- },
145
- toJson: (passphrase) => {
146
- const address = ["ecdsa", "ethereum"].includes(type) ? publicKey.length === 20 ? u8aToHex(publicKey) : u8aToHex(secp256k1Compress(publicKey)) : encodeAddress();
147
- return pairToJson(
148
- type,
149
- { address, meta },
150
- recode(passphrase),
151
- !!passphrase
152
- );
153
- },
154
- unlock: (passphrase) => {
155
- decodePkcs8(passphrase);
156
- },
157
- verify: (message, signature, signerPublic) => {
158
- return signatureVerify(
159
- message,
160
- signature,
161
- TYPE_ADDRESS[type](u8aToU8a(signerPublic))
162
- ).isValid;
163
- },
164
- vrfSign: (message, context, extra) => {
165
- if (isLocked(secretKey)) {
166
- throw new Error("Cannot sign with a locked key pair");
167
- }
168
- return sr25519VrfSign(message, { secretKey }, context, extra);
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
- export {
176
- createPair
177
- };
199
+ //#endregion
200
+ export { createPair };
@@ -1,14 +1,11 @@
1
- import { objectSpread } from "@polkadot/util";
2
1
  import { jsonEncryptFormat } from "@prosopo/util-crypto";
2
+ import { objectSpread } from "@polkadot/util";
3
+ //#region src/pair/toJson.ts
3
4
  function pairToJson(type, { address, meta }, encoded, isEncrypted) {
4
- return objectSpread(
5
- jsonEncryptFormat(encoded, ["pkcs8", type], isEncrypted),
6
- {
7
- address,
8
- meta
9
- }
10
- );
5
+ return objectSpread(jsonEncryptFormat(encoded, ["pkcs8", type], isEncrypted), {
6
+ address,
7
+ meta
8
+ });
11
9
  }
12
- export {
13
- pairToJson
14
- };
10
+ //#endregion
11
+ export { pairToJson };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prosopo/keyring",
3
- "version": "2.9.59",
3
+ "version": "2.9.61",
4
4
  "description": "Keypair generator for Prosopo",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -39,24 +39,24 @@
39
39
  "dependencies": {
40
40
  "@polkadot/util": "13.5.7",
41
41
  "@polkadot/util-crypto": "13.5.7",
42
- "@prosopo/common": "3.1.47",
43
- "@prosopo/types": "4.9.11",
44
- "@prosopo/util": "3.3.4",
42
+ "@prosopo/common": "3.1.48",
43
+ "@prosopo/types": "4.10.0",
44
+ "@prosopo/util": "3.3.5",
45
45
  "@prosopo/util-crypto": "13.5.30",
46
46
  "dotenv": "16.4.5"
47
47
  },
48
48
  "devDependencies": {
49
- "@prosopo/config": "3.3.4",
49
+ "@prosopo/config": "3.3.5",
50
50
  "@types/node": "22.10.2",
51
- "@vitest/coverage-v8": "3.2.4",
51
+ "@vitest/coverage-v8": "4.1.10",
52
52
  "concurrently": "9.0.1",
53
53
  "del-cli": "6.0.0",
54
54
  "npm-run-all": "4.1.5",
55
55
  "tslib": "2.7.0",
56
56
  "tsx": "4.20.3",
57
57
  "typescript": "5.6.2",
58
- "vite": "6.4.1",
59
- "vitest": "3.2.4"
58
+ "vite": "8.1.5",
59
+ "vitest": "4.1.10"
60
60
  },
61
61
  "sideEffects": false
62
62
  }