@firmachain/firma-js 0.2.14 → 0.2.18
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 +1 -0
- package/dist/index.js +1 -0
- package/dist/sdk/FirmaBankService.js +3 -3
- package/dist/sdk/FirmaContractService.js +8 -8
- package/dist/sdk/FirmaDistributionService.d.ts +4 -0
- package/dist/sdk/FirmaDistributionService.js +140 -66
- package/dist/sdk/FirmaFeeGrantService.js +6 -6
- package/dist/sdk/FirmaGovService.js +15 -15
- package/dist/sdk/FirmaMobileSDK.d.ts +27 -0
- package/dist/sdk/FirmaMobileSDK.js +45 -0
- package/dist/sdk/FirmaNftService.js +6 -6
- package/dist/sdk/FirmaStakingService.js +10 -10
- package/dist/sdk/FirmaTokenService.js +8 -8
- package/dist/sdk/FirmaUtil.d.ts +3 -0
- package/dist/sdk/FirmaUtil.js +56 -6
- package/dist/sdk/FirmaWalletService.d.ts +13 -1
- package/dist/sdk/FirmaWalletService.js +39 -5
- package/dist/sdk/firmachain/amino/addresses.d.ts +5 -0
- package/dist/sdk/firmachain/amino/addresses.js +46 -0
- package/dist/sdk/firmachain/amino/aminomsgs.d.ts +281 -0
- package/dist/sdk/firmachain/amino/aminomsgs.js +77 -0
- package/dist/sdk/firmachain/amino/aminotypes.d.ts +22 -0
- package/dist/sdk/firmachain/amino/aminotypes.js +521 -0
- package/dist/sdk/firmachain/amino/coins.d.ts +30 -0
- package/dist/sdk/firmachain/amino/coins.js +69 -0
- package/dist/sdk/firmachain/amino/encoding.d.ts +24 -0
- package/dist/sdk/firmachain/amino/encoding.js +234 -0
- package/dist/sdk/firmachain/amino/multisig.d.ts +10 -0
- package/dist/sdk/firmachain/amino/multisig.js +42 -0
- package/dist/sdk/firmachain/amino/paths.d.ts +6 -0
- package/dist/sdk/firmachain/amino/paths.js +18 -0
- package/dist/sdk/firmachain/amino/pubkeys.d.ts +47 -0
- package/dist/sdk/firmachain/amino/pubkeys.js +29 -0
- package/dist/sdk/firmachain/amino/secp256k1hdwallet.d.ts +94 -0
- package/dist/sdk/firmachain/amino/secp256k1hdwallet.js +437 -0
- package/dist/sdk/firmachain/amino/secp256k1wallet.d.ts +23 -0
- package/dist/sdk/firmachain/amino/secp256k1wallet.js +141 -0
- package/dist/sdk/firmachain/amino/signature.d.ts +16 -0
- package/dist/sdk/firmachain/amino/signature.js +36 -0
- package/dist/sdk/firmachain/amino/signdoc.d.ts +26 -0
- package/dist/sdk/firmachain/amino/signdoc.js +42 -0
- package/dist/sdk/firmachain/amino/signer.d.ts +33 -0
- package/dist/sdk/firmachain/amino/signer.js +2 -0
- package/dist/sdk/firmachain/amino/stdtx.d.ts +15 -0
- package/dist/sdk/firmachain/amino/stdtx.js +17 -0
- package/dist/sdk/firmachain/amino/wallet.d.ts +32 -0
- package/dist/sdk/firmachain/amino/wallet.js +132 -0
- package/dist/sdk/firmachain/bank/BankTxClient.d.ts +3 -2
- package/dist/sdk/firmachain/common/ITxClient.d.ts +4 -2
- package/dist/sdk/firmachain/common/ITxClient.js +10 -6
- package/dist/sdk/firmachain/common/LedgerSigningStargateClient.d.ts +27 -0
- package/dist/sdk/firmachain/common/LedgerSigningStargateClient.js +165 -0
- package/dist/sdk/firmachain/common/LedgerWallet.d.ts +9 -0
- package/dist/sdk/firmachain/common/LedgerWallet.js +150 -0
- package/dist/sdk/firmachain/common/signing.js +17 -1
- package/dist/sdk/firmachain/common/signingstargateclient.js +19 -3
- package/dist/sdk/firmachain/common/stargateclient.js +5 -2
- package/dist/sdk/firmachain/contract/ContractTxClient.d.ts +3 -2
- package/dist/sdk/firmachain/contract/ContractTxTypes.js +50 -9
- package/dist/sdk/firmachain/distribution/DistributionTxClient.d.ts +3 -2
- package/dist/sdk/firmachain/distribution/index.js +0 -1
- package/dist/sdk/firmachain/feegrant/FeeGrantTxClient.d.ts +3 -2
- package/dist/sdk/firmachain/feegrant/FeeGrantTxTypes.js +63 -13
- package/dist/sdk/firmachain/google/protobuf/any.js +3 -2
- package/dist/sdk/firmachain/google/protobuf/descriptor.js +1364 -324
- package/dist/sdk/firmachain/gov/GovTxClient.d.ts +3 -2
- package/dist/sdk/firmachain/gov/index.js +0 -1
- package/dist/sdk/firmachain/nft/NftTxClient.d.ts +3 -2
- package/dist/sdk/firmachain/staking/StakingTxClient.d.ts +3 -2
- package/dist/sdk/firmachain/token/TokenTxClient.d.ts +3 -2
- package/package.json +1 -1
|
@@ -0,0 +1,437 @@
|
|
|
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 (_) 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 __read = (this && this.__read) || function (o, n) {
|
|
50
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
51
|
+
if (!m) return o;
|
|
52
|
+
var i = m.call(o), r, ar = [], e;
|
|
53
|
+
try {
|
|
54
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
55
|
+
}
|
|
56
|
+
catch (error) { e = { error: error }; }
|
|
57
|
+
finally {
|
|
58
|
+
try {
|
|
59
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
60
|
+
}
|
|
61
|
+
finally { if (e) throw e.error; }
|
|
62
|
+
}
|
|
63
|
+
return ar;
|
|
64
|
+
};
|
|
65
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
|
|
66
|
+
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
|
|
67
|
+
to[j] = from[i];
|
|
68
|
+
return to;
|
|
69
|
+
};
|
|
70
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
71
|
+
exports.Secp256k1HdWallet = exports.extractKdfConfiguration = void 0;
|
|
72
|
+
var crypto_1 = require("@cosmjs/crypto");
|
|
73
|
+
var encoding_1 = require("@cosmjs/encoding");
|
|
74
|
+
var utils_1 = require("@cosmjs/utils");
|
|
75
|
+
var addresses_1 = require("./addresses");
|
|
76
|
+
var paths_1 = require("./paths");
|
|
77
|
+
var signature_1 = require("./signature");
|
|
78
|
+
var signdoc_1 = require("./signdoc");
|
|
79
|
+
var wallet_1 = require("./wallet");
|
|
80
|
+
var serializationTypeV1 = "secp256k1wallet-v1";
|
|
81
|
+
/**
|
|
82
|
+
* A KDF configuration that is not very strong but can be used on the main thread.
|
|
83
|
+
* It takes about 1 second in Node.js 16.0.0 and should have similar runtimes in other modern Wasm hosts.
|
|
84
|
+
*/
|
|
85
|
+
var basicPasswordHashingOptions = {
|
|
86
|
+
algorithm: "argon2id",
|
|
87
|
+
params: {
|
|
88
|
+
outputLength: 32,
|
|
89
|
+
opsLimit: 24,
|
|
90
|
+
memLimitKib: 12 * 1024,
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
function isDerivationJson(thing) {
|
|
94
|
+
if (!utils_1.isNonNullObject(thing))
|
|
95
|
+
return false;
|
|
96
|
+
if (typeof thing.hdPath !== "string")
|
|
97
|
+
return false;
|
|
98
|
+
if (typeof thing.prefix !== "string")
|
|
99
|
+
return false;
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
function extractKdfConfigurationV1(doc) {
|
|
103
|
+
return doc.kdf;
|
|
104
|
+
}
|
|
105
|
+
function extractKdfConfiguration(serialization) {
|
|
106
|
+
var root = JSON.parse(serialization);
|
|
107
|
+
if (!utils_1.isNonNullObject(root))
|
|
108
|
+
throw new Error("Root document is not an object.");
|
|
109
|
+
switch (root.type) {
|
|
110
|
+
case serializationTypeV1:
|
|
111
|
+
return extractKdfConfigurationV1(root);
|
|
112
|
+
default:
|
|
113
|
+
throw new Error("Unsupported serialization type");
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
exports.extractKdfConfiguration = extractKdfConfiguration;
|
|
117
|
+
var defaultOptions = {
|
|
118
|
+
bip39Password: "",
|
|
119
|
+
hdPaths: [paths_1.makeCosmoshubPath(0)],
|
|
120
|
+
prefix: "cosmos",
|
|
121
|
+
};
|
|
122
|
+
var Secp256k1HdWallet = /** @class */ (function () {
|
|
123
|
+
function Secp256k1HdWallet(mnemonic, options) {
|
|
124
|
+
var _a, _b;
|
|
125
|
+
var hdPaths = (_a = options.hdPaths) !== null && _a !== void 0 ? _a : defaultOptions.hdPaths;
|
|
126
|
+
var prefix = (_b = options.prefix) !== null && _b !== void 0 ? _b : defaultOptions.prefix;
|
|
127
|
+
this.secret = mnemonic;
|
|
128
|
+
this.seed = options.seed;
|
|
129
|
+
this.accounts = hdPaths.map(function (hdPath) { return ({
|
|
130
|
+
hdPath: hdPath,
|
|
131
|
+
prefix: prefix,
|
|
132
|
+
}); });
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Restores a wallet from the given BIP39 mnemonic.
|
|
136
|
+
*
|
|
137
|
+
* @param mnemonic Any valid English mnemonic.
|
|
138
|
+
* @param options An optional `Secp256k1HdWalletOptions` object optionally containing a bip39Password, hdPaths, and prefix.
|
|
139
|
+
*/
|
|
140
|
+
Secp256k1HdWallet.fromMnemonic = function (mnemonic, options) {
|
|
141
|
+
if (options === void 0) { options = {}; }
|
|
142
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
143
|
+
var mnemonicChecked, seed;
|
|
144
|
+
return __generator(this, function (_a) {
|
|
145
|
+
switch (_a.label) {
|
|
146
|
+
case 0:
|
|
147
|
+
mnemonicChecked = new crypto_1.EnglishMnemonic(mnemonic);
|
|
148
|
+
return [4 /*yield*/, crypto_1.Bip39.mnemonicToSeed(mnemonicChecked, options.bip39Password)];
|
|
149
|
+
case 1:
|
|
150
|
+
seed = _a.sent();
|
|
151
|
+
return [2 /*return*/, new Secp256k1HdWallet(mnemonicChecked, __assign(__assign({}, options), { seed: seed }))];
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* Generates a new wallet with a BIP39 mnemonic of the given length.
|
|
158
|
+
*
|
|
159
|
+
* @param length The number of words in the mnemonic (12, 15, 18, 21 or 24).
|
|
160
|
+
* @param options An optional `Secp256k1HdWalletOptions` object optionally containing a bip39Password, hdPaths, and prefix.
|
|
161
|
+
*/
|
|
162
|
+
Secp256k1HdWallet.generate = function (length, options) {
|
|
163
|
+
if (length === void 0) { length = 12; }
|
|
164
|
+
if (options === void 0) { options = {}; }
|
|
165
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
166
|
+
var entropyLength, entropy, mnemonic;
|
|
167
|
+
return __generator(this, function (_a) {
|
|
168
|
+
entropyLength = 4 * Math.floor((11 * length) / 33);
|
|
169
|
+
entropy = crypto_1.Random.getBytes(entropyLength);
|
|
170
|
+
mnemonic = crypto_1.Bip39.encode(entropy);
|
|
171
|
+
return [2 /*return*/, Secp256k1HdWallet.fromMnemonic(mnemonic.toString(), options)];
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
};
|
|
175
|
+
/**
|
|
176
|
+
* Restores a wallet from an encrypted serialization.
|
|
177
|
+
*
|
|
178
|
+
* @param password The user provided password used to generate an encryption key via a KDF.
|
|
179
|
+
* This is not normalized internally (see "Unicode normalization" to learn more).
|
|
180
|
+
*/
|
|
181
|
+
Secp256k1HdWallet.deserialize = function (serialization, password) {
|
|
182
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
183
|
+
var root;
|
|
184
|
+
return __generator(this, function (_a) {
|
|
185
|
+
root = JSON.parse(serialization);
|
|
186
|
+
if (!utils_1.isNonNullObject(root))
|
|
187
|
+
throw new Error("Root document is not an object.");
|
|
188
|
+
switch (root.type) {
|
|
189
|
+
case serializationTypeV1:
|
|
190
|
+
return [2 /*return*/, Secp256k1HdWallet.deserializeTypeV1(serialization, password)];
|
|
191
|
+
default:
|
|
192
|
+
throw new Error("Unsupported serialization type");
|
|
193
|
+
}
|
|
194
|
+
return [2 /*return*/];
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
};
|
|
198
|
+
/**
|
|
199
|
+
* Restores a wallet from an encrypted serialization.
|
|
200
|
+
*
|
|
201
|
+
* This is an advanced alternative to calling `deserialize(serialization, password)` directly, which allows
|
|
202
|
+
* you to offload the KDF execution to a non-UI thread (e.g. in a WebWorker).
|
|
203
|
+
*
|
|
204
|
+
* The caller is responsible for ensuring the key was derived with the given KDF configuration. This can be
|
|
205
|
+
* done using `extractKdfConfiguration(serialization)` and `executeKdf(password, kdfConfiguration)` from this package.
|
|
206
|
+
*/
|
|
207
|
+
Secp256k1HdWallet.deserializeWithEncryptionKey = function (serialization, encryptionKey) {
|
|
208
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
209
|
+
var root, untypedRoot, _a, decryptedBytes, decryptedDocument, mnemonic, accounts, firstPrefix_1, hdPaths;
|
|
210
|
+
return __generator(this, function (_b) {
|
|
211
|
+
switch (_b.label) {
|
|
212
|
+
case 0:
|
|
213
|
+
root = JSON.parse(serialization);
|
|
214
|
+
if (!utils_1.isNonNullObject(root))
|
|
215
|
+
throw new Error("Root document is not an object.");
|
|
216
|
+
untypedRoot = root;
|
|
217
|
+
_a = untypedRoot.type;
|
|
218
|
+
switch (_a) {
|
|
219
|
+
case serializationTypeV1: return [3 /*break*/, 1];
|
|
220
|
+
}
|
|
221
|
+
return [3 /*break*/, 3];
|
|
222
|
+
case 1: return [4 /*yield*/, wallet_1.decrypt(encoding_1.fromBase64(untypedRoot.data), encryptionKey, untypedRoot.encryption)];
|
|
223
|
+
case 2:
|
|
224
|
+
decryptedBytes = _b.sent();
|
|
225
|
+
decryptedDocument = JSON.parse(encoding_1.fromUtf8(decryptedBytes));
|
|
226
|
+
mnemonic = decryptedDocument.mnemonic, accounts = decryptedDocument.accounts;
|
|
227
|
+
utils_1.assert(typeof mnemonic === "string");
|
|
228
|
+
if (!Array.isArray(accounts))
|
|
229
|
+
throw new Error("Property 'accounts' is not an array");
|
|
230
|
+
if (!accounts.every(function (account) { return isDerivationJson(account); })) {
|
|
231
|
+
throw new Error("Account is not in the correct format.");
|
|
232
|
+
}
|
|
233
|
+
firstPrefix_1 = accounts[0].prefix;
|
|
234
|
+
if (!accounts.every(function (_a) {
|
|
235
|
+
var prefix = _a.prefix;
|
|
236
|
+
return prefix === firstPrefix_1;
|
|
237
|
+
})) {
|
|
238
|
+
throw new Error("Accounts do not all have the same prefix");
|
|
239
|
+
}
|
|
240
|
+
hdPaths = accounts.map(function (_a) {
|
|
241
|
+
var hdPath = _a.hdPath;
|
|
242
|
+
return crypto_1.stringToPath(hdPath);
|
|
243
|
+
});
|
|
244
|
+
return [2 /*return*/, Secp256k1HdWallet.fromMnemonic(mnemonic, {
|
|
245
|
+
hdPaths: hdPaths,
|
|
246
|
+
prefix: firstPrefix_1,
|
|
247
|
+
})];
|
|
248
|
+
case 3: throw new Error("Unsupported serialization type");
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
};
|
|
253
|
+
Secp256k1HdWallet.deserializeTypeV1 = function (serialization, password) {
|
|
254
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
255
|
+
var root, encryptionKey;
|
|
256
|
+
return __generator(this, function (_a) {
|
|
257
|
+
switch (_a.label) {
|
|
258
|
+
case 0:
|
|
259
|
+
root = JSON.parse(serialization);
|
|
260
|
+
if (!utils_1.isNonNullObject(root))
|
|
261
|
+
throw new Error("Root document is not an object.");
|
|
262
|
+
return [4 /*yield*/, wallet_1.executeKdf(password, root.kdf)];
|
|
263
|
+
case 1:
|
|
264
|
+
encryptionKey = _a.sent();
|
|
265
|
+
return [2 /*return*/, Secp256k1HdWallet.deserializeWithEncryptionKey(serialization, encryptionKey)];
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
};
|
|
270
|
+
Object.defineProperty(Secp256k1HdWallet.prototype, "mnemonic", {
|
|
271
|
+
get: function () {
|
|
272
|
+
return this.secret.toString();
|
|
273
|
+
},
|
|
274
|
+
enumerable: false,
|
|
275
|
+
configurable: true
|
|
276
|
+
});
|
|
277
|
+
Secp256k1HdWallet.prototype.getAccounts = function () {
|
|
278
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
279
|
+
var accountsWithPrivkeys;
|
|
280
|
+
return __generator(this, function (_a) {
|
|
281
|
+
switch (_a.label) {
|
|
282
|
+
case 0: return [4 /*yield*/, this.getAccountsWithPrivkeys()];
|
|
283
|
+
case 1:
|
|
284
|
+
accountsWithPrivkeys = _a.sent();
|
|
285
|
+
return [2 /*return*/, accountsWithPrivkeys.map(function (_a) {
|
|
286
|
+
var algo = _a.algo, pubkey = _a.pubkey, address = _a.address;
|
|
287
|
+
return ({
|
|
288
|
+
algo: algo,
|
|
289
|
+
pubkey: pubkey,
|
|
290
|
+
address: address,
|
|
291
|
+
});
|
|
292
|
+
})];
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
});
|
|
296
|
+
};
|
|
297
|
+
Secp256k1HdWallet.prototype.signAmino = function (signerAddress, signDoc) {
|
|
298
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
299
|
+
var accounts, account, privkey, pubkey, message, signature, signatureBytes;
|
|
300
|
+
return __generator(this, function (_a) {
|
|
301
|
+
switch (_a.label) {
|
|
302
|
+
case 0: return [4 /*yield*/, this.getAccountsWithPrivkeys()];
|
|
303
|
+
case 1:
|
|
304
|
+
accounts = _a.sent();
|
|
305
|
+
account = accounts.find(function (_a) {
|
|
306
|
+
var address = _a.address;
|
|
307
|
+
return address === signerAddress;
|
|
308
|
+
});
|
|
309
|
+
if (account === undefined) {
|
|
310
|
+
throw new Error("Address " + signerAddress + " not found in wallet");
|
|
311
|
+
}
|
|
312
|
+
privkey = account.privkey, pubkey = account.pubkey;
|
|
313
|
+
message = crypto_1.sha256(signdoc_1.serializeSignDoc(signDoc));
|
|
314
|
+
return [4 /*yield*/, crypto_1.Secp256k1.createSignature(message, privkey)];
|
|
315
|
+
case 2:
|
|
316
|
+
signature = _a.sent();
|
|
317
|
+
signatureBytes = new Uint8Array(__spreadArray(__spreadArray([], __read(signature.r(32))), __read(signature.s(32))));
|
|
318
|
+
return [2 /*return*/, {
|
|
319
|
+
signed: signDoc,
|
|
320
|
+
signature: signature_1.encodeSecp256k1Signature(pubkey, signatureBytes),
|
|
321
|
+
}];
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
};
|
|
326
|
+
/**
|
|
327
|
+
* Generates an encrypted serialization of this wallet.
|
|
328
|
+
*
|
|
329
|
+
* @param password The user provided password used to generate an encryption key via a KDF.
|
|
330
|
+
* This is not normalized internally (see "Unicode normalization" to learn more).
|
|
331
|
+
*/
|
|
332
|
+
Secp256k1HdWallet.prototype.serialize = function (password) {
|
|
333
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
334
|
+
var kdfConfiguration, encryptionKey;
|
|
335
|
+
return __generator(this, function (_a) {
|
|
336
|
+
switch (_a.label) {
|
|
337
|
+
case 0:
|
|
338
|
+
kdfConfiguration = basicPasswordHashingOptions;
|
|
339
|
+
return [4 /*yield*/, wallet_1.executeKdf(password, kdfConfiguration)];
|
|
340
|
+
case 1:
|
|
341
|
+
encryptionKey = _a.sent();
|
|
342
|
+
return [2 /*return*/, this.serializeWithEncryptionKey(encryptionKey, kdfConfiguration)];
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
});
|
|
346
|
+
};
|
|
347
|
+
/**
|
|
348
|
+
* Generates an encrypted serialization of this wallet.
|
|
349
|
+
*
|
|
350
|
+
* This is an advanced alternative to calling `serialize(password)` directly, which allows you to
|
|
351
|
+
* offload the KDF execution to a non-UI thread (e.g. in a WebWorker).
|
|
352
|
+
*
|
|
353
|
+
* The caller is responsible for ensuring the key was derived with the given KDF options. If this
|
|
354
|
+
* is not the case, the wallet cannot be restored with the original password.
|
|
355
|
+
*/
|
|
356
|
+
Secp256k1HdWallet.prototype.serializeWithEncryptionKey = function (encryptionKey, kdfConfiguration) {
|
|
357
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
358
|
+
var dataToEncrypt, dataToEncryptRaw, encryptionConfiguration, encryptedData, out;
|
|
359
|
+
return __generator(this, function (_a) {
|
|
360
|
+
switch (_a.label) {
|
|
361
|
+
case 0:
|
|
362
|
+
dataToEncrypt = {
|
|
363
|
+
mnemonic: this.mnemonic,
|
|
364
|
+
accounts: this.accounts.map(function (_a) {
|
|
365
|
+
var hdPath = _a.hdPath, prefix = _a.prefix;
|
|
366
|
+
return ({
|
|
367
|
+
hdPath: crypto_1.pathToString(hdPath),
|
|
368
|
+
prefix: prefix,
|
|
369
|
+
});
|
|
370
|
+
}),
|
|
371
|
+
};
|
|
372
|
+
dataToEncryptRaw = encoding_1.toUtf8(JSON.stringify(dataToEncrypt));
|
|
373
|
+
encryptionConfiguration = {
|
|
374
|
+
algorithm: wallet_1.supportedAlgorithms.xchacha20poly1305Ietf,
|
|
375
|
+
};
|
|
376
|
+
return [4 /*yield*/, wallet_1.encrypt(dataToEncryptRaw, encryptionKey, encryptionConfiguration)];
|
|
377
|
+
case 1:
|
|
378
|
+
encryptedData = _a.sent();
|
|
379
|
+
out = {
|
|
380
|
+
type: serializationTypeV1,
|
|
381
|
+
kdf: kdfConfiguration,
|
|
382
|
+
encryption: encryptionConfiguration,
|
|
383
|
+
data: encoding_1.toBase64(encryptedData),
|
|
384
|
+
};
|
|
385
|
+
return [2 /*return*/, JSON.stringify(out)];
|
|
386
|
+
}
|
|
387
|
+
});
|
|
388
|
+
});
|
|
389
|
+
};
|
|
390
|
+
Secp256k1HdWallet.prototype.getKeyPair = function (hdPath) {
|
|
391
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
392
|
+
var privkey, pubkey;
|
|
393
|
+
return __generator(this, function (_a) {
|
|
394
|
+
switch (_a.label) {
|
|
395
|
+
case 0:
|
|
396
|
+
privkey = crypto_1.Slip10.derivePath(crypto_1.Slip10Curve.Secp256k1, this.seed, hdPath).privkey;
|
|
397
|
+
return [4 /*yield*/, crypto_1.Secp256k1.makeKeypair(privkey)];
|
|
398
|
+
case 1:
|
|
399
|
+
pubkey = (_a.sent()).pubkey;
|
|
400
|
+
return [2 /*return*/, {
|
|
401
|
+
privkey: privkey,
|
|
402
|
+
pubkey: crypto_1.Secp256k1.compressPubkey(pubkey),
|
|
403
|
+
}];
|
|
404
|
+
}
|
|
405
|
+
});
|
|
406
|
+
});
|
|
407
|
+
};
|
|
408
|
+
Secp256k1HdWallet.prototype.getAccountsWithPrivkeys = function () {
|
|
409
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
410
|
+
var _this = this;
|
|
411
|
+
return __generator(this, function (_a) {
|
|
412
|
+
return [2 /*return*/, Promise.all(this.accounts.map(function (_a) {
|
|
413
|
+
var hdPath = _a.hdPath, prefix = _a.prefix;
|
|
414
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
415
|
+
var _b, privkey, pubkey, address;
|
|
416
|
+
return __generator(this, function (_c) {
|
|
417
|
+
switch (_c.label) {
|
|
418
|
+
case 0: return [4 /*yield*/, this.getKeyPair(hdPath)];
|
|
419
|
+
case 1:
|
|
420
|
+
_b = _c.sent(), privkey = _b.privkey, pubkey = _b.pubkey;
|
|
421
|
+
address = encoding_1.Bech32.encode(prefix, addresses_1.rawSecp256k1PubkeyToRawAddress(pubkey));
|
|
422
|
+
return [2 /*return*/, {
|
|
423
|
+
algo: "secp256k1",
|
|
424
|
+
privkey: privkey,
|
|
425
|
+
pubkey: pubkey,
|
|
426
|
+
address: address,
|
|
427
|
+
}];
|
|
428
|
+
}
|
|
429
|
+
});
|
|
430
|
+
});
|
|
431
|
+
}))];
|
|
432
|
+
});
|
|
433
|
+
});
|
|
434
|
+
};
|
|
435
|
+
return Secp256k1HdWallet;
|
|
436
|
+
}());
|
|
437
|
+
exports.Secp256k1HdWallet = Secp256k1HdWallet;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { StdSignDoc } from "./signdoc";
|
|
2
|
+
import { AccountData, AminoSignResponse, OfflineAminoSigner } from "./signer";
|
|
3
|
+
/**
|
|
4
|
+
* A wallet that holds a single secp256k1 keypair.
|
|
5
|
+
*
|
|
6
|
+
* If you want to work with BIP39 mnemonics and multiple accounts, use Secp256k1HdWallet.
|
|
7
|
+
*/
|
|
8
|
+
export declare class Secp256k1Wallet implements OfflineAminoSigner {
|
|
9
|
+
/**
|
|
10
|
+
* Creates a Secp256k1Wallet from the given private key
|
|
11
|
+
*
|
|
12
|
+
* @param privkey The private key.
|
|
13
|
+
* @param prefix The bech32 address prefix (human readable part). Defaults to "cosmos".
|
|
14
|
+
*/
|
|
15
|
+
static fromKey(privkey: Uint8Array, prefix?: string): Promise<Secp256k1Wallet>;
|
|
16
|
+
private readonly pubkey;
|
|
17
|
+
private readonly privkey;
|
|
18
|
+
private readonly prefix;
|
|
19
|
+
private constructor();
|
|
20
|
+
private get address();
|
|
21
|
+
getAccounts(): Promise<readonly AccountData[]>;
|
|
22
|
+
signAmino(signerAddress: string, signDoc: StdSignDoc): Promise<AminoSignResponse>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
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 (_) 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 __read = (this && this.__read) || function (o, n) {
|
|
39
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
40
|
+
if (!m) return o;
|
|
41
|
+
var i = m.call(o), r, ar = [], e;
|
|
42
|
+
try {
|
|
43
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
44
|
+
}
|
|
45
|
+
catch (error) { e = { error: error }; }
|
|
46
|
+
finally {
|
|
47
|
+
try {
|
|
48
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
49
|
+
}
|
|
50
|
+
finally { if (e) throw e.error; }
|
|
51
|
+
}
|
|
52
|
+
return ar;
|
|
53
|
+
};
|
|
54
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
|
|
55
|
+
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
|
|
56
|
+
to[j] = from[i];
|
|
57
|
+
return to;
|
|
58
|
+
};
|
|
59
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
60
|
+
exports.Secp256k1Wallet = void 0;
|
|
61
|
+
var crypto_1 = require("@cosmjs/crypto");
|
|
62
|
+
var encoding_1 = require("@cosmjs/encoding");
|
|
63
|
+
var addresses_1 = require("./addresses");
|
|
64
|
+
var signature_1 = require("./signature");
|
|
65
|
+
var signdoc_1 = require("./signdoc");
|
|
66
|
+
/**
|
|
67
|
+
* A wallet that holds a single secp256k1 keypair.
|
|
68
|
+
*
|
|
69
|
+
* If you want to work with BIP39 mnemonics and multiple accounts, use Secp256k1HdWallet.
|
|
70
|
+
*/
|
|
71
|
+
var Secp256k1Wallet = /** @class */ (function () {
|
|
72
|
+
function Secp256k1Wallet(privkey, pubkey, prefix) {
|
|
73
|
+
this.privkey = privkey;
|
|
74
|
+
this.pubkey = pubkey;
|
|
75
|
+
this.prefix = prefix;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Creates a Secp256k1Wallet from the given private key
|
|
79
|
+
*
|
|
80
|
+
* @param privkey The private key.
|
|
81
|
+
* @param prefix The bech32 address prefix (human readable part). Defaults to "cosmos".
|
|
82
|
+
*/
|
|
83
|
+
Secp256k1Wallet.fromKey = function (privkey, prefix) {
|
|
84
|
+
if (prefix === void 0) { prefix = "cosmos"; }
|
|
85
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
86
|
+
var uncompressed;
|
|
87
|
+
return __generator(this, function (_a) {
|
|
88
|
+
switch (_a.label) {
|
|
89
|
+
case 0: return [4 /*yield*/, crypto_1.Secp256k1.makeKeypair(privkey)];
|
|
90
|
+
case 1:
|
|
91
|
+
uncompressed = (_a.sent()).pubkey;
|
|
92
|
+
return [2 /*return*/, new Secp256k1Wallet(privkey, crypto_1.Secp256k1.compressPubkey(uncompressed), prefix)];
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
};
|
|
97
|
+
Object.defineProperty(Secp256k1Wallet.prototype, "address", {
|
|
98
|
+
get: function () {
|
|
99
|
+
return encoding_1.Bech32.encode(this.prefix, addresses_1.rawSecp256k1PubkeyToRawAddress(this.pubkey));
|
|
100
|
+
},
|
|
101
|
+
enumerable: false,
|
|
102
|
+
configurable: true
|
|
103
|
+
});
|
|
104
|
+
Secp256k1Wallet.prototype.getAccounts = function () {
|
|
105
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
106
|
+
return __generator(this, function (_a) {
|
|
107
|
+
return [2 /*return*/, [
|
|
108
|
+
{
|
|
109
|
+
algo: "secp256k1",
|
|
110
|
+
address: this.address,
|
|
111
|
+
pubkey: this.pubkey,
|
|
112
|
+
},
|
|
113
|
+
]];
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
};
|
|
117
|
+
Secp256k1Wallet.prototype.signAmino = function (signerAddress, signDoc) {
|
|
118
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
119
|
+
var message, signature, signatureBytes;
|
|
120
|
+
return __generator(this, function (_a) {
|
|
121
|
+
switch (_a.label) {
|
|
122
|
+
case 0:
|
|
123
|
+
if (signerAddress !== this.address) {
|
|
124
|
+
throw new Error("Address " + signerAddress + " not found in wallet");
|
|
125
|
+
}
|
|
126
|
+
message = new crypto_1.Sha256(signdoc_1.serializeSignDoc(signDoc)).digest();
|
|
127
|
+
return [4 /*yield*/, crypto_1.Secp256k1.createSignature(message, this.privkey)];
|
|
128
|
+
case 1:
|
|
129
|
+
signature = _a.sent();
|
|
130
|
+
signatureBytes = new Uint8Array(__spreadArray(__spreadArray([], __read(signature.r(32))), __read(signature.s(32))));
|
|
131
|
+
return [2 /*return*/, {
|
|
132
|
+
signed: signDoc,
|
|
133
|
+
signature: signature_1.encodeSecp256k1Signature(this.pubkey, signatureBytes),
|
|
134
|
+
}];
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
};
|
|
139
|
+
return Secp256k1Wallet;
|
|
140
|
+
}());
|
|
141
|
+
exports.Secp256k1Wallet = Secp256k1Wallet;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Pubkey } from "./pubkeys";
|
|
2
|
+
export interface StdSignature {
|
|
3
|
+
readonly pub_key: Pubkey;
|
|
4
|
+
readonly signature: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Takes a binary pubkey and signature to create a signature object
|
|
8
|
+
*
|
|
9
|
+
* @param pubkey a compressed secp256k1 public key
|
|
10
|
+
* @param signature a 64 byte fixed length representation of secp256k1 signature components r and s
|
|
11
|
+
*/
|
|
12
|
+
export declare function encodeSecp256k1Signature(pubkey: Uint8Array, signature: Uint8Array): StdSignature;
|
|
13
|
+
export declare function decodeSignature(signature: StdSignature): {
|
|
14
|
+
readonly pubkey: Uint8Array;
|
|
15
|
+
readonly signature: Uint8Array;
|
|
16
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.decodeSignature = exports.encodeSecp256k1Signature = void 0;
|
|
4
|
+
/* eslint-disable @typescript-eslint/naming-convention */
|
|
5
|
+
var encoding_1 = require("@cosmjs/encoding");
|
|
6
|
+
var encoding_2 = require("./encoding");
|
|
7
|
+
var pubkeys_1 = require("./pubkeys");
|
|
8
|
+
/**
|
|
9
|
+
* Takes a binary pubkey and signature to create a signature object
|
|
10
|
+
*
|
|
11
|
+
* @param pubkey a compressed secp256k1 public key
|
|
12
|
+
* @param signature a 64 byte fixed length representation of secp256k1 signature components r and s
|
|
13
|
+
*/
|
|
14
|
+
function encodeSecp256k1Signature(pubkey, signature) {
|
|
15
|
+
if (signature.length !== 64) {
|
|
16
|
+
throw new Error("Signature must be 64 bytes long. Cosmos SDK uses a 2x32 byte fixed length encoding for the secp256k1 signature integers r and s.");
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
pub_key: encoding_2.encodeSecp256k1Pubkey(pubkey),
|
|
20
|
+
signature: encoding_1.toBase64(signature),
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
exports.encodeSecp256k1Signature = encodeSecp256k1Signature;
|
|
24
|
+
function decodeSignature(signature) {
|
|
25
|
+
switch (signature.pub_key.type) {
|
|
26
|
+
// Note: please don't add cases here without writing additional unit tests
|
|
27
|
+
case pubkeys_1.pubkeyType.secp256k1:
|
|
28
|
+
return {
|
|
29
|
+
pubkey: encoding_1.fromBase64(signature.pub_key.value),
|
|
30
|
+
signature: encoding_1.fromBase64(signature.signature),
|
|
31
|
+
};
|
|
32
|
+
default:
|
|
33
|
+
throw new Error("Unsupported pubkey type");
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.decodeSignature = decodeSignature;
|