@gnolang/tm2-js-client 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.
Files changed (77) hide show
  1. package/LICENSE.md +201 -0
  2. package/README.md +78 -0
  3. package/bin/index.d.ts +4 -0
  4. package/bin/index.js +20 -0
  5. package/bin/proto/google/protobuf/any.d.ts +151 -0
  6. package/bin/proto/google/protobuf/any.js +124 -0
  7. package/bin/proto/index.d.ts +2 -0
  8. package/bin/proto/index.js +20 -0
  9. package/bin/proto/tm2/tx.d.ts +540 -0
  10. package/bin/proto/tm2/tx.js +365 -0
  11. package/bin/provider/endpoints.d.ts +28 -0
  12. package/bin/provider/endpoints.js +36 -0
  13. package/bin/provider/index.d.ts +6 -0
  14. package/bin/provider/index.js +22 -0
  15. package/bin/provider/jsonrpc/index.d.ts +1 -0
  16. package/bin/provider/jsonrpc/index.js +17 -0
  17. package/bin/provider/jsonrpc/jsonrpc.d.ts +27 -0
  18. package/bin/provider/jsonrpc/jsonrpc.js +219 -0
  19. package/bin/provider/jsonrpc/jsonrpc.test.d.ts +1 -0
  20. package/bin/provider/jsonrpc/jsonrpc.test.js +374 -0
  21. package/bin/provider/provider.d.ts +86 -0
  22. package/bin/provider/provider.js +2 -0
  23. package/bin/provider/types/abci.d.ts +39 -0
  24. package/bin/provider/types/abci.js +4 -0
  25. package/bin/provider/types/common.d.ts +154 -0
  26. package/bin/provider/types/common.js +2 -0
  27. package/bin/provider/types/index.d.ts +3 -0
  28. package/bin/provider/types/index.js +19 -0
  29. package/bin/provider/types/jsonrpc.d.ts +26 -0
  30. package/bin/provider/types/jsonrpc.js +2 -0
  31. package/bin/provider/utility/index.d.ts +2 -0
  32. package/bin/provider/utility/index.js +18 -0
  33. package/bin/provider/utility/provider.utility.d.ts +28 -0
  34. package/bin/provider/utility/provider.utility.js +194 -0
  35. package/bin/provider/utility/requests.utility.d.ts +33 -0
  36. package/bin/provider/utility/requests.utility.js +74 -0
  37. package/bin/provider/websocket/index.d.ts +1 -0
  38. package/bin/provider/websocket/index.js +17 -0
  39. package/bin/provider/websocket/ws.d.ts +55 -0
  40. package/bin/provider/websocket/ws.js +303 -0
  41. package/bin/provider/websocket/ws.test.d.ts +1 -0
  42. package/bin/provider/websocket/ws.test.js +535 -0
  43. package/bin/services/index.d.ts +2 -0
  44. package/bin/services/index.js +18 -0
  45. package/bin/services/rest/restService.d.ts +4 -0
  46. package/bin/services/rest/restService.js +72 -0
  47. package/bin/services/rest/restService.types.d.ts +6 -0
  48. package/bin/services/rest/restService.types.js +2 -0
  49. package/bin/wallet/index.d.ts +5 -0
  50. package/bin/wallet/index.js +21 -0
  51. package/bin/wallet/key/index.d.ts +1 -0
  52. package/bin/wallet/key/index.js +17 -0
  53. package/bin/wallet/key/key.d.ts +21 -0
  54. package/bin/wallet/key/key.js +102 -0
  55. package/bin/wallet/key/key.test.d.ts +1 -0
  56. package/bin/wallet/key/key.test.js +121 -0
  57. package/bin/wallet/ledger/index.d.ts +1 -0
  58. package/bin/wallet/ledger/index.js +17 -0
  59. package/bin/wallet/ledger/ledger.d.ts +22 -0
  60. package/bin/wallet/ledger/ledger.js +109 -0
  61. package/bin/wallet/signer.d.ts +29 -0
  62. package/bin/wallet/signer.js +2 -0
  63. package/bin/wallet/types/index.d.ts +2 -0
  64. package/bin/wallet/types/index.js +18 -0
  65. package/bin/wallet/types/sign.d.ts +17 -0
  66. package/bin/wallet/types/sign.js +4 -0
  67. package/bin/wallet/types/wallet.d.ts +5 -0
  68. package/bin/wallet/types/wallet.js +2 -0
  69. package/bin/wallet/utility/index.d.ts +1 -0
  70. package/bin/wallet/utility/index.js +17 -0
  71. package/bin/wallet/utility/utility.d.ts +30 -0
  72. package/bin/wallet/utility/utility.js +105 -0
  73. package/bin/wallet/wallet.d.ts +94 -0
  74. package/bin/wallet/wallet.js +340 -0
  75. package/bin/wallet/wallet.test.d.ts +1 -0
  76. package/bin/wallet/wallet.test.js +345 -0
  77. package/package.json +64 -0
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ exports.stringToUTF8 = exports.defaultAddressPrefix = exports.generateKeyPair = exports.generateEntropy = exports.generateHDPath = void 0;
43
+ var crypto_1 = require("@cosmjs/crypto");
44
+ var crypto_2 = __importDefault(require("crypto"));
45
+ /**
46
+ * Generates the HD path, for the specified index, in the form 'm/44'/118'/0'/0/i',
47
+ * where 'i' is the account index
48
+ * @param {number} [index=0] the account index
49
+ */
50
+ var generateHDPath = function (index) {
51
+ return [
52
+ crypto_1.Slip10RawIndex.hardened(44),
53
+ crypto_1.Slip10RawIndex.hardened(118),
54
+ crypto_1.Slip10RawIndex.hardened(0),
55
+ crypto_1.Slip10RawIndex.normal(0),
56
+ crypto_1.Slip10RawIndex.normal(index ? index : 0),
57
+ ];
58
+ };
59
+ exports.generateHDPath = generateHDPath;
60
+ /**
61
+ * Generates random entropy of the specified size (in B)
62
+ * @param {number} [size=32] the entropy size in bytes
63
+ */
64
+ var generateEntropy = function (size) {
65
+ var array = new Uint8Array(size ? size : 32);
66
+ // Generate random data
67
+ crypto_2.default.randomFillSync(array);
68
+ return array;
69
+ };
70
+ exports.generateEntropy = generateEntropy;
71
+ /**
72
+ * Generates a new Secp256k1 key-pair using
73
+ * the provided English mnemonic and account index
74
+ * @param {string} mnemonic the English mnemonic
75
+ * @param {number} [accountIndex=0] the account index
76
+ */
77
+ var generateKeyPair = function (mnemonic, accountIndex) { return __awaiter(void 0, void 0, void 0, function () {
78
+ var seed, privateKey, publicKey;
79
+ return __generator(this, function (_a) {
80
+ switch (_a.label) {
81
+ case 0: return [4 /*yield*/, crypto_1.Bip39.mnemonicToSeed(new crypto_1.EnglishMnemonic(mnemonic))];
82
+ case 1:
83
+ seed = _a.sent();
84
+ privateKey = crypto_1.Slip10.derivePath(crypto_1.Slip10Curve.Secp256k1, seed, (0, exports.generateHDPath)(accountIndex)).privkey;
85
+ return [4 /*yield*/, crypto_1.Secp256k1.makeKeypair(privateKey)];
86
+ case 2:
87
+ publicKey = (_a.sent()).pubkey;
88
+ return [2 /*return*/, {
89
+ publicKey: publicKey,
90
+ privateKey: privateKey,
91
+ }];
92
+ }
93
+ });
94
+ }); };
95
+ exports.generateKeyPair = generateKeyPair;
96
+ // Address prefix for TM2 networks
97
+ exports.defaultAddressPrefix = 'g';
98
+ /**
99
+ * Encodes a string into a Uint8Array
100
+ * @param {string} str the string to be encoded
101
+ */
102
+ var stringToUTF8 = function (str) {
103
+ return new TextEncoder().encode(str);
104
+ };
105
+ exports.stringToUTF8 = stringToUTF8;
@@ -0,0 +1,94 @@
1
+ import { Provider } from '../provider';
2
+ import { Signer } from './signer';
3
+ import { LedgerConnector } from '@cosmjs/ledger-amino';
4
+ import { Any, Tx } from '../proto';
5
+ import { AccountWalletOption, CreateWalletOptions } from './types';
6
+ /**
7
+ * Wallet is a single account abstraction
8
+ * that can interact with the blockchain
9
+ */
10
+ export declare class Wallet {
11
+ private provider;
12
+ private signer;
13
+ /**
14
+ * Connects the wallet to the specified {@link Provider}
15
+ * @param {Provider} provider the active {@link Provider}, if any
16
+ */
17
+ connect: (provider: Provider) => void;
18
+ /**
19
+ * Generates a private key-based wallet, using a random seed
20
+ * @param {AccountWalletOption} options the account options
21
+ */
22
+ static createRandom: (options?: AccountWalletOption) => Promise<Wallet>;
23
+ /**
24
+ * Generates a bip39 mnemonic-based wallet
25
+ * @param {string} mnemonic the bip39 mnemonic
26
+ * @param {CreateWalletOptions} options the wallet generation options
27
+ */
28
+ static fromMnemonic: (mnemonic: string, options?: CreateWalletOptions) => Promise<Wallet>;
29
+ /**
30
+ * Generates a private key-based wallet
31
+ * @param {string} privateKey the private key
32
+ * @param {AccountWalletOption} options the account options
33
+ */
34
+ static fromPrivateKey: (privateKey: Uint8Array, options?: AccountWalletOption) => Promise<Wallet>;
35
+ /**
36
+ * Creates a Ledger-based wallet
37
+ * @param {LedgerConnector} connector the Ledger device connector
38
+ * @param {CreateWalletOptions} options the wallet generation options
39
+ */
40
+ static fromLedger: (connector: LedgerConnector, options?: CreateWalletOptions) => Wallet;
41
+ /**
42
+ * Fetches the address associated with the wallet
43
+ */
44
+ getAddress: () => Promise<string>;
45
+ /**
46
+ * Fetches the account sequence for the wallet
47
+ * @param {number} [height=latest] the block height
48
+ */
49
+ getAccountSequence: (height?: number) => Promise<number>;
50
+ /**
51
+ * Fetches the account number for the wallet. Errors out if the
52
+ * account is not initialized
53
+ * @param {number} [height=latest] the block height
54
+ */
55
+ getAccountNumber: (height?: number) => Promise<number>;
56
+ /**
57
+ * Fetches the account balance for the specific denomination
58
+ * @param {string} [denomination=ugnot] the fund denomination
59
+ */
60
+ getBalance: (denomination?: string) => Promise<number>;
61
+ /**
62
+ * Fetches the current (recommended) average gas price
63
+ */
64
+ getGasPrice: () => Promise<number>;
65
+ /**
66
+ * Estimates the gas limit for the transaction
67
+ * @param {Tx} tx the transaction that needs estimating
68
+ */
69
+ estimateGas: (tx: Tx) => Promise<number>;
70
+ /**
71
+ * Returns the connected provider, if any
72
+ */
73
+ getProvider: () => Provider;
74
+ /**
75
+ * Generates a transaction signature, and appends it to the transaction
76
+ * @param {Tx} tx the transaction to be signed
77
+ * @param {(messages: Any[]) => any[]} decodeTxMessages tx message decode callback
78
+ * that should expand the concrete message fields into an object. Required because
79
+ * the transaction sign bytes are generated using sorted JSON, which requires
80
+ * encoded message values to be decoded for sorting
81
+ */
82
+ signTransaction: (tx: Tx, decodeTxMessages: (messages: Any[]) => any[]) => Promise<Tx>;
83
+ /**
84
+ * Encodes and sends the transaction.
85
+ * The transaction needs to be signed beforehand.
86
+ * Returns the transaction hash (base-64)
87
+ * @param {Tx} tx the signed transaction
88
+ */
89
+ sendTransaction: (tx: Tx) => Promise<string>;
90
+ /**
91
+ * Returns the associated signer
92
+ */
93
+ getSigner: () => Signer;
94
+ }
@@ -0,0 +1,340 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
50
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
51
+ if (ar || !(i in from)) {
52
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
53
+ ar[i] = from[i];
54
+ }
55
+ }
56
+ return to.concat(ar || Array.prototype.slice.call(from));
57
+ };
58
+ Object.defineProperty(exports, "__esModule", { value: true });
59
+ exports.Wallet = void 0;
60
+ var provider_1 = require("../provider");
61
+ var ledger_1 = require("./ledger");
62
+ var key_1 = require("./key");
63
+ var crypto_1 = require("@cosmjs/crypto");
64
+ var utility_1 = require("./utility");
65
+ var bip39_1 = require("@cosmjs/crypto/build/bip39");
66
+ var proto_1 = require("../proto");
67
+ var types_1 = require("./types");
68
+ var signdoc_1 = require("@cosmjs/amino/build/signdoc");
69
+ /**
70
+ * Wallet is a single account abstraction
71
+ * that can interact with the blockchain
72
+ */
73
+ var Wallet = exports.Wallet = /** @class */ (function () {
74
+ function Wallet() {
75
+ var _this = this;
76
+ /**
77
+ * Connects the wallet to the specified {@link Provider}
78
+ * @param {Provider} provider the active {@link Provider}, if any
79
+ */
80
+ this.connect = function (provider) {
81
+ _this.provider = provider;
82
+ };
83
+ // Account manipulation //
84
+ /**
85
+ * Fetches the address associated with the wallet
86
+ */
87
+ this.getAddress = function () {
88
+ return _this.signer.getAddress();
89
+ };
90
+ /**
91
+ * Fetches the account sequence for the wallet
92
+ * @param {number} [height=latest] the block height
93
+ */
94
+ this.getAccountSequence = function (height) { return __awaiter(_this, void 0, void 0, function () {
95
+ var address;
96
+ return __generator(this, function (_b) {
97
+ switch (_b.label) {
98
+ case 0:
99
+ if (!this.provider) {
100
+ throw new Error('provider not connected');
101
+ }
102
+ return [4 /*yield*/, this.getAddress()];
103
+ case 1:
104
+ address = _b.sent();
105
+ return [2 /*return*/, this.provider.getAccountSequence(address, height)];
106
+ }
107
+ });
108
+ }); };
109
+ /**
110
+ * Fetches the account number for the wallet. Errors out if the
111
+ * account is not initialized
112
+ * @param {number} [height=latest] the block height
113
+ */
114
+ this.getAccountNumber = function (height) { return __awaiter(_this, void 0, void 0, function () {
115
+ var address;
116
+ return __generator(this, function (_b) {
117
+ switch (_b.label) {
118
+ case 0:
119
+ if (!this.provider) {
120
+ throw new Error('provider not connected');
121
+ }
122
+ return [4 /*yield*/, this.getAddress()];
123
+ case 1:
124
+ address = _b.sent();
125
+ return [2 /*return*/, this.provider.getAccountNumber(address, height)];
126
+ }
127
+ });
128
+ }); };
129
+ /**
130
+ * Fetches the account balance for the specific denomination
131
+ * @param {string} [denomination=ugnot] the fund denomination
132
+ */
133
+ this.getBalance = function (denomination) { return __awaiter(_this, void 0, void 0, function () {
134
+ var address;
135
+ return __generator(this, function (_b) {
136
+ switch (_b.label) {
137
+ case 0:
138
+ if (!this.provider) {
139
+ throw new Error('provider not connected');
140
+ }
141
+ return [4 /*yield*/, this.getAddress()];
142
+ case 1:
143
+ address = _b.sent();
144
+ return [2 /*return*/, this.provider.getBalance(address, denomination ? denomination : 'ugnot')];
145
+ }
146
+ });
147
+ }); };
148
+ /**
149
+ * Fetches the current (recommended) average gas price
150
+ */
151
+ this.getGasPrice = function () { return __awaiter(_this, void 0, void 0, function () {
152
+ return __generator(this, function (_b) {
153
+ if (!this.provider) {
154
+ throw new Error('provider not connected');
155
+ }
156
+ return [2 /*return*/, this.provider.getGasPrice()];
157
+ });
158
+ }); };
159
+ /**
160
+ * Estimates the gas limit for the transaction
161
+ * @param {Tx} tx the transaction that needs estimating
162
+ */
163
+ this.estimateGas = function (tx) { return __awaiter(_this, void 0, void 0, function () {
164
+ return __generator(this, function (_b) {
165
+ if (!this.provider) {
166
+ throw new Error('provider not connected');
167
+ }
168
+ return [2 /*return*/, this.provider.estimateGas(tx)];
169
+ });
170
+ }); };
171
+ /**
172
+ * Returns the connected provider, if any
173
+ */
174
+ this.getProvider = function () {
175
+ return _this.provider;
176
+ };
177
+ /**
178
+ * Generates a transaction signature, and appends it to the transaction
179
+ * @param {Tx} tx the transaction to be signed
180
+ * @param {(messages: Any[]) => any[]} decodeTxMessages tx message decode callback
181
+ * that should expand the concrete message fields into an object. Required because
182
+ * the transaction sign bytes are generated using sorted JSON, which requires
183
+ * encoded message values to be decoded for sorting
184
+ */
185
+ this.signTransaction = function (tx, decodeTxMessages) { return __awaiter(_this, void 0, void 0, function () {
186
+ var status, chainID, address, accountNumber, accountSequence, publicKey, defaultSigTimestamp, signPayload, signBytes, wrappedKey, txSignature;
187
+ var _b;
188
+ return __generator(this, function (_c) {
189
+ switch (_c.label) {
190
+ case 0:
191
+ if (!this.provider) {
192
+ throw new Error('provider not connected');
193
+ }
194
+ // Make sure the tx fee is initialized
195
+ if (!tx.fee) {
196
+ throw new Error('invalid transaction fee provided');
197
+ }
198
+ return [4 /*yield*/, this.provider.getStatus()];
199
+ case 1:
200
+ status = _c.sent();
201
+ chainID = status.node_info.network;
202
+ return [4 /*yield*/, this.getAddress()];
203
+ case 2:
204
+ address = _c.sent();
205
+ return [4 /*yield*/, this.provider.getAccountNumber(address)];
206
+ case 3:
207
+ accountNumber = _c.sent();
208
+ return [4 /*yield*/, this.provider.getAccountSequence(address)];
209
+ case 4:
210
+ accountSequence = _c.sent();
211
+ return [4 /*yield*/, this.signer.getPublicKey()];
212
+ case 5:
213
+ publicKey = _c.sent();
214
+ defaultSigTimestamp = '0001-01-01T00:00:00Z';
215
+ signPayload = {
216
+ chain_id: chainID,
217
+ account_number: accountNumber.toString(10),
218
+ sequence: accountSequence.toString(10),
219
+ fee: {
220
+ gas_fee: tx.fee.gasFee,
221
+ gas_wanted: tx.fee.gasWanted.toString(10),
222
+ },
223
+ msgs: decodeTxMessages(tx.messages),
224
+ memo: tx.memo,
225
+ time: defaultSigTimestamp,
226
+ };
227
+ signBytes = (0, utility_1.stringToUTF8)((0, signdoc_1.sortedJsonStringify)(signPayload));
228
+ wrappedKey = {
229
+ key: publicKey,
230
+ };
231
+ _b = {
232
+ pubKey: {
233
+ typeUrl: types_1.Secp256k1PubKeyType,
234
+ value: proto_1.PubKeySecp256k1.encode(wrappedKey).finish(),
235
+ }
236
+ };
237
+ return [4 /*yield*/, this.getSigner().signData(signBytes)];
238
+ case 6:
239
+ txSignature = (_b.signature = _c.sent(),
240
+ _b);
241
+ // Append the signature
242
+ return [2 /*return*/, __assign(__assign({}, tx), { signatures: __spreadArray(__spreadArray([], tx.signatures, true), [txSignature], false) })];
243
+ }
244
+ });
245
+ }); };
246
+ /**
247
+ * Encodes and sends the transaction.
248
+ * The transaction needs to be signed beforehand.
249
+ * Returns the transaction hash (base-64)
250
+ * @param {Tx} tx the signed transaction
251
+ */
252
+ this.sendTransaction = function (tx) { return __awaiter(_this, void 0, void 0, function () {
253
+ var encodedTx;
254
+ return __generator(this, function (_b) {
255
+ if (!this.provider) {
256
+ throw new Error('provider not connected');
257
+ }
258
+ encodedTx = (0, provider_1.uint8ArrayToBase64)(proto_1.Tx.encode(tx).finish());
259
+ // Send the encoded transaction
260
+ return [2 /*return*/, this.provider.sendTransaction(encodedTx)];
261
+ });
262
+ }); };
263
+ /**
264
+ * Returns the associated signer
265
+ */
266
+ this.getSigner = function () {
267
+ return _this.signer;
268
+ };
269
+ }
270
+ var _a;
271
+ _a = Wallet;
272
+ // Wallet initialization //
273
+ /**
274
+ * Generates a private key-based wallet, using a random seed
275
+ * @param {AccountWalletOption} options the account options
276
+ */
277
+ Wallet.createRandom = function (options) { return __awaiter(void 0, void 0, void 0, function () {
278
+ var publicKey, privateKey, wallet;
279
+ var _b;
280
+ return __generator(_a, function (_c) {
281
+ switch (_c.label) {
282
+ case 0: return [4 /*yield*/, (0, utility_1.generateKeyPair)((0, bip39_1.entropyToMnemonic)((0, utility_1.generateEntropy)()), 0)];
283
+ case 1:
284
+ publicKey = (_b = _c.sent(), _b.publicKey), privateKey = _b.privateKey;
285
+ wallet = new Wallet();
286
+ wallet.signer = new key_1.KeySigner(privateKey, crypto_1.Secp256k1.compressPubkey(publicKey), options === null || options === void 0 ? void 0 : options.addressPrefix);
287
+ return [2 /*return*/, wallet];
288
+ }
289
+ });
290
+ }); };
291
+ /**
292
+ * Generates a bip39 mnemonic-based wallet
293
+ * @param {string} mnemonic the bip39 mnemonic
294
+ * @param {CreateWalletOptions} options the wallet generation options
295
+ */
296
+ Wallet.fromMnemonic = function (mnemonic, options) { return __awaiter(void 0, void 0, void 0, function () {
297
+ var publicKey, privateKey, wallet;
298
+ var _b;
299
+ return __generator(_a, function (_c) {
300
+ switch (_c.label) {
301
+ case 0: return [4 /*yield*/, (0, utility_1.generateKeyPair)(mnemonic, options === null || options === void 0 ? void 0 : options.accountIndex)];
302
+ case 1:
303
+ publicKey = (_b = _c.sent(), _b.publicKey), privateKey = _b.privateKey;
304
+ wallet = new Wallet();
305
+ wallet.signer = new key_1.KeySigner(privateKey, crypto_1.Secp256k1.compressPubkey(publicKey), options === null || options === void 0 ? void 0 : options.addressPrefix);
306
+ return [2 /*return*/, wallet];
307
+ }
308
+ });
309
+ }); };
310
+ /**
311
+ * Generates a private key-based wallet
312
+ * @param {string} privateKey the private key
313
+ * @param {AccountWalletOption} options the account options
314
+ */
315
+ Wallet.fromPrivateKey = function (privateKey, options) { return __awaiter(void 0, void 0, void 0, function () {
316
+ var publicKey, wallet;
317
+ return __generator(_a, function (_b) {
318
+ switch (_b.label) {
319
+ case 0: return [4 /*yield*/, crypto_1.Secp256k1.makeKeypair(privateKey)];
320
+ case 1:
321
+ publicKey = (_b.sent()).pubkey;
322
+ wallet = new Wallet();
323
+ wallet.signer = new key_1.KeySigner(privateKey, crypto_1.Secp256k1.compressPubkey(publicKey), options === null || options === void 0 ? void 0 : options.addressPrefix);
324
+ return [2 /*return*/, wallet];
325
+ }
326
+ });
327
+ }); };
328
+ /**
329
+ * Creates a Ledger-based wallet
330
+ * @param {LedgerConnector} connector the Ledger device connector
331
+ * @param {CreateWalletOptions} options the wallet generation options
332
+ */
333
+ Wallet.fromLedger = function (connector, options) {
334
+ var _b;
335
+ var wallet = new Wallet();
336
+ wallet.signer = new ledger_1.LedgerSigner(connector, (_b = options === null || options === void 0 ? void 0 : options.accountIndex) !== null && _b !== void 0 ? _b : 0, options === null || options === void 0 ? void 0 : options.addressPrefix);
337
+ return wallet;
338
+ };
339
+ return Wallet;
340
+ }());
@@ -0,0 +1 @@
1
+ export {};