@firmachain/firma-js 0.3.5 → 0.3.6-beta1
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/sdk/FirmaUtil.d.ts +21 -19
- package/dist/sdk/FirmaUtil.js +116 -151
- package/dist/sdk/FirmaWalletService.d.ts +0 -3
- package/dist/sdk/FirmaWalletService.js +11 -19
- package/dist/sdk/firmachain/common/ITxClient.d.ts +3 -1
- package/dist/sdk/firmachain/common/ITxClient.js +18 -4
- package/dist/sdk/firmachain/common/LedgerWallet.js +1 -1
- package/dist/sdk/firmachain/common/TxCommon.d.ts +2 -2
- package/dist/sdk/firmachain/common/events.d.ts +32 -0
- package/dist/sdk/firmachain/common/events.js +18 -0
- package/dist/sdk/firmachain/common/fee.d.ts +26 -0
- package/dist/sdk/firmachain/common/fee.js +85 -0
- package/dist/sdk/firmachain/common/signing.d.ts +19 -0
- package/dist/sdk/firmachain/common/signing.js +132 -0
- package/dist/sdk/firmachain/common/signingprotobufstargateclient.d.ts +34 -0
- package/dist/sdk/firmachain/common/signingprotobufstargateclient.js +319 -0
- package/dist/sdk/firmachain/common/signingstargateclient.d.ts +37 -0
- package/dist/sdk/firmachain/common/signingstargateclient.js +322 -0
- package/dist/sdk/firmachain/common/stargateClient.d.ts +161 -0
- package/dist/sdk/firmachain/common/stargateClient.js +433 -0
- package/dist/test/27.arbitrary_sign.test.js +59 -30
- package/dist/test/config_test.d.ts +2 -2
- package/dist/test/config_test.js +22 -5
- package/package.json +1 -1
- package/dist/sdk/firmachain/common/coins.d.ts +0 -30
- package/dist/sdk/firmachain/common/coins.js +0 -69
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
3
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
4
|
+
if (!m) return o;
|
|
5
|
+
var i = m.call(o), r, ar = [], e;
|
|
6
|
+
try {
|
|
7
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
8
|
+
}
|
|
9
|
+
catch (error) { e = { error: error }; }
|
|
10
|
+
finally {
|
|
11
|
+
try {
|
|
12
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
13
|
+
}
|
|
14
|
+
finally { if (e) throw e.error; }
|
|
15
|
+
}
|
|
16
|
+
return ar;
|
|
17
|
+
};
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
exports.calculateFee = exports.GasPrice = void 0;
|
|
20
|
+
var math_1 = require("@cosmjs/math");
|
|
21
|
+
var proto_signing_1 = require("@cosmjs/proto-signing");
|
|
22
|
+
/**
|
|
23
|
+
* Denom checker for the Cosmos SDK 0.42 denom pattern
|
|
24
|
+
* (https://github.com/cosmos/cosmos-sdk/blob/v0.42.4/types/coin.go#L599-L601).
|
|
25
|
+
*
|
|
26
|
+
* This is like a regexp but with helpful error messages.
|
|
27
|
+
*/
|
|
28
|
+
function checkDenom(denom) {
|
|
29
|
+
if (denom.length < 3 || denom.length > 128) {
|
|
30
|
+
throw new Error("Denom must be between 3 and 128 characters");
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* A gas price, i.e. the price of a single unit of gas. This is typically a fraction of
|
|
35
|
+
* the smallest fee token unit, such as 0.012utoken.
|
|
36
|
+
*/
|
|
37
|
+
var GasPrice = /** @class */ (function () {
|
|
38
|
+
function GasPrice(amount, denom) {
|
|
39
|
+
this.amount = amount;
|
|
40
|
+
this.denom = denom;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Parses a gas price formatted as `<amount><denom>`, e.g. `GasPrice.fromString("0.012utoken")`.
|
|
44
|
+
*
|
|
45
|
+
* The denom must match the Cosmos SDK 0.42 pattern (https://github.com/cosmos/cosmos-sdk/blob/v0.42.4/types/coin.go#L599-L601).
|
|
46
|
+
* See `GasPrice` in @cosmjs/stargate for a more generic matcher.
|
|
47
|
+
*
|
|
48
|
+
* Separators are not yet supported.
|
|
49
|
+
*/
|
|
50
|
+
GasPrice.fromString = function (gasPrice) {
|
|
51
|
+
// Use Decimal.fromUserInput and checkDenom for detailed checks and helpful error messages
|
|
52
|
+
var matchResult = gasPrice.match(/^([0-9.]+)([a-zA-Z][a-zA-Z0-9/:._-]*)$/);
|
|
53
|
+
if (!matchResult) {
|
|
54
|
+
throw new Error("Invalid gas price string");
|
|
55
|
+
}
|
|
56
|
+
var _a = __read(matchResult, 3), _ = _a[0], amount = _a[1], denom = _a[2];
|
|
57
|
+
checkDenom(denom);
|
|
58
|
+
var fractionalDigits = 18;
|
|
59
|
+
var decimalAmount = math_1.Decimal.fromUserInput(amount, fractionalDigits);
|
|
60
|
+
return new GasPrice(decimalAmount, denom);
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Returns a string representation of this gas price, e.g. "0.025uatom".
|
|
64
|
+
* This can be used as an input to `GasPrice.fromString`.
|
|
65
|
+
*/
|
|
66
|
+
GasPrice.prototype.toString = function () {
|
|
67
|
+
return this.amount.toString() + this.denom;
|
|
68
|
+
};
|
|
69
|
+
return GasPrice;
|
|
70
|
+
}());
|
|
71
|
+
exports.GasPrice = GasPrice;
|
|
72
|
+
function calculateFee(gasLimit, gasPrice) {
|
|
73
|
+
var processedGasPrice = typeof gasPrice === "string" ? GasPrice.fromString(gasPrice) : gasPrice;
|
|
74
|
+
var denom = processedGasPrice.denom, gasPriceAmount = processedGasPrice.amount;
|
|
75
|
+
// Note: Amount can exceed the safe integer range (https://github.com/cosmos/cosmjs/issues/1134),
|
|
76
|
+
// which we handle by converting from Decimal to string without going through number.
|
|
77
|
+
var amount = gasPriceAmount.multiply(new math_1.Uint53(gasLimit)).ceil().toString();
|
|
78
|
+
return {
|
|
79
|
+
gasLimit: BigInt(gasLimit),
|
|
80
|
+
amount: (0, proto_signing_1.coins)(amount, denom),
|
|
81
|
+
payer: "",
|
|
82
|
+
granter: ""
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
exports.calculateFee = calculateFee;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Coin } from "cosmjs-types/cosmos/base/v1beta1/coin";
|
|
2
|
+
import { SignMode } from "cosmjs-types/cosmos/tx/signing/v1beta1/signing";
|
|
3
|
+
import { SignDoc } from "cosmjs-types/cosmos/tx/v1beta1/tx";
|
|
4
|
+
import { Any } from "cosmjs-types/google/protobuf/any";
|
|
5
|
+
export interface SignerData {
|
|
6
|
+
readonly pubkey: Any;
|
|
7
|
+
readonly sequence: number;
|
|
8
|
+
}
|
|
9
|
+
export declare function makeAuthInfoBytes(signers: ReadonlyArray<SignerData>, feeAmount: readonly Coin[], gasLimit: number, granter?: string, payer?: string, signMode?: SignMode): Uint8Array;
|
|
10
|
+
export declare function makeSignDoc(bodyBytes: Uint8Array, authInfoBytes: Uint8Array, chainId: string, accountNumber: number): SignDoc;
|
|
11
|
+
export declare function makeSignBytes(signDoc: SignDoc): Uint8Array;
|
|
12
|
+
/**
|
|
13
|
+
* Creates AuthInfo bytes for protobuf signing with enhanced type safety
|
|
14
|
+
*/
|
|
15
|
+
export declare function makeAuthInfoBytesProtobuf(signers: ReadonlyArray<SignerData>, feeAmount: readonly Coin[], gasLimit: bigint | number, granter?: string, payer?: string): Uint8Array;
|
|
16
|
+
/**
|
|
17
|
+
* Creates a protobuf SignDoc with validation
|
|
18
|
+
*/
|
|
19
|
+
export declare function makeSignDocProtobuf(bodyBytes: Uint8Array, authInfoBytes: Uint8Array, chainId: string, accountNumber: bigint | number): SignDoc;
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
3
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
4
|
+
if (!m) return o;
|
|
5
|
+
var i = m.call(o), r, ar = [], e;
|
|
6
|
+
try {
|
|
7
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
8
|
+
}
|
|
9
|
+
catch (error) { e = { error: error }; }
|
|
10
|
+
finally {
|
|
11
|
+
try {
|
|
12
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
13
|
+
}
|
|
14
|
+
finally { if (e) throw e.error; }
|
|
15
|
+
}
|
|
16
|
+
return ar;
|
|
17
|
+
};
|
|
18
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
19
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
20
|
+
if (ar || !(i in from)) {
|
|
21
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
22
|
+
ar[i] = from[i];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
26
|
+
};
|
|
27
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
28
|
+
exports.makeSignDocProtobuf = exports.makeAuthInfoBytesProtobuf = exports.makeSignBytes = exports.makeSignDoc = exports.makeAuthInfoBytes = void 0;
|
|
29
|
+
/* eslint-disable @typescript-eslint/naming-convention */
|
|
30
|
+
var coin_1 = require("cosmjs-types/cosmos/base/v1beta1/coin");
|
|
31
|
+
var signing_1 = require("cosmjs-types/cosmos/tx/signing/v1beta1/signing");
|
|
32
|
+
var tx_1 = require("cosmjs-types/cosmos/tx/v1beta1/tx");
|
|
33
|
+
function makeSignerInfos(signers, signMode) {
|
|
34
|
+
return signers.map(function (_a) {
|
|
35
|
+
var pubkey = _a.pubkey, sequence = _a.sequence;
|
|
36
|
+
return tx_1.SignerInfo.fromPartial({
|
|
37
|
+
publicKey: pubkey,
|
|
38
|
+
modeInfo: {
|
|
39
|
+
single: { mode: signMode },
|
|
40
|
+
},
|
|
41
|
+
sequence: BigInt(sequence),
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
function makeAuthInfoBytes(signers, feeAmount, gasLimit, granter, payer, signMode) {
|
|
46
|
+
if (signMode === void 0) { signMode = signing_1.SignMode.SIGN_MODE_DIRECT; }
|
|
47
|
+
var fee = tx_1.Fee.fromPartial({
|
|
48
|
+
amount: __spreadArray([], __read(feeAmount), false),
|
|
49
|
+
gasLimit: BigInt(gasLimit),
|
|
50
|
+
granter: granter || "",
|
|
51
|
+
payer: payer || "",
|
|
52
|
+
});
|
|
53
|
+
var authInfo = tx_1.AuthInfo.fromPartial({
|
|
54
|
+
signerInfos: makeSignerInfos(signers, signMode),
|
|
55
|
+
fee: fee,
|
|
56
|
+
});
|
|
57
|
+
return tx_1.AuthInfo.encode(authInfo).finish();
|
|
58
|
+
}
|
|
59
|
+
exports.makeAuthInfoBytes = makeAuthInfoBytes;
|
|
60
|
+
function makeSignDoc(bodyBytes, authInfoBytes, chainId, accountNumber) {
|
|
61
|
+
return tx_1.SignDoc.fromPartial({
|
|
62
|
+
bodyBytes: bodyBytes,
|
|
63
|
+
authInfoBytes: authInfoBytes,
|
|
64
|
+
chainId: chainId,
|
|
65
|
+
accountNumber: BigInt(accountNumber),
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
exports.makeSignDoc = makeSignDoc;
|
|
69
|
+
function makeSignBytes(signDoc) {
|
|
70
|
+
// Ensure all required fields are present
|
|
71
|
+
if (!signDoc.bodyBytes || !signDoc.authInfoBytes || !signDoc.chainId || signDoc.accountNumber === undefined) {
|
|
72
|
+
throw new Error("SignDoc is missing required fields");
|
|
73
|
+
}
|
|
74
|
+
var doc = tx_1.SignDoc.fromPartial({
|
|
75
|
+
accountNumber: signDoc.accountNumber,
|
|
76
|
+
authInfoBytes: signDoc.authInfoBytes,
|
|
77
|
+
bodyBytes: signDoc.bodyBytes,
|
|
78
|
+
chainId: signDoc.chainId,
|
|
79
|
+
});
|
|
80
|
+
return tx_1.SignDoc.encode(doc).finish();
|
|
81
|
+
}
|
|
82
|
+
exports.makeSignBytes = makeSignBytes;
|
|
83
|
+
/**
|
|
84
|
+
* Creates AuthInfo bytes for protobuf signing with enhanced type safety
|
|
85
|
+
*/
|
|
86
|
+
function makeAuthInfoBytesProtobuf(signers, feeAmount, gasLimit, granter, payer) {
|
|
87
|
+
var normalizedGasLimit = typeof gasLimit === 'bigint' ? gasLimit : BigInt(gasLimit);
|
|
88
|
+
var fee = tx_1.Fee.fromPartial({
|
|
89
|
+
amount: feeAmount.map(function (coin) { return coin_1.Coin.fromPartial(coin); }),
|
|
90
|
+
gasLimit: normalizedGasLimit,
|
|
91
|
+
granter: granter || "",
|
|
92
|
+
payer: payer || "",
|
|
93
|
+
});
|
|
94
|
+
var signerInfos = signers.map(function (_a) {
|
|
95
|
+
var pubkey = _a.pubkey, sequence = _a.sequence;
|
|
96
|
+
return tx_1.SignerInfo.fromPartial({
|
|
97
|
+
publicKey: pubkey,
|
|
98
|
+
modeInfo: {
|
|
99
|
+
single: { mode: signing_1.SignMode.SIGN_MODE_DIRECT },
|
|
100
|
+
},
|
|
101
|
+
sequence: BigInt(sequence),
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
var authInfo = tx_1.AuthInfo.fromPartial({
|
|
105
|
+
signerInfos: signerInfos,
|
|
106
|
+
fee: fee,
|
|
107
|
+
});
|
|
108
|
+
return tx_1.AuthInfo.encode(authInfo).finish();
|
|
109
|
+
}
|
|
110
|
+
exports.makeAuthInfoBytesProtobuf = makeAuthInfoBytesProtobuf;
|
|
111
|
+
/**
|
|
112
|
+
* Creates a protobuf SignDoc with validation
|
|
113
|
+
*/
|
|
114
|
+
function makeSignDocProtobuf(bodyBytes, authInfoBytes, chainId, accountNumber) {
|
|
115
|
+
if (!bodyBytes || bodyBytes.length === 0) {
|
|
116
|
+
throw new Error("bodyBytes cannot be empty");
|
|
117
|
+
}
|
|
118
|
+
if (!authInfoBytes || authInfoBytes.length === 0) {
|
|
119
|
+
throw new Error("authInfoBytes cannot be empty");
|
|
120
|
+
}
|
|
121
|
+
if (!chainId) {
|
|
122
|
+
throw new Error("chainId cannot be empty");
|
|
123
|
+
}
|
|
124
|
+
var normalizedAccountNumber = typeof accountNumber === 'bigint' ? accountNumber : BigInt(accountNumber);
|
|
125
|
+
return tx_1.SignDoc.fromPartial({
|
|
126
|
+
bodyBytes: bodyBytes,
|
|
127
|
+
authInfoBytes: authInfoBytes,
|
|
128
|
+
chainId: chainId,
|
|
129
|
+
accountNumber: normalizedAccountNumber,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
exports.makeSignDocProtobuf = makeSignDocProtobuf;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { OfflineDirectSigner } from "@cosmjs/proto-signing";
|
|
2
|
+
import { Comet38Client } from "@cosmjs/tendermint-rpc";
|
|
3
|
+
import { Account } from "./accounts";
|
|
4
|
+
import { StargateClient, StargateClientOptions } from "./stargateClient";
|
|
5
|
+
export interface SignerData {
|
|
6
|
+
readonly accountNumber: number;
|
|
7
|
+
readonly sequence: number;
|
|
8
|
+
readonly chainId: string;
|
|
9
|
+
}
|
|
10
|
+
export interface SequenceResponse {
|
|
11
|
+
readonly accountNumber: number;
|
|
12
|
+
readonly sequence: number;
|
|
13
|
+
}
|
|
14
|
+
export interface ArbitraryVerifyData {
|
|
15
|
+
type: string;
|
|
16
|
+
signer: string;
|
|
17
|
+
data: string;
|
|
18
|
+
pubkey: string;
|
|
19
|
+
signature: string;
|
|
20
|
+
}
|
|
21
|
+
export interface MsgSignData {
|
|
22
|
+
readonly signer: string;
|
|
23
|
+
readonly data: string;
|
|
24
|
+
}
|
|
25
|
+
export declare class SigningProtobufStargateClient extends StargateClient {
|
|
26
|
+
private readonly signer;
|
|
27
|
+
private readonly _endpoint;
|
|
28
|
+
constructor(cometClient: Comet38Client, options: StargateClientOptions, signer: OfflineDirectSigner, endpoint: string);
|
|
29
|
+
static connectWithSigner(endpoint: string, signer: OfflineDirectSigner, options: StargateClientOptions): Promise<SigningProtobufStargateClient>;
|
|
30
|
+
experimentalAdr36Sign(signerAddress: string, data: Uint8Array | Uint8Array[]): Promise<ArbitraryVerifyData>;
|
|
31
|
+
static experimentalAdr36Verify(data: ArbitraryVerifyData, checkMsg: string): Promise<boolean>;
|
|
32
|
+
private toAccAddress;
|
|
33
|
+
getAccount(address: string): Promise<Account | null>;
|
|
34
|
+
}
|
|
@@ -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
|
+
}
|