@firmachain/firma-js 0.3.5 → 0.3.6

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 (28) hide show
  1. package/dist/sdk/FirmaUtil.d.ts +21 -19
  2. package/dist/sdk/FirmaUtil.js +116 -151
  3. package/dist/sdk/FirmaWalletService.d.ts +0 -3
  4. package/dist/sdk/FirmaWalletService.js +11 -19
  5. package/dist/sdk/firmachain/common/ITxClient.d.ts +3 -1
  6. package/dist/sdk/firmachain/common/ITxClient.js +18 -4
  7. package/dist/sdk/firmachain/common/LedgerWallet.js +1 -1
  8. package/dist/sdk/firmachain/common/SigningProtobufStargateClient.d.ts +34 -0
  9. package/dist/sdk/firmachain/common/SigningProtobufStargateClient.js +319 -0
  10. package/dist/sdk/firmachain/common/SigningStargateClient.d.ts +37 -0
  11. package/dist/sdk/firmachain/common/SigningStargateClient.js +322 -0
  12. package/dist/sdk/firmachain/common/StargateClient.d.ts +161 -0
  13. package/dist/sdk/firmachain/common/StargateClient.js +433 -0
  14. package/dist/sdk/firmachain/common/TxCommon.d.ts +2 -16
  15. package/dist/sdk/firmachain/common/accounts.d.ts +4 -0
  16. package/dist/sdk/firmachain/common/events.d.ts +36 -0
  17. package/dist/sdk/firmachain/common/events.js +18 -0
  18. package/dist/sdk/firmachain/common/fee.d.ts +26 -0
  19. package/dist/sdk/firmachain/common/fee.js +90 -0
  20. package/dist/sdk/firmachain/common/signing.d.ts +19 -0
  21. package/dist/sdk/firmachain/common/signing.js +132 -0
  22. package/dist/test/26.cosmwasm_query.test.js +1 -3
  23. package/dist/test/27.arbitrary_sign.test.js +61 -30
  24. package/dist/test/config_test.d.ts +9 -9
  25. package/dist/test/config_test.js +27 -26
  26. package/package.json +1 -1
  27. package/dist/sdk/firmachain/common/coins.d.ts +0 -30
  28. package/dist/sdk/firmachain/common/coins.js +0 -69
@@ -0,0 +1,319 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
+ return new (P || (P = Promise))(function (resolve, reject) {
20
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
24
+ });
25
+ };
26
+ var __generator = (this && this.__generator) || function (thisArg, body) {
27
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
+ function verb(n) { return function (v) { return step([n, v]); }; }
30
+ function step(op) {
31
+ if (f) throw new TypeError("Generator is already executing.");
32
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
33
+ 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;
34
+ if (y = 0, t) op = [op[0] & 2, t.value];
35
+ switch (op[0]) {
36
+ case 0: case 1: t = op; break;
37
+ case 4: _.label++; return { value: op[1], done: false };
38
+ case 5: _.label++; y = op[1]; op = [0]; continue;
39
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
+ default:
41
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
+ if (t[2]) _.ops.pop();
46
+ _.trys.pop(); continue;
47
+ }
48
+ op = body.call(thisArg, _);
49
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
+ }
52
+ };
53
+ var __read = (this && this.__read) || function (o, n) {
54
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
55
+ if (!m) return o;
56
+ var i = m.call(o), r, ar = [], e;
57
+ try {
58
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
59
+ }
60
+ catch (error) { e = { error: error }; }
61
+ finally {
62
+ try {
63
+ if (r && !r.done && (m = i["return"])) m.call(i);
64
+ }
65
+ finally { if (e) throw e.error; }
66
+ }
67
+ return ar;
68
+ };
69
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
70
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
71
+ if (ar || !(i in from)) {
72
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
73
+ ar[i] = from[i];
74
+ }
75
+ }
76
+ return to.concat(ar || Array.prototype.slice.call(from));
77
+ };
78
+ var __importDefault = (this && this.__importDefault) || function (mod) {
79
+ return (mod && mod.__esModule) ? mod : { "default": mod };
80
+ };
81
+ Object.defineProperty(exports, "__esModule", { value: true });
82
+ exports.SigningProtobufStargateClient = void 0;
83
+ var encoding_1 = require("@cosmjs/encoding");
84
+ var proto_signing_1 = require("@cosmjs/proto-signing");
85
+ var tendermint_rpc_1 = require("@cosmjs/tendermint-rpc");
86
+ var utils_1 = require("@cosmjs/utils");
87
+ var crypto_1 = require("@cosmjs/crypto");
88
+ var tendermint_rpc_2 = require("@cosmjs/tendermint-rpc");
89
+ var tx_1 = require("cosmjs-types/cosmos/tx/v1beta1/tx");
90
+ var any_1 = require("cosmjs-types/google/protobuf/any");
91
+ var signing_1 = require("cosmjs-types/cosmos/tx/signing/v1beta1/signing");
92
+ var keys_1 = require("cosmjs-types/cosmos/crypto/secp256k1/keys");
93
+ var accounts_1 = require("./accounts");
94
+ var axios_1 = __importDefault(require("axios"));
95
+ var StargateClient_1 = require("./StargateClient");
96
+ var SigningProtobufStargateClient = /** @class */ (function (_super) {
97
+ __extends(SigningProtobufStargateClient, _super);
98
+ function SigningProtobufStargateClient(cometClient, options, signer, endpoint) {
99
+ var _this = _super.call(this, cometClient, options) || this;
100
+ _this.signer = signer;
101
+ _this._endpoint = endpoint;
102
+ return _this;
103
+ }
104
+ SigningProtobufStargateClient.connectWithSigner = function (endpoint, signer, options) {
105
+ return __awaiter(this, void 0, void 0, function () {
106
+ var cometClient;
107
+ return __generator(this, function (_a) {
108
+ switch (_a.label) {
109
+ case 0: return [4 /*yield*/, tendermint_rpc_1.Comet38Client.connect(endpoint)];
110
+ case 1:
111
+ cometClient = _a.sent();
112
+ return [2 /*return*/, new SigningProtobufStargateClient(cometClient, options, signer, endpoint)];
113
+ }
114
+ });
115
+ });
116
+ };
117
+ SigningProtobufStargateClient.prototype.experimentalAdr36Sign = function (signerAddress, data) {
118
+ return __awaiter(this, void 0, void 0, function () {
119
+ var accountNumber, sequence, chainId, datas, msgs, accountFromSigner, txBody, fee, pubkey, signerInfo, authInfo, signDoc, signBytes, hash, rawWallet, privKey, signature, signatureBytes, jsonData;
120
+ return __generator(this, function (_a) {
121
+ switch (_a.label) {
122
+ case 0:
123
+ accountNumber = 0;
124
+ sequence = 0;
125
+ chainId = "";
126
+ datas = Array.isArray(data) ? data : [data];
127
+ msgs = datas.map(function (d) { return ({
128
+ typeUrl: "/cosmos.tx.v1beta1.MsgSignData",
129
+ value: any_1.Any.fromPartial({
130
+ typeUrl: "/cosmos.tx.v1beta1.MsgSignData",
131
+ value: new TextEncoder().encode(JSON.stringify({
132
+ signer: signerAddress,
133
+ data: (0, encoding_1.toBase64)(d),
134
+ })),
135
+ }),
136
+ }); });
137
+ return [4 /*yield*/, this.signer.getAccounts()];
138
+ case 1:
139
+ accountFromSigner = (_a.sent()).find(function (account) { return account.address === signerAddress; });
140
+ if (!accountFromSigner) {
141
+ throw new Error("Failed to retrieve account from signer");
142
+ }
143
+ txBody = tx_1.TxBody.fromPartial({
144
+ messages: msgs.map(function (msg) { return any_1.Any.fromPartial({
145
+ typeUrl: msg.typeUrl,
146
+ value: new Uint8Array(), // Simplified for ADR-036
147
+ }); }),
148
+ memo: "",
149
+ });
150
+ fee = tx_1.Fee.fromPartial({
151
+ amount: [],
152
+ gasLimit: BigInt(0),
153
+ });
154
+ pubkey = keys_1.PubKey.fromPartial({
155
+ key: accountFromSigner.pubkey,
156
+ });
157
+ signerInfo = tx_1.SignerInfo.fromPartial({
158
+ publicKey: any_1.Any.fromPartial({
159
+ typeUrl: "/cosmos.crypto.secp256k1.PubKey",
160
+ value: keys_1.PubKey.encode(pubkey).finish(),
161
+ }),
162
+ modeInfo: {
163
+ single: {
164
+ mode: signing_1.SignMode.SIGN_MODE_DIRECT,
165
+ },
166
+ },
167
+ sequence: BigInt(sequence),
168
+ });
169
+ authInfo = tx_1.AuthInfo.fromPartial({
170
+ signerInfos: [signerInfo],
171
+ fee: fee,
172
+ });
173
+ signDoc = tx_1.SignDoc.fromPartial({
174
+ bodyBytes: tx_1.TxBody.encode(txBody).finish(),
175
+ authInfoBytes: tx_1.AuthInfo.encode(authInfo).finish(),
176
+ chainId: chainId,
177
+ accountNumber: BigInt(accountNumber),
178
+ });
179
+ signBytes = (0, proto_signing_1.makeSignBytes)(signDoc);
180
+ hash = (0, crypto_1.sha256)(signBytes);
181
+ rawWallet = this.signer;
182
+ privKey = rawWallet.privkey;
183
+ if (!privKey) {
184
+ throw new Error("Private key not accessible for ADR-036 signing");
185
+ }
186
+ return [4 /*yield*/, crypto_1.Secp256k1.createSignature(hash, privKey)];
187
+ case 2:
188
+ signature = _a.sent();
189
+ signatureBytes = new Uint8Array(__spreadArray(__spreadArray([], __read(signature.r(32)), false), __read(signature.s(32)), false));
190
+ jsonData = {
191
+ type: "sign/MsgSignData",
192
+ signer: signerAddress,
193
+ data: (0, encoding_1.toBase64)(datas[0]),
194
+ pubkey: (0, encoding_1.toBase64)(accountFromSigner.pubkey),
195
+ signature: (0, encoding_1.toBase64)(signatureBytes)
196
+ };
197
+ return [2 /*return*/, jsonData];
198
+ }
199
+ });
200
+ });
201
+ };
202
+ SigningProtobufStargateClient.experimentalAdr36Verify = function (data, checkMsg) {
203
+ return __awaiter(this, void 0, void 0, function () {
204
+ var pubkeyBytes, signatureBytes, sourceMsg, accountNumber, sequence, chainId, txBody, fee, pubkey, signerInfo, authInfo, signDoc, signBytes, prehashed, secpSignature, rawSignerAddress, decodedSignerData, ok, error_1;
205
+ return __generator(this, function (_a) {
206
+ switch (_a.label) {
207
+ case 0:
208
+ _a.trys.push([0, 2, , 3]);
209
+ pubkeyBytes = (0, encoding_1.fromBase64)(data.pubkey);
210
+ signatureBytes = (0, encoding_1.fromBase64)(data.signature);
211
+ sourceMsg = Buffer.from(data.data, 'base64').toString();
212
+ if (sourceMsg !== checkMsg) {
213
+ throw new Error("Different Msg error. source:" + sourceMsg + ", target:" + checkMsg);
214
+ }
215
+ accountNumber = 0;
216
+ sequence = 0;
217
+ chainId = "";
218
+ txBody = tx_1.TxBody.fromPartial({
219
+ messages: [{
220
+ typeUrl: "/cosmos.tx.v1beta1.MsgSignData",
221
+ value: new Uint8Array(),
222
+ }],
223
+ memo: "",
224
+ });
225
+ fee = tx_1.Fee.fromPartial({
226
+ amount: [],
227
+ gasLimit: BigInt(0),
228
+ });
229
+ pubkey = keys_1.PubKey.fromPartial({
230
+ key: pubkeyBytes,
231
+ });
232
+ signerInfo = tx_1.SignerInfo.fromPartial({
233
+ publicKey: any_1.Any.fromPartial({
234
+ typeUrl: "/cosmos.crypto.secp256k1.PubKey",
235
+ value: keys_1.PubKey.encode(pubkey).finish(),
236
+ }),
237
+ modeInfo: {
238
+ single: {
239
+ mode: signing_1.SignMode.SIGN_MODE_DIRECT,
240
+ },
241
+ },
242
+ sequence: BigInt(sequence),
243
+ });
244
+ authInfo = tx_1.AuthInfo.fromPartial({
245
+ signerInfos: [signerInfo],
246
+ fee: fee,
247
+ });
248
+ signDoc = tx_1.SignDoc.fromPartial({
249
+ bodyBytes: tx_1.TxBody.encode(txBody).finish(),
250
+ authInfoBytes: tx_1.AuthInfo.encode(authInfo).finish(),
251
+ chainId: chainId,
252
+ accountNumber: BigInt(accountNumber),
253
+ });
254
+ signBytes = (0, proto_signing_1.makeSignBytes)(signDoc);
255
+ prehashed = (0, crypto_1.sha256)(signBytes);
256
+ secpSignature = crypto_1.Secp256k1Signature.fromFixedLength(signatureBytes);
257
+ rawSignerAddress = (0, tendermint_rpc_2.rawSecp256k1PubkeyToRawAddress)(pubkeyBytes);
258
+ decodedSignerData = (0, encoding_1.fromBech32)(data.signer).data;
259
+ if (!(0, utils_1.arrayContentEquals)(decodedSignerData, rawSignerAddress)) {
260
+ throw new Error("Found mismatch between signer in message and public key");
261
+ }
262
+ return [4 /*yield*/, crypto_1.Secp256k1.verifySignature(secpSignature, prehashed, pubkeyBytes)];
263
+ case 1:
264
+ ok = _a.sent();
265
+ return [2 /*return*/, ok];
266
+ case 2:
267
+ error_1 = _a.sent();
268
+ return [2 /*return*/, false];
269
+ case 3: return [2 /*return*/];
270
+ }
271
+ });
272
+ });
273
+ };
274
+ SigningProtobufStargateClient.prototype.toAccAddress = function (address) {
275
+ return (0, encoding_1.fromBech32)(address).data;
276
+ };
277
+ SigningProtobufStargateClient.prototype.getAccount = function (address) {
278
+ return __awaiter(this, void 0, void 0, function () {
279
+ var accAddress, hexAccAddress, axiosInstance, path, result, finalData, account, finalAccount, error_2;
280
+ return __generator(this, function (_a) {
281
+ switch (_a.label) {
282
+ case 0:
283
+ _a.trys.push([0, 2, , 3]);
284
+ accAddress = this.toAccAddress(address);
285
+ hexAccAddress = "0x01".concat(Buffer.from(accAddress).toString("hex"));
286
+ axiosInstance = axios_1.default.create({
287
+ baseURL: this._endpoint,
288
+ headers: {
289
+ Accept: "application/json",
290
+ },
291
+ timeout: 15000
292
+ });
293
+ path = "/abci_query";
294
+ return [4 /*yield*/, axiosInstance.get(path, {
295
+ params: {
296
+ path: '"store/acc/key"',
297
+ data: hexAccAddress
298
+ }
299
+ })];
300
+ case 1:
301
+ result = _a.sent();
302
+ finalData = result.data.result.response.value;
303
+ if (!finalData) {
304
+ return [2 /*return*/, null];
305
+ }
306
+ account = any_1.Any.decode(Buffer.from(finalData, "base64"));
307
+ finalAccount = (0, accounts_1.accountFromAny)(account);
308
+ return [2 /*return*/, finalAccount];
309
+ case 2:
310
+ error_2 = _a.sent();
311
+ return [2 /*return*/, null];
312
+ case 3: return [2 /*return*/];
313
+ }
314
+ });
315
+ });
316
+ };
317
+ return SigningProtobufStargateClient;
318
+ }(StargateClient_1.StargateClient));
319
+ exports.SigningProtobufStargateClient = SigningProtobufStargateClient;
@@ -0,0 +1,37 @@
1
+ import { EncodeObject, OfflineDirectSigner, Registry, GeneratedType } from "@cosmjs/proto-signing";
2
+ import { CometClient, HttpEndpoint } from "@cosmjs/tendermint-rpc";
3
+ import { SignDoc, TxRaw, Fee } from "cosmjs-types/cosmos/tx/v1beta1/tx";
4
+ import { Account } from "./accounts";
5
+ import { DeliverTxResponse, StargateClient, StargateClientOptions } from "./StargateClient";
6
+ export declare const defaultRegistryTypes: ReadonlyArray<[string, GeneratedType]>;
7
+ export interface SignerData {
8
+ readonly accountNumber: number;
9
+ readonly sequence: number;
10
+ readonly chainId: string;
11
+ }
12
+ export interface SequenceResponse {
13
+ readonly accountNumber: number;
14
+ readonly sequence: number;
15
+ }
16
+ export interface TxRawExt {
17
+ readonly txRaw: TxRaw;
18
+ readonly signature: string;
19
+ }
20
+ export interface SigningStargateClientOptions extends StargateClientOptions {
21
+ readonly registry?: Registry;
22
+ }
23
+ export declare class SigningStargateClient extends StargateClient {
24
+ readonly registry: Registry;
25
+ private readonly signer;
26
+ private static _endpoint;
27
+ static connectWithSigner(endpoint: string | HttpEndpoint, signer: OfflineDirectSigner, options?: SigningStargateClientOptions): Promise<SigningStargateClient>;
28
+ static createWithSigner(cometClient: CometClient, signer: OfflineDirectSigner, options?: SigningStargateClientOptions): Promise<SigningStargateClient>;
29
+ protected constructor(cometClient: CometClient | undefined, signer: OfflineDirectSigner, options: SigningStargateClientOptions);
30
+ signAndBroadcast(signerAddress: string, messages: readonly EncodeObject[], fee: Fee, memo?: string): Promise<DeliverTxResponse>;
31
+ static makeSignDocForSend(signerAddress: string, pubkeyStr: string, messages: readonly EncodeObject[], fee: Fee, memo: string, serverUrl: string, chainId: string, registry: Registry): Promise<SignDoc>;
32
+ sign(signerAddress: string, messages: readonly EncodeObject[], fee: Fee, memo: string, explicitSignerData?: SignerData): Promise<TxRaw>;
33
+ private signDirect;
34
+ signDirectForSignDoc(signerAddress: string, signDoc: SignDoc): Promise<TxRawExt>;
35
+ static getSequence(address: string): Promise<SequenceResponse>;
36
+ static getAccount(address: string): Promise<Account | null>;
37
+ }