-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
171 lines (147 loc) · 3.42 KB
/
utils.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
170
171
import { block } from 'nanocurrency-web'
import rpc from './rpc.js'
import * as constants from './constants.js'
// broadcasts a block and waits for its confirmation
export const confirmBlock = ({ ws, block, hash, rpcUrl }) =>
new Promise((resolve, reject) => {
// register confirmation listener
const listener = (data) => {
const d = JSON.parse(data)
if (d.topic !== 'confirmation') return
if (d.message.hash !== hash) return
// update websocket subscription
ws.send(
JSON.stringify({
action: 'update',
topic: 'confirmation',
options: {
accounts_del: [block.account]
}
})
)
// unregister event listener
ws.off('message', listener)
resolve(hash)
}
ws.on('message', listener)
// register node websocket subscription
ws.send(
JSON.stringify({
action: 'update',
topic: 'confirmation',
options: {
accounts_add: [block.account]
}
})
)
// broadcast block
rpc(
{
action: 'process',
json_block: true,
async: true,
block
},
{
url: rpcUrl
}
)
})
export const createSendBlock = async ({
accountInfo,
to,
amount,
privateKey,
workerUrl
}) => {
const data = {
walletBalanceRaw: accountInfo.balance,
fromAddress: accountInfo.account,
toAddress: to,
representativeAddress: constants.BURN_ACCOUNT,
frontier: accountInfo.frontier,
amountRaw: amount
}
const action = {
action: 'work_generate',
hash: accountInfo.frontier,
difficulty: constants.WORK_THRESHOLD_BETA
}
const res = await rpc(action, { url: workerUrl })
data.work = res.work
return block.send(data, privateKey)
}
export const createReceiveBlock = async ({
accountInfo,
hash,
amount,
privateKey,
workerUrl
}) => {
const data = {
walletBalanceRaw: accountInfo.balance,
toAddress: accountInfo.account,
representativeAddress: constants.BURN_ACCOUNT,
frontier: accountInfo.frontier,
transactionHash: hash,
amountRaw: amount
}
const action = {
action: 'work_generate',
hash: accountInfo.frontier,
difficulty: constants.WORK_THRESHOLD_BETA
}
const res = await rpc(action, { url: workerUrl })
data.work = res.work
return block.receive(data, privateKey)
}
export const createOpenBlock = async ({
account,
hash,
amount,
publicKey,
privateKey,
workerUrl
}) => {
const data = {
walletBalanceRaw: '0',
toAddress: account,
representativeAddress: constants.BURN_ACCOUNT,
frontier: constants.ZEROS,
transactionHash: hash,
amountRaw: amount
}
const action = {
action: 'work_generate',
hash: publicKey,
difficulty: constants.WORK_THRESHOLD_BETA
}
const res = await rpc(action, { url: workerUrl })
data.work = res.work
return block.receive(data, privateKey)
}
export const createChangeBlock = async ({
accountInfo,
rep,
privateKey,
workerUrl
}) => {
const data = {
walletBalanceRaw: accountInfo.balance,
address: accountInfo.account,
representativeAddress: rep,
frontier: accountInfo.frontier
}
const res = await rpc(
{
action: 'work_generate',
hash: accountInfo.frontier,
difficulty: constants.WORK_THRESHOLD_BETA
},
{
url: workerUrl
}
)
data.work = res.work
return block.representative(data, privateKey)
}