@talismn/keyring 0.1.4 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,5 @@
1
1
  import type { KeypairCurve } from "@talismn/crypto";
2
+ export type LedgerPolkadotCurve = "ed25519" | "ethereum";
2
3
  export type AccountPlatform = "ethereum" | "polkadot" | "solana" | "bitcoin";
3
4
  export type AccountBase = {
4
5
  address: string;
@@ -21,6 +22,7 @@ export type AccountWatchOnly = AccountBase & {
21
22
  };
22
23
  export type AccountLedgerPolkadot = AccountBase & {
23
24
  type: "ledger-polkadot";
25
+ curve: LedgerPolkadotCurve;
24
26
  app: string;
25
27
  accountIndex: number;
26
28
  addressOffset: number;
@@ -6,25 +6,38 @@ export declare const isAccountOfType: <Type extends AccountType>(account: Accoun
6
6
  export declare const isAccountInTypes: <Types extends AccountType[]>(account: Account | null | undefined, types: Types) => account is AccountOfType<Types[number]>;
7
7
  declare const ACCOUNT_TYPES_OWNED: readonly ["keypair", "ledger-ethereum", "ledger-polkadot", "polkadot-vault"];
8
8
  declare const ACCOUNT_TYPES_EXTERNAL: readonly ["contact", "watch-only", "ledger-ethereum", "ledger-polkadot", "polkadot-vault", "signet"];
9
- declare const ACCOUNT_TYPES_ETHEREUM: readonly ["contact", "watch-only", "keypair", "ledger-ethereum"];
10
- declare const ACCOUNT_TYPES_POLKADOT: readonly ["contact", "watch-only", "keypair", "ledger-polkadot", "polkadot-vault", "signet"];
9
+ declare const ACCOUNT_TYPES_ADDRESS_ETHEREUM: readonly ["contact", "watch-only", "keypair", "ledger-ethereum", "ledger-polkadot"];
10
+ declare const ACCOUNT_TYPES_PLATFORM_ETHEREUM: readonly ["contact", "watch-only", "keypair", "ledger-ethereum"];
11
+ declare const ACCOUNT_TYPES_ADDRESS_SS58: readonly ["contact", "watch-only", "keypair", "ledger-polkadot", "polkadot-vault", "signet"];
12
+ declare const ACCOUNT_TYPES_PLATFORM_POLKADOT: readonly ["contact", "watch-only", "keypair", "ledger-polkadot", "polkadot-vault", "signet"];
11
13
  declare const ACCOUNT_TYPES_BITCOIN: readonly ["contact", "watch-only"];
12
14
  export declare const isAccountExternal: (account: Account | null | undefined) => account is AccountOfType<(typeof ACCOUNT_TYPES_EXTERNAL)[number]>;
13
15
  export declare const isAccountOwned: (account: Account | null | undefined) => account is AccountOfType<(typeof ACCOUNT_TYPES_OWNED)[number]>;
14
16
  export declare const isAccountPortfolio: (account: Account | null | undefined) => account is Account;
15
17
  export declare const isAccountNotContact: (acc: Account) => acc is import("./account").AccountKeypair | import("./account").AccountWatchOnly | AccountLedgerPolkadot | import("./account").AccountLedgerEthereum | import("./account").AccountPolkadotVault | import("./account").AccountSignet;
16
- type AccountEthereum = Extract<Account, {
17
- type: (typeof ACCOUNT_TYPES_ETHEREUM)[number];
18
+ type AccountAddressEthereum = Extract<Account, {
19
+ type: (typeof ACCOUNT_TYPES_ADDRESS_ETHEREUM)[number];
18
20
  }> & {
19
21
  address: `0x${string}`;
20
22
  };
21
- export declare const isAccountEthereum: (account: Account | null | undefined) => account is AccountEthereum;
22
- type AccountPolkadot = Extract<Account, {
23
- type: (typeof ACCOUNT_TYPES_POLKADOT)[number];
23
+ export declare const isAccountAddressEthereum: (account: Account | null | undefined) => account is AccountAddressEthereum;
24
+ type AccountPlatformEthereum = Extract<Account, {
25
+ type: (typeof ACCOUNT_TYPES_PLATFORM_ETHEREUM)[number];
26
+ }> & {
27
+ address: `0x${string}`;
28
+ };
29
+ export declare const isAccountPlatformEthereum: (account: Account | null | undefined) => account is AccountPlatformEthereum;
30
+ type AccountPlatformPolkadot = Extract<Account, {
31
+ type: (typeof ACCOUNT_TYPES_PLATFORM_POLKADOT)[number];
32
+ }>;
33
+ export declare const isAccountPlatformPolkadot: (account: Account | null | undefined) => account is AccountPlatformPolkadot;
34
+ type AccountAddressSs58 = Extract<Account, {
35
+ type: (typeof ACCOUNT_TYPES_ADDRESS_SS58)[number];
24
36
  }> & {
25
37
  genesisHash?: `0x${string}`;
26
38
  };
27
- export declare const isAccountPolkadot: (account: Account | null | undefined) => account is AccountPolkadot;
39
+ export declare const isAccountAddressSs58: (account: Account | null | undefined) => account is AccountAddressSs58;
40
+ export declare const isAccountLedgerPolkadot: (account: Account | null | undefined) => account is AccountLedgerPolkadot;
28
41
  export declare const isAccountLedgerPolkadotGeneric: (account: Account | null | undefined) => account is AccountLedgerPolkadot & {
29
42
  genesisHash: undefined;
30
43
  };
@@ -20,12 +20,21 @@ const isAccountPortfolio = account => {
20
20
  return isAccountOwned(account) || isAccountOfType(account, "watch-only") && account.isPortfolio;
21
21
  };
22
22
  const isAccountNotContact = acc => acc.type !== "contact";
23
- const isAccountEthereum = account => {
23
+ const isAccountAddressEthereum = account => {
24
24
  return !!account && crypto$1.isEthereumAddress(account.address);
25
25
  };
26
- const isAccountPolkadot = account => {
26
+ const isAccountPlatformEthereum = account => {
27
+ return !!account && account.type !== "ledger-polkadot" && crypto$1.isEthereumAddress(account.address);
28
+ };
29
+ const isAccountPlatformPolkadot = account => {
30
+ return !!account && account.type !== "ledger-ethereum" && (isAccountAddressEthereum(account) || isAccountAddressSs58(account));
31
+ };
32
+ const isAccountAddressSs58 = account => {
27
33
  return !!account && crypto$1.detectAddressEncoding(account.address) === "ss58";
28
34
  };
35
+ const isAccountLedgerPolkadot = account => {
36
+ return isAccountOfType(account, "ledger-polkadot");
37
+ };
29
38
  const isAccountLedgerPolkadotGeneric = account => {
30
39
  return isAccountOfType(account, "ledger-polkadot") && !account.genesisHash;
31
40
  };
@@ -139,8 +148,14 @@ class Keyring {
139
148
  });
140
149
  }
141
150
  static load(data) {
142
- // TODO: schema check ?
143
151
  if (!data.accounts || !data.mnemonics) throw new Error("Invalid data");
152
+
153
+ // automatic upgrade : set default values for newly introduced properties
154
+ for (const account of data.accounts) {
155
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
156
+ // @ts-expect-error
157
+ if (account.type === "ledger-polkadot" && !account.curve) account.curve = "ed25519";
158
+ }
144
159
  return new Keyring(data);
145
160
  }
146
161
  async checkPassword(password, reset = false) {
@@ -414,14 +429,17 @@ exports.Keyring = Keyring;
414
429
  exports.getAccountGenesisHash = getAccountGenesisHash;
415
430
  exports.getAccountPlatform = getAccountPlatform;
416
431
  exports.getAccountSignetUrl = getAccountSignetUrl;
432
+ exports.isAccountAddressEthereum = isAccountAddressEthereum;
433
+ exports.isAccountAddressSs58 = isAccountAddressSs58;
417
434
  exports.isAccountBitcoin = isAccountBitcoin;
418
- exports.isAccountEthereum = isAccountEthereum;
419
435
  exports.isAccountExternal = isAccountExternal;
420
436
  exports.isAccountInTypes = isAccountInTypes;
437
+ exports.isAccountLedgerPolkadot = isAccountLedgerPolkadot;
421
438
  exports.isAccountLedgerPolkadotGeneric = isAccountLedgerPolkadotGeneric;
422
439
  exports.isAccountLedgerPolkadotLegacy = isAccountLedgerPolkadotLegacy;
423
440
  exports.isAccountNotContact = isAccountNotContact;
424
441
  exports.isAccountOfType = isAccountOfType;
425
442
  exports.isAccountOwned = isAccountOwned;
426
- exports.isAccountPolkadot = isAccountPolkadot;
443
+ exports.isAccountPlatformEthereum = isAccountPlatformEthereum;
444
+ exports.isAccountPlatformPolkadot = isAccountPlatformPolkadot;
427
445
  exports.isAccountPortfolio = isAccountPortfolio;
@@ -20,12 +20,21 @@ const isAccountPortfolio = account => {
20
20
  return isAccountOwned(account) || isAccountOfType(account, "watch-only") && account.isPortfolio;
21
21
  };
22
22
  const isAccountNotContact = acc => acc.type !== "contact";
23
- const isAccountEthereum = account => {
23
+ const isAccountAddressEthereum = account => {
24
24
  return !!account && crypto$1.isEthereumAddress(account.address);
25
25
  };
26
- const isAccountPolkadot = account => {
26
+ const isAccountPlatformEthereum = account => {
27
+ return !!account && account.type !== "ledger-polkadot" && crypto$1.isEthereumAddress(account.address);
28
+ };
29
+ const isAccountPlatformPolkadot = account => {
30
+ return !!account && account.type !== "ledger-ethereum" && (isAccountAddressEthereum(account) || isAccountAddressSs58(account));
31
+ };
32
+ const isAccountAddressSs58 = account => {
27
33
  return !!account && crypto$1.detectAddressEncoding(account.address) === "ss58";
28
34
  };
35
+ const isAccountLedgerPolkadot = account => {
36
+ return isAccountOfType(account, "ledger-polkadot");
37
+ };
29
38
  const isAccountLedgerPolkadotGeneric = account => {
30
39
  return isAccountOfType(account, "ledger-polkadot") && !account.genesisHash;
31
40
  };
@@ -139,8 +148,14 @@ class Keyring {
139
148
  });
140
149
  }
141
150
  static load(data) {
142
- // TODO: schema check ?
143
151
  if (!data.accounts || !data.mnemonics) throw new Error("Invalid data");
152
+
153
+ // automatic upgrade : set default values for newly introduced properties
154
+ for (const account of data.accounts) {
155
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
156
+ // @ts-expect-error
157
+ if (account.type === "ledger-polkadot" && !account.curve) account.curve = "ed25519";
158
+ }
144
159
  return new Keyring(data);
145
160
  }
146
161
  async checkPassword(password, reset = false) {
@@ -414,14 +429,17 @@ exports.Keyring = Keyring;
414
429
  exports.getAccountGenesisHash = getAccountGenesisHash;
415
430
  exports.getAccountPlatform = getAccountPlatform;
416
431
  exports.getAccountSignetUrl = getAccountSignetUrl;
432
+ exports.isAccountAddressEthereum = isAccountAddressEthereum;
433
+ exports.isAccountAddressSs58 = isAccountAddressSs58;
417
434
  exports.isAccountBitcoin = isAccountBitcoin;
418
- exports.isAccountEthereum = isAccountEthereum;
419
435
  exports.isAccountExternal = isAccountExternal;
420
436
  exports.isAccountInTypes = isAccountInTypes;
437
+ exports.isAccountLedgerPolkadot = isAccountLedgerPolkadot;
421
438
  exports.isAccountLedgerPolkadotGeneric = isAccountLedgerPolkadotGeneric;
422
439
  exports.isAccountLedgerPolkadotLegacy = isAccountLedgerPolkadotLegacy;
423
440
  exports.isAccountNotContact = isAccountNotContact;
424
441
  exports.isAccountOfType = isAccountOfType;
425
442
  exports.isAccountOwned = isAccountOwned;
426
- exports.isAccountPolkadot = isAccountPolkadot;
443
+ exports.isAccountPlatformEthereum = isAccountPlatformEthereum;
444
+ exports.isAccountPlatformPolkadot = isAccountPlatformPolkadot;
427
445
  exports.isAccountPortfolio = isAccountPortfolio;
@@ -18,12 +18,21 @@ const isAccountPortfolio = account => {
18
18
  return isAccountOwned(account) || isAccountOfType(account, "watch-only") && account.isPortfolio;
19
19
  };
20
20
  const isAccountNotContact = acc => acc.type !== "contact";
21
- const isAccountEthereum = account => {
21
+ const isAccountAddressEthereum = account => {
22
22
  return !!account && isEthereumAddress(account.address);
23
23
  };
24
- const isAccountPolkadot = account => {
24
+ const isAccountPlatformEthereum = account => {
25
+ return !!account && account.type !== "ledger-polkadot" && isEthereumAddress(account.address);
26
+ };
27
+ const isAccountPlatformPolkadot = account => {
28
+ return !!account && account.type !== "ledger-ethereum" && (isAccountAddressEthereum(account) || isAccountAddressSs58(account));
29
+ };
30
+ const isAccountAddressSs58 = account => {
25
31
  return !!account && detectAddressEncoding(account.address) === "ss58";
26
32
  };
33
+ const isAccountLedgerPolkadot = account => {
34
+ return isAccountOfType(account, "ledger-polkadot");
35
+ };
27
36
  const isAccountLedgerPolkadotGeneric = account => {
28
37
  return isAccountOfType(account, "ledger-polkadot") && !account.genesisHash;
29
38
  };
@@ -137,8 +146,14 @@ class Keyring {
137
146
  });
138
147
  }
139
148
  static load(data) {
140
- // TODO: schema check ?
141
149
  if (!data.accounts || !data.mnemonics) throw new Error("Invalid data");
150
+
151
+ // automatic upgrade : set default values for newly introduced properties
152
+ for (const account of data.accounts) {
153
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
154
+ // @ts-expect-error
155
+ if (account.type === "ledger-polkadot" && !account.curve) account.curve = "ed25519";
156
+ }
142
157
  return new Keyring(data);
143
158
  }
144
159
  async checkPassword(password, reset = false) {
@@ -408,4 +423,4 @@ const accountFromStorage = data => {
408
423
  return Object.freeze(copy);
409
424
  };
410
425
 
411
- export { Keyring, getAccountGenesisHash, getAccountPlatform, getAccountSignetUrl, isAccountBitcoin, isAccountEthereum, isAccountExternal, isAccountInTypes, isAccountLedgerPolkadotGeneric, isAccountLedgerPolkadotLegacy, isAccountNotContact, isAccountOfType, isAccountOwned, isAccountPolkadot, isAccountPortfolio };
426
+ export { Keyring, getAccountGenesisHash, getAccountPlatform, getAccountSignetUrl, isAccountAddressEthereum, isAccountAddressSs58, isAccountBitcoin, isAccountExternal, isAccountInTypes, isAccountLedgerPolkadot, isAccountLedgerPolkadotGeneric, isAccountLedgerPolkadotLegacy, isAccountNotContact, isAccountOfType, isAccountOwned, isAccountPlatformEthereum, isAccountPlatformPolkadot, isAccountPortfolio };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@talismn/keyring",
3
- "version": "0.1.4",
3
+ "version": "1.0.0",
4
4
  "author": "Talisman",
5
5
  "homepage": "https://talisman.xyz",
6
6
  "license": "GPL-3.0-or-later",
@@ -22,7 +22,7 @@
22
22
  "node": ">=18"
23
23
  },
24
24
  "dependencies": {
25
- "@talismn/crypto": "0.1.3"
25
+ "@talismn/crypto": "0.1.4"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@types/jest": "^29.5.14",