From 37fd1fb76dd9a01151a3877afcc7b5c2409e70d4 Mon Sep 17 00:00:00 2001 From: Antoni Spaanderman <56turtle56@gmail.com> Date: Sun, 24 Jul 2022 18:44:27 +0200 Subject: [PATCH] move parseMultisigScript to bitcoin.util.ts --- frontend/src/app/bitcoin.utils.ts | 40 ++++++++++++++++++- .../address-labels.component.ts | 39 +----------------- 2 files changed, 40 insertions(+), 39 deletions(-) diff --git a/frontend/src/app/bitcoin.utils.ts b/frontend/src/app/bitcoin.utils.ts index e5cdde87f..d1f0d6553 100644 --- a/frontend/src/app/bitcoin.utils.ts +++ b/frontend/src/app/bitcoin.utils.ts @@ -1,5 +1,4 @@ import { Transaction, Vin } from './interfaces/electrs.interface'; -import { parseMultisigScript } from './components/address-labels/address-labels.component'; const P2SH_P2WPKH_COST = 21 * 4; // the WU cost for the non-witness part of P2SH-P2WPKH const P2SH_P2WSH_COST = 35 * 4; // the WU cost for the non-witness part of P2SH-P2WSH @@ -114,6 +113,45 @@ export function calcSegwitFeeGains(tx: Transaction) { }; } +/** extracts m and n from a multisig script (asm), returns nothing if it is not a multisig script */ +export function parseMultisigScript(script: string): void | { m: number, n: number } { + if (!script) { + return; + } + const ops = script.split(' '); + if (ops.length < 3 || ops.pop() !== 'OP_CHECKMULTISIG') { + return; + } + const opN = ops.pop(); + if (!opN.startsWith('OP_PUSHNUM_')) { + return; + } + const n = parseInt(opN.match(/[0-9]+/)[0], 10); + if (ops.length < n * 2 + 1) { + return; + } + // pop n public keys + for (let i = 0; i < n; i++) { + if (!/^0((2|3)\w{64}|4\w{128})$/.test(ops.pop())) { + return; + } + if (!/^OP_PUSHBYTES_(33|65)$/.test(ops.pop())) { + return; + } + } + const opM = ops.pop(); + if (!opM.startsWith('OP_PUSHNUM_')) { + return; + } + const m = parseInt(opM.match(/[0-9]+/)[0], 10); + + if (ops.length) { + return; + } + + return { m, n }; +} + // https://github.com/shesek/move-decimal-point export function moveDec(num: number, n: number) { let frac, int, neg, ref; 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 6dc10f6e9..331114ff4 100644 --- a/frontend/src/app/components/address-labels/address-labels.component.ts +++ b/frontend/src/app/components/address-labels/address-labels.component.ts @@ -1,6 +1,7 @@ import { Component, ChangeDetectionStrategy, Input, OnChanges } from '@angular/core'; import { Vin, Vout } from '../../interfaces/electrs.interface'; import { StateService } from 'src/app/services/state.service'; +import { parseMultisigScript } from 'src/app/bitcoin.utils'; @Component({ selector: 'app-address-labels', @@ -109,41 +110,3 @@ export class AddressLabelsComponent implements OnChanges { this.detectMultisig(this.vout.scriptpubkey_asm); } } - -export function parseMultisigScript(script: string): void | { m: number, n: number } { - if (!script) { - return; - } - const ops = script.split(' '); - if (ops.length < 3 || ops.pop() !== 'OP_CHECKMULTISIG') { - return; - } - const opN = ops.pop(); - if (!opN.startsWith('OP_PUSHNUM_')) { - return; - } - const n = parseInt(opN.match(/[0-9]+/)[0], 10); - if (ops.length < n * 2 + 1) { - return; - } - // pop n public keys - for (let i = 0; i < n; i++) { - if (!/^0((2|3)\w{64}|4\w{128})$/.test(ops.pop())) { - return; - } - if (!/^OP_PUSHBYTES_(33|65)$/.test(ops.pop())) { - return; - } - } - const opM = ops.pop(); - if (!opM.startsWith('OP_PUSHNUM_')) { - return; - } - const m = parseInt(opM.match(/[0-9]+/)[0], 10); - - if (ops.length) { - return; - } - - return { m, n }; -}