From 1f6504898a6ceee4da59579444d09f2ad7fa40d9 Mon Sep 17 00:00:00 2001 From: Antoni Spaanderman <56turtle56@gmail.com> Date: Sat, 5 Feb 2022 16:50:10 +0100 Subject: [PATCH 1/5] detect lightning htlc and unilateral close --- .../address-labels.component.ts | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/frontend/src/app/components/address-labels/address-labels.component.ts b/frontend/src/app/components/address-labels/address-labels.component.ts index bde749025..958b8cd81 100644 --- a/frontend/src/app/components/address-labels/address-labels.component.ts +++ b/frontend/src/app/components/address-labels/address-labels.component.ts @@ -46,15 +46,21 @@ export class AddressLabelsComponent implements OnInit { return; } - [ - // {regexp: /^OP_DUP OP_HASH160/, label: 'HTLC'}, - {regexp: /^OP_IF OP_PUSHBYTES_33 \w{33} OP_ELSE OP_PUSHBYTES_2 \w{2} OP_CSV OP_DROP/, label: 'Force Close'} - ].forEach((item) => { - if (item.regexp.test(this.vin.inner_witnessscript_asm)) { - this.lightning = item.label; - } - } - ); + // https://github.com/lightning/bolts/blob/master/03-transactions.md#commitment-transaction-outputs + if (/^OP_IF OP_PUSHBYTES_33 \w{66} OP_ELSE OP_PUSHBYTES_(1 \w{2}|2 \w{4}) OP_CSV OP_DROP OP_PUSHBYTES_33 \w{66} OP_ENDIF OP_CHECKSIG$/.test(this.vin.inner_witnessscript_asm)) { + if (this.vin.witness[this.vin.witness.length - 2] == '01') { + this.lightning = 'Revoked Force Close'; + } else { + this.lightning = 'Force Close'; + } + // https://github.com/lightning/bolts/blob/master/03-transactions.md#offered-htlc-outputs + } else if (/^OP_DUP OP_HASH160 OP_PUSHBYTES_20 \w{40} OP_EQUAL OP_IF OP_CHECKSIG OP_ELSE OP_PUSHBYTES_33 \w{66} OP_SWAP OP_SIZE OP_PUSHBYTES_1 20 OP_EQUAL OP_NOTIF OP_DROP OP_PUSHNUM_2 OP_SWAP OP_PUSHBYTES_33 \w{66} OP_PUSHNUM_2 OP_CHECKMULTISIG OP_ELSE OP_HASH160 OP_PUSHBYTES_20 \w{40} OP_EQUALVERIFY OP_CHECKSIG OP_ENDIF OP_ENDIF$/) { + if (this.vin.witness[this.vin.witness.length - 2].length == 66) { + this.lightning = 'Revoked HTLC'; + } else { + this.lightning = 'HTLC'; + } + } if (this.lightning) { return; From 7565aa7a2592e5b3081bb31ca84e038bd1dfa77e Mon Sep 17 00:00:00 2001 From: Antoni Spaanderman <56turtle56@gmail.com> Date: Sat, 5 Feb 2022 16:58:41 +0100 Subject: [PATCH 2/5] fix indentation + detect htlc with `option_anchors` --- .../address-labels.component.ts | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/frontend/src/app/components/address-labels/address-labels.component.ts b/frontend/src/app/components/address-labels/address-labels.component.ts index 958b8cd81..fb827e65b 100644 --- a/frontend/src/app/components/address-labels/address-labels.component.ts +++ b/frontend/src/app/components/address-labels/address-labels.component.ts @@ -48,19 +48,19 @@ export class AddressLabelsComponent implements OnInit { // https://github.com/lightning/bolts/blob/master/03-transactions.md#commitment-transaction-outputs if (/^OP_IF OP_PUSHBYTES_33 \w{66} OP_ELSE OP_PUSHBYTES_(1 \w{2}|2 \w{4}) OP_CSV OP_DROP OP_PUSHBYTES_33 \w{66} OP_ENDIF OP_CHECKSIG$/.test(this.vin.inner_witnessscript_asm)) { - if (this.vin.witness[this.vin.witness.length - 2] == '01') { - this.lightning = 'Revoked Force Close'; - } else { - this.lightning = 'Force Close'; - } + if (this.vin.witness[this.vin.witness.length - 2] == '01') { + this.lightning = 'Revoked Force Close'; + } else { + this.lightning = 'Force Close'; + } // https://github.com/lightning/bolts/blob/master/03-transactions.md#offered-htlc-outputs - } else if (/^OP_DUP OP_HASH160 OP_PUSHBYTES_20 \w{40} OP_EQUAL OP_IF OP_CHECKSIG OP_ELSE OP_PUSHBYTES_33 \w{66} OP_SWAP OP_SIZE OP_PUSHBYTES_1 20 OP_EQUAL OP_NOTIF OP_DROP OP_PUSHNUM_2 OP_SWAP OP_PUSHBYTES_33 \w{66} OP_PUSHNUM_2 OP_CHECKMULTISIG OP_ELSE OP_HASH160 OP_PUSHBYTES_20 \w{40} OP_EQUALVERIFY OP_CHECKSIG OP_ENDIF OP_ENDIF$/) { - if (this.vin.witness[this.vin.witness.length - 2].length == 66) { - this.lightning = 'Revoked HTLC'; - } else { - this.lightning = 'HTLC'; - } - } + } else if (/^OP_DUP OP_HASH160 OP_PUSHBYTES_20 \w{40} OP_EQUAL OP_IF OP_CHECKSIG OP_ELSE OP_PUSHBYTES_33 \w{66} OP_SWAP OP_SIZE OP_PUSHBYTES_1 20 OP_EQUAL OP_NOTIF OP_DROP OP_PUSHNUM_2 OP_SWAP OP_PUSHBYTES_33 \w{66} OP_PUSHNUM_2 OP_CHECKMULTISIG OP_ELSE OP_HASH160 OP_PUSHBYTES_20 \w{40} OP_EQUALVERIFY OP_CHECKSIG OP_ENDIF (OP_PUSHNUM_1 OP_CHECKSEQUENCEVERIFY OP_DROP |)OP_ENDIF$/) { + if (this.vin.witness[this.vin.witness.length - 2].length == 66) { + this.lightning = 'Revoked HTLC'; + } else { + this.lightning = 'HTLC'; + } + } if (this.lightning) { return; From 148e340ea6e2c463cff674231d1ec9811647fcf4 Mon Sep 17 00:00:00 2001 From: Antoni Spaanderman <56turtle56@gmail.com> Date: Sat, 5 Feb 2022 17:26:50 +0100 Subject: [PATCH 3/5] actually test htlc, fix indentation (again) and detect multisig --- .../address-labels.component.ts | 69 ++++++++++--------- 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/frontend/src/app/components/address-labels/address-labels.component.ts b/frontend/src/app/components/address-labels/address-labels.component.ts index fb827e65b..40db539b1 100644 --- a/frontend/src/app/components/address-labels/address-labels.component.ts +++ b/frontend/src/app/components/address-labels/address-labels.component.ts @@ -54,51 +54,54 @@ export class AddressLabelsComponent implements OnInit { this.lightning = 'Force Close'; } // https://github.com/lightning/bolts/blob/master/03-transactions.md#offered-htlc-outputs - } else if (/^OP_DUP OP_HASH160 OP_PUSHBYTES_20 \w{40} OP_EQUAL OP_IF OP_CHECKSIG OP_ELSE OP_PUSHBYTES_33 \w{66} OP_SWAP OP_SIZE OP_PUSHBYTES_1 20 OP_EQUAL OP_NOTIF OP_DROP OP_PUSHNUM_2 OP_SWAP OP_PUSHBYTES_33 \w{66} OP_PUSHNUM_2 OP_CHECKMULTISIG OP_ELSE OP_HASH160 OP_PUSHBYTES_20 \w{40} OP_EQUALVERIFY OP_CHECKSIG OP_ENDIF (OP_PUSHNUM_1 OP_CHECKSEQUENCEVERIFY OP_DROP |)OP_ENDIF$/) { - if (this.vin.witness[this.vin.witness.length - 2].length == 66) { - this.lightning = 'Revoked HTLC'; - } else { - this.lightning = 'HTLC'; - } + } else if (/^OP_DUP OP_HASH160 OP_PUSHBYTES_20 \w{40} OP_EQUAL OP_IF OP_CHECKSIG OP_ELSE OP_PUSHBYTES_33 \w{66} OP_SWAP OP_SIZE OP_PUSHBYTES_1 20 OP_EQUAL OP_NOTIF OP_DROP OP_PUSHNUM_2 OP_SWAP OP_PUSHBYTES_33 \w{66} OP_PUSHNUM_2 OP_CHECKMULTISIG OP_ELSE OP_HASH160 OP_PUSHBYTES_20 \w{40} OP_EQUALVERIFY OP_CHECKSIG OP_ENDIF (OP_PUSHNUM_1 OP_CHECKSEQUENCEVERIFY OP_DROP |)OP_ENDIF$/.test(this.vin.inner_witnessscript_asm)) { + if (this.vin.witness[this.vin.witness.length - 2].length == 66) { + this.lightning = 'Revoked HTLC'; + } else { + this.lightning = 'HTLC'; + } } if (this.lightning) { return; } - if (this.vin.inner_witnessscript_asm.indexOf('OP_CHECKMULTISIG') > -1) { - const matches = this.getMatches(this.vin.inner_witnessscript_asm, /OP_PUSHNUM_([0-9])/g, 1); - this.multisig = true; - this.multisigM = parseInt(matches[0], 10); - this.multisigN = parseInt(matches[1], 10); + this.detectMultisig(this.vin.inner_witnessscript_asm); + } - if (this.multisigM === 1 && this.multisigN === 1) { - this.multisig = false; - } + this.detectMultisig(this.vin.inner_redeemscript_asm); + } + + detectMultisig(script: string) { + const ops = script.split(' '); + if (ops.pop() != 'OP_CHECKMULTISIG') { + return; + } + const opN = ops.pop(); + if (!opN.startsWith('OP_PUSHNUM_')) { + return; + } + const n = parseInt(opN.match(/[0-9]+/)[0]); + // pop n public keys + for (var i = 0; i < n; i++) { + if (ops.pop().length != 66) { + return; + } + if (ops.pop() != 'OP_PUSHBYTES_33') { + return; } } - - if (this.vin.inner_redeemscript_asm && this.vin.inner_redeemscript_asm.indexOf('OP_CHECKMULTISIG') > -1) { - const matches = this.getMatches(this.vin.inner_redeemscript_asm, /OP_PUSHNUM_([0-9])/g, 1); - this.multisig = true; - this.multisigM = matches[0]; - this.multisigN = matches[1]; + const opM = ops.pop(); + if (!opM.startsWith('OP_PUSHNUM_')) { + return; } + const m = parseInt(opN.match(/[0-9]+/)[0]); + + this.multisig = true; + this.multisigM = m; + this.multisigN = n; } handleVout() { } - - getMatches(str: string, regex: RegExp, index: number) { - if (!index) { - index = 1; - } - const matches = []; - let match; - while (match = regex.exec(str)) { - matches.push(match[index]); - } - return matches; - } - } From bb8bfa0e3ad14d797aa1ac8600e7b6874f0caca9 Mon Sep 17 00:00:00 2001 From: Antoni Spaanderman <56turtle56@gmail.com> Date: Sat, 5 Feb 2022 17:29:42 +0100 Subject: [PATCH 4/5] copy paste moment --- .../app/components/address-labels/address-labels.component.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/src/app/components/address-labels/address-labels.component.ts b/frontend/src/app/components/address-labels/address-labels.component.ts index 40db539b1..3ec2d079b 100644 --- a/frontend/src/app/components/address-labels/address-labels.component.ts +++ b/frontend/src/app/components/address-labels/address-labels.component.ts @@ -95,7 +95,7 @@ export class AddressLabelsComponent implements OnInit { if (!opM.startsWith('OP_PUSHNUM_')) { return; } - const m = parseInt(opN.match(/[0-9]+/)[0]); + const m = parseInt(opM.match(/[0-9]+/)[0]); this.multisig = true; this.multisigM = m; From 9f3a3bd4d7c0215ae55967358754e5e1c0b5db5f Mon Sep 17 00:00:00 2001 From: Antoni Spaanderman <56turtle56@gmail.com> Date: Sun, 6 Feb 2022 12:41:37 +0100 Subject: [PATCH 5/5] also detect uncompressed pubkeys + fix errors --- .../address-labels/address-labels.component.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/frontend/src/app/components/address-labels/address-labels.component.ts b/frontend/src/app/components/address-labels/address-labels.component.ts index 3ec2d079b..75bbe7cba 100644 --- a/frontend/src/app/components/address-labels/address-labels.component.ts +++ b/frontend/src/app/components/address-labels/address-labels.component.ts @@ -73,8 +73,11 @@ export class AddressLabelsComponent implements OnInit { } detectMultisig(script: string) { + if (!script) { + return; + } const ops = script.split(' '); - if (ops.pop() != 'OP_CHECKMULTISIG') { + if (ops.length < 3 || ops.pop() != 'OP_CHECKMULTISIG') { return; } const opN = ops.pop(); @@ -82,12 +85,15 @@ export class AddressLabelsComponent implements OnInit { return; } const n = parseInt(opN.match(/[0-9]+/)[0]); + if (ops.length < n * 2 + 1) { + return; + } // pop n public keys for (var i = 0; i < n; i++) { - if (ops.pop().length != 66) { + if (!/^0((2|3)\w{64}|4\w{128})$/.test(ops.pop())) { return; } - if (ops.pop() != 'OP_PUSHBYTES_33') { + if (!/^OP_PUSHBYTES_(33|65)$/.test(ops.pop())) { return; } }