-
Notifications
You must be signed in to change notification settings - Fork 1
/
908_atomic_swap_sample.html
205 lines (174 loc) · 7.61 KB
/
908_atomic_swap_sample.html
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
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="nem2-sdk-0.13.1.js"></script>
<script>$(function() {
const nem = require("/node_modules/nem2-sdk");
const rxjs = require("/node_modules/rxjs/operators");
const sha3_256 = require("/node_modules/js-sha3").sha3_256;
const alicePublic = nem.Account.generateNewAccount(nem.NetworkType.MIJIN_TEST);
const alicePrivate = nem.Account.generateNewAccount(nem.NetworkType.MIJIN_TEST);
const bobPrivate = nem.Account.createFromPrivateKey('CD52E4CA8F6EC1ADBE4BEF392BAD666179A495F87CBC15176F30C8A82EBA90D6', nem.NetworkType.MIJIN_TEST);
const bobPublic = nem.Account.generateNewAccount(nem.NetworkType.MIJIN_TEST); //空
console.log("alice public");
console.log(alicePublic.address);
console.log(alicePublic.publicKey);
console.log("bob private");
console.log(bobPrivate.address);
console.log(bobPrivate.publicKey);
const NODE_PUBLIC = "https://catapult-test.opening-line.jp:3001";
const GEN_HASH_PUBLIC = "453052FDC4EB23BF0D7280C103F7797133A633B68A81986165B76FCE248AB235";
const txHttpPublic = new nem.TransactionHttp(NODE_PUBLIC);
const NODE_PRIVATE = "http://3.112.149.203:3000";
const txHttpPrivate = new nem.TransactionHttp(NODE_PRIVATE);
const GEN_HASH_PRIVATE = "3125DF8F8C726660526886BAD6531A51A520845D984BE47943F9F844885CC7CA";
const hash = sha3_256.create();
const random = nem.Crypto.randomBytes(10);
const aliceSecret = hash.update(random).hex().toUpperCase();
console.log(aliceSecret);
const wsEndpointPublic = NODE_PUBLIC.replace('https', 'wss');
const wsEndpointPrivate = NODE_PRIVATE.replace('http', 'ws');
const listenerPublic = new nem.Listener(wsEndpointPublic, WebSocket);
const listenerPrivate = new nem.Listener(wsEndpointPrivate, WebSocket);
var hasBuilt = false;
listenerPublic.open().then(() => {
listenerPublic
.unconfirmedAdded(alicePublic.address)
.subscribe(
function(_){
console.log("--unconfirmed transaction(alice)--");
console.log(_);
},
err => console.error(err)
);
listenerPublic
.confirmed(alicePublic.address)
.subscribe(
function(_){
console.log("==confirmed transaction(alice)==");
if(!hasBuilt){
console.log("start sample program.");
buildProcess();
hasBuilt = true;
}
},
err => console.error(err)
);
//TX2 private bob->Lock(alice) by alice's secret
listenerPublic
.unconfirmedAdded(alicePublic.address)
.pipe(
rxjs.filter((tx) => tx.transactionInfo !== undefined && tx.type === 16722),
rxjs.mergeMap(_ => {
const lockTxPrivate = nem.SecretLockTransaction.create(
nem.Deadline.create(),
nem.NetworkCurrencyMosaic.createRelative(1),
nem.UInt64.fromUint(84 * 3600 / 15),
nem.HashType.Op_Sha3_256,
_.secret,
alicePrivate.address,
nem.NetworkType.MIJIN_TEST
);
console.log(lockTxPrivate);
const lockTxPrivateSigned = bobPrivate.sign(lockTxPrivate, GEN_HASH_PRIVATE);
$('#items2-ctrl ul').append('<li><a target="_blank" href="' + NODE_PRIVATE + '/transaction/' + lockTxPrivateSigned.hash + '/status">' + lockTxPrivateSigned.hash + '</a></li>');
$('#items3-ctrl ul').append('<li><a target="_blank" href="' + NODE_PRIVATE + '/transaction/' + lockTxPrivateSigned.hash + '">' + lockTxPrivateSigned.hash + '</a></li>');
return txHttpPrivate
.announce(lockTxPrivateSigned)
})
)
.subscribe(x => {
console.log(x);
}, err => console.error(err));
});
listenerPrivate.open().then(() => {
//TX3:private Lock(bob)->alice using proof
listenerPrivate
.confirmed(bobPrivate.address)
.pipe(
rxjs.filter((tx) => tx.transactionInfo !== undefined && tx.type === 16722),
rxjs.mergeMap(_ => {
const aliceProof = random.toString('hex');
//private (Lock)Bob->Alice 作ったaliceProofで取り出す
const proofTxPrivate = nem.SecretProofTransaction.create(
nem.Deadline.create(),
nem.HashType.Op_Sha3_256,
aliceSecret,
alicePrivate.address,
aliceProof,
nem.NetworkType.MIJIN_TEST
);
console.log(proofTxPrivate);
const proofTxPrivateSigned = alicePrivate.sign(proofTxPrivate, GEN_HASH_PRIVATE);
$('#items2-ctrl ul').append('<li><a target="_blank" href="' + NODE_PRIVATE + '/transaction/' + proofTxPrivateSigned.hash + '/status">' + proofTxPrivateSigned.hash + '</a></li>');
$('#items3-ctrl ul').append('<li><a target="_blank" href="' + NODE_PRIVATE + '/transaction/' + proofTxPrivateSigned.hash + '">' + proofTxPrivateSigned.hash + '</a></li>');
return txHttpPrivate.announce(proofTxPrivateSigned);
})
)
.subscribe(
function(_){
console.log("--confirmed transaction(bob private)--");
console.log(_);
},
err => console.error(err)
);
//TX4:public Lock(alice)->bob by alice's proof
listenerPrivate
.unconfirmedAdded(alicePrivate.address)
.pipe(
rxjs.filter((tx) => tx.transactionInfo !== undefined && tx.type === 16978),
rxjs.mergeMap(_ => {
const proofTxPublic = nem.SecretProofTransaction.create(
nem.Deadline.create(),
nem.HashType.Op_Sha3_256,
_.secret,
bobPublic.address,
_.proof,
nem.NetworkType.MIJIN_TEST
);
console.log(proofTxPublic);
const proofTxPublicSigned = bobPublic.sign(proofTxPublic, GEN_HASH_PUBLIC);
$('#items2-ctrl ul').append('<li><a target="_blank" href="' + NODE_PUBLIC + '/transaction/' + proofTxPublicSigned.hash + '/status">' + proofTxPublicSigned.hash + '</a></li>');
$('#items3-ctrl ul').append('<li><a target="_blank" href="' + NODE_PUBLIC + '/transaction/' + proofTxPublicSigned.hash + '">' + proofTxPublicSigned.hash + '</a></li>');
return txHttpPublic.announce(proofTxPublicSigned)
})
)
.subscribe(
function(_){
console.log("--confirmed transaction(alice private)--");
console.log(_);
},
err => console.error(err)
);
});
function buildProcess(){
//TX1 public alice->Lock(bob)
const lockTxPublic = nem.SecretLockTransaction.create(
nem.Deadline.create(),
nem.NetworkCurrencyMosaic.createRelative(1),
nem.UInt64.fromUint(96 * 3600 / 15),
nem.HashType.Op_Sha3_256,
aliceSecret,
bobPublic.address,
nem.NetworkType.MIJIN_TEST
);
const lockTxPublicSigned = alicePublic.sign(lockTxPublic, GEN_HASH_PUBLIC);
txHttpPublic
.announce(lockTxPublicSigned)
.subscribe(x => {
console.log(x);
$('#items2-ctrl ul').append('<li><a target="_blank" href="' + NODE_PUBLIC + '/transaction/' + lockTxPublicSigned.hash + '/status">' + lockTxPublicSigned.hash + '</a></li>');
$('#items3-ctrl ul').append('<li><a target="_blank" href="' + NODE_PUBLIC + '/transaction/' + lockTxPublicSigned.hash + '">' + lockTxPublicSigned.hash + '</a></li>');
}, err => console.error(err));
}
});</script>
</head>
<body>
<h1>sample</h1>
<h3>state確認</h3><div id="items2-ctrl"><ul></ul></div>
<h3>confirmed確認</h3><div id="items3-ctrl"><ul></ul></div>
</body>
</html>