forked from sanjaybuddapar/motogiga
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.ts
46 lines (37 loc) · 1.01 KB
/
helpers.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
import { Buffer } from "buffer";
import { Transaction } from "bitcoinjs-lib";
import bigDecimal from "js-big-decimal";
const MEMPOOL_BROADCAST_SERVICE = "https://blockstream.info/testnet/api/tx";
export const transactionSubmitter = async (txHex: string) => {
const request = await fetch(MEMPOOL_BROADCAST_SERVICE, {
method: "POST",
body: txHex,
});
const data = await request.text();
return data;
};
export const assembleScript = (pubkey: any, xop: string) => {
const xopHex = Buffer.from(xop);
const script = [
pubkey,
"OP_CHECKSIG",
"OP_0",
"OP_IF",
Buffer.from("ord"),
"01",
Buffer.from("text/plain"),
"07",
xopHex,
"OP_0",
xopHex,
"OP_ENDIF",
];
return script;
};
export const getVsize = (rawTxHex: string): bigDecimal => {
const transaction = Transaction.fromHex(rawTxHex);
return new bigDecimal(transaction.weight() / 4);
};
export const sleep = (ms: number): Promise<void> => {
return new Promise((resolve) => setTimeout(resolve, ms));
};