move parseMultisigScript to bitcoin.util.ts

This commit is contained in:
Antoni Spaanderman 2022-07-24 18:44:27 +02:00
parent 777a1bb4c1
commit 37fd1fb76d
No known key found for this signature in database
GPG Key ID: AE0B68E552E5DF8C
2 changed files with 40 additions and 39 deletions

View File

@ -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;

View File

@ -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 };
}