did-beta-bch 1.0.0 → 1.0.1
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/verify.js +7 -5
- package/package.json +1 -1
package/dist/verify.js
CHANGED
|
@@ -10,15 +10,16 @@ function bitcoinMsgHash(message) {
|
|
|
10
10
|
const lenByte = new Uint8Array([msgBytes.length]);
|
|
11
11
|
return hash256(new Uint8Array([...prefix, ...lenByte, ...msgBytes]));
|
|
12
12
|
}
|
|
13
|
-
function recoverBchAddress(sigHex, msgHash) {
|
|
13
|
+
function recoverBchAddress(sigHex, msgHash, expectedAddress) {
|
|
14
14
|
const sig = Uint8Array.from(sigHex.match(/.{2}/g).map(b => parseInt(b, 16)));
|
|
15
15
|
const tryRecover = (compact, id) => {
|
|
16
16
|
try {
|
|
17
17
|
const pub = secp256k1.recoverPublicKeyCompressed(compact, id, msgHash);
|
|
18
18
|
if (!(pub instanceof Uint8Array))
|
|
19
19
|
return null;
|
|
20
|
+
const addrPrefix = (expectedAddress?.split(":")[0] ?? "bitcoincash");
|
|
20
21
|
for (const type of ["p2pkhWithTokens", "p2pkh"]) {
|
|
21
|
-
const r = encodeCashAddress({ prefix:
|
|
22
|
+
const r = encodeCashAddress({ prefix: addrPrefix, type, payload: hash160(pub) });
|
|
22
23
|
if (r?.address)
|
|
23
24
|
return r.address;
|
|
24
25
|
if (typeof r === "string")
|
|
@@ -29,7 +30,8 @@ function recoverBchAddress(sigHex, msgHash) {
|
|
|
29
30
|
return null;
|
|
30
31
|
};
|
|
31
32
|
if (sig.length === 65) {
|
|
32
|
-
const
|
|
33
|
+
const recoveryId = (sig[0] & 0x03);
|
|
34
|
+
const addr = tryRecover(sig.slice(1), recoveryId);
|
|
33
35
|
if (addr)
|
|
34
36
|
return addr;
|
|
35
37
|
}
|
|
@@ -97,7 +99,7 @@ export async function verifyIdentity(resolved) {
|
|
|
97
99
|
}
|
|
98
100
|
// 2. BCH signature recovery
|
|
99
101
|
const msgHash = bitcoinMsgHash(message);
|
|
100
|
-
const recovered = recoverBchAddress(sigHex, msgHash);
|
|
102
|
+
const recovered = recoverBchAddress(sigHex, msgHash, bchAddress);
|
|
101
103
|
const bchValid = recovered === bchAddress;
|
|
102
104
|
// 3. Payload integrity
|
|
103
105
|
if (!message.startsWith(`${PROTOCOL_ID}/bch-proof/`)) {
|
|
@@ -116,7 +118,7 @@ export function verifyChallengeProof(proof, nonce) {
|
|
|
116
118
|
if (!proof.payload.startsWith(`${PROTOCOL_ID}/challenge`))
|
|
117
119
|
return false;
|
|
118
120
|
const msgHash = bitcoinMsgHash(proof.payload);
|
|
119
|
-
const recovered = recoverBchAddress(proof.signature, msgHash);
|
|
121
|
+
const recovered = recoverBchAddress(proof.signature, msgHash, proof.address);
|
|
120
122
|
return recovered === proof.address;
|
|
121
123
|
}
|
|
122
124
|
/**
|