@trustwallet/wallet-core 3.0.3 → 3.0.4

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.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { TW } from "./generated/core_proto";
2
- import { WalletCore } from "./wallet-core";
2
+ import { WalletCore } from "./src/wallet-core";
3
+ import * as KeyStore from "./src/keystore";
3
4
  declare function load(): Promise<WalletCore>;
4
5
  export declare const initWasm: typeof load;
5
- export { TW, WalletCore };
6
+ export { TW, WalletCore, KeyStore };
package/dist/index.js CHANGED
@@ -5,8 +5,10 @@
5
5
  // terms governing use, modification, and redistribution, is contained in the
6
6
  // file LICENSE at the root of the source code distribution tree.
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
- exports.TW = exports.initWasm = void 0;
8
+ exports.KeyStore = exports.TW = exports.initWasm = void 0;
9
9
  var Loader = require("./lib/wallet-core");
10
10
  var core_proto_1 = require("./generated/core_proto");
11
11
  Object.defineProperty(exports, "TW", { enumerable: true, get: function () { return core_proto_1.TW; } });
12
+ var KeyStore = require("./src/keystore");
13
+ exports.KeyStore = KeyStore;
12
14
  exports.initWasm = Loader;
Binary file
@@ -0,0 +1,19 @@
1
+ import { WalletCore, CoinType, PrivateKey, StoredKey } from "../wallet-core";
2
+ import * as Types from "./types";
3
+ export declare class Default implements Types.IKeyStore {
4
+ private readonly core;
5
+ private readonly storage;
6
+ constructor(core: WalletCore, storage: Types.IStorage);
7
+ hasWallet(id: string): Promise<boolean>;
8
+ load(id: string): Promise<Types.Wallet>;
9
+ loadAll(): Promise<Types.Wallet[]>;
10
+ delete(id: string, password: string): Promise<void>;
11
+ mapWallet(storedKey: StoredKey): Types.Wallet;
12
+ mapStoredKey(wallet: Types.Wallet): StoredKey;
13
+ importWallet(wallet: Types.Wallet): Promise<void>;
14
+ import(mnemonic: string, name: string, password: string, coins: CoinType[]): Promise<Types.Wallet>;
15
+ importKey(key: Uint8Array, name: string, password: string, coin: CoinType): Promise<Types.Wallet>;
16
+ addAccounts(id: string, password: string, coins: CoinType[]): Promise<Types.Wallet>;
17
+ getKey(id: string, password: string, account: Types.ActiveAccount): Promise<PrivateKey>;
18
+ export(id: string, password: string): Promise<string | Uint8Array>;
19
+ }
@@ -0,0 +1,127 @@
1
+ "use strict";
2
+ // Copyright © 2017-2022 Trust Wallet.
3
+ //
4
+ // This file is part of Trust. The full Trust copyright notice, including
5
+ // terms governing use, modification, and redistribution, is contained in the
6
+ // file LICENSE at the root of the source code distribution tree.
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.Default = void 0;
9
+ var Types = require("./types");
10
+ var Default = /** @class */ (function () {
11
+ function Default(core, storage) {
12
+ this.core = core;
13
+ this.storage = storage;
14
+ }
15
+ Default.prototype.hasWallet = function (id) {
16
+ return this.storage
17
+ .get(id)
18
+ .then(function (wallet) { return true; })
19
+ .catch(function (error) { return false; });
20
+ };
21
+ Default.prototype.load = function (id) {
22
+ return this.storage.get(id);
23
+ };
24
+ Default.prototype.loadAll = function () {
25
+ return this.storage.loadAll();
26
+ };
27
+ Default.prototype.delete = function (id, password) {
28
+ return this.storage.delete(id, password);
29
+ };
30
+ Default.prototype.mapWallet = function (storedKey) {
31
+ var json = storedKey.exportJSON();
32
+ return JSON.parse(Buffer.from(json).toString());
33
+ };
34
+ Default.prototype.mapStoredKey = function (wallet) {
35
+ var json = Buffer.from(JSON.stringify(wallet));
36
+ return this.core.StoredKey.importJSON(json);
37
+ };
38
+ Default.prototype.importWallet = function (wallet) {
39
+ return this.storage.set(wallet.id, wallet);
40
+ };
41
+ Default.prototype.import = function (mnemonic, name, password, coins) {
42
+ var _this = this;
43
+ return new Promise(function (resolve, reject) {
44
+ var _a = _this.core, Mnemonic = _a.Mnemonic, StoredKey = _a.StoredKey, HDWallet = _a.HDWallet;
45
+ if (!Mnemonic.isValid(mnemonic)) {
46
+ throw Types.Error.InvalidMnemonic;
47
+ }
48
+ var pass = Buffer.from(password);
49
+ var storedKey = StoredKey.importHDWallet(mnemonic, name, pass, coins[0]);
50
+ var hdWallet = HDWallet.createWithMnemonic(mnemonic, "");
51
+ coins.forEach(function (coin) {
52
+ storedKey.accountForCoin(coin, hdWallet);
53
+ });
54
+ var wallet = _this.mapWallet(storedKey);
55
+ storedKey.delete();
56
+ hdWallet.delete();
57
+ _this.importWallet(wallet)
58
+ .then(function () { return resolve(wallet); })
59
+ .catch(function (error) { return reject(error); });
60
+ });
61
+ };
62
+ Default.prototype.importKey = function (key, name, password, coin) {
63
+ var _this = this;
64
+ return new Promise(function (resolve, reject) {
65
+ var _a = _this.core, StoredKey = _a.StoredKey, PrivateKey = _a.PrivateKey, Curve = _a.Curve;
66
+ // FIXME: get curve from coin
67
+ if (!PrivateKey.isValid(key, Curve.secp256k1) ||
68
+ !PrivateKey.isValid(key, Curve.ed25519)) {
69
+ throw Types.Error.InvalidKey;
70
+ }
71
+ var pass = Buffer.from(password);
72
+ var storedKey = StoredKey.importPrivateKey(key, name, pass, coin);
73
+ var wallet = _this.mapWallet(storedKey);
74
+ storedKey.delete();
75
+ _this.importWallet(wallet)
76
+ .then(function () { return resolve(wallet); })
77
+ .catch(function (error) { return reject(error); });
78
+ });
79
+ };
80
+ Default.prototype.addAccounts = function (id, password, coins) {
81
+ var _this = this;
82
+ return this.load(id).then(function (wallet) {
83
+ var storedKey = _this.mapStoredKey(wallet);
84
+ var hdWallet = storedKey.wallet(Buffer.from(password));
85
+ coins.forEach(function (coin) {
86
+ storedKey.accountForCoin(coin, hdWallet);
87
+ });
88
+ var newWallet = _this.mapWallet(storedKey);
89
+ storedKey.delete();
90
+ hdWallet.delete();
91
+ return _this.importWallet(newWallet).then(function () { return newWallet; });
92
+ });
93
+ };
94
+ Default.prototype.getKey = function (id, password, account) {
95
+ var _this = this;
96
+ return this.load(id).then(function (wallet) {
97
+ var storedKey = _this.mapStoredKey(wallet);
98
+ var hdWallet = storedKey.wallet(Buffer.from(password));
99
+ var coin = _this.core.CoinType.values["" + account.coin];
100
+ var privateKey = hdWallet.getKey(coin, account.derivationPath);
101
+ storedKey.delete();
102
+ hdWallet.delete();
103
+ return privateKey;
104
+ });
105
+ };
106
+ Default.prototype.export = function (id, password) {
107
+ var _this = this;
108
+ return this.load(id).then(function (wallet) {
109
+ var storedKey = _this.mapStoredKey(wallet);
110
+ var value;
111
+ switch (wallet.type) {
112
+ case Types.WalletType.Mnemonic:
113
+ value = storedKey.decryptMnemonic(Buffer.from(password));
114
+ break;
115
+ case Types.WalletType.PrivateKey:
116
+ value = storedKey.decryptPrivateKey(Buffer.from(password));
117
+ break;
118
+ default:
119
+ throw Types.Error.InvalidJSON;
120
+ }
121
+ storedKey.delete();
122
+ return value;
123
+ });
124
+ };
125
+ return Default;
126
+ }());
127
+ exports.Default = Default;
@@ -0,0 +1,12 @@
1
+ import { Storage } from "webextension-polyfill";
2
+ import * as Types from "./types";
3
+ export declare class ExtensionStorage implements Types.IStorage {
4
+ readonly storage: Storage.StorageArea;
5
+ readonly walletIdsKey: string;
6
+ constructor(walletIdsKey: string, storage: Storage.StorageArea);
7
+ get(id: string): Promise<Types.Wallet>;
8
+ set(id: string, wallet: Types.Wallet): Promise<void>;
9
+ loadAll(): Promise<Types.Wallet[]>;
10
+ delete(id: string, password: string): Promise<void>;
11
+ private getWalletIds;
12
+ }
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ // Copyright © 2017-2022 Trust Wallet.
3
+ //
4
+ // This file is part of Trust. The full Trust copyright notice, including
5
+ // terms governing use, modification, and redistribution, is contained in the
6
+ // file LICENSE at the root of the source code distribution tree.
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.ExtensionStorage = void 0;
9
+ var Types = require("./types");
10
+ // Extension KeyStore
11
+ var ExtensionStorage = /** @class */ (function () {
12
+ function ExtensionStorage(walletIdsKey, storage) {
13
+ this.walletIdsKey = walletIdsKey;
14
+ this.storage = storage;
15
+ }
16
+ ExtensionStorage.prototype.get = function (id) {
17
+ return this.storage.get(id).then(function (object) {
18
+ var wallet = object[id];
19
+ if (wallet === undefined) {
20
+ throw Types.Error.WalletNotFound;
21
+ }
22
+ return wallet;
23
+ });
24
+ };
25
+ ExtensionStorage.prototype.set = function (id, wallet) {
26
+ var _this = this;
27
+ return this.getWalletIds().then(function (ids) {
28
+ var _a;
29
+ if (ids.indexOf(id) === -1) {
30
+ ids.push(id);
31
+ }
32
+ return _this.storage.set((_a = {},
33
+ _a[id] = wallet,
34
+ _a[_this.walletIdsKey] = ids,
35
+ _a));
36
+ });
37
+ };
38
+ ExtensionStorage.prototype.loadAll = function () {
39
+ var _this = this;
40
+ return this.getWalletIds().then(function (ids) {
41
+ if (ids.length === 0) {
42
+ return [];
43
+ }
44
+ return _this.storage
45
+ .get(ids)
46
+ .then(function (wallets) { return Object.keys(wallets).map(function (key) { return wallets[key]; }); });
47
+ });
48
+ };
49
+ ExtensionStorage.prototype.delete = function (id, password) {
50
+ var _this = this;
51
+ return this.getWalletIds().then(function (ids) {
52
+ var index = ids.indexOf(id);
53
+ if (index === -1) {
54
+ return;
55
+ }
56
+ ids.splice(index, 1);
57
+ return _this.storage
58
+ .remove(id)
59
+ .then(function () {
60
+ var _a;
61
+ return _this.storage.set((_a = {}, _a[_this.walletIdsKey] = ids, _a));
62
+ });
63
+ });
64
+ };
65
+ ExtensionStorage.prototype.getWalletIds = function () {
66
+ var _this = this;
67
+ return this.storage.get(this.walletIdsKey).then(function (object) {
68
+ var ids = object[_this.walletIdsKey];
69
+ return ids === undefined ? [] : ids;
70
+ });
71
+ };
72
+ return ExtensionStorage;
73
+ }());
74
+ exports.ExtensionStorage = ExtensionStorage;
@@ -0,0 +1,10 @@
1
+ import * as Types from "./types";
2
+ export declare class FileSystemStorage implements Types.IStorage {
3
+ private readonly directory;
4
+ constructor(directory: string);
5
+ getFilename(id: any): string;
6
+ get(id: string): Promise<Types.Wallet>;
7
+ set(id: string, wallet: Types.Wallet): Promise<void>;
8
+ loadAll(): Promise<Types.Wallet[]>;
9
+ delete(id: string, password: string): Promise<void>;
10
+ }
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ // Copyright © 2017-2022 Trust Wallet.
3
+ //
4
+ // This file is part of Trust. The full Trust copyright notice, including
5
+ // terms governing use, modification, and redistribution, is contained in the
6
+ // file LICENSE at the root of the source code distribution tree.
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.FileSystemStorage = void 0;
9
+ var fs = require("fs/promises");
10
+ // FileSystem Storage
11
+ var FileSystemStorage = /** @class */ (function () {
12
+ function FileSystemStorage(directory) {
13
+ this.directory = directory.endsWith("/") ? directory : directory + "/";
14
+ }
15
+ FileSystemStorage.prototype.getFilename = function (id) {
16
+ return this.directory + id + ".json";
17
+ };
18
+ FileSystemStorage.prototype.get = function (id) {
19
+ return fs
20
+ .readFile(this.getFilename(id))
21
+ .then(function (data) { return JSON.parse(data.toString()); });
22
+ };
23
+ FileSystemStorage.prototype.set = function (id, wallet) {
24
+ return fs.writeFile(this.getFilename(id), JSON.stringify(wallet));
25
+ };
26
+ FileSystemStorage.prototype.loadAll = function () {
27
+ var _this = this;
28
+ return fs.readdir(this.directory).then(function (files) {
29
+ return Promise.all(files
30
+ .filter(function (file) { return file.endsWith(".json"); })
31
+ .map(function (file) { return _this.get(file.replace(".json", "")); }));
32
+ });
33
+ };
34
+ FileSystemStorage.prototype.delete = function (id, password) {
35
+ return fs.unlink(this.getFilename(id));
36
+ };
37
+ return FileSystemStorage;
38
+ }());
39
+ exports.FileSystemStorage = FileSystemStorage;
@@ -0,0 +1,4 @@
1
+ export { Default } from "./default-impl";
2
+ export * from "./types";
3
+ export { FileSystemStorage } from "./fs-storage";
4
+ export { ExtensionStorage } from "./extension-storage";
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ // Copyright © 2017-2022 Trust Wallet.
3
+ //
4
+ // This file is part of Trust. The full Trust copyright notice, including
5
+ // terms governing use, modification, and redistribution, is contained in the
6
+ // file LICENSE at the root of the source code distribution tree.
7
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
8
+ if (k2 === undefined) k2 = k;
9
+ var desc = Object.getOwnPropertyDescriptor(m, k);
10
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
11
+ desc = { enumerable: true, get: function() { return m[k]; } };
12
+ }
13
+ Object.defineProperty(o, k2, desc);
14
+ }) : (function(o, m, k, k2) {
15
+ if (k2 === undefined) k2 = k;
16
+ o[k2] = m[k];
17
+ }));
18
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
19
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.ExtensionStorage = exports.FileSystemStorage = exports.Default = void 0;
23
+ var default_impl_1 = require("./default-impl");
24
+ Object.defineProperty(exports, "Default", { enumerable: true, get: function () { return default_impl_1.Default; } });
25
+ __exportStar(require("./types"), exports);
26
+ var fs_storage_1 = require("./fs-storage");
27
+ Object.defineProperty(exports, "FileSystemStorage", { enumerable: true, get: function () { return fs_storage_1.FileSystemStorage; } });
28
+ var extension_storage_1 = require("./extension-storage");
29
+ Object.defineProperty(exports, "ExtensionStorage", { enumerable: true, get: function () { return extension_storage_1.ExtensionStorage; } });
@@ -0,0 +1,47 @@
1
+ import { CoinType, PrivateKey } from "../wallet-core";
2
+ export declare enum WalletType {
3
+ Mnemonic = "mnemonic",
4
+ PrivateKey = "privateKey",
5
+ WatchOnly = "watchOnly",
6
+ Hardware = "hardware"
7
+ }
8
+ export declare enum Error {
9
+ WalletNotFound = "wallet not found",
10
+ AccountNotFound = "account not found",
11
+ InvalidPassword = "invalid password",
12
+ InvalidMnemonic = "invalid mnemonic",
13
+ InvalidJSON = "invalid JSON",
14
+ InvalidKey = "invalid key"
15
+ }
16
+ export interface ActiveAccount {
17
+ address: string;
18
+ coin: number;
19
+ publicKey: string;
20
+ derivationPath: string;
21
+ extendedPublicKey?: string;
22
+ }
23
+ export interface Wallet {
24
+ id: string;
25
+ type: WalletType;
26
+ name: string;
27
+ version: number;
28
+ activeAccounts: ActiveAccount[];
29
+ }
30
+ export interface IKeyStore {
31
+ hasWallet(id: string): Promise<boolean>;
32
+ load(id: string): Promise<Wallet>;
33
+ loadAll(): Promise<Wallet[]>;
34
+ import(mnemonic: string, name: string, password: string, coins: CoinType[]): Promise<Wallet>;
35
+ importKey(key: Uint8Array, name: string, password: string, coin: CoinType): Promise<Wallet>;
36
+ importWallet(wallet: Wallet): Promise<void>;
37
+ addAccounts(id: string, password: string, coins: CoinType[]): Promise<Wallet>;
38
+ getKey(id: string, password: string, account: ActiveAccount): Promise<PrivateKey>;
39
+ delete(id: string, password: string): Promise<void>;
40
+ export(id: string, password: string): Promise<string | Uint8Array>;
41
+ }
42
+ export interface IStorage {
43
+ get(id: string): Promise<Wallet>;
44
+ set(id: string, wallet: Wallet): Promise<void>;
45
+ loadAll(): Promise<Wallet[]>;
46
+ delete(id: string, password: string): Promise<void>;
47
+ }
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ // Copyright © 2017-2022 Trust Wallet.
3
+ //
4
+ // This file is part of Trust. The full Trust copyright notice, including
5
+ // terms governing use, modification, and redistribution, is contained in the
6
+ // file LICENSE at the root of the source code distribution tree.
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.Error = exports.WalletType = void 0;
9
+ var WalletType;
10
+ (function (WalletType) {
11
+ WalletType["Mnemonic"] = "mnemonic";
12
+ WalletType["PrivateKey"] = "privateKey";
13
+ WalletType["WatchOnly"] = "watchOnly";
14
+ WalletType["Hardware"] = "hardware";
15
+ })(WalletType = exports.WalletType || (exports.WalletType = {}));
16
+ var Error;
17
+ (function (Error) {
18
+ Error["WalletNotFound"] = "wallet not found";
19
+ Error["AccountNotFound"] = "account not found";
20
+ Error["InvalidPassword"] = "invalid password";
21
+ Error["InvalidMnemonic"] = "invalid mnemonic";
22
+ Error["InvalidJSON"] = "invalid JSON";
23
+ Error["InvalidKey"] = "invalid key";
24
+ })(Error = exports.Error || (exports.Error = {}));
@@ -38,6 +38,7 @@ export class Purpose {
38
38
  }
39
39
  export class Cardano {
40
40
  static minAdaAmount(tokenBundle: Uint8Array | Buffer): number;
41
+ static getStakingAddress(baseAddress: string): string;
41
42
  }
42
43
  export class GroestlcoinAddress {
43
44
  static equal(lhs: GroestlcoinAddress, rhs: GroestlcoinAddress): boolean;
@@ -557,6 +558,13 @@ export class BitcoinAddress {
557
558
  keyhash(): Uint8Array;
558
559
  delete(): void;
559
560
  }
561
+ export class DerivationPathIndex {
562
+ static create(value: number, hardened: boolean): DerivationPathIndex;
563
+ value(): number;
564
+ hardened(): boolean;
565
+ description(): string;
566
+ delete(): void;
567
+ }
560
568
  export class SS58AddressType {
561
569
  value: number;
562
570
  static polkadot: SS58AddressType;
@@ -621,6 +629,19 @@ export class PrivateKey {
621
629
  signZilliqaSchnorr(message: Uint8Array | Buffer): Uint8Array;
622
630
  delete(): void;
623
631
  }
632
+ export class DerivationPath {
633
+ static create(purpose: Purpose, coin: number, account: number, change: number, address: number): DerivationPath;
634
+ static createWithString(string: string): DerivationPath;
635
+ purpose(): Purpose;
636
+ coin(): number;
637
+ account(): number;
638
+ change(): number;
639
+ address(): number;
640
+ description(): string;
641
+ indexAt(index: number): DerivationPathIndex;
642
+ indicesCount(): number;
643
+ delete(): void;
644
+ }
624
645
  export class HDVersion {
625
646
  value: number;
626
647
  static none: HDVersion;
@@ -721,11 +742,13 @@ export interface WalletCore {
721
742
  Mnemonic: typeof Mnemonic;
722
743
  Blockchain: typeof Blockchain;
723
744
  BitcoinAddress: typeof BitcoinAddress;
745
+ DerivationPathIndex: typeof DerivationPathIndex;
724
746
  SS58AddressType: typeof SS58AddressType;
725
747
  StellarVersionByte: typeof StellarVersionByte;
726
748
  StoredKeyEncryptionLevel: typeof StoredKeyEncryptionLevel;
727
749
  BitcoinScript: typeof BitcoinScript;
728
750
  PrivateKey: typeof PrivateKey;
751
+ DerivationPath: typeof DerivationPath;
729
752
  HDVersion: typeof HDVersion;
730
753
  HRP: typeof HRP;
731
754
  PrivateKeyType: typeof PrivateKeyType;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trustwallet/wallet-core",
3
- "version": "3.0.3",
3
+ "version": "3.0.4",
4
4
  "description": "wallet core wasm and protobuf models",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -11,7 +11,7 @@
11
11
  "codegen:js-browser": "pbjs -t static-module '../src/proto/*.proto' -w closure --no-delimited --force-long -o ../samples/wasm/core_proto.js",
12
12
  "codegen:ts": "pbts -o generated/core_proto.d.ts generated/core_proto.js",
13
13
  "clean": "rm -rf dist generated && mkdir -p dist/generated generated",
14
- "build": "npm run clean && npm run generate && cp -R generated lib dist && tsc && cp wallet-core.d.ts dist",
14
+ "build": "npm run clean && npm run generate && cp -R generated lib dist && tsc && cp src/wallet-core.d.ts dist/src",
15
15
  "build-and-test": "npm run copy:wasm && npm run build && npm test",
16
16
  "copy:wasm": "mkdir -p lib && cp ../wasm-build/wasm/wallet-core.* lib",
17
17
  "copy:wasm-sample": "cp ../wasm-build/wasm/wallet-core.* ../samples/wasm/"
@@ -35,6 +35,8 @@
35
35
  "devDependencies": {
36
36
  "@types/chai": "^4.3.0",
37
37
  "@types/mocha": "^9.1.0",
38
+ "@types/node": "^18.7.18",
39
+ "@types/webextension-polyfill": "^0.9.0",
38
40
  "buffer": "^6.0.3",
39
41
  "chai": "^4.3.6",
40
42
  "mocha": "^9.2.2",