-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitcoin.js
More file actions
181 lines (157 loc) · 4.1 KB
/
Copy pathbitcoin.js
File metadata and controls
181 lines (157 loc) · 4.1 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
/*
* 工具
*/
const Address = require('btc-address')
const binConv = require('binstring')
var request = require('request')
const bitcoin = require('bitcoinjs-lib')
const bscript = bitcoin.script
const OPS = require('bitcoin-ops')
const OP_INT_BASE = OPS.OP_RESERVED; // OP_1 - 1
var config = require('./config')
var path = require('path')
var logger = config.logger()
const substrate = require('oo7-substrate')
const {
decode
} = require('oo7-substrate/src/codec.js')
const {
toBtcAddress,
bytesToRIHex,
bytesToHex,
hexToBytes
} = require('oo7-substrate/src/utils')
// XAccounts_CrossChainAddressMapOf #ADDRESS
// ->bitcoin address
function pubkeyToBtcAddress(pub, network) {
let n = network == 'mainnet' ? bitcoin.networks.mainnet : bitcoin.networks.testnet
var pubKeyBuffer = Buffer.from(pub, 'hex')
var addr = bitcoin.ECPair.fromPublicKeyBuffer(pubKeyBuffer, n).getAddress()
return addr
}
function hashToBtcAdress(hash, kind, network) {
let h = hexToBytes(hash)
let n = 'testnet'
let t = 'pubkeyhash'
switch (kind) {
case 'P2SH':
t = 'scripthash'
break
default:
t = 'pubkeyhash'
break
}
switch (network) {
case 'Mainnet':
n = 'mainnet'
break
default:
n = 'testnet'
break
}
var address = new Address(
binConv(h, {
in: 'hex',
out: 'bytes'
}),
t,
n
)
console.log(h, n, t, address.toString())
return address.toString()
}
function layoutToBtcAddress(btc_layout) {
let b = hexToBytes(btc_layout)
let v = b.slice(0, 1)
let h = b.slice(1, 21)
let c = b.slice(22, 25)
let n = 'testnet'
let t = 'pubkeyhash'
switch (v) {
case 0:
n = 'mainnet'
t = 'pubkeyhash'
break
case 5:
n = 'mainnet'
t = 'scripthash'
break
case 111:
n = 'testnet'
t = 'pubkeyhash'
break
case 196:
n = 'testnet'
t = 'scripthash'
break
default:
break
}
var address = new Address(
binConv(h, {
in: 'hex',
out: 'bytes'
}),
t,
n
)
console.log(v, n, t, address.toString())
return address.toString()
}
function queryBTCBalance(hash, kind, network, callback) {
let address = hashToBtcAdress(hash, kind, network)
let url = 'http://api.blockcypher.com/v1/btc/'+(('Testnet' == network)?'test3':'main')+'/addrs/' + address + '?unspentOnly=true&confirmations=1&r=' + Math.random()
console.log(address, url)
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
let data = JSON.parse(body)
callback({
address: address,
balance: data.balance
})
} else {
logger.error(error)
}
})
}
function parseTrusteeAddrInfo(hex, callback) {
let TrusteeAddrInfo = decode(hexToBytes(hex), 'TrusteeAddrInfo')
let address = TrusteeAddrInfo.addr.toString()
let redeem_script = bytesToHex(TrusteeAddrInfo.redeem_script)
redeem_script = parseRedeemScript(redeem_script)
request('https://api.chainx.org/bitx/testnet/' + address.address + '/balance?r=' + Math.random(), function (error, response, body) {
if (!error && response.statusCode == 200) {
let data = JSON.parse(body)
callback({
address: address,
balance: data.balance
})
} else {
// logger.error(error)
}
})
}
function parseRedeemScript(hex) {
var data = bscript.decompile(Buffer.from(hex.toString(), 'hex'))
// console.log(data)
// console.log('OP_INT_BASE ',OP_INT_BASE,OPS.OP_0)
var m = data[0] - OP_INT_BASE
var n = data[data.length - 2] - OP_INT_BASE
var pubkeys = data.slice(1, -2)
var keys = []
// console.log('MultilSig:' + m + '/' + n)
for (var i = 0; i < pubkeys.length; i++) {
keys[i] = bytesToHex(pubkeys[i])
console.log('pubkey[' + i + ']=' + bytesToHex(pubkeys[i]), pubkeyToBtcAddress(bytesToHex(pubkeys[i])))
}
return {
m: m,
n: n,
keys: keys
}
}
exports.parseTrusteeAddrInfo = parseTrusteeAddrInfo
exports.parseRedeemScript = parseRedeemScript
exports.pubkeyToBtcAddress = pubkeyToBtcAddress
exports.queryBTCBalance = queryBTCBalance
exports.hashToBtcAdress = hashToBtcAdress