-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathHashedTimeLock.java
69 lines (59 loc) · 1.81 KB
/
HashedTimeLock.java
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
package bt.sample;
import bt.*;
import bt.compiler.CompilerVersion;
import bt.compiler.TargetCompilerVersion;
import bt.ui.EmulatorWindow;
/**
* A hashed time lock smart contract.
*
* This small sample can be extended to implement cross-chain atomic swaps.
*
* This contract should be initialized with a hashlock and beneficiary.
*
* If the beneficiary sends the correct key the funds are unlocked. If the
* creator send a transaction and the timelock as passed, the funds are
* withdraw.
*
* @author jjos
*/
@TargetCompilerVersion(CompilerVersion.v0_0_0)
public class HashedTimeLock extends Contract {
/** Expected activation fee in BURST */
public static final long ACTIVATION_FEE = 1 * ONE_BURST;
/** Timeout in minutes */
public static final int TIMEOUT = 12;
Register hashlock;
Address beneficiary;
Timestamp timelock;
Register key, hashedKey;
/**
* This constructor is called the first time the contract receives a
* transaction.
*
* The contract creator should send a transaction with the amount to be
* locked. After this transaction is received the timelock is set with
* the configured TIMEOUT.
*/
public HashedTimeLock() {
timelock = getBlockTimestamp().addMinutes(TIMEOUT);
}
/**
* Any new transaction received will be handled by this function.
*
* The benefitiary should send a message with the key to unlock the funds. The
* creator can withdraw the funds if the timelock has expired.
*
*/
public void txReceived() {
if (getCurrentTx().checkMessageSHA256(hashedKey)) {
sendBalance(beneficiary);
}
if (getBlockTimestamp().ge(timelock)) {
// creator can claim back the balance after the timelock
sendAmount(getCurrentBalance(), getCreator());
}
}
public static void main(String[] args) throws Exception {
new EmulatorWindow(HashedTimeLock.class);
}
}