@clonegod/ttd-bsc-common 1.0.56 → 1.0.57
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.
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare class SoulPointSignature {
|
|
2
|
+
static generate48SPSignature(privateKey: string, txs: string[]): string;
|
|
3
|
+
static verify48SPSignature(signature: string, txs: string[], expectedSignerAddress: string): boolean;
|
|
4
|
+
static getSignerAddress(signedTx: string): string;
|
|
5
|
+
static testSignatureVerification(): Promise<void>;
|
|
6
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
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.SoulPointSignature = void 0;
|
|
13
|
+
const ethers_1 = require("ethers");
|
|
14
|
+
class SoulPointSignature {
|
|
15
|
+
static generate48SPSignature(privateKey, txs) {
|
|
16
|
+
try {
|
|
17
|
+
const wallet = new ethers_1.ethers.Wallet(privateKey);
|
|
18
|
+
const txHashes = txs.map(tx => {
|
|
19
|
+
if (tx.startsWith('0x')) {
|
|
20
|
+
return ethers_1.ethers.utils.keccak256(tx);
|
|
21
|
+
}
|
|
22
|
+
return tx;
|
|
23
|
+
});
|
|
24
|
+
const concatenatedHashes = ethers_1.ethers.utils.concat(txHashes);
|
|
25
|
+
const messageHash = ethers_1.ethers.utils.keccak256(concatenatedHashes);
|
|
26
|
+
const signature = wallet._signingKey().signDigest(messageHash);
|
|
27
|
+
return signature.compact;
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
throw new Error(`生成48 SoulPoint签名失败: ${error}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
static verify48SPSignature(signature, txs, expectedSignerAddress) {
|
|
34
|
+
try {
|
|
35
|
+
const txHashes = txs.map(tx => {
|
|
36
|
+
if (tx.startsWith('0x')) {
|
|
37
|
+
return ethers_1.ethers.utils.keccak256(tx);
|
|
38
|
+
}
|
|
39
|
+
return tx;
|
|
40
|
+
});
|
|
41
|
+
const concatenatedHashes = ethers_1.ethers.utils.concat(txHashes);
|
|
42
|
+
const messageHash = ethers_1.ethers.utils.keccak256(concatenatedHashes);
|
|
43
|
+
const recoveredAddress = ethers_1.ethers.utils.recoverAddress(messageHash, signature);
|
|
44
|
+
return recoveredAddress.toLowerCase() === expectedSignerAddress.toLowerCase();
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
console.error('验证48 SoulPoint签名失败:', error);
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
static getSignerAddress(signedTx) {
|
|
52
|
+
try {
|
|
53
|
+
const tx = ethers_1.ethers.utils.parseTransaction(signedTx);
|
|
54
|
+
if (!tx.from) {
|
|
55
|
+
throw new Error('无法从交易中提取签名者地址');
|
|
56
|
+
}
|
|
57
|
+
return tx.from;
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
throw new Error(`提取签名者地址失败: ${error}`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
static testSignatureVerification() {
|
|
64
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
65
|
+
console.log('=== 48 SoulPoint 签名测试 ===');
|
|
66
|
+
const tx1_raw = '0xf8ad82e69c85012a05f200830329189455d398326f99059ff775485246999027b319795580b844a9059cbb0000000000000000000000006df68f71f19081751850160118fc755bfeb036120000000000000000000000000000000000000000000000056bc75e2d631000008193a0ade014e655a2d1645efb5f37a142c2369ab96d2d962861dd353fa33fe15b76cda041b291fb742d07c0d475cb2611e9dd6010bbd052c79f99e73c7f9e3653afb6f7';
|
|
67
|
+
const tx2_raw = '0xf8ec8307f58584b8c6fe72830927c094bddbcbaa9cf9603b7055aad963506ede71692f1280b8830000000300000000000000000000000000000000000000000000000087250559b9145f9000000000000000000000000000000000000000000000011f35cc4934f0b52a80bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c55d398326f99059ff775485246999027b319795500006400000000000000000af935e000000000000000008193a0f63bed83d054bdecfb6852f980ed62f35922fe5b68296b603b290a3eff41fabea04b73872c7e68f541bc97eba52abf956eac4a0e198b762e1f9a872336f6748eed';
|
|
68
|
+
const signer_address = '0xF3A611849696ec5801745496be3f7C8124C344c3';
|
|
69
|
+
const signer_privateKey = '48acf19375e8a27309fe5394728abc2eb6d5a0a4feb6b6c53207ca1c256a6739';
|
|
70
|
+
const sign_result = '0x83e4b3a6af20e58315554b5bc38a8398cfca44a75d42973a4454378b0dc9cae63c229b52341d1ddfc4e3ad4360e518c1f1363e2d21fcba507e8e2e10e266edd201';
|
|
71
|
+
try {
|
|
72
|
+
const tx1Hash = ethers_1.ethers.utils.keccak256(tx1_raw);
|
|
73
|
+
const tx2Hash = ethers_1.ethers.utils.keccak256(tx2_raw);
|
|
74
|
+
console.log('tx1 hash:', tx1Hash);
|
|
75
|
+
console.log('tx2 hash:', tx2Hash);
|
|
76
|
+
const signature = yield this.generate48SPSignature(signer_privateKey, [tx1_raw, tx2_raw]);
|
|
77
|
+
console.log('生成的签名:', signature);
|
|
78
|
+
console.log('期望的签名:', sign_result);
|
|
79
|
+
const isValid = this.verify48SPSignature(signature, [tx1_raw, tx2_raw], signer_address);
|
|
80
|
+
console.log('签名验证结果:', isValid);
|
|
81
|
+
const isExpectedValid = this.verify48SPSignature(sign_result, [tx1_raw, tx2_raw], signer_address);
|
|
82
|
+
console.log('期望签名验证结果:', isExpectedValid);
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
console.error('测试失败:', error);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
exports.SoulPointSignature = SoulPointSignature;
|
|
91
|
+
if (require.main === module) {
|
|
92
|
+
SoulPointSignature.testSignatureVerification().catch(console.error);
|
|
93
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clonegod/ttd-bsc-common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.57",
|
|
4
4
|
"description": "BSC common library",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"push": "npm run build && npm publish"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@clonegod/ttd-core": "2.0.
|
|
17
|
+
"@clonegod/ttd-core": "2.0.86",
|
|
18
18
|
"axios": "^1.12.0",
|
|
19
19
|
"dotenv": "^16.4.7",
|
|
20
20
|
"ethers": "^5.8.0"
|