@clonegod/ttd-bsc-common 3.0.33 → 3.0.34
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/trade/caller_manager.js +18 -15
- package/dist/utils/fast_signer.js +59 -25
- package/package.json +1 -1
|
@@ -27,7 +27,6 @@ class CallerManager {
|
|
|
27
27
|
return __awaiter(this, void 0, void 0, function* () {
|
|
28
28
|
const walletInfos = (0, dist_1.load_wallet_multi)(this.config.callerGroupIds, false);
|
|
29
29
|
const allWallets = walletInfos.map(info => new ethers_1.ethers.Wallet(info.private_key, this.config.provider));
|
|
30
|
-
logger.info(`CallerManager: loaded ${allWallets.length} wallets for group ${this.config.groupId}`, allWallets.map(w => w.address));
|
|
31
30
|
const vaultCallersKey = `${this.config.chainName}:${VAULT_CALLERS_KEY}`;
|
|
32
31
|
const rawJson = yield this.redis.hgetvalue(vaultCallersKey, this.config.groupId);
|
|
33
32
|
let allowedAddresses = [];
|
|
@@ -39,14 +38,11 @@ class CallerManager {
|
|
|
39
38
|
logger.error(`CallerManager: failed to parse vault whitelist for group ${this.config.groupId}`, err);
|
|
40
39
|
}
|
|
41
40
|
}
|
|
41
|
+
let skipped = [];
|
|
42
42
|
if (allowedAddresses.length > 0) {
|
|
43
43
|
const allowedSet = new Set(allowedAddresses.map(a => a.toLowerCase()));
|
|
44
44
|
this.callers = allWallets.filter(w => allowedSet.has(w.address.toLowerCase()));
|
|
45
|
-
|
|
46
|
-
const skipped = allWallets.filter(w => !allowedSet.has(w.address.toLowerCase()));
|
|
47
|
-
if (skipped.length > 0) {
|
|
48
|
-
logger.warn(`CallerManager: skipped ${skipped.length} wallets not in Vault whitelist`, skipped.map(w => w.address));
|
|
49
|
-
}
|
|
45
|
+
skipped = allWallets.filter(w => !allowedSet.has(w.address.toLowerCase()));
|
|
50
46
|
}
|
|
51
47
|
else {
|
|
52
48
|
this.callers = allWallets;
|
|
@@ -55,18 +51,25 @@ class CallerManager {
|
|
|
55
51
|
if (this.callers.length === 0) {
|
|
56
52
|
throw new Error('CallerManager: no valid callers after whitelist matching');
|
|
57
53
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
54
|
+
const nonceKey = this.getNonceRedisKey();
|
|
55
|
+
const callerAddresses = this.callers.map(c => c.address.toLowerCase());
|
|
56
|
+
const nonces = yield this.redis.hmget(nonceKey, callerAddresses);
|
|
57
|
+
const callerSummary = {};
|
|
58
|
+
let missingNonceCount = 0;
|
|
59
|
+
this.callers.forEach((caller, i) => {
|
|
60
|
+
const nonce = nonces[i];
|
|
61
|
+
if (nonce === null || nonce === undefined) {
|
|
62
|
+
callerSummary[caller.address] = 'MISSING';
|
|
63
|
+
missingNonceCount++;
|
|
64
64
|
}
|
|
65
65
|
else {
|
|
66
|
-
|
|
66
|
+
callerSummary[caller.address] = `nonce=${nonce}`;
|
|
67
67
|
}
|
|
68
|
-
})
|
|
69
|
-
|
|
68
|
+
});
|
|
69
|
+
if (missingNonceCount > 0) {
|
|
70
|
+
logger.warn(`CallerManager: ${missingNonceCount} callers nonce not found in Redis, stream-trade may not be running`);
|
|
71
|
+
}
|
|
72
|
+
logger.info(`CallerManager initialized for ${this.config.groupId}: loaded=${allWallets.length}, active=${this.callers.length}, skipped=${skipped.length}`, callerSummary);
|
|
70
73
|
});
|
|
71
74
|
}
|
|
72
75
|
acquireCaller() {
|
|
@@ -1,34 +1,38 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.patchEthersV5Signer = patchEthersV5Signer;
|
|
4
|
-
let
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
return true;
|
|
8
|
-
let secp;
|
|
4
|
+
let patchedV5 = false;
|
|
5
|
+
let patchedV6 = false;
|
|
6
|
+
function tryRequire(moduleName) {
|
|
9
7
|
try {
|
|
10
|
-
|
|
8
|
+
return require(moduleName);
|
|
11
9
|
}
|
|
12
|
-
catch (
|
|
13
|
-
|
|
14
|
-
return false;
|
|
10
|
+
catch (_a) {
|
|
11
|
+
return null;
|
|
15
12
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const signingKeyModule = require('@ethersproject/signing-key');
|
|
22
|
-
const bytesModule = require('@ethersproject/bytes');
|
|
23
|
-
SigningKey = signingKeyModule.SigningKey;
|
|
24
|
-
arrayify = bytesModule.arrayify;
|
|
25
|
-
hexZeroPad = bytesModule.hexZeroPad;
|
|
26
|
-
splitSignature = bytesModule.splitSignature;
|
|
27
|
-
}
|
|
28
|
-
catch (err) {
|
|
29
|
-
console.warn('[fast_signer] @ethersproject/signing-key not found, skipping patch', err);
|
|
13
|
+
}
|
|
14
|
+
function patchEthersV5Signer() {
|
|
15
|
+
const secp = tryRequire('secp256k1');
|
|
16
|
+
if (!secp) {
|
|
17
|
+
console.warn('[fast_signer] secp256k1 native module not found, skipping patch');
|
|
30
18
|
return false;
|
|
31
19
|
}
|
|
20
|
+
if (patchEthersV5WithSecp(secp))
|
|
21
|
+
return true;
|
|
22
|
+
if (patchEthersV6WithSecp(secp))
|
|
23
|
+
return true;
|
|
24
|
+
console.log('[fast_signer] no compatible ethers version found, skipping patch');
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
function patchEthersV5WithSecp(secp) {
|
|
28
|
+
if (patchedV5)
|
|
29
|
+
return true;
|
|
30
|
+
const signingKeyModule = tryRequire('@ethersproject/signing-key');
|
|
31
|
+
const bytesModule = tryRequire('@ethersproject/bytes');
|
|
32
|
+
if (!signingKeyModule || !bytesModule)
|
|
33
|
+
return false;
|
|
34
|
+
const { SigningKey } = signingKeyModule;
|
|
35
|
+
const { arrayify, hexZeroPad, splitSignature } = bytesModule;
|
|
32
36
|
const originalSignDigest = SigningKey.prototype.signDigest;
|
|
33
37
|
SigningKey.prototype.signDigest = function (digest) {
|
|
34
38
|
const privKeyBytes = arrayify(this.privateKey);
|
|
@@ -43,11 +47,41 @@ function patchEthersV5Signer() {
|
|
|
43
47
|
return splitSignature({ recoveryParam: recid, r, s });
|
|
44
48
|
}
|
|
45
49
|
catch (err) {
|
|
46
|
-
console.warn('[fast_signer] native sign failed, fallback
|
|
50
|
+
console.warn('[fast_signer v5] native sign failed, fallback', err);
|
|
47
51
|
return originalSignDigest.call(this, digest);
|
|
48
52
|
}
|
|
49
53
|
};
|
|
50
|
-
|
|
54
|
+
patchedV5 = true;
|
|
51
55
|
console.log('[fast_signer] ethers v5 SigningKey patched with native secp256k1');
|
|
52
56
|
return true;
|
|
53
57
|
}
|
|
58
|
+
function patchEthersV6WithSecp(secp) {
|
|
59
|
+
if (patchedV6)
|
|
60
|
+
return true;
|
|
61
|
+
const ethersV6 = tryRequire('ethers');
|
|
62
|
+
if (!ethersV6 || !ethersV6.SigningKey || !ethersV6.Signature || !ethersV6.getBytes) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
const { SigningKey, Signature, getBytes } = ethersV6;
|
|
66
|
+
const originalSign = SigningKey.prototype.sign;
|
|
67
|
+
SigningKey.prototype.sign = function (digest) {
|
|
68
|
+
const privKeyBytes = getBytes(this.privateKey);
|
|
69
|
+
const digestBytes = getBytes(digest);
|
|
70
|
+
if (digestBytes.length !== 32) {
|
|
71
|
+
return originalSign.call(this, digest);
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
const { signature, recid } = secp.ecdsaSign(digestBytes, privKeyBytes);
|
|
75
|
+
const r = '0x' + Buffer.from(signature.slice(0, 32)).toString('hex').padStart(64, '0');
|
|
76
|
+
const s = '0x' + Buffer.from(signature.slice(32, 64)).toString('hex').padStart(64, '0');
|
|
77
|
+
return Signature.from({ r, s, v: recid ? 0x1c : 0x1b });
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
console.warn('[fast_signer v6] native sign failed, fallback', err);
|
|
81
|
+
return originalSign.call(this, digest);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
patchedV6 = true;
|
|
85
|
+
console.log('[fast_signer] ethers v6 SigningKey patched with native secp256k1');
|
|
86
|
+
return true;
|
|
87
|
+
}
|