This repository has been archived by the owner on Aug 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flash.js
300 lines (249 loc) · 8.3 KB
/
flash.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
297
298
299
300
////////////////////////////////////////////////
////////////// FLASH EXAMPLE /////////////////
////////////////////////////////////////////////
// This example sets up a channel //////
// then makes a transfer from USER ONE //////
// to USER TWO. //////
////////////////////////////////////////////////
const IOTACrypto = require("iota.crypto.js")
const transfer = require("./lib/transfer")
const multisig = require("./lib/multisig")
const Helpers = require("./functions")
const oneSeed =
"POLUYZLOHULUXCKSZYENYILPHLSONCQNADFYXDUWFXZUHPEWQGKKXKENCEFFHOGJWGYEEAYWFEAYFB9E9"
const oneSettlement =
"HWFKUMA99MLBWLPHSJSLDXFLZCAFYVSZDA9HYBCESWLNOAUJUGW9NWX9VFYPPDZZAHVPVIEAUEDAV9JJDUBNNZLQZZ"
const twoSeed =
"ISMUOASMSSPICBGMRFEDCYJVKRWOCXNJTRKVGMCH9LLNTCDEITPWQJJBKLERBFMVQKZGZVNPOSJDZAWDR"
const twoSettlement =
"UZHYBEXTXPCNHXOROPMLGJFQSJHRMKXTANGRNSJFYABEERGTXWCHDQHEHNIMUPAACZKLOGFSAAQTZALZDRD9HH9LHD"
//////////////////////////////////
// INITIAL CHANNEL CONDITIONS
// Security level
const SECURITY = 2
// Number of parties taking signing part in the channel
const SIGNERS_COUNT = 2
// Flash tree depth
const TREE_DEPTH = 4
// Total channel Balance
const CHANNEL_BALANCE = 2000
// Users deposits
const DEPOSITS = [1000, 1000]
//////////////////////////////////
// INITAL FLASH OBJECTS
// USER ONE - Initial Flash Object
var oneFlash = {
userIndex: 0,
userSeed: oneSeed,
index: 0,
security: SECURITY,
depth: TREE_DEPTH,
bundles: [],
partialDigests: [],
flash: {
signersCount: SIGNERS_COUNT,
balance: CHANNEL_BALANCE,
deposit: DEPOSITS.slice(), // Clone correctly
outputs: {},
transfers: []
}
}
//////////////////////////////////
// USER TWO - Initial Flash Object
var twoFlash = {
userIndex: 1,
userSeed: twoSeed,
index: 0,
security: SECURITY,
depth: TREE_DEPTH,
bundles: [],
partialDigests: [],
flash: {
signersCount: SIGNERS_COUNT,
balance: CHANNEL_BALANCE,
deposit: DEPOSITS.slice(), // Clone correctly
outputs: {},
transfers: []
}
}
console.log("Flash objects created!")
//////////////////////////////
////// SETUP CHANNEL //////
//////////////////////////////
// GENERATE DIGESTS
// USER ONE
// Create digests for the start of the channel
for (let i = 0; i < TREE_DEPTH + 1; i++) {
// Create new digest
const digest = multisig.getDigest(
oneFlash.userSeed,
oneFlash.index,
oneFlash.security
)
// Increment key index
oneFlash.index++
oneFlash.partialDigests.push(digest)
}
// USER TWO
// Create digests for the start of the channel
for (let i = 0; i < TREE_DEPTH + 1; i++) {
// Create new digest
const digest = multisig.getDigest(
twoFlash.userSeed,
twoFlash.index,
twoFlash.security
)
// Increment key index
twoFlash.index++
twoFlash.partialDigests.push(digest)
}
console.log("Inital digests generated!")
//////////////////////////////////
// INITAL MULTISIG
// Make an array of digests
let allDigests = []
console.log(oneFlash.partialDigests)
allDigests[oneFlash.userIndex] = oneFlash.partialDigests
allDigests[twoFlash.userIndex] = twoFlash.partialDigests
// Generate the first addresses
let oneMultisigs = oneFlash.partialDigests.map((digest, index) => {
// Create address
let addy = multisig.composeAddress(
allDigests.map(userDigests => userDigests[index])
)
// Add key index in
addy.index = digest.index
// Add the signing index to the object IMPORTANT
addy.signingIndex = oneFlash.userIndex * digest.security
// Get the sum of all digest security to get address security sum
addy.securitySum = allDigests
.map(userDigests => userDigests[index])
.reduce((acc, v) => acc + v.security, 0)
// Add Security
addy.security = digest.security
return addy
})
let twoMultisigs = twoFlash.partialDigests.map((digest, index) => {
// Create address
let addy = multisig.composeAddress(
allDigests.map(userDigests => userDigests[index])
)
// Add key index in
addy.index = digest.index
// Add the signing index to the object IMPORTANT
addy.signingIndex = twoFlash.userIndex * digest.security
// Get the sum of all digest security to get address security sum
addy.securitySum = allDigests
.map(userDigests => userDigests[index])
.reduce((acc, v) => acc + v.security, 0)
// Add Security
addy.security = digest.security
return addy
})
console.log("Multisigs generated!")
//////////////////////////////////
// CONSUME & ORGANISE ADDRESSES FOR USE
// Set remainder address (Same on both users)
oneFlash.flash.remainderAddress = oneMultisigs.shift()
twoFlash.flash.remainderAddress = twoMultisigs.shift()
// Nest trees
for (let i = 1; i < oneMultisigs.length; i++) {
oneMultisigs[i - 1].children.push(oneMultisigs[i])
}
for (let i = 1; i < twoMultisigs.length; i++) {
twoMultisigs[i - 1].children.push(twoMultisigs[i])
}
// Set deposit address (Same on both users)
// NOTE: Checksum added so users can consume manually
// let depositAddress = iota.utils.addChecksum(multisigs[0].address)
// oneFlash.flash.depositAddress = depositAddress
// twoFlash.flash.depositAddress = depositAddress
console.log("deposit one address", oneMultisigs[0].address)
console.log("deposit two address", twoMultisigs[0].address)
// Set Flash root
oneFlash.flash.root = oneMultisigs.shift()
twoFlash.flash.root = twoMultisigs.shift()
// Set settlement addresses (Usually sent over when the digests are.)
let settlementAddresses = [oneSettlement, twoSettlement]
oneFlash.flash.settlementAddresses = settlementAddresses
twoFlash.flash.settlementAddresses = settlementAddresses
// Set digest/key index
oneFlash.index = oneFlash.partialDigests.length
twoFlash.index = twoFlash.partialDigests.length
console.log("Channel Setup!")
console.log(
"Transactable tokens: ",
oneFlash.flash.deposit.reduce((acc, v) => acc + v)
)
//////////////////////////////
////// TRANSACTING //////
//////////////////////////////
// COMPOSE TX from USER ONE
console.log("Creating Transaction")
console.log("Sending 200 tokens to ", twoSettlement)
// Create transfer array pointing to USER TWO
let transfers = [
{
value: 200,
address: twoSettlement
}
]
// Create TX
var bundles = Helpers.createTransaction(oneFlash, transfers, false)
/////////////////////////////////
/// SIGN BUNDLES
// Get signatures for the bundles
let oneSignatures = Helpers.signTransaction(oneFlash, bundles)
// Generate USER TWO'S Singatures
let twoSignatures = Helpers.signTransaction(twoFlash, bundles)
// Sign bundle with your USER ONE'S signatures
let signedBundles = transfer.appliedSignatures(bundles, oneSignatures)
// ADD USER TWOS'S signatures to the partially signed bundles
signedBundles = transfer.appliedSignatures(signedBundles, twoSignatures)
/////////////////////////////////
/// APPLY SIGNED BUNDLES
// Apply transfers to User ONE
oneFlash = Helpers.applyTransfers(oneFlash, signedBundles)
// Save latest channel bundles
oneFlash.bundles = signedBundles
// Apply transfers to User TWO
twoFlash = Helpers.applyTransfers(twoFlash, signedBundles)
// Save latest channel bundles
twoFlash.bundles = signedBundles
console.log("Transaction Applied!")
console.log(
"Transactable tokens: ",
oneFlash.flash.deposit.reduce((acc, v) => acc + v)
)
// TO DO: ADD 2 MORE TXs to demo branching
//////////////////////////////
// CLOSE Channel
// Supplying the CORRECT varibles to create a closing bundle
bundles = Helpers.createTransaction(
oneFlash,
oneFlash.flash.settlementAddresses,
true
)
/////////////////////////////////
/// SIGN BUNDLES
// Get signatures for the bundles
oneSignatures = Helpers.signTransaction(oneFlash, bundles)
// Generate USER TWO'S Singatures
twoSignatures = Helpers.signTransaction(twoFlash, bundles)
// Sign bundle with your USER ONE'S signatures
signedBundles = transfer.appliedSignatures(bundles, oneSignatures)
// ADD USER TWOS'S signatures to the partially signed bundles
signedBundles = transfer.appliedSignatures(signedBundles, twoSignatures)
/////////////////////////////////
/// APPLY SIGNED BUNDLES
// Apply transfers to User ONE
oneFlash = Helpers.applyTransfers(oneFlash, signedBundles)
// Save latest channel bundles
oneFlash.bundles = signedBundles
// Apply transfers to User TWO
twoFlash = Helpers.applyTransfers(twoFlash, signedBundles)
// Save latest channel bundles
twoFlash.bundles = signedBundles
console.log("Channel Closed")
console.log("Final Bundle to be attached: ")
console.log(signedBundles[0])