@palliora.org/chainsdk 0.3.0 → 0.3.2

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/dist/index.d.cts CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as _polkadot_util_crypto_types from '@polkadot/util-crypto/types';
2
2
  import { KeyringPair } from '@polkadot/keyring/types';
3
+ export { KeyringPair } from '@polkadot/keyring/types';
3
4
  import * as _polkadot_types_types from '@polkadot/types/types';
4
5
  import { ISubmittableResult } from '@polkadot/types/types';
5
6
  import { ApiPromise, Keyring, WsProvider } from '@polkadot/api';
@@ -15,6 +16,7 @@ import * as utilCrypto from '@polkadot/util-crypto';
15
16
  export { utilCrypto };
16
17
  import * as wasmCrypto from '@polkadot/wasm-crypto';
17
18
  export { wasmCrypto };
19
+ export { Keyring } from '@polkadot/keyring';
18
20
 
19
21
  declare enum CryptoType {
20
22
  SR25519 = "sr25519",
@@ -36,7 +38,7 @@ declare enum AccountSourceType {
36
38
  * @param cryptoType - The crypto type: 'sr25519' | 'ed25519' | 'ecdsa' (default: 'sr25519').
37
39
  * @returns The keypair object.
38
40
  */
39
- declare function createAccount(input: string, type: AccountSourceType, name?: string, cryptoType?: CryptoType): Promise<KeyringPair>;
41
+ declare function createAccount(input: string, type: AccountSourceType, name?: string, cryptoType?: CryptoType, signedMsg?: string): Promise<KeyringPair>;
40
42
  declare function pairFromPrivateKeyHex(privateKeyHex: string, cryptoType: CryptoType): _polkadot_util_crypto_types.Keypair;
41
43
 
42
44
  declare const API_RPC: {
@@ -816,8 +818,6 @@ declare function watchForSubmissionReceipt(requestId: string, timeoutMs?: number
816
818
  */
817
819
  declare function getAgreementCreatedRequestId(blockHeight: number, extrinsicIndex: number): Promise<string | null>;
818
820
 
819
- declare const provider: WsProvider;
820
-
821
821
  interface ComputeContract {
822
822
  contract_type: "Active" | "Dormant";
823
823
  guardians: GuardianAddress[];
@@ -1258,6 +1258,7 @@ declare let PALLIORA_WS: string;
1258
1258
  declare let PALLIORA_RPC_URL: string;
1259
1259
  declare let DEBUG: boolean;
1260
1260
  declare let TX_WAIT_FINALIZATION: boolean;
1261
+ declare let provider: WsProvider;
1261
1262
  declare function configure(opts: {
1262
1263
  pallioraWs?: string;
1263
1264
  pallioraRpcUrl?: string;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as _polkadot_util_crypto_types from '@polkadot/util-crypto/types';
2
2
  import { KeyringPair } from '@polkadot/keyring/types';
3
+ export { KeyringPair } from '@polkadot/keyring/types';
3
4
  import * as _polkadot_types_types from '@polkadot/types/types';
4
5
  import { ISubmittableResult } from '@polkadot/types/types';
5
6
  import { ApiPromise, Keyring, WsProvider } from '@polkadot/api';
@@ -15,6 +16,7 @@ import * as utilCrypto from '@polkadot/util-crypto';
15
16
  export { utilCrypto };
16
17
  import * as wasmCrypto from '@polkadot/wasm-crypto';
17
18
  export { wasmCrypto };
19
+ export { Keyring } from '@polkadot/keyring';
18
20
 
19
21
  declare enum CryptoType {
20
22
  SR25519 = "sr25519",
@@ -36,7 +38,7 @@ declare enum AccountSourceType {
36
38
  * @param cryptoType - The crypto type: 'sr25519' | 'ed25519' | 'ecdsa' (default: 'sr25519').
37
39
  * @returns The keypair object.
38
40
  */
39
- declare function createAccount(input: string, type: AccountSourceType, name?: string, cryptoType?: CryptoType): Promise<KeyringPair>;
41
+ declare function createAccount(input: string, type: AccountSourceType, name?: string, cryptoType?: CryptoType, signedMsg?: string): Promise<KeyringPair>;
40
42
  declare function pairFromPrivateKeyHex(privateKeyHex: string, cryptoType: CryptoType): _polkadot_util_crypto_types.Keypair;
41
43
 
42
44
  declare const API_RPC: {
@@ -816,8 +818,6 @@ declare function watchForSubmissionReceipt(requestId: string, timeoutMs?: number
816
818
  */
817
819
  declare function getAgreementCreatedRequestId(blockHeight: number, extrinsicIndex: number): Promise<string | null>;
818
820
 
819
- declare const provider: WsProvider;
820
-
821
821
  interface ComputeContract {
822
822
  contract_type: "Active" | "Dormant";
823
823
  guardians: GuardianAddress[];
@@ -1258,6 +1258,7 @@ declare let PALLIORA_WS: string;
1258
1258
  declare let PALLIORA_RPC_URL: string;
1259
1259
  declare let DEBUG: boolean;
1260
1260
  declare let TX_WAIT_FINALIZATION: boolean;
1261
+ declare let provider: WsProvider;
1261
1262
  declare function configure(opts: {
1262
1263
  pallioraWs?: string;
1263
1264
  pallioraRpcUrl?: string;
package/dist/index.js CHANGED
@@ -1,3 +1,14 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __typeError = (msg) => {
3
+ throw TypeError(msg);
4
+ };
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
8
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
9
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
10
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
11
+
1
12
  // src/account/account.ts
2
13
  import { waitReady } from "@polkadot/wasm-crypto";
3
14
 
@@ -21,7 +32,9 @@ var AccountSourceType = /* @__PURE__ */ ((AccountSourceType2) => {
21
32
  import { Keyring } from "@polkadot/api";
22
33
  import { hexToU8a } from "@polkadot/util";
23
34
  import { ed25519PairFromSecret, secp256k1PairFromSeed } from "@polkadot/util-crypto";
24
- async function createAccount(input, type, name = "default", cryptoType = "sr25519" /* SR25519 */) {
35
+ import { keccak256 } from "viem";
36
+ import { poseidon1, poseidon2 } from "poseidon-lite";
37
+ async function createAccount(input, type, name = "default", cryptoType = "sr25519" /* SR25519 */, signedMsg = "sign") {
25
38
  await waitReady();
26
39
  const keyring2 = new Keyring({ type: cryptoType });
27
40
  switch (type) {
@@ -48,6 +61,12 @@ async function createAccount(input, type, name = "default", cryptoType = "sr2551
48
61
  cryptoType
49
62
  );
50
63
  return account;
64
+ case "derived" /* DERIVED */:
65
+ if (!input.startsWith("0x")) {
66
+ throw new Error("Invalid signature format for derived account. Expected hex string starting with 0x.");
67
+ }
68
+ const signKeyBytes = sigToSeed(signedMsg, input);
69
+ return createAccount(signKeyBytes, "seed" /* SEED */, name, cryptoType);
51
70
  default:
52
71
  throw new Error("Invalid input type");
53
72
  }
@@ -83,6 +102,15 @@ function pairFromPrivateKeyHex(privateKeyHex, cryptoType) {
83
102
  throw new Error(`Unsupported crypto type: ${cryptoType}`);
84
103
  }
85
104
  }
105
+ function sigToSeed(msg, sign) {
106
+ const seed = BigInt(keccak256(sign));
107
+ const encoder = new TextEncoder();
108
+ const signBytes = encoder.encode(msg);
109
+ const signHex = Array.from(signBytes).map((b) => b.toString(16).padStart(2, "0")).join("");
110
+ const SALT_SIGN = poseidon1([BigInt(`0x${signHex}`)]);
111
+ const signKeyBytes = poseidon2([seed.toString(), SALT_SIGN]).toString(16).slice(0, 64);
112
+ return signKeyBytes;
113
+ }
86
114
 
87
115
  // src/chain/spec.ts
88
116
  var API_RPC = {
@@ -756,16 +784,19 @@ function assert(condition, message) {
756
784
  }
757
785
 
758
786
  // src/config.ts
787
+ import { WsProvider } from "@polkadot/api";
759
788
  var env = typeof process !== "undefined" && process.env ? process.env : {};
760
789
  var PALLIORA_WS = env.PALLIORA_WS || "wss://manas-rpc.palliora.org";
761
790
  var PALLIORA_RPC_URL = env.PALLIORA_RPC_URL || "wss://manas-rpc.palliora.org";
762
791
  var DEBUG = env.DEBUG === "true" || false;
763
792
  var TX_WAIT_FINALIZATION = env.TX_WAIT_FINALIZATION === "true" || false;
793
+ var provider = new WsProvider(PALLIORA_WS, 1e4);
764
794
  function configure(opts) {
765
795
  if (opts.pallioraWs !== void 0) PALLIORA_WS = opts.pallioraWs;
766
796
  if (opts.pallioraRpcUrl !== void 0) PALLIORA_RPC_URL = opts.pallioraRpcUrl;
767
797
  if (opts.debug !== void 0) DEBUG = opts.debug;
768
798
  if (opts.txWaitFinalization !== void 0) TX_WAIT_FINALIZATION = opts.txWaitFinalization;
799
+ provider = new WsProvider(PALLIORA_WS, 1e4);
769
800
  }
770
801
 
771
802
  // src/utils/helper.ts
@@ -867,20 +898,24 @@ var tokenToBigint = toAtomicPaliAmount;
867
898
 
868
899
  // src/chain/singleton.ts
869
900
  import { ApiPromise, Keyring as Keyring2 } from "@polkadot/api";
870
-
871
- // src/chain/wsProvider.ts
872
- import { WsProvider } from "@polkadot/api";
873
- var provider = new WsProvider(PALLIORA_WS, 1e4);
874
-
875
- // src/chain/singleton.ts
876
901
  import { waitReady as waitReady2 } from "@polkadot/wasm-crypto";
877
902
  var api = null;
903
+ var apiUrl = null;
878
904
  var keyring = null;
879
905
  var encKeyring = null;
880
906
  var apiListenersAttached = false;
881
907
  var apiTeardownInProgress = false;
882
908
  async function getApi(cb) {
883
909
  if (!provider) return;
910
+ if (api && PALLIORA_WS !== apiUrl) {
911
+ const staleApi = api;
912
+ api = null;
913
+ apiUrl = null;
914
+ apiListenersAttached = false;
915
+ apiTeardownInProgress = false;
916
+ await staleApi.disconnect().catch(() => {
917
+ });
918
+ }
884
919
  if (!api) {
885
920
  api = await ApiPromise.create({
886
921
  provider,
@@ -888,6 +923,7 @@ async function getApi(cb) {
888
923
  types: API_TYPES,
889
924
  signedExtensions: API_EXTENSIONS
890
925
  });
926
+ apiUrl = PALLIORA_WS;
891
927
  }
892
928
  const isNode = typeof process !== "undefined" && typeof process.exit === "function";
893
929
  const isTest = typeof process !== "undefined" && process.env?.NODE_ENV === "test";
@@ -2107,6 +2143,464 @@ export * from "@polkadot/util";
2107
2143
  import * as utilCrypto from "@polkadot/util-crypto";
2108
2144
  import * as wasmCrypto from "@polkadot/wasm-crypto";
2109
2145
  import { ApiPromise as ApiPromise2, WsProvider as WsProvider3, HttpProvider } from "@polkadot/api";
2146
+
2147
+ // node_modules/.pnpm/@polkadot+keyring@13.5.9_@polkadot+util-crypto@13.5.9_@polkadot+util@13.5.9__@polkadot+util@13.5.9/node_modules/@polkadot/keyring/bundle.js
2148
+ import { decodeAddress as decodeAddress3, encodeAddress as encodeAddress2, setSS58Format } from "@polkadot/util-crypto";
2149
+
2150
+ // node_modules/.pnpm/@polkadot+keyring@13.5.9_@polkadot+util-crypto@13.5.9_@polkadot+util@13.5.9__@polkadot+util@13.5.9/node_modules/@polkadot/keyring/keyring.js
2151
+ import { hexToU8a as hexToU8a2, isHex as isHex2, stringToU8a } from "@polkadot/util";
2152
+ import { base64Decode, decodeAddress as decodeAddress2, ed25519PairFromSeed as ed25519FromSeed2, encodeAddress, ethereumEncode as ethereumEncode2, hdEthereum, keyExtractSuri, keyFromPath as keyFromPath2, mnemonicToLegacySeed, mnemonicToMiniSecret, secp256k1PairFromSeed as secp256k1FromSeed2, sr25519PairFromSeed as sr25519FromSeed2 } from "@polkadot/util-crypto";
2153
+
2154
+ // node_modules/.pnpm/@polkadot+keyring@13.5.9_@polkadot+util-crypto@13.5.9_@polkadot+util@13.5.9__@polkadot+util@13.5.9/node_modules/@polkadot/keyring/pair/index.js
2155
+ import { objectSpread as objectSpread2, u8aConcat as u8aConcat2, u8aEmpty, u8aEq as u8aEq2, u8aToHex, u8aToU8a } from "@polkadot/util";
2156
+ import { blake2AsU8a, ed25519PairFromSeed as ed25519FromSeed, ed25519Sign, ethereumEncode, keccakAsU8a, keyExtractPath, keyFromPath, secp256k1Compress, secp256k1Expand, secp256k1PairFromSeed as secp256k1FromSeed, secp256k1Sign, signatureVerify, sr25519PairFromSeed as sr25519FromSeed, sr25519Sign, sr25519VrfSign, sr25519VrfVerify } from "@polkadot/util-crypto";
2157
+
2158
+ // node_modules/.pnpm/@polkadot+keyring@13.5.9_@polkadot+util-crypto@13.5.9_@polkadot+util@13.5.9__@polkadot+util@13.5.9/node_modules/@polkadot/keyring/pair/decode.js
2159
+ import { u8aEq } from "@polkadot/util";
2160
+ import { jsonDecryptData } from "@polkadot/util-crypto";
2161
+
2162
+ // node_modules/.pnpm/@polkadot+keyring@13.5.9_@polkadot+util-crypto@13.5.9_@polkadot+util@13.5.9__@polkadot+util@13.5.9/node_modules/@polkadot/keyring/pair/defaults.js
2163
+ var PAIR_DIV = new Uint8Array([161, 35, 3, 33, 0]);
2164
+ var PAIR_HDR = new Uint8Array([48, 83, 2, 1, 1, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32]);
2165
+ var PUB_LENGTH = 32;
2166
+ var SEC_LENGTH = 64;
2167
+ var SEED_LENGTH = 32;
2168
+
2169
+ // node_modules/.pnpm/@polkadot+keyring@13.5.9_@polkadot+util-crypto@13.5.9_@polkadot+util@13.5.9__@polkadot+util@13.5.9/node_modules/@polkadot/keyring/pair/decode.js
2170
+ var SEED_OFFSET = PAIR_HDR.length;
2171
+ function decodePair(passphrase, encrypted, _encType) {
2172
+ const encType = Array.isArray(_encType) || _encType === void 0 ? _encType : [_encType];
2173
+ const decrypted = jsonDecryptData(encrypted, passphrase, encType);
2174
+ const header = decrypted.subarray(0, PAIR_HDR.length);
2175
+ if (!u8aEq(header, PAIR_HDR)) {
2176
+ throw new Error("Invalid encoding header found in body");
2177
+ }
2178
+ let secretKey = decrypted.subarray(SEED_OFFSET, SEED_OFFSET + SEC_LENGTH);
2179
+ let divOffset = SEED_OFFSET + SEC_LENGTH;
2180
+ let divider = decrypted.subarray(divOffset, divOffset + PAIR_DIV.length);
2181
+ if (!u8aEq(divider, PAIR_DIV)) {
2182
+ divOffset = SEED_OFFSET + SEED_LENGTH;
2183
+ secretKey = decrypted.subarray(SEED_OFFSET, divOffset);
2184
+ divider = decrypted.subarray(divOffset, divOffset + PAIR_DIV.length);
2185
+ if (!u8aEq(divider, PAIR_DIV)) {
2186
+ throw new Error("Invalid encoding divider found in body");
2187
+ }
2188
+ }
2189
+ const pubOffset = divOffset + PAIR_DIV.length;
2190
+ const publicKey = decrypted.subarray(pubOffset, pubOffset + PUB_LENGTH);
2191
+ return {
2192
+ publicKey,
2193
+ secretKey
2194
+ };
2195
+ }
2196
+
2197
+ // node_modules/.pnpm/@polkadot+keyring@13.5.9_@polkadot+util-crypto@13.5.9_@polkadot+util@13.5.9__@polkadot+util@13.5.9/node_modules/@polkadot/keyring/pair/encode.js
2198
+ import { u8aConcat } from "@polkadot/util";
2199
+ import { naclEncrypt, scryptEncode, scryptToU8a } from "@polkadot/util-crypto";
2200
+ function encodePair({ publicKey, secretKey }, passphrase) {
2201
+ if (!secretKey) {
2202
+ throw new Error("Expected a valid secretKey to be passed to encode");
2203
+ }
2204
+ const encoded = u8aConcat(PAIR_HDR, secretKey, PAIR_DIV, publicKey);
2205
+ if (!passphrase) {
2206
+ return encoded;
2207
+ }
2208
+ const { params, password, salt } = scryptEncode(passphrase);
2209
+ const { encrypted, nonce } = naclEncrypt(encoded, password.subarray(0, 32));
2210
+ return u8aConcat(scryptToU8a(salt, params), nonce, encrypted);
2211
+ }
2212
+
2213
+ // node_modules/.pnpm/@polkadot+keyring@13.5.9_@polkadot+util-crypto@13.5.9_@polkadot+util@13.5.9__@polkadot+util@13.5.9/node_modules/@polkadot/keyring/pair/toJson.js
2214
+ import { objectSpread } from "@polkadot/util";
2215
+ import { jsonEncryptFormat } from "@polkadot/util-crypto";
2216
+ function pairToJson(type, { address, meta }, encoded, isEncrypted) {
2217
+ return objectSpread(jsonEncryptFormat(encoded, ["pkcs8", type], isEncrypted), {
2218
+ address,
2219
+ meta
2220
+ });
2221
+ }
2222
+
2223
+ // node_modules/.pnpm/@polkadot+keyring@13.5.9_@polkadot+util-crypto@13.5.9_@polkadot+util@13.5.9__@polkadot+util@13.5.9/node_modules/@polkadot/keyring/pair/index.js
2224
+ var SIG_TYPE_NONE = new Uint8Array();
2225
+ var TYPE_FROM_SEED = {
2226
+ ecdsa: secp256k1FromSeed,
2227
+ ed25519: ed25519FromSeed,
2228
+ ethereum: secp256k1FromSeed,
2229
+ sr25519: sr25519FromSeed
2230
+ };
2231
+ var TYPE_PREFIX = {
2232
+ ecdsa: new Uint8Array([2]),
2233
+ ed25519: new Uint8Array([0]),
2234
+ ethereum: new Uint8Array([2]),
2235
+ sr25519: new Uint8Array([1])
2236
+ };
2237
+ var TYPE_SIGNATURE = {
2238
+ ecdsa: (m, p) => secp256k1Sign(m, p, "blake2"),
2239
+ ed25519: ed25519Sign,
2240
+ ethereum: (m, p) => secp256k1Sign(m, p, "keccak"),
2241
+ sr25519: sr25519Sign
2242
+ };
2243
+ var TYPE_ADDRESS = {
2244
+ ecdsa: (p) => p.length > 32 ? blake2AsU8a(p) : p,
2245
+ ed25519: (p) => p,
2246
+ ethereum: (p) => p.length === 20 ? p : keccakAsU8a(secp256k1Expand(p)),
2247
+ sr25519: (p) => p
2248
+ };
2249
+ function isLocked(secretKey) {
2250
+ return !secretKey || u8aEmpty(secretKey);
2251
+ }
2252
+ function vrfHash(proof, context, extra) {
2253
+ return blake2AsU8a(u8aConcat2(context || "", extra || "", proof));
2254
+ }
2255
+ function createPair({ toSS58, type }, { publicKey, secretKey }, meta = {}, encoded = null, encTypes) {
2256
+ const decodePkcs8 = (passphrase, userEncoded) => {
2257
+ const decoded = decodePair(passphrase, userEncoded || encoded, encTypes);
2258
+ if (decoded.secretKey.length === 64) {
2259
+ publicKey = decoded.publicKey;
2260
+ secretKey = decoded.secretKey;
2261
+ } else {
2262
+ const pair = TYPE_FROM_SEED[type](decoded.secretKey);
2263
+ publicKey = pair.publicKey;
2264
+ secretKey = pair.secretKey;
2265
+ }
2266
+ };
2267
+ const recode = (passphrase) => {
2268
+ isLocked(secretKey) && encoded && decodePkcs8(passphrase, encoded);
2269
+ encoded = encodePair({ publicKey, secretKey }, passphrase);
2270
+ encTypes = void 0;
2271
+ return encoded;
2272
+ };
2273
+ const encodeAddress3 = () => {
2274
+ const raw = TYPE_ADDRESS[type](publicKey);
2275
+ return type === "ethereum" ? ethereumEncode(raw) : toSS58(raw);
2276
+ };
2277
+ return {
2278
+ get address() {
2279
+ return encodeAddress3();
2280
+ },
2281
+ get addressRaw() {
2282
+ const raw = TYPE_ADDRESS[type](publicKey);
2283
+ return type === "ethereum" ? raw.slice(-20) : raw;
2284
+ },
2285
+ get isLocked() {
2286
+ return isLocked(secretKey);
2287
+ },
2288
+ get meta() {
2289
+ return meta;
2290
+ },
2291
+ get publicKey() {
2292
+ return publicKey;
2293
+ },
2294
+ get type() {
2295
+ return type;
2296
+ },
2297
+ // eslint-disable-next-line sort-keys
2298
+ decodePkcs8,
2299
+ derive: (suri, meta2) => {
2300
+ if (type === "ethereum") {
2301
+ throw new Error("Unable to derive on this keypair");
2302
+ } else if (isLocked(secretKey)) {
2303
+ throw new Error("Cannot derive on a locked keypair");
2304
+ }
2305
+ const { path: path2 } = keyExtractPath(suri);
2306
+ const derived = keyFromPath({ publicKey, secretKey }, path2, type);
2307
+ return createPair({ toSS58, type }, derived, meta2, null);
2308
+ },
2309
+ encodePkcs8: (passphrase) => {
2310
+ return recode(passphrase);
2311
+ },
2312
+ lock: () => {
2313
+ secretKey = new Uint8Array();
2314
+ },
2315
+ setMeta: (additional) => {
2316
+ meta = objectSpread2({}, meta, additional);
2317
+ },
2318
+ sign: (message, options = {}) => {
2319
+ if (isLocked(secretKey)) {
2320
+ throw new Error("Cannot sign with a locked key pair");
2321
+ }
2322
+ return u8aConcat2(options.withType ? TYPE_PREFIX[type] : SIG_TYPE_NONE, TYPE_SIGNATURE[type](u8aToU8a(message), { publicKey, secretKey }));
2323
+ },
2324
+ toJson: (passphrase) => {
2325
+ const address = ["ecdsa", "ethereum"].includes(type) ? publicKey.length === 20 ? u8aToHex(publicKey) : u8aToHex(secp256k1Compress(publicKey)) : encodeAddress3();
2326
+ return pairToJson(type, { address, meta }, recode(passphrase), !!passphrase);
2327
+ },
2328
+ unlock: (passphrase) => {
2329
+ return decodePkcs8(passphrase);
2330
+ },
2331
+ verify: (message, signature, signerPublic) => {
2332
+ return signatureVerify(message, signature, TYPE_ADDRESS[type](u8aToU8a(signerPublic))).isValid;
2333
+ },
2334
+ vrfSign: (message, context, extra) => {
2335
+ if (isLocked(secretKey)) {
2336
+ throw new Error("Cannot sign with a locked key pair");
2337
+ }
2338
+ if (type === "sr25519") {
2339
+ return sr25519VrfSign(message, { secretKey }, context, extra);
2340
+ }
2341
+ const proof = TYPE_SIGNATURE[type](u8aToU8a(message), { publicKey, secretKey });
2342
+ return u8aConcat2(vrfHash(proof, context, extra), proof);
2343
+ },
2344
+ vrfVerify: (message, vrfResult, signerPublic, context, extra) => {
2345
+ if (type === "sr25519") {
2346
+ return sr25519VrfVerify(message, vrfResult, publicKey, context, extra);
2347
+ }
2348
+ const result = signatureVerify(message, u8aConcat2(TYPE_PREFIX[type], vrfResult.subarray(32)), TYPE_ADDRESS[type](u8aToU8a(signerPublic)));
2349
+ return result.isValid && u8aEq2(vrfResult.subarray(0, 32), vrfHash(vrfResult.subarray(32), context, extra));
2350
+ }
2351
+ };
2352
+ }
2353
+
2354
+ // node_modules/.pnpm/@polkadot+keyring@13.5.9_@polkadot+util-crypto@13.5.9_@polkadot+util@13.5.9__@polkadot+util@13.5.9/node_modules/@polkadot/keyring/defaults.js
2355
+ var DEV_PHRASE = "bottom drive obey lake curtain smoke basket hold race lonely fit walk";
2356
+
2357
+ // node_modules/.pnpm/@polkadot+keyring@13.5.9_@polkadot+util-crypto@13.5.9_@polkadot+util@13.5.9__@polkadot+util@13.5.9/node_modules/@polkadot/keyring/pairs.js
2358
+ import { isHex, isU8a, u8aToHex as u8aToHex2, u8aToU8a as u8aToU8a2 } from "@polkadot/util";
2359
+ import { decodeAddress } from "@polkadot/util-crypto";
2360
+ var _map;
2361
+ var Pairs = class {
2362
+ constructor() {
2363
+ __privateAdd(this, _map, {});
2364
+ }
2365
+ add(pair) {
2366
+ __privateGet(this, _map)[decodeAddress(pair.address).toString()] = pair;
2367
+ return pair;
2368
+ }
2369
+ all() {
2370
+ return Object.values(__privateGet(this, _map));
2371
+ }
2372
+ get(address) {
2373
+ const pair = __privateGet(this, _map)[decodeAddress(address).toString()];
2374
+ if (!pair) {
2375
+ throw new Error(`Unable to retrieve keypair '${isU8a(address) || isHex(address) ? u8aToHex2(u8aToU8a2(address)) : address}'`);
2376
+ }
2377
+ return pair;
2378
+ }
2379
+ remove(address) {
2380
+ delete __privateGet(this, _map)[decodeAddress(address).toString()];
2381
+ }
2382
+ };
2383
+ _map = new WeakMap();
2384
+
2385
+ // node_modules/.pnpm/@polkadot+keyring@13.5.9_@polkadot+util-crypto@13.5.9_@polkadot+util@13.5.9__@polkadot+util@13.5.9/node_modules/@polkadot/keyring/keyring.js
2386
+ var PairFromSeed = {
2387
+ ecdsa: (seed) => secp256k1FromSeed2(seed),
2388
+ ed25519: (seed) => ed25519FromSeed2(seed),
2389
+ ethereum: (seed) => secp256k1FromSeed2(seed),
2390
+ sr25519: (seed) => sr25519FromSeed2(seed)
2391
+ };
2392
+ function pairToPublic({ publicKey }) {
2393
+ return publicKey;
2394
+ }
2395
+ var _pairs, _type, _ss58;
2396
+ var Keyring3 = class {
2397
+ constructor(options = {}) {
2398
+ __privateAdd(this, _pairs);
2399
+ __privateAdd(this, _type);
2400
+ __privateAdd(this, _ss58);
2401
+ __publicField(this, "decodeAddress", decodeAddress2);
2402
+ /**
2403
+ * @name encodeAddress
2404
+ * @description Encodes the input into an ss58 representation
2405
+ */
2406
+ __publicField(this, "encodeAddress", (address, ss58Format) => {
2407
+ return this.type === "ethereum" ? ethereumEncode2(address) : encodeAddress(address, ss58Format ?? __privateGet(this, _ss58));
2408
+ });
2409
+ options.type = options.type || "ed25519";
2410
+ if (!["ecdsa", "ethereum", "ed25519", "sr25519"].includes(options.type || "undefined")) {
2411
+ throw new Error(`Expected a keyring type of either 'ed25519', 'sr25519', 'ethereum' or 'ecdsa', found '${options.type || "unknown"}`);
2412
+ }
2413
+ __privateSet(this, _pairs, new Pairs());
2414
+ __privateSet(this, _ss58, options.ss58Format);
2415
+ __privateSet(this, _type, options.type);
2416
+ }
2417
+ /**
2418
+ * @description retrieve the pairs (alias for getPairs)
2419
+ */
2420
+ get pairs() {
2421
+ return this.getPairs();
2422
+ }
2423
+ /**
2424
+ * @description retrieve the publicKeys (alias for getPublicKeys)
2425
+ */
2426
+ get publicKeys() {
2427
+ return this.getPublicKeys();
2428
+ }
2429
+ /**
2430
+ * @description Returns the type of the keyring, ed25519, sr25519 or ecdsa
2431
+ */
2432
+ get type() {
2433
+ return __privateGet(this, _type);
2434
+ }
2435
+ /**
2436
+ * @name addPair
2437
+ * @summary Stores an account, given a keyring pair, as a Key/Value (public key, pair) in Keyring Pair Dictionary
2438
+ */
2439
+ addPair(pair) {
2440
+ return __privateGet(this, _pairs).add(pair);
2441
+ }
2442
+ /**
2443
+ * @name addFromAddress
2444
+ * @summary Stores an account, given an account address, as a Key/Value (public key, pair) in Keyring Pair Dictionary
2445
+ * @description Allows user to explicitly provide separate inputs including account address or public key, and optionally
2446
+ * the associated account metadata, and the default encoded value as arguments (that may be obtained from the json file
2447
+ * of an account backup), and then generates a keyring pair from them that it passes to
2448
+ * `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.
2449
+ */
2450
+ addFromAddress(address, meta = {}, encoded = null, type = this.type, ignoreChecksum, encType) {
2451
+ const publicKey = this.decodeAddress(address, ignoreChecksum);
2452
+ return this.addPair(createPair({ toSS58: this.encodeAddress, type }, { publicKey, secretKey: new Uint8Array() }, meta, encoded, encType));
2453
+ }
2454
+ /**
2455
+ * @name addFromJson
2456
+ * @summary Stores an account, given JSON data, as a Key/Value (public key, pair) in Keyring Pair Dictionary
2457
+ * @description Allows user to provide a json object argument that contains account information (that may be obtained from the json file
2458
+ * of an account backup), and then generates a keyring pair from it that it passes to
2459
+ * `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.
2460
+ */
2461
+ addFromJson(json, ignoreChecksum) {
2462
+ return this.addPair(this.createFromJson(json, ignoreChecksum));
2463
+ }
2464
+ /**
2465
+ * @name addFromMnemonic
2466
+ * @summary Stores an account, given a mnemonic, as a Key/Value (public key, pair) in Keyring Pair Dictionary
2467
+ * @description Allows user to provide a mnemonic (seed phrase that is provided when account is originally created)
2468
+ * argument and a metadata argument that contains account information (that may be obtained from the json file
2469
+ * of an account backup), and then generates a keyring pair from it that it passes to
2470
+ * `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.
2471
+ */
2472
+ addFromMnemonic(mnemonic, meta = {}, type = this.type, wordlist) {
2473
+ return this.addFromUri(mnemonic, meta, type, wordlist);
2474
+ }
2475
+ /**
2476
+ * @name addFromPair
2477
+ * @summary Stores an account created from an explicit publicKey/secreteKey combination
2478
+ */
2479
+ addFromPair(pair, meta = {}, type = this.type) {
2480
+ return this.addPair(this.createFromPair(pair, meta, type));
2481
+ }
2482
+ /**
2483
+ * @name addFromSeed
2484
+ * @summary Stores an account, given seed data, as a Key/Value (public key, pair) in Keyring Pair Dictionary
2485
+ * @description Stores in a keyring pair dictionary the public key of the pair as a key and the pair as the associated value.
2486
+ * Allows user to provide the account seed as an argument, and then generates a keyring pair from it that it passes to
2487
+ * `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.
2488
+ */
2489
+ addFromSeed(seed, meta = {}, type = this.type) {
2490
+ return this.addPair(createPair({ toSS58: this.encodeAddress, type }, PairFromSeed[type](seed), meta, null));
2491
+ }
2492
+ /**
2493
+ * @name addFromUri
2494
+ * @summary Creates an account via an suri
2495
+ * @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)
2496
+ */
2497
+ addFromUri(suri, meta = {}, type = this.type, wordlist) {
2498
+ return this.addPair(this.createFromUri(suri, meta, type, wordlist));
2499
+ }
2500
+ /**
2501
+ * @name createFromJson
2502
+ * @description Creates a pair from a JSON keyfile
2503
+ */
2504
+ createFromJson({ address, encoded, encoding: { content, type, version }, meta }, ignoreChecksum) {
2505
+ if (version === "3" && content[0] !== "pkcs8") {
2506
+ throw new Error(`Unable to decode non-pkcs8 type, [${content.join(",")}] found}`);
2507
+ }
2508
+ const cryptoType = version === "0" || !Array.isArray(content) ? this.type : content[1];
2509
+ const encType = !Array.isArray(type) ? [type] : type;
2510
+ if (!["ed25519", "sr25519", "ecdsa", "ethereum"].includes(cryptoType)) {
2511
+ throw new Error(`Unknown crypto type ${cryptoType}`);
2512
+ }
2513
+ const publicKey = isHex2(address) ? hexToU8a2(address) : this.decodeAddress(address, ignoreChecksum);
2514
+ const decoded = isHex2(encoded) ? hexToU8a2(encoded) : base64Decode(encoded);
2515
+ return createPair({ toSS58: this.encodeAddress, type: cryptoType }, { publicKey, secretKey: new Uint8Array() }, meta, decoded, encType);
2516
+ }
2517
+ /**
2518
+ * @name createFromPair
2519
+ * @summary Creates a pair from an explicit publicKey/secreteKey combination
2520
+ */
2521
+ createFromPair(pair, meta = {}, type = this.type) {
2522
+ return createPair({ toSS58: this.encodeAddress, type }, pair, meta, null);
2523
+ }
2524
+ /**
2525
+ * @name createFromUri
2526
+ * @summary Creates a Keypair from an suri
2527
+ * @description This creates a pair from the suri, but does not add it to the keyring
2528
+ */
2529
+ createFromUri(_suri, meta = {}, type = this.type, wordlist) {
2530
+ const suri = _suri.startsWith("//") ? `${DEV_PHRASE}${_suri}` : _suri;
2531
+ const { derivePath, password, path: path2, phrase } = keyExtractSuri(suri);
2532
+ let seed;
2533
+ const isPhraseHex = isHex2(phrase, 256);
2534
+ if (isPhraseHex) {
2535
+ seed = hexToU8a2(phrase);
2536
+ } else {
2537
+ const parts = phrase.split(" ");
2538
+ if ([12, 15, 18, 21, 24].includes(parts.length)) {
2539
+ seed = type === "ethereum" ? mnemonicToLegacySeed(phrase, "", false, 64) : mnemonicToMiniSecret(phrase, password, wordlist);
2540
+ } else {
2541
+ if (phrase.length > 32) {
2542
+ throw new Error("specified phrase is not a valid mnemonic and is invalid as a raw seed at > 32 bytes");
2543
+ }
2544
+ seed = stringToU8a(phrase.padEnd(32));
2545
+ }
2546
+ }
2547
+ const derived = type === "ethereum" ? isPhraseHex ? PairFromSeed[type](seed) : hdEthereum(seed, derivePath.substring(1)) : keyFromPath2(PairFromSeed[type](seed), path2, type);
2548
+ return createPair({ toSS58: this.encodeAddress, type }, derived, meta, null);
2549
+ }
2550
+ /**
2551
+ * @name getPair
2552
+ * @summary Retrieves an account keyring pair from the Keyring Pair Dictionary, given an account address
2553
+ * @description Returns a keyring pair value from the keyring pair dictionary by performing
2554
+ * a key lookup using the provided account address or public key (after decoding it).
2555
+ */
2556
+ getPair(address) {
2557
+ return __privateGet(this, _pairs).get(address);
2558
+ }
2559
+ /**
2560
+ * @name getPairs
2561
+ * @summary Retrieves all account keyring pairs from the Keyring Pair Dictionary
2562
+ * @description Returns an array list of all the keyring pair values that are stored in the keyring pair dictionary.
2563
+ */
2564
+ getPairs() {
2565
+ return __privateGet(this, _pairs).all();
2566
+ }
2567
+ /**
2568
+ * @name getPublicKeys
2569
+ * @summary Retrieves Public Keys of all Keyring Pairs stored in the Keyring Pair Dictionary
2570
+ * @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.
2571
+ */
2572
+ getPublicKeys() {
2573
+ return __privateGet(this, _pairs).all().map(pairToPublic);
2574
+ }
2575
+ /**
2576
+ * @name removePair
2577
+ * @description Deletes the provided input address or public key from the stored Keyring Pair Dictionary.
2578
+ */
2579
+ removePair(address) {
2580
+ __privateGet(this, _pairs).remove(address);
2581
+ }
2582
+ /**
2583
+ * @name setSS58Format;
2584
+ * @description Sets the ss58 format for the keyring
2585
+ */
2586
+ setSS58Format(ss58) {
2587
+ __privateSet(this, _ss58, ss58);
2588
+ }
2589
+ /**
2590
+ * @name toJson
2591
+ * @summary Returns a JSON object associated with the input argument that contains metadata assocated with an account
2592
+ * @description Returns a JSON object containing the metadata associated with an account
2593
+ * when valid address or public key and when the account passphrase is provided if the account secret
2594
+ * is not already unlocked and available in memory. Note that in [Polkadot-JS Apps](https://github.com/polkadot-js/apps) the user
2595
+ * may backup their account to a JSON file that contains this information.
2596
+ */
2597
+ toJson(address, passphrase) {
2598
+ return __privateGet(this, _pairs).get(address).toJson(passphrase);
2599
+ }
2600
+ };
2601
+ _pairs = new WeakMap();
2602
+ _type = new WeakMap();
2603
+ _ss58 = new WeakMap();
2110
2604
  export {
2111
2605
  API_EXTENSIONS,
2112
2606
  API_RPC,
@@ -2119,6 +2613,7 @@ export {
2119
2613
  DEFAULT_EMPTY_PAYLOAD,
2120
2614
  FileFromMetadataRef,
2121
2615
  HttpProvider,
2616
+ Keyring3 as Keyring,
2122
2617
  MCryptFs,
2123
2618
  MCryptFsReader,
2124
2619
  MCryptFsWriter,