forked from andrewmalta13/test-common-wallet
-
Notifications
You must be signed in to change notification settings - Fork 3
/
commonWallet.js
122 lines (111 loc) · 3.59 KB
/
commonWallet.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
var bitcoinjs = require('bitcoinjs-lib')
var bitcoin = require('./bitcoin')
var request = require('request')
var simpleCommonWallet = function (options) {
var commonBlockchain = options.commonBlockchain
var network = options.network
var wif = (options.wif) ? options.wif : bitcoin.WIFKeyFromSeed(options.seed, network)
var address = bitcoin.getAddressFromWIF(wif, network)
var signMessage = function (message, cb) {
var key = bitcoinjs.ECKey.fromWIF(wif)
var network = (network === 'testnet') ? bitcoinjs.networks.testnet : null
cb(null, bitcoinjs.Message.sign(key, message, network).toString('base64'))
}
// callback (error, tx.to)
var signRawTransaction = function (txHex, cb) {
var input = 0
var options
var _txHex
if (typeof (txHex) === 'object') {
options = txHex
_txHex = options.txHex
input = options.input || 0
} else if (typeof (txHex === 'string')) {
_txHex = txHex
}
var _tx = bitcoinjs.Transaction.fromHex(_txHex)
var tx = bitcoinjs.TransactionBuilder.fromTransaction(_tx)
var key = bitcoinjs.ECKey.fromWIF(wif)
tx.sign(input, key)
var builtTx = tx.buildIncomplete()
var txid = builtTx.getId()
cb(false, builtTx.toHex(), txid)
}
var createTransaction = function (opts, callback) {
var value = opts.value
var destinationAddress = opts.destinationAddress
commonBlockchain.Addresses.Unspents([address], function (err, addressesUnspents) {
if (err && !addressesUnspents) {
callback('error creating transaction: ' + err, null)
return
}
var unspentOutputs = addressesUnspents[0]
unspentOutputs.forEach(function (utxo) {
utxo.txHash = utxo.txid
utxo.index = utxo.vout
})
bitcoin.buildTransaction({
sourceWIF: wif,
destinationAddress: destinationAddress,
value: value,
network: network,
skipSign: opts.skipSign,
rawUnspentOutputs: unspentOutputs,
propagateCallback: (opts.propagate) ? commonBlockchain.Transactions.Propagate : null
}, function (err, transaction) {
callback(err, transaction)
})
})
}
var __hosts = {}
var walletRequest = function (options, callback) {
var host = options.host
var path = options.path
var url = host + path
options.url = options.url || url
var nonce = __hosts[host].nonce
signMessage(nonce, function (err, signedNonce) {
if (err) {
// TODO
}
var headers = {
'x-common-wallet-address': address,
'x-common-wallet-network': network,
'x-common-wallet-signed-nonce': signedNonce
}
options.headers = options.headers ? options.headers.concat(headers) : headers
request(options, function (err, res, body) {
__hosts[host] = {
nonce: res.headers['x-common-wallet-nonce'],
verifiedAddress: res.headers['x-common-wallet-verified-address']
}
callback(err, res, body)
})
})
}
var login = function (host, callback) {
request({
url: host + '/nonce',
headers: {
'x-common-wallet-address': address,
'x-common-wallet-network': 'testnet'
}
}, function (err, res, body) {
__hosts[host] = {
nonce: res.headers['x-common-wallet-nonce']
}
callback(err, res, body)
})
}
var commonWallet = {
request: walletRequest,
login: login,
network: network,
signRawTransaction: signRawTransaction,
signMessage: signMessage,
address: address,
createTransaction: createTransaction
}
return commonWallet
}
module.exports = simpleCommonWallet