@keplr-wallet/background 0.12.312 → 0.13.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.
- package/build/index.d.ts +1 -0
- package/build/index.js +7 -1
- package/build/index.js.map +1 -1
- package/build/keyring-cosmos/service.d.ts +10 -0
- package/build/keyring-cosmos/service.js +100 -0
- package/build/keyring-cosmos/service.js.map +1 -1
- package/build/keyring-ethereum/service.d.ts +5 -0
- package/build/keyring-ethereum/service.js +66 -0
- package/build/keyring-ethereum/service.js.map +1 -1
- package/build/recent-send-history/api.d.ts +31 -0
- package/build/recent-send-history/api.js +97 -0
- package/build/recent-send-history/api.js.map +1 -0
- package/build/recent-send-history/handler.js +36 -0
- package/build/recent-send-history/handler.js.map +1 -1
- package/build/recent-send-history/init.js +5 -0
- package/build/recent-send-history/init.js.map +1 -1
- package/build/recent-send-history/messages.d.ts +76 -1
- package/build/recent-send-history/messages.js +121 -1
- package/build/recent-send-history/messages.js.map +1 -1
- package/build/recent-send-history/service.d.ts +262 -9
- package/build/recent-send-history/service.js +2103 -812
- package/build/recent-send-history/service.js.map +1 -1
- package/build/recent-send-history/types.d.ts +214 -22
- package/build/recent-send-history/types.js +21 -0
- package/build/recent-send-history/types.js.map +1 -1
- package/build/tx/service.d.ts +2 -0
- package/build/tx/service.js +35 -0
- package/build/tx/service.js.map +1 -1
- package/build/tx-ethereum/service.d.ts +2 -0
- package/build/tx-ethereum/service.js +42 -0
- package/build/tx-ethereum/service.js.map +1 -1
- package/build/tx-executor/constants.d.ts +1 -0
- package/build/tx-executor/constants.js +5 -0
- package/build/tx-executor/constants.js.map +1 -0
- package/build/tx-executor/handler.d.ts +3 -0
- package/build/tx-executor/handler.js +45 -0
- package/build/tx-executor/handler.js.map +1 -0
- package/build/tx-executor/index.d.ts +3 -0
- package/build/tx-executor/index.js +20 -0
- package/build/tx-executor/index.js.map +1 -0
- package/build/tx-executor/init.d.ts +3 -0
- package/build/tx-executor/init.js +14 -0
- package/build/tx-executor/init.js.map +1 -0
- package/build/tx-executor/internal.d.ts +4 -0
- package/build/tx-executor/internal.js +24 -0
- package/build/tx-executor/internal.js.map +1 -0
- package/build/tx-executor/messages.d.ts +53 -0
- package/build/tx-executor/messages.js +116 -0
- package/build/tx-executor/messages.js.map +1 -0
- package/build/tx-executor/service.d.ts +67 -0
- package/build/tx-executor/service.js +715 -0
- package/build/tx-executor/service.js.map +1 -0
- package/build/tx-executor/types.d.ts +105 -0
- package/build/tx-executor/types.js +33 -0
- package/build/tx-executor/types.js.map +1 -0
- package/build/tx-executor/utils/cosmos.d.ts +59 -0
- package/build/tx-executor/utils/cosmos.js +526 -0
- package/build/tx-executor/utils/cosmos.js.map +1 -0
- package/build/tx-executor/utils/evm.d.ts +4 -0
- package/build/tx-executor/utils/evm.js +236 -0
- package/build/tx-executor/utils/evm.js.map +1 -0
- package/package.json +13 -13
- package/src/index.ts +24 -1
- package/src/keyring-cosmos/service.ts +151 -0
- package/src/keyring-ethereum/service.ts +103 -6
- package/src/recent-send-history/api.ts +119 -0
- package/src/recent-send-history/handler.ts +84 -0
- package/src/recent-send-history/init.ts +10 -0
- package/src/recent-send-history/messages.ts +163 -1
- package/src/recent-send-history/service.ts +3042 -1153
- package/src/recent-send-history/types.ts +268 -31
- package/src/tx/service.ts +41 -0
- package/src/tx-ethereum/service.ts +57 -0
- package/src/tx-executor/constants.ts +1 -0
- package/src/tx-executor/handler.ts +71 -0
- package/src/tx-executor/index.ts +3 -0
- package/src/tx-executor/init.ts +20 -0
- package/src/tx-executor/internal.ts +9 -0
- package/src/tx-executor/messages.ts +157 -0
- package/src/tx-executor/service.ts +1025 -0
- package/src/tx-executor/types.ts +161 -0
- package/src/tx-executor/utils/cosmos.ts +771 -0
- package/src/tx-executor/utils/evm.ts +310 -0
|
@@ -0,0 +1,236 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.fillUnsignedEVMTx = void 0;
|
|
13
|
+
const simple_fetch_1 = require("@keplr-wallet/simple-fetch");
|
|
14
|
+
const unit_1 = require("@keplr-wallet/unit");
|
|
15
|
+
const ETH_FEE_HISTORY_REWARD_PERCENTILES = [25, 50, 75];
|
|
16
|
+
const ETH_FEE_SETTINGS_BY_FEE_TYPE = {
|
|
17
|
+
low: {
|
|
18
|
+
percentile: ETH_FEE_HISTORY_REWARD_PERCENTILES[0],
|
|
19
|
+
},
|
|
20
|
+
average: {
|
|
21
|
+
percentile: ETH_FEE_HISTORY_REWARD_PERCENTILES[1],
|
|
22
|
+
},
|
|
23
|
+
high: {
|
|
24
|
+
percentile: ETH_FEE_HISTORY_REWARD_PERCENTILES[2],
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
const FEE_MULTIPLIERS = {
|
|
28
|
+
low: 1.1,
|
|
29
|
+
average: 1.25,
|
|
30
|
+
high: 1.5,
|
|
31
|
+
};
|
|
32
|
+
const GAS_ADJUSTMENT_NUM = BigInt(13);
|
|
33
|
+
const GAS_ADJUSTMENT_DEN = BigInt(10);
|
|
34
|
+
const TX_COUNT_ID = 1;
|
|
35
|
+
const LATEST_BLOCK_ID = 2;
|
|
36
|
+
const FEE_HISTORY_ID = 3;
|
|
37
|
+
const ESTIMATE_GAS_ID = 4;
|
|
38
|
+
const MAX_PRIORITY_FEE_ID = 5;
|
|
39
|
+
const toBigIntFromTxField = (value) => {
|
|
40
|
+
if (typeof value === "string" ||
|
|
41
|
+
typeof value === "number" ||
|
|
42
|
+
typeof value === "bigint") {
|
|
43
|
+
return BigInt(value);
|
|
44
|
+
}
|
|
45
|
+
if (value && typeof value === "object" && "toString" in value) {
|
|
46
|
+
return BigInt(value.toString());
|
|
47
|
+
}
|
|
48
|
+
throw new Error("Unsupported numeric value in unsigned transaction");
|
|
49
|
+
};
|
|
50
|
+
function fillUnsignedEVMTx(origin, evmInfo, signer, tx, feeType = "average") {
|
|
51
|
+
var _a;
|
|
52
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
+
const hasProvidedPriorityFee = tx.maxPriorityFeePerGas != null;
|
|
54
|
+
const hasProvidedGasLimit = tx.gasLimit != null;
|
|
55
|
+
const getTransactionCountRequest = {
|
|
56
|
+
jsonrpc: "2.0",
|
|
57
|
+
method: "eth_getTransactionCount",
|
|
58
|
+
params: [signer, "pending"],
|
|
59
|
+
id: TX_COUNT_ID,
|
|
60
|
+
};
|
|
61
|
+
const getBlockRequest = {
|
|
62
|
+
jsonrpc: "2.0",
|
|
63
|
+
method: "eth_getBlockByNumber",
|
|
64
|
+
params: ["latest", false],
|
|
65
|
+
id: LATEST_BLOCK_ID,
|
|
66
|
+
};
|
|
67
|
+
const getFeeHistoryRequest = hasProvidedPriorityFee
|
|
68
|
+
? null
|
|
69
|
+
: {
|
|
70
|
+
jsonrpc: "2.0",
|
|
71
|
+
method: "eth_feeHistory",
|
|
72
|
+
params: [20, "latest", ETH_FEE_HISTORY_REWARD_PERCENTILES],
|
|
73
|
+
id: FEE_HISTORY_ID,
|
|
74
|
+
};
|
|
75
|
+
const estimateGasRequest = hasProvidedGasLimit
|
|
76
|
+
? null
|
|
77
|
+
: {
|
|
78
|
+
jsonrpc: "2.0",
|
|
79
|
+
method: "eth_estimateGas",
|
|
80
|
+
params: [
|
|
81
|
+
{
|
|
82
|
+
from: signer,
|
|
83
|
+
to: tx.to,
|
|
84
|
+
value: tx.value,
|
|
85
|
+
data: tx.data,
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
id: ESTIMATE_GAS_ID,
|
|
89
|
+
};
|
|
90
|
+
const getMaxPriorityFeePerGasRequest = hasProvidedPriorityFee
|
|
91
|
+
? null
|
|
92
|
+
: {
|
|
93
|
+
jsonrpc: "2.0",
|
|
94
|
+
method: "eth_maxPriorityFeePerGas",
|
|
95
|
+
params: [],
|
|
96
|
+
id: MAX_PRIORITY_FEE_ID,
|
|
97
|
+
};
|
|
98
|
+
// rpc request in batch (as 2.0 jsonrpc supports batch requests)
|
|
99
|
+
const batchRequest = [
|
|
100
|
+
getTransactionCountRequest,
|
|
101
|
+
getBlockRequest,
|
|
102
|
+
...(getFeeHistoryRequest ? [getFeeHistoryRequest] : []),
|
|
103
|
+
...(estimateGasRequest ? [estimateGasRequest] : []),
|
|
104
|
+
...(getMaxPriorityFeePerGasRequest ? [getMaxPriorityFeePerGasRequest] : []),
|
|
105
|
+
];
|
|
106
|
+
const { data: rpcResponses } = yield (0, simple_fetch_1.simpleFetch)(evmInfo.rpc, {
|
|
107
|
+
method: "POST",
|
|
108
|
+
headers: {
|
|
109
|
+
"content-type": "application/json",
|
|
110
|
+
"request-source": origin,
|
|
111
|
+
},
|
|
112
|
+
body: JSON.stringify(batchRequest),
|
|
113
|
+
});
|
|
114
|
+
if (!Array.isArray(rpcResponses) ||
|
|
115
|
+
rpcResponses.length !== batchRequest.length) {
|
|
116
|
+
throw new Error("Invalid batch response format");
|
|
117
|
+
}
|
|
118
|
+
const getResult = (id, optional = false) => {
|
|
119
|
+
const res = rpcResponses.find((r) => r.id === id);
|
|
120
|
+
if (!res) {
|
|
121
|
+
if (optional) {
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
throw new Error(`No response for id=${id}`);
|
|
125
|
+
}
|
|
126
|
+
if (res.error) {
|
|
127
|
+
throw new Error(`RPC error (id=${id}): ${res.error.code} ${res.error.message}`);
|
|
128
|
+
}
|
|
129
|
+
return res.result;
|
|
130
|
+
};
|
|
131
|
+
// find responses by id
|
|
132
|
+
const nonceHex = getResult(TX_COUNT_ID);
|
|
133
|
+
if (!nonceHex) {
|
|
134
|
+
throw new Error("Failed to get nonce to fill unsigned transaction");
|
|
135
|
+
}
|
|
136
|
+
const latestBlock = getResult(LATEST_BLOCK_ID);
|
|
137
|
+
if (!latestBlock) {
|
|
138
|
+
throw new Error("Failed to get latest block to fill unsigned transaction");
|
|
139
|
+
}
|
|
140
|
+
const feeHistory = hasProvidedPriorityFee
|
|
141
|
+
? undefined
|
|
142
|
+
: getResult(FEE_HISTORY_ID);
|
|
143
|
+
const gasLimitHex = hasProvidedGasLimit
|
|
144
|
+
? undefined
|
|
145
|
+
: getResult(ESTIMATE_GAS_ID, true);
|
|
146
|
+
const networkMaxPriorityFeePerGasHex = hasProvidedPriorityFee
|
|
147
|
+
? undefined
|
|
148
|
+
: getResult(MAX_PRIORITY_FEE_ID, true);
|
|
149
|
+
let maxPriorityFeePerGasDec;
|
|
150
|
+
if (hasProvidedPriorityFee) {
|
|
151
|
+
if (tx.maxPriorityFeePerGas == null) {
|
|
152
|
+
throw new Error("maxPriorityFeePerGas is required but missing");
|
|
153
|
+
}
|
|
154
|
+
maxPriorityFeePerGasDec = new unit_1.Dec(toBigIntFromTxField(tx.maxPriorityFeePerGas));
|
|
155
|
+
}
|
|
156
|
+
else if ((feeHistory === null || feeHistory === void 0 ? void 0 : feeHistory.reward) && feeHistory.reward.length > 0) {
|
|
157
|
+
const percentile = (_a = ETH_FEE_SETTINGS_BY_FEE_TYPE[feeType].percentile) !== null && _a !== void 0 ? _a : ETH_FEE_HISTORY_REWARD_PERCENTILES[1];
|
|
158
|
+
const percentileIndex = ETH_FEE_HISTORY_REWARD_PERCENTILES.indexOf(percentile);
|
|
159
|
+
if (percentileIndex >= 0) {
|
|
160
|
+
const rewards = feeHistory.reward
|
|
161
|
+
.map((block) => block[percentileIndex])
|
|
162
|
+
.filter((v) => v != null)
|
|
163
|
+
.map((v) => BigInt(v));
|
|
164
|
+
if (rewards.length > 0) {
|
|
165
|
+
const sum = rewards.reduce((acc, x) => acc + x, BigInt(0));
|
|
166
|
+
const mean = sum / BigInt(rewards.length);
|
|
167
|
+
const sortedRewards = [...rewards].sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
|
|
168
|
+
const median = sortedRewards[Math.floor(sortedRewards.length / 2)];
|
|
169
|
+
// use 1 Gwei deviation threshold to decide between mean and median
|
|
170
|
+
const deviationThreshold = BigInt(1 * Math.pow(10, 9)); // 1 Gwei
|
|
171
|
+
const deviation = mean > median ? mean - median : median - mean;
|
|
172
|
+
const pick = deviation > deviationThreshold
|
|
173
|
+
? mean > median
|
|
174
|
+
? mean
|
|
175
|
+
: median
|
|
176
|
+
: mean;
|
|
177
|
+
maxPriorityFeePerGasDec = new unit_1.Dec(pick);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (networkMaxPriorityFeePerGasHex) {
|
|
182
|
+
const networkMaxPriorityFeePerGasDec = new unit_1.Dec(BigInt(networkMaxPriorityFeePerGasHex));
|
|
183
|
+
if (!maxPriorityFeePerGasDec ||
|
|
184
|
+
(maxPriorityFeePerGasDec &&
|
|
185
|
+
networkMaxPriorityFeePerGasDec.gt(maxPriorityFeePerGasDec))) {
|
|
186
|
+
maxPriorityFeePerGasDec = networkMaxPriorityFeePerGasDec;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if (!maxPriorityFeePerGasDec) {
|
|
190
|
+
throw new Error("Failed to calculate maxPriorityFeePerGas to fill unsigned transaction");
|
|
191
|
+
}
|
|
192
|
+
if (!latestBlock.baseFeePerGas) {
|
|
193
|
+
throw new Error("Failed to get baseFeePerGas to fill unsigned transaction");
|
|
194
|
+
}
|
|
195
|
+
const multiplier = new unit_1.Dec(FEE_MULTIPLIERS[feeType]);
|
|
196
|
+
// Calculate maxFeePerGas = baseFeePerGas + maxPriorityFeePerGas
|
|
197
|
+
const baseFeePerGasDec = new unit_1.Dec(BigInt(latestBlock.baseFeePerGas));
|
|
198
|
+
const suggestedFeeFromBase = baseFeePerGasDec.mul(multiplier);
|
|
199
|
+
const providedMaxFeePerGasDec = tx.maxFeePerGas
|
|
200
|
+
? new unit_1.Dec(toBigIntFromTxField(tx.maxFeePerGas))
|
|
201
|
+
: undefined;
|
|
202
|
+
const maxFeePerGasDec = providedMaxFeePerGasDec && hasProvidedPriorityFee
|
|
203
|
+
? providedMaxFeePerGasDec.gte(suggestedFeeFromBase.add(maxPriorityFeePerGasDec))
|
|
204
|
+
? providedMaxFeePerGasDec
|
|
205
|
+
: suggestedFeeFromBase.add(maxPriorityFeePerGasDec)
|
|
206
|
+
: suggestedFeeFromBase.add(maxPriorityFeePerGasDec);
|
|
207
|
+
const maxFeePerGasHex = `0x${maxFeePerGasDec
|
|
208
|
+
.truncate()
|
|
209
|
+
.toBigNumber()
|
|
210
|
+
.toString(16)}`;
|
|
211
|
+
const maxPriorityFeePerGasHex = `0x${maxPriorityFeePerGasDec
|
|
212
|
+
.truncate()
|
|
213
|
+
.toBigNumber()
|
|
214
|
+
.toString(16)}`;
|
|
215
|
+
const finalNonce = tx.nonce != null
|
|
216
|
+
? Math.max(Number(tx.nonce), parseInt(nonceHex, 16))
|
|
217
|
+
: parseInt(nonceHex, 16);
|
|
218
|
+
let finalGasLimit;
|
|
219
|
+
if (tx.gasLimit != null) {
|
|
220
|
+
finalGasLimit = tx.gasLimit;
|
|
221
|
+
}
|
|
222
|
+
else if (gasLimitHex) {
|
|
223
|
+
const estimatedGas = toBigIntFromTxField(gasLimitHex);
|
|
224
|
+
const adjustedGas = (estimatedGas * GAS_ADJUSTMENT_NUM + (GAS_ADJUSTMENT_DEN - BigInt(1))) /
|
|
225
|
+
GAS_ADJUSTMENT_DEN;
|
|
226
|
+
finalGasLimit = `0x${adjustedGas.toString(16)}`;
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
throw new Error("Failed to estimate gas to fill unsigned transaction");
|
|
230
|
+
}
|
|
231
|
+
const newUnsignedTx = Object.assign(Object.assign({}, tx), { nonce: finalNonce, maxFeePerGas: maxFeePerGasHex, maxPriorityFeePerGas: maxPriorityFeePerGasHex, gasLimit: finalGasLimit });
|
|
232
|
+
return newUnsignedTx;
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
exports.fillUnsignedEVMTx = fillUnsignedEVMTx;
|
|
236
|
+
//# sourceMappingURL=evm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"evm.js","sourceRoot":"","sources":["../../../src/tx-executor/utils/evm.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,6DAAyD;AAEzD,6CAAyC;AAIzC,MAAM,kCAAkC,GAAG,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;AACxD,MAAM,4BAA4B,GAK9B;IACF,GAAG,EAAE;QACH,UAAU,EAAE,kCAAkC,CAAC,CAAC,CAAC;KAClD;IACD,OAAO,EAAE;QACP,UAAU,EAAE,kCAAkC,CAAC,CAAC,CAAC;KAClD;IACD,IAAI,EAAE;QACJ,UAAU,EAAE,kCAAkC,CAAC,CAAC,CAAC;KAClD;CACF,CAAC;AAEF,MAAM,eAAe,GAAwC;IAC3D,GAAG,EAAE,GAAG;IACR,OAAO,EAAE,IAAI;IACb,IAAI,EAAE,GAAG;CACV,CAAC;AACF,MAAM,kBAAkB,GAAG,MAAM,CAAC,EAAE,CAAC,CAAC;AACtC,MAAM,kBAAkB,GAAG,MAAM,CAAC,EAAE,CAAC,CAAC;AAEtC,MAAM,WAAW,GAAG,CAAC,CAAC;AACtB,MAAM,eAAe,GAAG,CAAC,CAAC;AAC1B,MAAM,cAAc,GAAG,CAAC,CAAC;AACzB,MAAM,eAAe,GAAG,CAAC,CAAC;AAC1B,MAAM,mBAAmB,GAAG,CAAC,CAAC;AAI9B,MAAM,mBAAmB,GAAG,CAA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,QAAQ,IAAI,IAAI,EAAE;YACvB,aAAa,GAAG,EAAE,CAAC,QAAQ,CAAC;SAC7B;aAAM,IAAI,WAAW,EAAE;YACtB,MAAM,YAAY,GAAG,mBAAmB,CAAC,WAAW,CAAC,CAAC;YACtD,MAAM,WAAW,GACf,CAAC,YAAY,GAAG,kBAAkB,GAAG,CAAC,kBAAkB,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;gBACtE,kBAAkB,CAAC;YACrB,aAAa,GAAG,KAAK,WAAW,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC;SACjD;aAAM;YACL,MAAM,IAAI,KAAK,CAAC,qDAAqD,CAAC,CAAC;SACxE;QAED,MAAM,aAAa,mCACd,EAAE,KACL,KAAK,EAAE,UAAU,EACjB,YAAY,EAAE,eAAe,EAC7B,oBAAoB,EAAE,uBAAuB,EAC7C,QAAQ,EAAE,aAAa,GACxB,CAAC;QAEF,OAAO,aAAa,CAAC;;CACtB;AA5PD,8CA4PC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@keplr-wallet/background",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"main": "build/index.js",
|
|
5
5
|
"author": "chainapsis",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -32,17 +32,17 @@
|
|
|
32
32
|
"@ethersproject/hash": "^5.7.0",
|
|
33
33
|
"@ethersproject/transactions": "^5.7.0",
|
|
34
34
|
"@ethersproject/wallet": "^5.7.0",
|
|
35
|
-
"@keplr-wallet/chain-validator": "0.
|
|
36
|
-
"@keplr-wallet/common": "0.
|
|
37
|
-
"@keplr-wallet/cosmos": "0.
|
|
38
|
-
"@keplr-wallet/crypto": "0.
|
|
39
|
-
"@keplr-wallet/ledger-cosmos": "0.
|
|
40
|
-
"@keplr-wallet/popup": "0.
|
|
41
|
-
"@keplr-wallet/proto-types": "0.
|
|
42
|
-
"@keplr-wallet/router": "0.
|
|
43
|
-
"@keplr-wallet/simple-fetch": "0.
|
|
44
|
-
"@keplr-wallet/types": "0.
|
|
45
|
-
"@keplr-wallet/unit": "0.
|
|
35
|
+
"@keplr-wallet/chain-validator": "0.13.0",
|
|
36
|
+
"@keplr-wallet/common": "0.13.0",
|
|
37
|
+
"@keplr-wallet/cosmos": "0.13.0",
|
|
38
|
+
"@keplr-wallet/crypto": "0.13.0",
|
|
39
|
+
"@keplr-wallet/ledger-cosmos": "0.13.0",
|
|
40
|
+
"@keplr-wallet/popup": "0.13.0",
|
|
41
|
+
"@keplr-wallet/proto-types": "0.13.0",
|
|
42
|
+
"@keplr-wallet/router": "0.13.0",
|
|
43
|
+
"@keplr-wallet/simple-fetch": "0.13.0",
|
|
44
|
+
"@keplr-wallet/types": "0.13.0",
|
|
45
|
+
"@keplr-wallet/unit": "0.13.0",
|
|
46
46
|
"@ledgerhq/hw-app-eth": "6.42.8",
|
|
47
47
|
"@ledgerhq/hw-app-starknet": "^3.0.1",
|
|
48
48
|
"@ledgerhq/hw-transport": "^6.31.4",
|
|
@@ -69,5 +69,5 @@
|
|
|
69
69
|
"mobx-utils": "^6",
|
|
70
70
|
"starknet": "^7"
|
|
71
71
|
},
|
|
72
|
-
"gitHead": "
|
|
72
|
+
"gitHead": "83c23d58e9ee42ed3b67c27033d1af11a7c575d7"
|
|
73
73
|
}
|
package/src/index.ts
CHANGED
|
@@ -31,6 +31,7 @@ import * as RecentSendHistory from "./recent-send-history/internal";
|
|
|
31
31
|
import * as SidePanel from "./side-panel/internal";
|
|
32
32
|
import * as Settings from "./settings/internal";
|
|
33
33
|
import * as ManageViewAssetToken from "./manage-view-asset-token/internal";
|
|
34
|
+
import * as BackgroundTxExecutor from "./tx-executor/internal";
|
|
34
35
|
|
|
35
36
|
export * from "./chains";
|
|
36
37
|
export * from "./chains-ui";
|
|
@@ -58,6 +59,7 @@ export * from "./side-panel";
|
|
|
58
59
|
export * from "./settings";
|
|
59
60
|
export * from "./manage-view-asset-token";
|
|
60
61
|
export * from "./tx-ethereum";
|
|
62
|
+
export * from "./tx-executor";
|
|
61
63
|
|
|
62
64
|
import { KVStore } from "@keplr-wallet/common";
|
|
63
65
|
import { ChainInfo, ModularChainInfo } from "@keplr-wallet/types";
|
|
@@ -291,12 +293,17 @@ export function init(
|
|
|
291
293
|
keyRingBitcoinService
|
|
292
294
|
);
|
|
293
295
|
|
|
296
|
+
const txExecutableMQ =
|
|
297
|
+
BackgroundTxExecutor.createSingleChannelEventBus<BackgroundTxExecutor.TxExecutionEvent>();
|
|
298
|
+
|
|
294
299
|
const recentSendHistoryService =
|
|
295
300
|
new RecentSendHistory.RecentSendHistoryService(
|
|
296
301
|
storeCreator("recent-send-history"),
|
|
297
302
|
chainsService,
|
|
298
303
|
backgroundTxService,
|
|
299
|
-
|
|
304
|
+
analyticsService,
|
|
305
|
+
notification,
|
|
306
|
+
txExecutableMQ.publisher
|
|
300
307
|
);
|
|
301
308
|
|
|
302
309
|
const settingsService = new Settings.SettingsService(
|
|
@@ -312,6 +319,19 @@ export function init(
|
|
|
312
319
|
chainsService
|
|
313
320
|
);
|
|
314
321
|
|
|
322
|
+
const backgroundTxExecutorService =
|
|
323
|
+
new BackgroundTxExecutor.BackgroundTxExecutorService(
|
|
324
|
+
storeCreator("background-tx-executor"),
|
|
325
|
+
chainsService,
|
|
326
|
+
keyRingCosmosService,
|
|
327
|
+
keyRingEthereumService,
|
|
328
|
+
backgroundTxService,
|
|
329
|
+
backgroundTxEthereumService,
|
|
330
|
+
analyticsService,
|
|
331
|
+
recentSendHistoryService,
|
|
332
|
+
txExecutableMQ.subscriber
|
|
333
|
+
);
|
|
334
|
+
|
|
315
335
|
Interaction.init(router, interactionService);
|
|
316
336
|
Permission.init(router, permissionService);
|
|
317
337
|
Chains.init(
|
|
@@ -367,6 +387,7 @@ export function init(
|
|
|
367
387
|
SidePanel.init(router, sidePanelService);
|
|
368
388
|
Settings.init(router, settingsService);
|
|
369
389
|
ManageViewAssetToken.init(router, manageViewAssetTokenService);
|
|
390
|
+
BackgroundTxExecutor.init(router, backgroundTxExecutorService);
|
|
370
391
|
|
|
371
392
|
return {
|
|
372
393
|
initFn: async () => {
|
|
@@ -406,6 +427,8 @@ export function init(
|
|
|
406
427
|
await chainsService.afterInit();
|
|
407
428
|
|
|
408
429
|
await manageViewAssetTokenService.init();
|
|
430
|
+
|
|
431
|
+
await backgroundTxExecutorService.init();
|
|
409
432
|
},
|
|
410
433
|
keyRingService: keyRingV2Service,
|
|
411
434
|
analyticsService: analyticsService,
|
|
@@ -365,6 +365,87 @@ export class KeyRingCosmosService {
|
|
|
365
365
|
);
|
|
366
366
|
}
|
|
367
367
|
|
|
368
|
+
/**
|
|
369
|
+
* Sign a amino-encoded transaction with pre-authorization
|
|
370
|
+
* @dev only sign the transaction, not simulate or broadcast
|
|
371
|
+
*/
|
|
372
|
+
async signAminoPreAuthorized(
|
|
373
|
+
origin: string,
|
|
374
|
+
vaultId: string,
|
|
375
|
+
chainId: string,
|
|
376
|
+
signer: string,
|
|
377
|
+
signDoc: StdSignDoc
|
|
378
|
+
): Promise<AminoSignResponse> {
|
|
379
|
+
const chainInfo = this.chainsService.getChainInfoOrThrow(chainId);
|
|
380
|
+
// if (chainInfo.hideInUI) {
|
|
381
|
+
// throw new Error("Can't sign for hidden chain");
|
|
382
|
+
// }
|
|
383
|
+
const isEthermintLike = KeyRingService.isEthermintLike(chainInfo);
|
|
384
|
+
|
|
385
|
+
const keyInfo = this.keyRingService.getKeyInfo(vaultId);
|
|
386
|
+
if (!keyInfo) {
|
|
387
|
+
throw new Error("Null key info");
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
if (keyInfo.type === "ledger" || keyInfo.type === "keystone") {
|
|
391
|
+
throw new Error(
|
|
392
|
+
"Pre-authorized signing is not supported for hardware wallets"
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
signDoc = {
|
|
397
|
+
...signDoc,
|
|
398
|
+
memo: escapeHTML(signDoc.memo),
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
signDoc = trimAminoSignDoc(signDoc);
|
|
402
|
+
signDoc = sortObjectByKey(signDoc);
|
|
403
|
+
|
|
404
|
+
const key = await this.getKey(vaultId, chainId);
|
|
405
|
+
const bech32Prefix =
|
|
406
|
+
this.chainsService.getChainInfoOrThrow(chainId).bech32Config
|
|
407
|
+
?.bech32PrefixAccAddr ?? "";
|
|
408
|
+
const bech32Address = new Bech32Address(key.address).toBech32(bech32Prefix);
|
|
409
|
+
if (signer !== bech32Address) {
|
|
410
|
+
throw new Error("Signer mismatched");
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
const isADR36SignDoc = checkAndValidateADR36AminoSignDoc(
|
|
414
|
+
signDoc,
|
|
415
|
+
bech32Prefix
|
|
416
|
+
);
|
|
417
|
+
if (isADR36SignDoc) {
|
|
418
|
+
throw new Error("Only transaction signing is supported for now");
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
const signResponse = await this.keyRingService.sign(
|
|
422
|
+
chainId,
|
|
423
|
+
vaultId,
|
|
424
|
+
serializeSignDoc(signDoc),
|
|
425
|
+
isEthermintLike ? "keccak256" : "sha256"
|
|
426
|
+
);
|
|
427
|
+
const signature = new Uint8Array([...signResponse.r, ...signResponse.s]);
|
|
428
|
+
const msgTypes = signDoc.msgs
|
|
429
|
+
.filter((msg) => msg.type)
|
|
430
|
+
.map((msg) => msg.type);
|
|
431
|
+
|
|
432
|
+
try {
|
|
433
|
+
this.trackError(chainInfo, signer, signDoc.sequence, {
|
|
434
|
+
isInternal: true,
|
|
435
|
+
origin,
|
|
436
|
+
signMode: "amino",
|
|
437
|
+
msgTypes,
|
|
438
|
+
});
|
|
439
|
+
} catch (e) {
|
|
440
|
+
console.log(e);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
return {
|
|
444
|
+
signed: signDoc,
|
|
445
|
+
signature: encodeSecp256k1Signature(key.pubKey, signature),
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
|
|
368
449
|
async privilegeSignAminoWithdrawRewards(
|
|
369
450
|
env: Env,
|
|
370
451
|
origin: string,
|
|
@@ -969,6 +1050,76 @@ export class KeyRingCosmosService {
|
|
|
969
1050
|
);
|
|
970
1051
|
}
|
|
971
1052
|
|
|
1053
|
+
/**
|
|
1054
|
+
* Sign a direct-encoded transaction with pre-authorization
|
|
1055
|
+
* @dev only sign the transaction, not simulate or broadcast
|
|
1056
|
+
*/
|
|
1057
|
+
async signDirectPreAuthorized(
|
|
1058
|
+
origin: string,
|
|
1059
|
+
vaultId: string,
|
|
1060
|
+
chainId: string,
|
|
1061
|
+
signer: string,
|
|
1062
|
+
signDoc: SignDoc
|
|
1063
|
+
): Promise<DirectSignResponse> {
|
|
1064
|
+
const chainInfo = this.chainsService.getChainInfoOrThrow(chainId);
|
|
1065
|
+
const isEthermintLike = KeyRingService.isEthermintLike(chainInfo);
|
|
1066
|
+
|
|
1067
|
+
const keyInfo = this.keyRingService.getKeyInfo(vaultId);
|
|
1068
|
+
if (!keyInfo) {
|
|
1069
|
+
throw new Error("Null key info");
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
if (keyInfo.type === "ledger" || keyInfo.type === "keystone") {
|
|
1073
|
+
throw new Error(
|
|
1074
|
+
"Pre-authorized signing is not supported for hardware wallets"
|
|
1075
|
+
);
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
const key = await this.getKey(vaultId, chainId);
|
|
1079
|
+
const bech32Prefix =
|
|
1080
|
+
this.chainsService.getChainInfoOrThrow(chainId).bech32Config
|
|
1081
|
+
?.bech32PrefixAccAddr ?? "";
|
|
1082
|
+
const bech32Address = new Bech32Address(key.address).toBech32(bech32Prefix);
|
|
1083
|
+
if (signer !== bech32Address) {
|
|
1084
|
+
throw new Error("Signer mismatched");
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
const signDocBytes = SignDoc.encode(signDoc).finish();
|
|
1088
|
+
const _sig = await this.keyRingService.sign(
|
|
1089
|
+
chainId,
|
|
1090
|
+
vaultId,
|
|
1091
|
+
signDocBytes,
|
|
1092
|
+
isEthermintLike ? "keccak256" : "sha256"
|
|
1093
|
+
);
|
|
1094
|
+
const signature = new Uint8Array([..._sig.r, ..._sig.s]);
|
|
1095
|
+
|
|
1096
|
+
const msgTypes = TxBody.decode(signDoc.bodyBytes).messages.map(
|
|
1097
|
+
(msg) => msg.typeUrl
|
|
1098
|
+
);
|
|
1099
|
+
|
|
1100
|
+
try {
|
|
1101
|
+
const authInfo = AuthInfo.decode(signDoc.authInfoBytes);
|
|
1102
|
+
if (authInfo.signerInfos.length === 1) {
|
|
1103
|
+
this.trackError(chainInfo, signer, authInfo.signerInfos[0].sequence, {
|
|
1104
|
+
isInternal: true,
|
|
1105
|
+
origin,
|
|
1106
|
+
signMode: "direct",
|
|
1107
|
+
msgTypes,
|
|
1108
|
+
});
|
|
1109
|
+
}
|
|
1110
|
+
} catch (e) {
|
|
1111
|
+
console.log(e);
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
return {
|
|
1115
|
+
signed: {
|
|
1116
|
+
...signDoc,
|
|
1117
|
+
accountNumber: Long.fromString(signDoc.accountNumber),
|
|
1118
|
+
},
|
|
1119
|
+
signature: encodeSecp256k1Signature(key.pubKey, signature),
|
|
1120
|
+
};
|
|
1121
|
+
}
|
|
1122
|
+
|
|
972
1123
|
async signDirectAux(
|
|
973
1124
|
env: Env,
|
|
974
1125
|
origin: string,
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
ChainInfo,
|
|
12
12
|
EthereumSignResponse,
|
|
13
13
|
EthSignType,
|
|
14
|
+
JsonRpcResponse,
|
|
14
15
|
} from "@keplr-wallet/types";
|
|
15
16
|
import { Bech32Address } from "@keplr-wallet/cosmos";
|
|
16
17
|
import { Buffer } from "buffer/";
|
|
@@ -113,6 +114,17 @@ export class KeyRingEthereumService {
|
|
|
113
114
|
if (unsignedTx.authorizationList) {
|
|
114
115
|
throw new Error("EIP-7702 transactions are not supported.");
|
|
115
116
|
}
|
|
117
|
+
|
|
118
|
+
const hasRequiredErc20Approvals =
|
|
119
|
+
unsignedTx.requiredErc20Approvals &&
|
|
120
|
+
Array.isArray(unsignedTx.requiredErc20Approvals) &&
|
|
121
|
+
unsignedTx.requiredErc20Approvals.length > 0;
|
|
122
|
+
|
|
123
|
+
if (!env.isInternalMsg && hasRequiredErc20Approvals) {
|
|
124
|
+
throw new Error(
|
|
125
|
+
"Required ERC20 approvals are not supported for external messages"
|
|
126
|
+
);
|
|
127
|
+
}
|
|
116
128
|
}
|
|
117
129
|
|
|
118
130
|
try {
|
|
@@ -318,6 +330,96 @@ export class KeyRingEthereumService {
|
|
|
318
330
|
);
|
|
319
331
|
}
|
|
320
332
|
|
|
333
|
+
/**
|
|
334
|
+
* Sign an Ethereum transaction with pre-authorization
|
|
335
|
+
* @dev only sign the transaction, not simulate or broadcast
|
|
336
|
+
*/
|
|
337
|
+
async signEthereumPreAuthorized(
|
|
338
|
+
vaultId: string,
|
|
339
|
+
chainId: string,
|
|
340
|
+
signer: string,
|
|
341
|
+
message: Uint8Array,
|
|
342
|
+
signType: EthSignType
|
|
343
|
+
): Promise<EthereumSignResponse> {
|
|
344
|
+
const chainInfo = this.chainsService.getChainInfoOrThrow(chainId);
|
|
345
|
+
// if (chainInfo.hideInUI) {
|
|
346
|
+
// throw new Error("Can't sign for hidden chain");
|
|
347
|
+
// }
|
|
348
|
+
const isEthermintLike = KeyRingService.isEthermintLike(chainInfo);
|
|
349
|
+
const evmInfo = ChainsService.getEVMInfo(chainInfo);
|
|
350
|
+
|
|
351
|
+
if (!isEthermintLike && !evmInfo) {
|
|
352
|
+
throw new Error("Not ethermint like and EVM chain");
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const keyInfo = this.keyRingService.getKeyInfo(vaultId);
|
|
356
|
+
if (!keyInfo) {
|
|
357
|
+
throw new Error("Null key info");
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
if (keyInfo.type === "ledger" || keyInfo.type === "keystone") {
|
|
361
|
+
throw new Error(
|
|
362
|
+
"Pre-authorized signing is not supported for hardware wallets"
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
if (signType === EthSignType.TRANSACTION) {
|
|
367
|
+
const unsignedTx = JSON.parse(Buffer.from(message).toString());
|
|
368
|
+
if (unsignedTx.authorizationList) {
|
|
369
|
+
throw new Error("EIP-7702 transactions are not supported.");
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
try {
|
|
374
|
+
Bech32Address.validate(signer);
|
|
375
|
+
} catch {
|
|
376
|
+
// Ignore mixed-case checksum
|
|
377
|
+
signer = (
|
|
378
|
+
signer.substring(0, 2) === "0x" ? signer : "0x" + signer
|
|
379
|
+
).toLowerCase();
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
const key = await this.keyRingCosmosService.getKey(vaultId, chainId);
|
|
383
|
+
if (
|
|
384
|
+
signer !== key.bech32Address &&
|
|
385
|
+
signer !== key.ethereumHexAddress.toLowerCase()
|
|
386
|
+
) {
|
|
387
|
+
throw new Error("Signer mismatched");
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
if (signType !== EthSignType.TRANSACTION) {
|
|
391
|
+
throw new Error(
|
|
392
|
+
"Pre-authorized signing is only supported for transaction for now"
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
const unsignedTx: UnsignedTransaction = JSON.parse(
|
|
397
|
+
Buffer.from(message).toString()
|
|
398
|
+
);
|
|
399
|
+
const isEIP1559 =
|
|
400
|
+
!!unsignedTx.maxFeePerGas || !!unsignedTx.maxPriorityFeePerGas;
|
|
401
|
+
if (isEIP1559) {
|
|
402
|
+
unsignedTx.type = TransactionTypes.eip1559;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
const signature = await this.keyRingService.sign(
|
|
406
|
+
chainId,
|
|
407
|
+
vaultId,
|
|
408
|
+
Buffer.from(serialize(unsignedTx).replace("0x", ""), "hex"),
|
|
409
|
+
"keccak256"
|
|
410
|
+
);
|
|
411
|
+
|
|
412
|
+
return {
|
|
413
|
+
signingData: Buffer.from(JSON.stringify(unsignedTx), "utf8"),
|
|
414
|
+
signature: Buffer.concat([
|
|
415
|
+
signature.r,
|
|
416
|
+
signature.s,
|
|
417
|
+
// The metamask doesn't seem to consider the chain id in this case... (maybe bug on metamask?)
|
|
418
|
+
signature.v ? Buffer.from("1c", "hex") : Buffer.from("1b", "hex"),
|
|
419
|
+
]),
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
|
|
321
423
|
async request<T = any>(
|
|
322
424
|
env: Env,
|
|
323
425
|
origin: string,
|
|
@@ -1190,12 +1292,7 @@ export class KeyRingEthereumService {
|
|
|
1190
1292
|
this.chainsService.getEVMInfoOrThrow(currentChainId);
|
|
1191
1293
|
|
|
1192
1294
|
return (
|
|
1193
|
-
await simpleFetch<{
|
|
1194
|
-
jsonrpc: string;
|
|
1195
|
-
id: number;
|
|
1196
|
-
result: any;
|
|
1197
|
-
error?: Error;
|
|
1198
|
-
}>(currentChainEVMInfo.rpc, {
|
|
1295
|
+
await simpleFetch<JsonRpcResponse<any>>(currentChainEVMInfo.rpc, {
|
|
1199
1296
|
method: "POST",
|
|
1200
1297
|
headers: {
|
|
1201
1298
|
"content-type": "application/json",
|