-
Notifications
You must be signed in to change notification settings - Fork 40
/
index.ts
112 lines (96 loc) · 3.64 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import secp256k1 from 'secp256k1';
import { BIP32Interface } from 'bip32';
import { HDSigner, Psbt } from 'bitcoinjs-lib';
import { BitcoinNetwork } from '../interface';
import { PsbtValidator } from '../bitcoin/PsbtValidator';
import { PsbtHelper } from '../bitcoin/PsbtHelper';
import { getNetwork } from './getNetwork';
export class AccountSigner implements HDSigner {
publicKey: Buffer;
fingerprint: Buffer;
private readonly node: BIP32Interface
constructor(accountNode: BIP32Interface, mfp?: Buffer) {
this.node = accountNode;
this.publicKey = this.node.publicKey
this.fingerprint = mfp || this.node.fingerprint
}
derivePath(path: string): HDSigner {
try {
let splitPath = path.split('/');
if (splitPath[0] == 'm') {
splitPath = splitPath.slice(1)
}
if (splitPath.length > 2) {
splitPath = splitPath.slice(-2);
}
const childNode = splitPath.reduce((prevHd, indexStr) => {
let index;
if (indexStr.slice(-1) === `'`) {
index = parseInt(indexStr.slice(0, -1), 10);
return prevHd.deriveHardened(index);
} else {
index = parseInt(indexStr, 10);
return prevHd.derive(index);
}
}, this.node)
return new AccountSigner(childNode, this.fingerprint)
} catch (e) {
throw new Error('invalid path')
}
}
sign(hash: Buffer): Buffer {
return this.node.sign(hash)
}
}
const validator = (pubkey: Buffer, msghash: Buffer, signature: Buffer) => {
return secp256k1.ecdsaVerify(new Uint8Array(signature), new Uint8Array(msghash), new Uint8Array(pubkey))
}
export class BtcTx {
private tx: Psbt;
private network: BitcoinNetwork;
constructor(base64Psbt: string, network: BitcoinNetwork) {
this.tx = Psbt.fromBase64(base64Psbt, { network: getNetwork(network) })
this.network = network;
}
validateTx(accountSigner: AccountSigner) {
const validator = new PsbtValidator(this.tx, this.network);
return validator.validate(accountSigner);
}
extractPsbtJson() {
const psbtHelper = new PsbtHelper(this.tx, this.network);
const changeAddress = psbtHelper.changeAddresses
const unit = this.network === BitcoinNetwork.Main ? 'sats' : 'tsats';
const transaction = {
from: psbtHelper.fromAddresses.join(","),
to: psbtHelper.toAddresses.join(","),
value: `${psbtHelper.sendAmount} ${unit}`,
fee: `${psbtHelper.fee} ${unit}`,
network: `${this.network}net`,
}
if(changeAddress.length > 0){
return {...transaction, changeAddress: changeAddress.join(",")}
}
return transaction
}
extractPsbtJsonString() {
return Object.entries(this.extractPsbtJson()).map(([key, value]) => `${key}: ${value}\n`).join('')
}
signTx(accountSigner: AccountSigner) {
try {
this.tx.signAllInputsHD(accountSigner)
if (this.tx.validateSignaturesOfAllInputs(validator)) {
this.tx.finalizeAllInputs();
const txId = this.tx.extractTransaction().getId();
const txHex = this.tx.extractTransaction().toHex();
return {
txId,
txHex
}
} else {
throw new Error('Signature verification failed');
}
} catch (e) {
throw new Error('Sign transaction failed');
}
}
}