All files / platform / btc.ts

100.00% Branches 0/0
5.17% Lines 3/58
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
x2
x2
x2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

























































































import { Service, Dir, Exec, Spawn, Port } from '../context.ts';
import { Fn, Name, joined, merged } from '../format.ts';
import { Sub } from './btc/zeromq.ts';

export function Btc (...options: Partial<Btc>[]): Btc {
  const {
    cli       = 'bitcoin-cli',
    node      = 'bitcoind', // elementsd
    regTest   = true,
    rpcPort   = regTest ? 18443 : 8443,
    rpcUser   = 'fadroma',
    rpcPass   = 'fadroma',
    rpcQueue  = 32,
    dataRoot  = '/tmp/fadroma/test/btc/',
    dataDir   = joined('', dataRoot, 'data',   +new Date()),
    walletDir = joined('', dataRoot, 'wallet', +new Date()),
    zmqPort   = 48485,
    txIndex   = false,
    ...rest
  } = merged(...options);

  const context = {
    node, spawnNode,
    cli, execCli,
    regTest, rpcPort, rpcUser, rpcPass, rpcQueue,
    dataRoot, dataDir, walletDir, zmqPort, txIndex,
    localnet: Service('BTC Localnet', () => spawnNode(),
      Dir(walletDir, execCli('createwallet', walletDir),
        execCli(`-rpcwallet=${walletDir}`, '-generate'))),
    subscribe,
    ...rest
  };

  function spawnNode (...args: string[]) {
    return Service(`Spawn(${node})`, Port(rpcPort, Dir(dataDir, 
      Spawn(node, '-server',
        rpcPort && ('-rpcport=' + rpcPort),
        rpcUser && `-rpcuser=fadroma`,
        rpcPass && `-rpcpassword=${rpcPass}`,
        dataDir && `-datadir=${dataDir}`,
        regTest && '-regtest',
        txIndex && '-txindex',
        zmqPort && ('-zmqpubhashblock=tcp:/' + '/127.0.0.1:' + zmqPort),
        zmqPort && ('-zmqpubhashtx=tcp:/'    + '/127.0.0.1:' + zmqPort),
        rpcQueue && ('-rpcworkqueue=' + rpcQueue),
        ...args)))) as Spawn;
  }

  function execCli (...args: string[]): Fn<[Dir]> {
    return Dir(dataDir, Exec(cli, 
      rpcPort && ('-rpcport=' + rpcPort),
      rpcUser && `-rpcuser=fadroma`,
      rpcPass && `-rpcpassword=${rpcPass}`,
      dataDir && `-datadir=${dataDir}`,
      regTest && '-regtest',
      ...args));
  }

  function subscribe (onZmq) {
    //await portWait({ port: zmqPort });
    return Sub(zmqPort, onZmq);
  }

  return context
}

export type Btc = {
  cli:       string,
  node:      string,
  regTest:   boolean,
  dataRoot:  string,
  dataDir:   string,
  walletDir: string,
  httpPort:  number,
  zmqPort:   number,
  rpcPort:   number,
  rpcUser:   string,
  rpcPass:   string,
  rpcQueue:  number,
  txIndex:   boolean,
  /** Spawn Bitcoin daemon. */
  spawnNode: (..._: string[]) => Spawn,
  /** Call Bitcoin CLI. */
  execCli:   (..._: string[]) => Exec,
  /** Call Bitcoin RPC. */
  rpc:       Fn,
  /** Subscribe to Bitcoin note via ZeroMQ. */
  subscribe: Fn<[Fn]>,
  /** Run Bitcoin localnet. */
  localnet:  Service,
};