@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.
- 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 +19 -0
- package/dist/_virtual/_rolldown/runtime.js +3 -0
- package/dist/accounts/getPair.js +28 -38
- package/dist/accounts/index.js +1 -7
- package/dist/accounts/mnemonic.js +17 -13
- package/dist/accounts/testAccounts.js +36 -42
- 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/pairs.js +22 -26
- package/dist/pair/decode.js +29 -29
- package/dist/pair/defaults.js +29 -30
- package/dist/pair/encode.js +14 -15
- package/dist/pair/index.js +188 -165
- package/dist/pair/toJson.js +8 -11
- package/package.json +8 -8
- package/tsconfig.tsbuildinfo +1 -1
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,19 +1,16 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
require("./
|
|
4
|
-
require("./keyring/
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
exports.
|
|
12
|
-
exports.
|
|
13
|
-
exports.
|
|
14
|
-
exports.
|
|
15
|
-
exports.getDefaultProviders =
|
|
16
|
-
exports.getDefaultSiteKeys =
|
|
17
|
-
exports.
|
|
18
|
-
exports.Keyring = keyring.Keyring;
|
|
19
|
-
exports.Pairs = pairs.Pairs;
|
|
2
|
+
const require_index = require("./pair/index.cjs");
|
|
3
|
+
const require_pairs = require("./keyring/pairs.cjs");
|
|
4
|
+
const require_keyring = require("./keyring/keyring.cjs");
|
|
5
|
+
const require_mnemonic = require("./accounts/mnemonic.cjs");
|
|
6
|
+
const require_getPair = require("./accounts/getPair.cjs");
|
|
7
|
+
const require_testAccounts = require("./accounts/testAccounts.cjs");
|
|
8
|
+
exports.DEV_PHRASE = require_keyring.DEV_PHRASE;
|
|
9
|
+
exports.Keyring = require_keyring.Keyring;
|
|
10
|
+
exports.Pairs = require_pairs.Pairs;
|
|
11
|
+
exports.createPair = require_index.createPair;
|
|
12
|
+
exports.generateMiniSecret = require_mnemonic.generateMiniSecret;
|
|
13
|
+
exports.generateMnemonic = require_mnemonic.generateMnemonic;
|
|
14
|
+
exports.getDefaultProviders = require_testAccounts.getDefaultProviders;
|
|
15
|
+
exports.getDefaultSiteKeys = require_testAccounts.getDefaultSiteKeys;
|
|
16
|
+
exports.getPair = require_getPair.getPair;
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
exports.
|
|
6
|
-
exports.Keyring = keyring.Keyring;
|
|
7
|
-
exports.Pairs = pairs.Pairs;
|
|
1
|
+
const require_pairs = require("./pairs.cjs");
|
|
2
|
+
const require_keyring = require("./keyring.cjs");
|
|
3
|
+
exports.DEV_PHRASE = require_keyring.DEV_PHRASE;
|
|
4
|
+
exports.Keyring = require_keyring.Keyring;
|
|
5
|
+
exports.Pairs = require_pairs.Pairs;
|
|
@@ -1,271 +1,262 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
1
|
+
const require_index = require("../pair/index.cjs");
|
|
2
|
+
const require_pairs = require("./pairs.cjs");
|
|
3
|
+
let _prosopo_util_crypto = require("@prosopo/util-crypto");
|
|
4
|
+
let _polkadot_util = require("@polkadot/util");
|
|
5
|
+
let _polkadot_util_hex = require("@polkadot/util/hex");
|
|
6
|
+
let _polkadot_util_is = require("@polkadot/util/is");
|
|
7
|
+
//#region src/keyring/keyring.ts
|
|
8
|
+
var DEV_PHRASE = "bottom drive obey lake curtain smoke basket hold race lonely fit walk";
|
|
9
|
+
var PairFromSeed = {
|
|
10
|
+
sr25519: (seed) => (0, _prosopo_util_crypto.sr25519FromSeed)(seed),
|
|
11
|
+
ed25519: () => {
|
|
12
|
+
throw new Error("Not Implemented");
|
|
13
|
+
},
|
|
14
|
+
ecdsa: () => {
|
|
15
|
+
throw new Error("Not Implemented");
|
|
16
|
+
},
|
|
17
|
+
ethereum: () => {
|
|
18
|
+
throw new Error("Not Implemented");
|
|
19
|
+
}
|
|
21
20
|
};
|
|
22
21
|
function pairToPublic({ publicKey }) {
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
class Keyring {
|
|
26
|
-
constructor(options = {}) {
|
|
27
|
-
this.decodeAddress = utilCrypto.decodeAddress;
|
|
28
|
-
this.encodeAddress = (address, ss58Format) => {
|
|
29
|
-
return utilCrypto.encodeAddress(address, ss58Format ?? this.#ss58);
|
|
30
|
-
};
|
|
31
|
-
options.type = options.type || "sr25519";
|
|
32
|
-
if (!["sr25519"].includes(options.type || "undefined")) {
|
|
33
|
-
throw new Error(
|
|
34
|
-
`Expected a keyring type of either 'sr25519', found '${options.type || "unknown"}`
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
this.#pairs = new pairs.Pairs();
|
|
38
|
-
this.#ss58 = options.ss58Format;
|
|
39
|
-
this.#type = options.type;
|
|
40
|
-
}
|
|
41
|
-
#pairs;
|
|
42
|
-
#type;
|
|
43
|
-
#ss58;
|
|
44
|
-
/**
|
|
45
|
-
* @description retrieve the pairs (alias for getPairs)
|
|
46
|
-
*/
|
|
47
|
-
get pairs() {
|
|
48
|
-
return this.getPairs();
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* @description retrieve the publicKeys (alias for getPublicKeys)
|
|
52
|
-
*/
|
|
53
|
-
get publicKeys() {
|
|
54
|
-
return this.getPublicKeys();
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* @description Returns the type of the keyring, ed25519, sr25519 or ecdsa
|
|
58
|
-
*/
|
|
59
|
-
get type() {
|
|
60
|
-
return this.#type;
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* @name addPair
|
|
64
|
-
* @summary Stores an account, given a keyring pair, as a Key/Value (public key, pair) in Keyring Pair Dictionary
|
|
65
|
-
*/
|
|
66
|
-
addPair(pair) {
|
|
67
|
-
return this.#pairs.add(pair);
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* @name addFromAddress
|
|
71
|
-
* @summary Stores an account, given an account address, as a Key/Value (public key, pair) in Keyring Pair Dictionary
|
|
72
|
-
* @description Allows user to explicitly provide separate inputs including account address or public key, and optionally
|
|
73
|
-
* the associated account metadata, and the default encoded value as arguments (that may be obtained from the json file
|
|
74
|
-
* of an account backup), and then generates a keyring pair from them that it passes to
|
|
75
|
-
* `addPair` to stores in a keyring pair dictionary the public key of the generated pair as a key and the pair as the associated value.
|
|
76
|
-
*/
|
|
77
|
-
addFromAddress(address, meta = {}, encoded = null, type = this.type, ignoreChecksum, encType) {
|
|
78
|
-
const publicKey = this.decodeAddress(address, ignoreChecksum);
|
|
79
|
-
return this.addPair(
|
|
80
|
-
index.createPair(
|
|
81
|
-
{ toSS58: this.encodeAddress, type },
|
|
82
|
-
{ publicKey, secretKey: new Uint8Array() },
|
|
83
|
-
meta,
|
|
84
|
-
encoded,
|
|
85
|
-
encType
|
|
86
|
-
)
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* @name addFromJson
|
|
91
|
-
* @summary Stores an account, given JSON data, as a Key/Value (public key, pair) in Keyring Pair Dictionary
|
|
92
|
-
* @description Allows user to provide a json object argument that contains account information (that may be obtained from the json file
|
|
93
|
-
* of an account backup), and then generates a keyring pair from it that it passes to
|
|
94
|
-
* `addPair` to stores in a keyring pair dictionary the public key of the generated pair as a key and the pair as the associated value.
|
|
95
|
-
*/
|
|
96
|
-
addFromJson(json, ignoreChecksum) {
|
|
97
|
-
return this.addPair(this.createFromJson(json, ignoreChecksum));
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* @name addFromMnemonic
|
|
101
|
-
* @summary Stores an account, given a mnemonic, as a Key/Value (public key, pair) in Keyring Pair Dictionary
|
|
102
|
-
* @description Allows user to provide a mnemonic (seed phrase that is provided when account is originally created)
|
|
103
|
-
* argument and a metadata argument that contains account information (that may be obtained from the json file
|
|
104
|
-
* of an account backup), and then generates a keyring pair from it that it passes to
|
|
105
|
-
* `addPair` to stores in a keyring pair dictionary the public key of the generated pair as a key and the pair as the associated value.
|
|
106
|
-
*/
|
|
107
|
-
addFromMnemonic(mnemonic, meta = {}, type = this.type) {
|
|
108
|
-
return this.addFromUri(mnemonic, meta, type);
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* @name addFromPair
|
|
112
|
-
* @summary Stores an account created from an explicit publicKey/secreteKey combination
|
|
113
|
-
*/
|
|
114
|
-
addFromPair(pair, meta = {}, type = this.type) {
|
|
115
|
-
return this.addPair(this.createFromPair(pair, meta, type));
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* @name addFromSeed
|
|
119
|
-
* @summary Stores an account, given seed data, as a Key/Value (public key, pair) in Keyring Pair Dictionary
|
|
120
|
-
* @description Stores in a keyring pair dictionary the public key of the pair as a key and the pair as the associated value.
|
|
121
|
-
* Allows user to provide the account seed as an argument, and then generates a keyring pair from it that it passes to
|
|
122
|
-
* `addPair` to store in a keyring pair dictionary the public key of the generated pair as a key and the pair as the associated value.
|
|
123
|
-
*/
|
|
124
|
-
addFromSeed(seed, meta = {}, type = this.type) {
|
|
125
|
-
return this.addPair(
|
|
126
|
-
index.createPair(
|
|
127
|
-
{ toSS58: this.encodeAddress, type },
|
|
128
|
-
PairFromSeed[type](seed),
|
|
129
|
-
meta,
|
|
130
|
-
null
|
|
131
|
-
)
|
|
132
|
-
);
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* @name addFromUri
|
|
136
|
-
* @summary Creates an account via an suri
|
|
137
|
-
* @description Extracts the phrase, path and password from a SURI format for specifying secret keys `<secret>/<soft-key>//<hard-key>///<password>` (the `///password` may be omitted, and `/<soft-key>` and `//<hard-key>` maybe repeated and mixed). The secret can be a hex string, mnemonic phrase or a string (to be padded)
|
|
138
|
-
*/
|
|
139
|
-
addFromUri(suri, meta = {}, type = this.type) {
|
|
140
|
-
return this.addPair(this.createFromUri(suri, meta, type));
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* @name createFromJson
|
|
144
|
-
* @description Creates a pair from a JSON keyfile
|
|
145
|
-
*/
|
|
146
|
-
createFromJson({
|
|
147
|
-
address,
|
|
148
|
-
encoded,
|
|
149
|
-
encoding: { content, type, version },
|
|
150
|
-
meta
|
|
151
|
-
}, ignoreChecksum) {
|
|
152
|
-
if (version === "3" && content[0] !== "pkcs8") {
|
|
153
|
-
throw new Error(
|
|
154
|
-
`Unable to decode non-pkcs8 type, [${content.join(",")}] found}`
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
const cryptoType = version === "0" || !Array.isArray(content) ? this.type : content[1];
|
|
158
|
-
if (!cryptoType) {
|
|
159
|
-
throw new Error("cryptoType is undefined");
|
|
160
|
-
}
|
|
161
|
-
const encType = !Array.isArray(type) ? [type] : type;
|
|
162
|
-
if (!["sr25519"].includes(cryptoType)) {
|
|
163
|
-
throw new Error(`Unknown crypto type ${cryptoType}`);
|
|
164
|
-
}
|
|
165
|
-
const publicKey = is.isHex(address) ? hex.hexToU8a(address) : this.decodeAddress(address, ignoreChecksum);
|
|
166
|
-
const decoded = is.isHex(encoded) ? hex.hexToU8a(encoded) : utilCrypto.base64Decode(encoded);
|
|
167
|
-
return index.createPair(
|
|
168
|
-
{ toSS58: this.encodeAddress, type: cryptoType },
|
|
169
|
-
{ publicKey, secretKey: new Uint8Array() },
|
|
170
|
-
meta,
|
|
171
|
-
decoded,
|
|
172
|
-
encType
|
|
173
|
-
);
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* @name createFromPair
|
|
177
|
-
* @summary Creates a pair from an explicit publicKey/secreteKey combination
|
|
178
|
-
*/
|
|
179
|
-
createFromPair(pair, meta = {}, type = this.type) {
|
|
180
|
-
return index.createPair({ toSS58: this.encodeAddress, type }, pair, meta, null);
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
* @name createFromUri
|
|
184
|
-
* @summary Creates a Keypair from an suri
|
|
185
|
-
* @description This creates a pair from the suri, but does not add it to the keyring
|
|
186
|
-
*/
|
|
187
|
-
createFromUri(_suri, meta = {}, type = this.type) {
|
|
188
|
-
const suri = _suri.startsWith("//") ? `${DEV_PHRASE}${_suri}` : _suri;
|
|
189
|
-
const { derivePath, password, path, phrase } = utilCrypto.keyExtractSuri(suri);
|
|
190
|
-
let seed;
|
|
191
|
-
const isPhraseHex = is.isHex(phrase, 256);
|
|
192
|
-
if (isPhraseHex) {
|
|
193
|
-
seed = hex.hexToU8a(phrase);
|
|
194
|
-
} else {
|
|
195
|
-
const parts = phrase.split(" ");
|
|
196
|
-
if ([12, 15, 18, 21, 24].includes(parts.length)) {
|
|
197
|
-
seed = type === "ethereum" ? (() => {
|
|
198
|
-
throw new Error(
|
|
199
|
-
"Not implemented - Prosopo Keyring supports sr25519 only"
|
|
200
|
-
);
|
|
201
|
-
})() : utilCrypto.mnemonicToMiniSecret(phrase, password);
|
|
202
|
-
} else {
|
|
203
|
-
if (phrase.length > 32) {
|
|
204
|
-
throw new Error(
|
|
205
|
-
"specified phrase is not a valid mnemonic and is invalid as a raw seed at > 32 bytes"
|
|
206
|
-
);
|
|
207
|
-
}
|
|
208
|
-
seed = util.stringToU8a(phrase.padEnd(32));
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
const derived = utilCrypto.keyFromPath(PairFromSeed[type](seed), path, type);
|
|
212
|
-
return index.createPair(
|
|
213
|
-
{ toSS58: this.encodeAddress, type },
|
|
214
|
-
derived,
|
|
215
|
-
meta,
|
|
216
|
-
null
|
|
217
|
-
);
|
|
218
|
-
}
|
|
219
|
-
/**
|
|
220
|
-
* @name getPair
|
|
221
|
-
* @summary Retrieves an account keyring pair from the Keyring Pair Dictionary, given an account address
|
|
222
|
-
* @description Returns a keyring pair value from the keyring pair dictionary by performing
|
|
223
|
-
* a key lookup using the provided account address or public key (after decoding it).
|
|
224
|
-
*/
|
|
225
|
-
getPair(address) {
|
|
226
|
-
return this.#pairs.get(address);
|
|
227
|
-
}
|
|
228
|
-
/**
|
|
229
|
-
* @name getPairs
|
|
230
|
-
* @summary Retrieves all account keyring pairs from the Keyring Pair Dictionary
|
|
231
|
-
* @description Returns an array list of all the keyring pair values that are stored in the keyring pair dictionary.
|
|
232
|
-
*/
|
|
233
|
-
getPairs() {
|
|
234
|
-
return this.#pairs.all();
|
|
235
|
-
}
|
|
236
|
-
/**
|
|
237
|
-
* @name getPublicKeys
|
|
238
|
-
* @summary Retrieves Public Keys of all Keyring Pairs stored in the Keyring Pair Dictionary
|
|
239
|
-
* @description Returns an array list of all the public keys associated with each of the keyring pair values that are stored in the keyring pair dictionary.
|
|
240
|
-
*/
|
|
241
|
-
getPublicKeys() {
|
|
242
|
-
return this.#pairs.all().map(pairToPublic);
|
|
243
|
-
}
|
|
244
|
-
/**
|
|
245
|
-
* @name removePair
|
|
246
|
-
* @description Deletes the provided input address or public key from the stored Keyring Pair Dictionary.
|
|
247
|
-
*/
|
|
248
|
-
removePair(address) {
|
|
249
|
-
this.#pairs.remove(address);
|
|
250
|
-
}
|
|
251
|
-
/**
|
|
252
|
-
* @name setSS58Format;
|
|
253
|
-
* @description Sets the ss58 format for the keyring
|
|
254
|
-
*/
|
|
255
|
-
setSS58Format(ss58) {
|
|
256
|
-
this.#ss58 = ss58;
|
|
257
|
-
}
|
|
258
|
-
/**
|
|
259
|
-
* @name toJson
|
|
260
|
-
* @summary Returns a JSON object associated with the input argument that contains metadata assocated with an account
|
|
261
|
-
* @description Returns a JSON object containing the metadata associated with an account
|
|
262
|
-
* when valid address or public key and when the account passphrase is provided if the account secret
|
|
263
|
-
* is not already unlocked and available in memory. Note that in [Polkadot-JS Apps](https://github.com/polkadot-js/apps) the user
|
|
264
|
-
* may backup their account to a JSON file that contains this information.
|
|
265
|
-
*/
|
|
266
|
-
toJson(address, passphrase) {
|
|
267
|
-
return this.#pairs.get(address).toJson(passphrase);
|
|
268
|
-
}
|
|
22
|
+
return publicKey;
|
|
269
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* # @polkadot/keyring
|
|
26
|
+
*
|
|
27
|
+
* ## Overview
|
|
28
|
+
*
|
|
29
|
+
* @name Keyring
|
|
30
|
+
* @summary Keyring management of user accounts
|
|
31
|
+
* @description Allows generation of keyring pairs from a variety of input combinations, such as
|
|
32
|
+
* json object containing account address or public key, account metadata, and account encoded using
|
|
33
|
+
* `addFromJson`, or by providing those values as arguments separately to `addFromAddress`,
|
|
34
|
+
* or by providing the mnemonic (seed phrase) and account metadata as arguments to `addFromMnemonic`.
|
|
35
|
+
* Stores the keyring pairs in a keyring pair dictionary. Removal of the keyring pairs from the keyring pair
|
|
36
|
+
* dictionary is achieved using `removePair`. Retrieval of all the stored pairs via `getPairs` or perform
|
|
37
|
+
* lookup of a pair for a given account address or public key using `getPair`. JSON metadata associated with
|
|
38
|
+
* an account may be obtained using `toJson` accompanied by the account passphrase.
|
|
39
|
+
*/
|
|
40
|
+
var Keyring = class {
|
|
41
|
+
#pairs;
|
|
42
|
+
#type;
|
|
43
|
+
#ss58;
|
|
44
|
+
constructor(options = {}) {
|
|
45
|
+
this.decodeAddress = _prosopo_util_crypto.decodeAddress;
|
|
46
|
+
this.encodeAddress = (address, ss58Format) => {
|
|
47
|
+
return (0, _prosopo_util_crypto.encodeAddress)(address, ss58Format ?? this.#ss58);
|
|
48
|
+
};
|
|
49
|
+
options.type = options.type || "sr25519";
|
|
50
|
+
if (!["sr25519"].includes(options.type || "undefined")) throw new Error(`Expected a keyring type of either 'sr25519', found '${options.type || "unknown"}`);
|
|
51
|
+
this.#pairs = new require_pairs.Pairs();
|
|
52
|
+
this.#ss58 = options.ss58Format;
|
|
53
|
+
this.#type = options.type;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* @description retrieve the pairs (alias for getPairs)
|
|
57
|
+
*/
|
|
58
|
+
get pairs() {
|
|
59
|
+
return this.getPairs();
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* @description retrieve the publicKeys (alias for getPublicKeys)
|
|
63
|
+
*/
|
|
64
|
+
get publicKeys() {
|
|
65
|
+
return this.getPublicKeys();
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* @description Returns the type of the keyring, ed25519, sr25519 or ecdsa
|
|
69
|
+
*/
|
|
70
|
+
get type() {
|
|
71
|
+
return this.#type;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* @name addPair
|
|
75
|
+
* @summary Stores an account, given a keyring pair, as a Key/Value (public key, pair) in Keyring Pair Dictionary
|
|
76
|
+
*/
|
|
77
|
+
addPair(pair) {
|
|
78
|
+
return this.#pairs.add(pair);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* @name addFromAddress
|
|
82
|
+
* @summary Stores an account, given an account address, as a Key/Value (public key, pair) in Keyring Pair Dictionary
|
|
83
|
+
* @description Allows user to explicitly provide separate inputs including account address or public key, and optionally
|
|
84
|
+
* the associated account metadata, and the default encoded value as arguments (that may be obtained from the json file
|
|
85
|
+
* of an account backup), and then generates a keyring pair from them that it passes to
|
|
86
|
+
* `addPair` to stores in a keyring pair dictionary the public key of the generated pair as a key and the pair as the associated value.
|
|
87
|
+
*/
|
|
88
|
+
addFromAddress(address, meta = {}, encoded = null, type = this.type, ignoreChecksum, encType) {
|
|
89
|
+
const publicKey = this.decodeAddress(address, ignoreChecksum);
|
|
90
|
+
return this.addPair(require_index.createPair({
|
|
91
|
+
toSS58: this.encodeAddress,
|
|
92
|
+
type
|
|
93
|
+
}, {
|
|
94
|
+
publicKey,
|
|
95
|
+
secretKey: /* @__PURE__ */ new Uint8Array()
|
|
96
|
+
}, meta, encoded, encType));
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* @name addFromJson
|
|
100
|
+
* @summary Stores an account, given JSON data, as a Key/Value (public key, pair) in Keyring Pair Dictionary
|
|
101
|
+
* @description Allows user to provide a json object argument that contains account information (that may be obtained from the json file
|
|
102
|
+
* of an account backup), and then generates a keyring pair from it that it passes to
|
|
103
|
+
* `addPair` to stores in a keyring pair dictionary the public key of the generated pair as a key and the pair as the associated value.
|
|
104
|
+
*/
|
|
105
|
+
addFromJson(json, ignoreChecksum) {
|
|
106
|
+
return this.addPair(this.createFromJson(json, ignoreChecksum));
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* @name addFromMnemonic
|
|
110
|
+
* @summary Stores an account, given a mnemonic, as a Key/Value (public key, pair) in Keyring Pair Dictionary
|
|
111
|
+
* @description Allows user to provide a mnemonic (seed phrase that is provided when account is originally created)
|
|
112
|
+
* argument and a metadata argument that contains account information (that may be obtained from the json file
|
|
113
|
+
* of an account backup), and then generates a keyring pair from it that it passes to
|
|
114
|
+
* `addPair` to stores in a keyring pair dictionary the public key of the generated pair as a key and the pair as the associated value.
|
|
115
|
+
*/
|
|
116
|
+
addFromMnemonic(mnemonic, meta = {}, type = this.type) {
|
|
117
|
+
return this.addFromUri(mnemonic, meta, type);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* @name addFromPair
|
|
121
|
+
* @summary Stores an account created from an explicit publicKey/secreteKey combination
|
|
122
|
+
*/
|
|
123
|
+
addFromPair(pair, meta = {}, type = this.type) {
|
|
124
|
+
return this.addPair(this.createFromPair(pair, meta, type));
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* @name addFromSeed
|
|
128
|
+
* @summary Stores an account, given seed data, as a Key/Value (public key, pair) in Keyring Pair Dictionary
|
|
129
|
+
* @description Stores in a keyring pair dictionary the public key of the pair as a key and the pair as the associated value.
|
|
130
|
+
* Allows user to provide the account seed as an argument, and then generates a keyring pair from it that it passes to
|
|
131
|
+
* `addPair` to store in a keyring pair dictionary the public key of the generated pair as a key and the pair as the associated value.
|
|
132
|
+
*/
|
|
133
|
+
addFromSeed(seed, meta = {}, type = this.type) {
|
|
134
|
+
return this.addPair(require_index.createPair({
|
|
135
|
+
toSS58: this.encodeAddress,
|
|
136
|
+
type
|
|
137
|
+
}, PairFromSeed[type](seed), meta, null));
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* @name addFromUri
|
|
141
|
+
* @summary Creates an account via an suri
|
|
142
|
+
* @description Extracts the phrase, path and password from a SURI format for specifying secret keys `<secret>/<soft-key>//<hard-key>///<password>` (the `///password` may be omitted, and `/<soft-key>` and `//<hard-key>` maybe repeated and mixed). The secret can be a hex string, mnemonic phrase or a string (to be padded)
|
|
143
|
+
*/
|
|
144
|
+
addFromUri(suri, meta = {}, type = this.type) {
|
|
145
|
+
return this.addPair(this.createFromUri(suri, meta, type));
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* @name createFromJson
|
|
149
|
+
* @description Creates a pair from a JSON keyfile
|
|
150
|
+
*/
|
|
151
|
+
createFromJson({ address, encoded, encoding: { content, type, version }, meta }, ignoreChecksum) {
|
|
152
|
+
if (version === "3" && content[0] !== "pkcs8") throw new Error(`Unable to decode non-pkcs8 type, [${content.join(",")}] found}`);
|
|
153
|
+
const cryptoType = version === "0" || !Array.isArray(content) ? this.type : content[1];
|
|
154
|
+
if (!cryptoType) throw new Error("cryptoType is undefined");
|
|
155
|
+
const encType = !Array.isArray(type) ? [type] : type;
|
|
156
|
+
if (!["sr25519"].includes(cryptoType)) throw new Error(`Unknown crypto type ${cryptoType}`);
|
|
157
|
+
const publicKey = (0, _polkadot_util_is.isHex)(address) ? (0, _polkadot_util_hex.hexToU8a)(address) : this.decodeAddress(address, ignoreChecksum);
|
|
158
|
+
const decoded = (0, _polkadot_util_is.isHex)(encoded) ? (0, _polkadot_util_hex.hexToU8a)(encoded) : (0, _prosopo_util_crypto.base64Decode)(encoded);
|
|
159
|
+
return require_index.createPair({
|
|
160
|
+
toSS58: this.encodeAddress,
|
|
161
|
+
type: cryptoType
|
|
162
|
+
}, {
|
|
163
|
+
publicKey,
|
|
164
|
+
secretKey: /* @__PURE__ */ new Uint8Array()
|
|
165
|
+
}, meta, decoded, encType);
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* @name createFromPair
|
|
169
|
+
* @summary Creates a pair from an explicit publicKey/secreteKey combination
|
|
170
|
+
*/
|
|
171
|
+
createFromPair(pair, meta = {}, type = this.type) {
|
|
172
|
+
return require_index.createPair({
|
|
173
|
+
toSS58: this.encodeAddress,
|
|
174
|
+
type
|
|
175
|
+
}, pair, meta, null);
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* @name createFromUri
|
|
179
|
+
* @summary Creates a Keypair from an suri
|
|
180
|
+
* @description This creates a pair from the suri, but does not add it to the keyring
|
|
181
|
+
*/
|
|
182
|
+
createFromUri(_suri, meta = {}, type = this.type) {
|
|
183
|
+
const suri = _suri.startsWith("//") ? `${DEV_PHRASE}${_suri}` : _suri;
|
|
184
|
+
const { derivePath, password, path, phrase } = (0, _prosopo_util_crypto.keyExtractSuri)(suri);
|
|
185
|
+
let seed;
|
|
186
|
+
if ((0, _polkadot_util_is.isHex)(phrase, 256)) seed = (0, _polkadot_util_hex.hexToU8a)(phrase);
|
|
187
|
+
else {
|
|
188
|
+
const parts = phrase.split(" ");
|
|
189
|
+
if ([
|
|
190
|
+
12,
|
|
191
|
+
15,
|
|
192
|
+
18,
|
|
193
|
+
21,
|
|
194
|
+
24
|
|
195
|
+
].includes(parts.length)) seed = type === "ethereum" ? (() => {
|
|
196
|
+
throw new Error("Not implemented - Prosopo Keyring supports sr25519 only");
|
|
197
|
+
})() : (0, _prosopo_util_crypto.mnemonicToMiniSecret)(phrase, password);
|
|
198
|
+
else {
|
|
199
|
+
if (phrase.length > 32) throw new Error("specified phrase is not a valid mnemonic and is invalid as a raw seed at > 32 bytes");
|
|
200
|
+
seed = (0, _polkadot_util.stringToU8a)(phrase.padEnd(32));
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
const derived = (0, _prosopo_util_crypto.keyFromPath)(PairFromSeed[type](seed), path, type);
|
|
204
|
+
return require_index.createPair({
|
|
205
|
+
toSS58: this.encodeAddress,
|
|
206
|
+
type
|
|
207
|
+
}, derived, meta, null);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* @name getPair
|
|
211
|
+
* @summary Retrieves an account keyring pair from the Keyring Pair Dictionary, given an account address
|
|
212
|
+
* @description Returns a keyring pair value from the keyring pair dictionary by performing
|
|
213
|
+
* a key lookup using the provided account address or public key (after decoding it).
|
|
214
|
+
*/
|
|
215
|
+
getPair(address) {
|
|
216
|
+
return this.#pairs.get(address);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* @name getPairs
|
|
220
|
+
* @summary Retrieves all account keyring pairs from the Keyring Pair Dictionary
|
|
221
|
+
* @description Returns an array list of all the keyring pair values that are stored in the keyring pair dictionary.
|
|
222
|
+
*/
|
|
223
|
+
getPairs() {
|
|
224
|
+
return this.#pairs.all();
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* @name getPublicKeys
|
|
228
|
+
* @summary Retrieves Public Keys of all Keyring Pairs stored in the Keyring Pair Dictionary
|
|
229
|
+
* @description Returns an array list of all the public keys associated with each of the keyring pair values that are stored in the keyring pair dictionary.
|
|
230
|
+
*/
|
|
231
|
+
getPublicKeys() {
|
|
232
|
+
return this.#pairs.all().map(pairToPublic);
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* @name removePair
|
|
236
|
+
* @description Deletes the provided input address or public key from the stored Keyring Pair Dictionary.
|
|
237
|
+
*/
|
|
238
|
+
removePair(address) {
|
|
239
|
+
this.#pairs.remove(address);
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* @name setSS58Format;
|
|
243
|
+
* @description Sets the ss58 format for the keyring
|
|
244
|
+
*/
|
|
245
|
+
setSS58Format(ss58) {
|
|
246
|
+
this.#ss58 = ss58;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* @name toJson
|
|
250
|
+
* @summary Returns a JSON object associated with the input argument that contains metadata assocated with an account
|
|
251
|
+
* @description Returns a JSON object containing the metadata associated with an account
|
|
252
|
+
* when valid address or public key and when the account passphrase is provided if the account secret
|
|
253
|
+
* is not already unlocked and available in memory. Note that in [Polkadot-JS Apps](https://github.com/polkadot-js/apps) the user
|
|
254
|
+
* may backup their account to a JSON file that contains this information.
|
|
255
|
+
*/
|
|
256
|
+
toJson(address, passphrase) {
|
|
257
|
+
return this.#pairs.get(address).toJson(passphrase);
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
//#endregion
|
|
270
261
|
exports.DEV_PHRASE = DEV_PHRASE;
|
|
271
262
|
exports.Keyring = Keyring;
|
|
@@ -1,28 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
remove(address) {
|
|
25
|
-
delete this.#map[utilCrypto.decodeAddress(address).toString()];
|
|
26
|
-
}
|
|
27
|
-
}
|
|
1
|
+
let _prosopo_util_crypto = require("@prosopo/util-crypto");
|
|
2
|
+
let _polkadot_util = require("@polkadot/util");
|
|
3
|
+
let _prosopo_util = require("@prosopo/util");
|
|
4
|
+
//#region src/keyring/pairs.ts
|
|
5
|
+
var Pairs = class {
|
|
6
|
+
#map = {};
|
|
7
|
+
add(pair) {
|
|
8
|
+
this.#map[(0, _prosopo_util_crypto.decodeAddress)(pair.address).toString()] = pair;
|
|
9
|
+
return pair;
|
|
10
|
+
}
|
|
11
|
+
all() {
|
|
12
|
+
return Object.values(this.#map);
|
|
13
|
+
}
|
|
14
|
+
get(address) {
|
|
15
|
+
const pair = this.#map[(0, _prosopo_util_crypto.decodeAddress)(address).toString()];
|
|
16
|
+
if (!pair) throw new Error(`Unable to retrieve keypair '${(0, _polkadot_util.isU8a)(address) || (0, _polkadot_util.isHex)(address) ? (0, _prosopo_util.u8aToHex)((0, _polkadot_util.u8aToU8a)(address)) : address}'`);
|
|
17
|
+
return pair;
|
|
18
|
+
}
|
|
19
|
+
remove(address) {
|
|
20
|
+
delete this.#map[(0, _prosopo_util_crypto.decodeAddress)(address).toString()];
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
//#endregion
|
|
28
24
|
exports.Pairs = Pairs;
|