-
Notifications
You must be signed in to change notification settings - Fork 1
/
907_aggregate_comp_payload_sample.html
110 lines (92 loc) · 3.82 KB
/
907_aggregate_comp_payload_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
<!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 NODE = 'https://catapult-test.opening-line.jp:3001';
const GENERATION_HASH = "453052FDC4EB23BF0D7280C103F7797133A633B68A81986165B76FCE248AB235";
const alice = nem.Account.generateNewAccount(nem.NetworkType.MIJIN_TEST);
const bob = nem.Account.generateNewAccount(nem.NetworkType.MIJIN_TEST);
const carol = nem.Account.generateNewAccount(nem.NetworkType.MIJIN_TEST);
console.log("alice");
console.log(alice.address);
console.log(alice.publicKey);
console.log("bob");
console.log(bob.publicKey);
console.log(bob);
const wsEndpoint = NODE.replace('https', 'wss');
const listener = new nem.Listener(wsEndpoint, WebSocket);
var hasBuilt = false;
listener.open().then(() => {
listener
.unconfirmedAdded(alice.address)
.subscribe(
function(_){
console.log("--unconfirmed transaction(alice)--");
console.log(_);
},
err => console.error(err));
listener
.confirmed(alice.address)
.subscribe(
function(_){
console.log("==confirmed transaction(alice)==");
if(!hasBuilt){
console.log("start sample program.");
buildProcess();
hasBuilt = true;
}
},
err => console.error(err));
});
function buildProcess(){
const sendAmount = nem.NetworkCurrencyMosaic.createRelative(10);
const backAmount = nem.NetworkCurrencyMosaic.createRelative(1);
const aliceTx = nem.TransferTransaction.create(nem.Deadline.create(), bob.address, [sendAmount], nem.PlainMessage.create('payout'), nem.NetworkType.MIJIN_TEST);
const bobTx = nem.TransferTransaction.create(nem.Deadline.create(), alice.address, [backAmount], nem.PlainMessage.create('payout'), nem.NetworkType.MIJIN_TEST);
//署名台帳作成
const aggregateTransaction = nem.AggregateTransaction.createComplete(
nem.Deadline.create(),
[
aliceTx.toAggregate(alice.publicAccount),
bobTx.toAggregate(bob.publicAccount)
],
nem.NetworkType.MIJIN_TEST,
[]
);
//Aliceで署名してシリアライズをBobに渡す
const aliceSignedTx = aggregateTransaction.signWith(alice, GENERATION_HASH);
//別端末でBobが署名
const bobSignedTx = nem.CosignatureTransaction.signTransactionPayload(bob, aliceSignedTx.payload, GENERATION_HASH);
console.log(bobSignedTx);
//Alice側でまとめてTx再作成→アナウンス
const cosignSignedTxs = [
new nem.CosignatureSignedTransaction(bobSignedTx.parentHash, bobSignedTx.signature, bobSignedTx.signer)
];
const recreatedTx = nem.TransactionMapping.createFromPayload(aliceSignedTx.payload);
const signedTx = recreatedTx.signTransactionGivenSignatures(alice, cosignSignedTxs, GENERATION_HASH);
console.log(signedTx.payload);
const transactionHttp = new nem.TransactionHttp(NODE);
transactionHttp
.announce(signedTx)
.subscribe(x =>{
console.log(x);
$('#items2-ctrl ul').append('<li><a target="_blank" href="' + NODE + '/transaction/' + signedTx.hash + '/status">' + signedTx.hash + '</a></li>');
$('#items3-ctrl ul').append('<li><a target="_blank" href="' + NODE + '/transaction/' + signedTx.hash + '">' + signedTx.hash + '</a></li>');
},
err => console.error(err)
);
}
});</script>
</head>
<body>
<h1>sample</h1>
<h3>state確認</h1><div id="items2-ctrl"><ul></ul></div>
<h3>confirmed確認</h1><div id="items3-ctrl"><ul></ul></div>
</body>
</html>