forked from interplanaria/bitsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
170 lines (147 loc) · 3.96 KB
/
index.js
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const axios = require('axios')
const bsv = require('bsv')
const Toychain = require('toychain')
const assert = require('assert')
const defaultPrototype = [
{ "o0": "OP_0", "o1": "OP_RETURN", "s2": "Hello Bitcoin Simulator" }
]
const bip44 = (i) => {
return `m/44'/0'/0'/0/${i}`
}
const config = {
mine: 1,
delay: 1000,
rpc: 'http://root:bitcoin@127.0.0.1:18332',
xpriv: 'xprv9s21ZrQH143K2AUh9yj3SmnzFVpHFgfGh23tz9iQb6p86yee29B1CcUenjSvWUFtNQ6oBho8PwPZNPi758kTFvJnxs1SoYbtUbyZeTK9zBC',
}
class Bitsim {
constructor(o) {
this.config = config
if (o) Object.assign(this.config, o)
let zeroKey = bsv.HDPrivateKey.fromString(this.config.xpriv).deriveChild("m/44'/0'/0'/0/0").publicKey
this.config.zeroAddress = bsv.Address.fromPublicKey(zeroKey).toString()
this.toychain = new Toychain({
xpriv: this.config.xpriv,
storage: {
name: 'toychain'
}
})
}
async init() {
console.log('initialzation step')
let rawHex = await this.fund()
console.log('1. funding toychain with coinbase', rawHex)
this.toychain.clone({
tx: rawHex
})
console.log('2. mining 100 blocks')
await this.mine(100)
for(let i=0; i<25; i++) {
this.toychain.add({
v: 1,
edge: { in: 1, out: 2 },
out: defaultPrototype
})
}
console.log('3. bootstrapping toychain tree')
await this.toychain.push({
rpc: this.config.rpc
})
await this.mine(1)
console.log('bitsim initialization complete')
}
async rpc(method, params) {
let parameters = params || []
const r = await axios({
method: 'post',
url: this.config.rpc,
data: {
method: method,
params: parameters,
},
})
return r.data.result
}
async getMempool() {
const res = await this.rpc('getrawmempool')
return res
}
async add(n, proto) {
const number = n || 50
const prototype = proto || defaultPrototype
for(let i=0; i<number; i++) {
this.toychain.add({
v: 1,
edge: {in: 1, out: 2},
out: prototype
})
}
await this.toychain.push({
rpc: this.config.rpc
})
}
async fund(a) {
const address = a || this.config.zeroAddress
const m = await this.mine(1, address)
const res = await this.rpc('getblock', [m[0], 2])
return res.tx[0].hex
}
async mine(n, a) {
const number = n || this.config.mine
const address = a || this.config.zeroAddress
const res = await this.rpc('generatetoaddress', [number, address])
return res
}
async fork(hashOrHeight) {
let nhash
let nheight
if (typeof hashOrHeight === 'number') {
nheight = hashOrHeight + 1
} else {
let header = await this.getBlockHeader(hashOrHeight)
nheight = header.height + 1
}
await this.invalidateBlock(nheight)
}
async invalidateBlock(hashOrHeight) {
let hash = hashOrHeight
if (typeof hash === 'number') hash = await this.getBlockHash(hashOrHeight)
const r = await this.rpc('invalidateblock', [hash])
}
async getBlockHash(height) {
const res = await this.rpc('getblockhash', [height])
return res
}
async getBlockHeader(hash) {
const res = await this.rpc('getblockheader', [hash])
return res
}
async getLatestHeader() {
const r = await this.rpc('getbestblockhash', [] )
const header = await this.getBlockHeader(r)
return header
}
async getMempoolInfo() {
const res = await this.rpc('getmempoolinfo', [] )
return res
}
async getRawMempool() {
const res = await this.rpc('getrawmempool', [] )
return res
}
async last(d) {
const delay = d || this.config.delay
await new Promise((r) => setTimeout(r, delay))
const node = await this.getLatestHeader()
return node
}
async getRandomBlock(max) {
const height = Math.floor(Math.random() * Math.floor(max))
const hash = await this.getBlockHash(height)
return {
height: height,
hash: hash,
}
}
}
module.exports = Bitsim