forked from chatch/hashed-timelock-contract-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
htlc.js
296 lines (268 loc) · 9.12 KB
/
htlc.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
const {assertEqualBN} = require('./helper/assert')
const {
bufToStr,
getBalance,
htlcArrayToObj,
isSha256Hash,
newSecretHashPair,
nowSeconds,
random32,
txContractId,
txGas,
txLoggedArgs,
} = require('./helper/utils')
const HashedTimelock = artifacts.require('./HashedTimelock.sol')
const REQUIRE_FAILED_MSG = 'Returned error: VM Exception while processing transaction: revert'
const hourSeconds = 3600
const timeLock1Hour = nowSeconds() + hourSeconds
const oneFinney = web3.utils.toWei(web3.utils.toBN(1), 'finney')
contract('HashedTimelock', accounts => {
const sender = accounts[1]
const receiver = accounts[2]
it('newContract() should create new contract and store correct details', async () => {
const hashPair = newSecretHashPair()
const htlc = await HashedTimelock.deployed()
const txReceipt = await htlc.newContract(
receiver,
hashPair.hash,
timeLock1Hour,
{
from: sender,
value: oneFinney,
}
)
const logArgs = txLoggedArgs(txReceipt)
const contractId = logArgs.contractId
assert(isSha256Hash(contractId))
assert.equal(logArgs.sender, sender)
assert.equal(logArgs.receiver, receiver)
assertEqualBN(logArgs.amount, oneFinney)
assert.equal(logArgs.hashlock, hashPair.hash)
assert.equal(logArgs.timelock, timeLock1Hour)
const contractArr = await htlc.getContract.call(contractId)
const contract = htlcArrayToObj(contractArr)
assert.equal(contract.sender, sender)
assert.equal(contract.receiver, receiver)
assertEqualBN(contract.amount, oneFinney)
assert.equal(contract.hashlock, hashPair.hash)
assert.equal(contract.timelock.toNumber(), timeLock1Hour)
assert.isFalse(contract.withdrawn)
assert.isFalse(contract.refunded)
assert.equal(
contract.preimage,
'0x0000000000000000000000000000000000000000000000000000000000000000'
)
})
it('newContract() should fail when no ETH sent', async () => {
const hashPair = newSecretHashPair()
const htlc = await HashedTimelock.deployed()
try {
await htlc.newContract(receiver, hashPair.hash, timeLock1Hour, {
from: sender,
value: 0,
})
assert.fail('expected failure due to 0 value transferred')
} catch (err) {
assert.isTrue(err.message.startsWith(REQUIRE_FAILED_MSG))
}
})
it('newContract() should fail with timelocks in the past', async () => {
const hashPair = newSecretHashPair()
const pastTimelock = nowSeconds() - 1
const htlc = await HashedTimelock.deployed()
try {
await htlc.newContract(receiver, hashPair.hash, pastTimelock, {
from: sender,
value: oneFinney,
})
assert.fail('expected failure due past timelock')
} catch (err) {
assert.isTrue(err.message.startsWith(REQUIRE_FAILED_MSG))
}
})
it('newContract() should reject a duplicate contract request', async () => {
const hashPair = newSecretHashPair()
const htlc = await HashedTimelock.deployed()
await htlc.newContract(receiver, hashPair.hash, timeLock1Hour, {
from: sender,
value: oneFinney,
})
// now call again with the exact same parameters
try {
await htlc.newContract(receiver, hashPair.hash, timeLock1Hour, {
from: sender,
value: oneFinney,
})
assert.fail('expected failure due to duplicate request')
} catch (err) {
assert.isTrue(err.message.startsWith(REQUIRE_FAILED_MSG))
}
})
it('withdraw() should send receiver funds when given the correct secret preimage', async () => {
const hashPair = newSecretHashPair()
const htlc = await HashedTimelock.deployed()
const newContractTx = await htlc.newContract(
receiver,
hashPair.hash,
timeLock1Hour,
{
from: sender,
value: oneFinney,
}
)
const contractId = txContractId(newContractTx)
const receiverBalBefore = await getBalance(receiver)
// receiver calls withdraw with the secret to get the funds
const withdrawTx = await htlc.withdraw(contractId, hashPair.secret, {
from: receiver,
})
const tx = await web3.eth.getTransaction(withdrawTx.tx)
// Check contract funds are now at the receiver address
const expectedBal = receiverBalBefore
.add(oneFinney)
.sub(txGas(withdrawTx, tx.gasPrice))
assertEqualBN(
await getBalance(receiver),
expectedBal,
"receiver balance doesn't match"
)
const contractArr = await htlc.getContract.call(contractId)
const contract = htlcArrayToObj(contractArr)
assert.isTrue(contract.withdrawn) // withdrawn set
assert.isFalse(contract.refunded) // refunded still false
assert.equal(contract.preimage, hashPair.secret)
})
it('withdraw() should fail if preimage does not hash to hashX', async () => {
const hashPair = newSecretHashPair()
const htlc = await HashedTimelock.deployed()
const newContractTx = await htlc.newContract(
receiver,
hashPair.hash,
timeLock1Hour,
{
from: sender,
value: oneFinney,
}
)
const contractId = txContractId(newContractTx)
// receiver calls withdraw with an invalid secret
const wrongSecret = bufToStr(random32())
try {
await htlc.withdraw(contractId, wrongSecret, {from: receiver})
assert.fail('expected failure due to 0 value transferred')
} catch (err) {
assert.isTrue(err.message.startsWith(REQUIRE_FAILED_MSG))
}
})
it('withdraw() should fail if caller is not the receiver', async () => {
const hashPair = newSecretHashPair()
const htlc = await HashedTimelock.deployed()
const newContractTx = await htlc.newContract(
receiver,
hashPair.hash,
timeLock1Hour,
{
from: sender,
value: oneFinney,
}
)
const contractId = txContractId(newContractTx)
const someGuy = accounts[4]
try {
await htlc.withdraw(contractId, hashPair.secret, {from: someGuy})
assert.fail('expected failure due to wrong receiver')
} catch (err) {
assert.isTrue(err.message.startsWith(REQUIRE_FAILED_MSG))
}
})
it('withdraw() should fail after timelock expiry', async () => {
const hashPair = newSecretHashPair()
const htlc = await HashedTimelock.new()
const timelock1Second = nowSeconds() + 1
const newContractTx = await htlc.newContract(
receiver,
hashPair.hash,
timelock1Second,
{
from: sender,
value: oneFinney,
}
)
const contractId = txContractId(newContractTx)
// wait one second so we move past the timelock time
return new Promise((resolve, reject) =>
setTimeout(async () => {
// attempt to withdraw and check that it is not allowed
try {
await htlc.withdraw(contractId, hashPair.secret, {from: receiver})
reject(
new Error('expected failure due to withdraw after timelock expired')
)
} catch (err) {
assert.isTrue(err.message.startsWith(REQUIRE_FAILED_MSG))
resolve()
}
}, 1000)
)
})
// Remove skip if using timelock guard (currently commented out)
it.skip('refund() should pass after timelock expiry', async () => {
const hashPair = newSecretHashPair()
const htlc = await HashedTimelock.new()
const timelock1Second = nowSeconds() + 1
const newContractTx = await htlc.newContract(
receiver,
hashPair.hash,
timelock1Second,
{
from: sender,
value: oneFinney,
}
)
const contractId = txContractId(newContractTx)
// wait one second so we move past the timelock time
return new Promise((resolve, reject) =>
setTimeout(async () => {
const balBefore = await getBalance(sender)
const refundTx = await htlc.refund(contractId, {from: sender})
const tx = await web3.eth.getTransaction(refundTx.tx)
// Check contract funds are now at the senders address
const expectedBal = balBefore.add(oneFinney).sub(txGas(refundTx, tx.gasPrice))
assertEqualBN(
await getBalance(sender),
expectedBal,
"sender balance doesn't match"
)
const contract = await htlc.getContract.call(contractId)
assert.isTrue(contract[6]) // refunded set
assert.isFalse(contract[5]) // withdrawn still false
}, 1000)
)
})
it('refund() should fail before the timelock expiry', async () => {
const hashPair = newSecretHashPair()
const htlc = await HashedTimelock.deployed()
const newContractTx = await htlc.newContract(
receiver,
hashPair.hash,
timeLock1Hour,
{
from: sender,
value: oneFinney,
}
)
const contractId = txContractId(newContractTx)
try {
await htlc.refund(contractId, {from: sender})
assert.fail('expected failure due to timelock')
} catch (err) {
assert.isTrue(err.message.startsWith(REQUIRE_FAILED_MSG))
}
})
it("getContract() returns empty record when contract doesn't exist", async () => {
const htlc = await HashedTimelock.deployed()
const contract = await htlc.getContract.call('0xabcdef')
const sender = contract[0]
assert.equal(Number(sender), 0)
})
})