-
Notifications
You must be signed in to change notification settings - Fork 1
/
901_multilevel_multisig_sample.html
173 lines (148 loc) · 5.63 KB
/
901_multilevel_multisig_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
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<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");
//Alice Bob Carol Dave Ellen Frank
//Alice[asset]<-(2-of-3){Bob[security] and Carol and (Dave<-(1-of-2){Ellen or Frank})}
const NODE = 'https://catapult-test.opening-line.jp:3001';
const GENERATION_HASH = "453052FDC4EB23BF0D7280C103F7797133A633B68A81986165B76FCE248AB235";
const txHttp = new nem.TransactionHttp(NODE);
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);
const dave = nem.Account.generateNewAccount(nem.NetworkType.MIJIN_TEST);
const dave2 = nem.Account.generateNewAccount(nem.NetworkType.MIJIN_TEST);
const ellen = nem.Account.generateNewAccount(nem.NetworkType.MIJIN_TEST);
const frank = nem.Account.generateNewAccount(nem.NetworkType.MIJIN_TEST);
console.log(alice.address);
console.log(alice.privateKey);
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)==");
console.log(_);
if(!hasBuilt){
console.log("[[start sample program]]");
buildProcess();
hasBuilt = true;
}
},
err => console.error(err)
);
//ここにリスナーを追加します。
});
function buildProcess(){
const addType = nem.MultisigCosignatoryModificationType.Add;
const removeType = nem.MultisigCosignatoryModificationType.Remove;
const ellenOrFrankMultisigTx = nem.ModifyMultisigAccountTransaction.create(
nem.Deadline.create(),
1,1,
[
new nem.MultisigCosignatoryModification(addType, ellen),
new nem.MultisigCosignatoryModification(addType, frank)
],
nem.NetworkType.MIJIN_TEST
);
const bobAndDaveAndCarolMultisigTx = nem.ModifyMultisigAccountTransaction.create(
nem.Deadline.create(),
2,2,
[
new nem.MultisigCosignatoryModification(addType, bob),
new nem.MultisigCosignatoryModification(addType, dave),
new nem.MultisigCosignatoryModification(addType, carol),
],
nem.NetworkType.MIJIN_TEST
);
const aggregateTx = nem.AggregateTransaction.createComplete(
nem.Deadline.create(),
[
ellenOrFrankMultisigTx.toAggregate(dave.publicAccount),
bobAndDaveAndCarolMultisigTx.toAggregate(alice.publicAccount),
],
nem.NetworkType.MIJIN_TEST,
[]
);
const signedTx = alice.signTransactionWithCosignatories(
aggregateTx,
[bob,carol,dave,ellen,frank],
GENERATION_HASH,
);
appendInfo(NODE,signedTx,alice);
txHttp
.announce(signedTx)
.subscribe(x => {
console.log(x)
}, err => console.error(err));
listener
.confirmed(alice.address)
.pipe(
rxjs.filter((tx) => tx.transactionInfo !== undefined && tx.transactionInfo.hash === signedTx.hash),
rxjs.mergeMap(_ => {
const switchDaveToMultisigTx = nem.ModifyMultisigAccountTransaction.create(
nem.Deadline.create(),
0,0,
[
new nem.MultisigCosignatoryModification(addType, dave2),
new nem.MultisigCosignatoryModification(removeType, dave),
],
nem.NetworkType.MIJIN_TEST
);
const aggregateTx2 = nem.AggregateTransaction.createComplete(
nem.Deadline.create(),
[
ellenOrFrankMultisigTx.toAggregate(dave2.publicAccount),
switchDaveToMultisigTx.toAggregate(alice.publicAccount),
],
nem.NetworkType.MIJIN_TEST,
[]
);
const signedTx2 = dave2.signTransactionWithCosignatories(
aggregateTx2,
[carol,ellen,frank],//bob,daveは必要なし
GENERATION_HASH,
);
console.log(signedTx2);
appendInfo(NODE,signedTx2,alice.pulicAccount);
return txHttp.announce(signedTx2)
})
)
.subscribe(x => {
console.log(x);
}, err => console.error(err));
}
//結果出力
function appendInfo(node,signedTx,account){
$('#items1 ul').append('<li><a target="_blank" href="' + node + '/account/' + account.publicKey + '">' + account.address.address + '</a></li>');
$('#items2 ul').append('<li><a target="_blank" href="' + node + '/transaction/' + signedTx.hash + '/status">' + signedTx.hash + '</a></li>');
$('#items3 ul').append('<li><a target="_blank" href="' + node + '/transaction/' + signedTx.hash + '">' + signedTx.hash + '</a></li>');
}
})</script>
</head>
<body>
<h1>sample</h1>
<h3>account</h3><div id="items1"><ul></ul></div>
<h3>state</h3><div id="items2"><ul></ul></div>
<h3>confirmed</h3><div id="items3"><ul></ul></div>
</body>
</html>