forked from makevoid/blockchain-pen
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbitcore_ext.coffee
161 lines (120 loc) · 3.66 KB
/
bitcore_ext.coffee
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
TX_FEE = 10000
bitcore = require 'bitcore'
class BlockCypher
@pushtx: (tx_hash, callback, errback) ->
pushtx_url = "https://api.blockcypher.com/v1/btc/main/txs/push"
post_params =
tx: tx_hash
HTTP.post pushtx_url, post_params, callback, errback
class HTTP
@post: (url, params, callback, errback) ->
success = (data) ->
callback data
error = (fail_message) ->
fail_message = JSON.parse fail_message.response
errback fail_message.error
data =
tx: params.tx
ajax =
contentType: 'application/json',
data: JSON.stringify(data),
dataType: 'json',
processData: false,
type: 'POST',
success: success,
error: error,
url: url
$.ajax ajax
class BitcoreExt
constructor: (@address, @pvt_key_string) ->
store_utxos: (tx_ids) ->
store = localStorage
utxos = if store.utxos then JSON.parse(store.utxos) else []
utxos = utxos.concat tx_ids
store.utxos = JSON.stringify utxos
sign_and_broadcast: (message, utxos, callback, errback) ->
store = localStorage
# tools
does_include = (array, element) ->
array.indexOf(element) != -1
is_empty = (val) ->
!val || val.length == 0
# console.log "sign and broadcast"
tx_amount = 1000
# console.log "utxo_count", utxos.length
utxos_out = []
# total_amount_sathoshis = 0
tx_ids = []
for utxo in utxos
# console.log utxos
amount_satoshis = utxo.value
# total_amount_sathoshis += amount_satoshis
amount_btc = new bitcore.Unit.fromSatoshis(amount_satoshis).BTC
tx_id = utxo.tx_hash_big_endian
tx_ids.push tx_id
# USE ALL INPUTS VERSION (TODO: FIXME:)
#
# if store && store.utxos && does_include(JSON.parse(store.utxos), tx_id)
# console.log "skipping transaction: #{tx_id}"
# continue
utxos_out.push
address: @address
txId: tx_id
scriptPubKey: utxo.script
amount: amount_btc
vout: utxo.tx_output_n
# break if amount_satoshis > TX_FEE+tx_amount
console.log "UTXOS:", utxos_out
console.log "...proceeding:"
console.log is_empty utxos_out
unless is_empty utxos_out
fee = TX_FEE # from 5000 it should be a good fee
address = @address
amount = tx_amount
pvt_key = @pvt_key_string
# console.log "utxos_out: ", utxos_out
transaction = new bitcore.Transaction()
.from(utxos_out)
.to(address, amount)
.change(address)
.fee(fee)
# actual
#
# transaction
# .addData(message)
# proposed
transaction
.addData("{\"a\":")
.addData("\"b\"}")
# saves the hash: { a: "b" }.to_json
#
#
# transaction
# .addData(message)
# .addData(message)
# .addData(message)
transaction.sign(pvt_key)
# usage:
#
# 4.20
#
# http://blockchainpen.com
#
#
# reset local storage
#
# backup private key
try
tx_hash = transaction.serialize()
catch
console.log "retrying serialization with unchecked = true"
tx_hash = transaction.serialize true
BlockCypher.pushtx tx_hash, (tx_response) =>
@store_utxos tx_ids
tx_id = tx_response.tx.hash
callback tx_id
, errback
else
console.error "ERROR: Not enough UTXOs"
errback "It seems you don't have any available transaction output to spend, you have to wait for the next block to be confirmed or you can deposit at least 0.11 mbtc to be able to write another message."
module.exports = BitcoreExt if module?