@talismn/keyring 0.1.3 → 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,5 +1,6 @@
1
1
  import type { KeypairCurve } from "@talismn/crypto";
2
- export type AccountPlatform = "ethereum" | "polkadot" | "solana";
2
+ export type LedgerPolkadotCurve = "ed25519" | "ethereum";
3
+ export type AccountPlatform = "ethereum" | "polkadot" | "solana" | "bitcoin";
3
4
  export type AccountBase = {
4
5
  address: string;
5
6
  name: 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,30 +6,48 @@ 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"];
13
+ declare const ACCOUNT_TYPES_BITCOIN: readonly ["contact", "watch-only"];
11
14
  export declare const isAccountExternal: (account: Account | null | undefined) => account is AccountOfType<(typeof ACCOUNT_TYPES_EXTERNAL)[number]>;
12
15
  export declare const isAccountOwned: (account: Account | null | undefined) => account is AccountOfType<(typeof ACCOUNT_TYPES_OWNED)[number]>;
13
16
  export declare const isAccountPortfolio: (account: Account | null | undefined) => account is Account;
14
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;
15
- type AccountEthereum = Extract<Account, {
16
- type: (typeof ACCOUNT_TYPES_ETHEREUM)[number];
18
+ type AccountAddressEthereum = Extract<Account, {
19
+ type: (typeof ACCOUNT_TYPES_ADDRESS_ETHEREUM)[number];
17
20
  }> & {
18
21
  address: `0x${string}`;
19
22
  };
20
- export declare const isAccountEthereum: (account: Account | null | undefined) => account is AccountEthereum;
21
- type AccountPolkadot = Extract<Account, {
22
- 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];
23
36
  }> & {
24
37
  genesisHash?: `0x${string}`;
25
38
  };
26
- 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;
27
41
  export declare const isAccountLedgerPolkadotGeneric: (account: Account | null | undefined) => account is AccountLedgerPolkadot & {
28
42
  genesisHash: undefined;
29
43
  };
30
44
  export declare const isAccountLedgerPolkadotLegacy: (account: Account | null | undefined) => account is AccountLedgerPolkadot & {
31
45
  genesisHash: `0x${string}`;
32
46
  };
47
+ type AccountBitcoin = Extract<Account, {
48
+ type: (typeof ACCOUNT_TYPES_BITCOIN)[number];
49
+ }>;
50
+ export declare const isAccountBitcoin: (account: Account | null | undefined) => account is AccountBitcoin;
33
51
  export declare const getAccountGenesisHash: (account: Account | null | undefined) => `0x${string}` | undefined;
34
52
  export declare const getAccountSignetUrl: (account: Account | null | undefined) => string | undefined;
35
53
  export declare const getAccountPlatform: (account: Account | null | undefined) => import("@talismn/crypto").Platform | undefined;
@@ -20,18 +20,30 @@ 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
  };
32
41
  const isAccountLedgerPolkadotLegacy = account => {
33
42
  return isAccountOfType(account, "ledger-polkadot") && !!account.genesisHash;
34
43
  };
44
+ const isAccountBitcoin = account => {
45
+ return !!account && crypto$1.isBitcoinAddress(account.address);
46
+ };
35
47
  const getAccountGenesisHash = account => {
36
48
  if (!account) return undefined;
37
49
  return "genesisHash" in account ? account.genesisHash || undefined : undefined;
@@ -136,8 +148,14 @@ class Keyring {
136
148
  });
137
149
  }
138
150
  static load(data) {
139
- // TODO: schema check ?
140
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
+ }
141
159
  return new Keyring(data);
142
160
  }
143
161
  async checkPassword(password, reset = false) {
@@ -411,13 +429,17 @@ exports.Keyring = Keyring;
411
429
  exports.getAccountGenesisHash = getAccountGenesisHash;
412
430
  exports.getAccountPlatform = getAccountPlatform;
413
431
  exports.getAccountSignetUrl = getAccountSignetUrl;
414
- exports.isAccountEthereum = isAccountEthereum;
432
+ exports.isAccountAddressEthereum = isAccountAddressEthereum;
433
+ exports.isAccountAddressSs58 = isAccountAddressSs58;
434
+ exports.isAccountBitcoin = isAccountBitcoin;
415
435
  exports.isAccountExternal = isAccountExternal;
416
436
  exports.isAccountInTypes = isAccountInTypes;
437
+ exports.isAccountLedgerPolkadot = isAccountLedgerPolkadot;
417
438
  exports.isAccountLedgerPolkadotGeneric = isAccountLedgerPolkadotGeneric;
418
439
  exports.isAccountLedgerPolkadotLegacy = isAccountLedgerPolkadotLegacy;
419
440
  exports.isAccountNotContact = isAccountNotContact;
420
441
  exports.isAccountOfType = isAccountOfType;
421
442
  exports.isAccountOwned = isAccountOwned;
422
- exports.isAccountPolkadot = isAccountPolkadot;
443
+ exports.isAccountPlatformEthereum = isAccountPlatformEthereum;
444
+ exports.isAccountPlatformPolkadot = isAccountPlatformPolkadot;
423
445
  exports.isAccountPortfolio = isAccountPortfolio;
@@ -20,18 +20,30 @@ 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
  };
32
41
  const isAccountLedgerPolkadotLegacy = account => {
33
42
  return isAccountOfType(account, "ledger-polkadot") && !!account.genesisHash;
34
43
  };
44
+ const isAccountBitcoin = account => {
45
+ return !!account && crypto$1.isBitcoinAddress(account.address);
46
+ };
35
47
  const getAccountGenesisHash = account => {
36
48
  if (!account) return undefined;
37
49
  return "genesisHash" in account ? account.genesisHash || undefined : undefined;
@@ -136,8 +148,14 @@ class Keyring {
136
148
  });
137
149
  }
138
150
  static load(data) {
139
- // TODO: schema check ?
140
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
+ }
141
159
  return new Keyring(data);
142
160
  }
143
161
  async checkPassword(password, reset = false) {
@@ -411,13 +429,17 @@ exports.Keyring = Keyring;
411
429
  exports.getAccountGenesisHash = getAccountGenesisHash;
412
430
  exports.getAccountPlatform = getAccountPlatform;
413
431
  exports.getAccountSignetUrl = getAccountSignetUrl;
414
- exports.isAccountEthereum = isAccountEthereum;
432
+ exports.isAccountAddressEthereum = isAccountAddressEthereum;
433
+ exports.isAccountAddressSs58 = isAccountAddressSs58;
434
+ exports.isAccountBitcoin = isAccountBitcoin;
415
435
  exports.isAccountExternal = isAccountExternal;
416
436
  exports.isAccountInTypes = isAccountInTypes;
437
+ exports.isAccountLedgerPolkadot = isAccountLedgerPolkadot;
417
438
  exports.isAccountLedgerPolkadotGeneric = isAccountLedgerPolkadotGeneric;
418
439
  exports.isAccountLedgerPolkadotLegacy = isAccountLedgerPolkadotLegacy;
419
440
  exports.isAccountNotContact = isAccountNotContact;
420
441
  exports.isAccountOfType = isAccountOfType;
421
442
  exports.isAccountOwned = isAccountOwned;
422
- exports.isAccountPolkadot = isAccountPolkadot;
443
+ exports.isAccountPlatformEthereum = isAccountPlatformEthereum;
444
+ exports.isAccountPlatformPolkadot = isAccountPlatformPolkadot;
423
445
  exports.isAccountPortfolio = isAccountPortfolio;
@@ -1,4 +1,4 @@
1
- import { isEthereumAddress, detectAddressEncoding, platformFromCurve, platformFromAddress, pbkdf2, stringToBytes, bytesToString, isValidMnemonic, mnemonicToEntropy, entropyToMnemonic, isAddressEqual, normalizeAddress, entropyToSeed, deriveKeypair, getPublicKeyFromSecret, addressEncodingFromCurve, addressFromPublicKey, blake3 } from '@talismn/crypto';
1
+ import { isEthereumAddress, detectAddressEncoding, isBitcoinAddress, platformFromCurve, platformFromAddress, pbkdf2, stringToBytes, bytesToString, isValidMnemonic, mnemonicToEntropy, entropyToMnemonic, isAddressEqual, normalizeAddress, entropyToSeed, deriveKeypair, getPublicKeyFromSecret, addressEncodingFromCurve, addressFromPublicKey, blake3 } from '@talismn/crypto';
2
2
 
3
3
  const isAccountOfType = (account, type) => {
4
4
  return account?.type === type;
@@ -18,18 +18,30 @@ 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
  };
30
39
  const isAccountLedgerPolkadotLegacy = account => {
31
40
  return isAccountOfType(account, "ledger-polkadot") && !!account.genesisHash;
32
41
  };
42
+ const isAccountBitcoin = account => {
43
+ return !!account && isBitcoinAddress(account.address);
44
+ };
33
45
  const getAccountGenesisHash = account => {
34
46
  if (!account) return undefined;
35
47
  return "genesisHash" in account ? account.genesisHash || undefined : undefined;
@@ -134,8 +146,14 @@ class Keyring {
134
146
  });
135
147
  }
136
148
  static load(data) {
137
- // TODO: schema check ?
138
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
+ }
139
157
  return new Keyring(data);
140
158
  }
141
159
  async checkPassword(password, reset = false) {
@@ -405,4 +423,4 @@ const accountFromStorage = data => {
405
423
  return Object.freeze(copy);
406
424
  };
407
425
 
408
- export { Keyring, getAccountGenesisHash, getAccountPlatform, getAccountSignetUrl, 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.3",
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.2"
25
+ "@talismn/crypto": "0.1.4"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@types/jest": "^29.5.14",
@@ -31,8 +31,8 @@
31
31
  "jest-fixed-jsdom": "^0.0.9",
32
32
  "ts-jest": "^29.2.5",
33
33
  "typescript": "^5.6.3",
34
- "@talismn/eslint-config": "0.0.3",
35
- "@talismn/tsconfig": "0.0.2"
34
+ "@talismn/tsconfig": "0.0.2",
35
+ "@talismn/eslint-config": "0.0.3"
36
36
  },
37
37
  "preconstruct": {
38
38
  "entrypoints": [