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