Skip to content

Commit 716956d

Browse files
committed
[Cashtab] [xecjs-message upgrade p1/3] Deprecate segwit support from xecjs-message
Summary: T2784 Remove segwit support from the xecjs-message library. Test the async signing function in Cashtab to verify that segwit removal did not break any modified functions. Publish this version to npm as a tagged dev release so that unit tests in Cashtab are not broken by local dependency. Test Plan: npm start Sign a message Confirm dev log displays 'Both methods not broken' Verify a message Confirm verification Reviewers: #bitcoin_abc, emack Reviewed By: #bitcoin_abc, emack Differential Revision: https://reviews.bitcoinabc.org/D12645
1 parent b17d944 commit 716956d

File tree

6 files changed

+43
-64
lines changed

6 files changed

+43
-64
lines changed

web/cashtab/package-lock.json

Lines changed: 10 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/cashtab/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
"webpack-manifest-plugin": "^4.0.2",
9393
"wif": "^2.0.6",
9494
"workbox-webpack-plugin": "^6.4.1",
95-
"xecjs-message": "^1.0.5"
95+
"xecjs-message": "^1.0.5-b"
9696
},
9797
"scripts": {
9898
"start": "node scripts/start.js",

web/cashtab/src/components/SignVerifyMsg/SignVerifyMsg.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ const SignVerifyMsg = () => {
9898
useState(false);
9999
const [messageVerificationSigError, setMessageVerificationSigError] =
100100
useState(false);
101-
const signMessageByPk = () => {
101+
const signMessageByPk = async () => {
102102
try {
103103
// First, get required params
104104
const keyPair = getECPairFromWIF(wallet.Path1899.fundingWif);
@@ -109,6 +109,22 @@ const SignVerifyMsg = () => {
109109
.sign(msgToSign, privKey, keyPair.compressed)
110110
.toString('base64');
111111

112+
// Also test the async function
113+
let asyncSignature = await (
114+
await xecMessage.signAsync(
115+
msgToSign,
116+
privKey,
117+
keyPair.compressed,
118+
)
119+
).toString('base64');
120+
121+
console.log(`sync sig`, messageSignature);
122+
console.log(`asyncSignature`, asyncSignature);
123+
124+
if (messageSignature === asyncSignature) {
125+
console.log(`Both methods not broken`);
126+
}
127+
112128
setMessageSignature(messageSignature);
113129
messageSignedNotification(messageSignature);
114130
} catch (err) {

web/xecjs-message/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,9 @@
3333
**removed**
3434

3535
- No changes. This is the first version published from the BitcoinABC monorepo.
36+
37+
# 1.0.6
38+
39+
**removed**
40+
41+
- All segwit support

web/xecjs-message/index.js

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ const secp256k1 = require('secp256k1');
44
const varuint = require('varuint-bitcoin');
55
const ecashaddr = require('ecashaddrjs');
66

7-
const SEGWIT_TYPES = {
8-
P2WPKH: 'p2wpkh',
9-
P2SH_P2WPKH: 'p2sh(p2wpkh)',
10-
};
11-
127
function sha256(b) {
138
return createHash('sha256').update(b).digest();
149
}
@@ -19,13 +14,9 @@ function hash160(buffer) {
1914
return createHash('ripemd160').update(sha256(buffer)).digest();
2015
}
2116

22-
function encodeSignature(signature, recovery, compressed, segwitType) {
23-
if (segwitType !== undefined) {
24-
recovery += 8;
25-
if (segwitType === SEGWIT_TYPES.P2WPKH) recovery += 4;
26-
} else {
27-
if (compressed) recovery += 4;
28-
}
17+
function encodeSignature(signature, recovery, compressed) {
18+
if (compressed) recovery += 4;
19+
2920
return Buffer.concat([Buffer.alloc(1, recovery + 27), signature]);
3021
}
3122

@@ -39,11 +30,6 @@ function decodeSignature(buffer) {
3930

4031
return {
4132
compressed: !!(flagByte & 12),
42-
segwitType: !(flagByte & 8)
43-
? null
44-
: !(flagByte & 4)
45-
? SEGWIT_TYPES.P2SH_P2WPKH
46-
: SEGWIT_TYPES.P2WPKH,
4733
recovery: flagByte & 3,
4834
signature: buffer.slice(1),
4935
};
@@ -72,30 +58,10 @@ function prepareSign(messagePrefixArg, sigOptions) {
7258
sigOptions = messagePrefixArg;
7359
messagePrefixArg = undefined;
7460
}
75-
let { segwitType, extraEntropy } = sigOptions || {};
76-
if (
77-
segwitType &&
78-
(typeof segwitType === 'string' || segwitType instanceof String)
79-
) {
80-
segwitType = segwitType.toLowerCase();
81-
}
82-
if (
83-
segwitType &&
84-
segwitType !== SEGWIT_TYPES.P2SH_P2WPKH &&
85-
segwitType !== SEGWIT_TYPES.P2WPKH
86-
) {
87-
throw new Error(
88-
'Unrecognized segwitType: use "' +
89-
SEGWIT_TYPES.P2SH_P2WPKH +
90-
'" or "' +
91-
SEGWIT_TYPES.P2WPKH +
92-
'"',
93-
);
94-
}
61+
let { extraEntropy } = sigOptions || {};
9562

9663
return {
9764
messagePrefixArg,
98-
segwitType,
9965
extraEntropy,
10066
};
10167
}
@@ -105,27 +71,22 @@ function isSigner(obj) {
10571
}
10672

10773
function sign(message, privateKey, compressed, messagePrefix, sigOptions) {
108-
const { messagePrefixArg, segwitType, extraEntropy } = prepareSign(
74+
const { messagePrefixArg, extraEntropy } = prepareSign(
10975
messagePrefix,
11076
sigOptions,
11177
);
11278
const hash = magicHash(message, messagePrefixArg);
11379
const sigObj = isSigner(privateKey)
11480
? privateKey.sign(hash, extraEntropy)
11581
: secp256k1.sign(hash, privateKey, { data: extraEntropy });
116-
return encodeSignature(
117-
sigObj.signature,
118-
sigObj.recovery,
119-
compressed,
120-
segwitType,
121-
);
82+
return encodeSignature(sigObj.signature, sigObj.recovery, compressed);
12283
}
12384

12485
function signAsync(message, privateKey, compressed, messagePrefix, sigOptions) {
125-
let messagePrefixArg, segwitType, extraEntropy;
86+
let messagePrefixArg, extraEntropy;
12687
return Promise.resolve()
12788
.then(() => {
128-
({ messagePrefixArg, segwitType, extraEntropy } = prepareSign(
89+
({ messagePrefixArg, extraEntropy } = prepareSign(
12990
messagePrefix,
13091
sigOptions,
13192
));
@@ -139,7 +100,6 @@ function signAsync(message, privateKey, compressed, messagePrefix, sigOptions) {
139100
sigObj.signature,
140101
sigObj.recovery,
141102
compressed,
142-
segwitType,
143103
);
144104
});
145105
}

web/xecjs-message/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "xecjs-message",
3-
"version": "1.0.5",
3+
"version": "1.0.5b",
44
"description": "xecjs-message",
55
"keywords": [
66
"xecjs-message",

0 commit comments

Comments
 (0)