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
/
functions.js
79 lines (75 loc) · 2.25 KB
/
functions.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
////////////////////////////////////////////////
////////////// FLASH EXAMPLE /////////////////
////////////////////////////////////////////////
// This provides access to basic functions /////
// required for the channels use. /////
////////////////////////////////////////////////
const IOTACrypto = require("iota.crypto.js")
const transfer = require("../lib/transfer")
const multisig = require("../lib/multisig")
const createTransaction = (user, actions, close) => {
//////////////////////////////
/// Check for a Branch
// From the LEAF recurse up the tree to the ROOT
// and find how many new addresses need to be
// generated if any.
let toUse = multisig.updateLeafToRoot(user.flash.root)
if (toUse.generate != 0) {
// Tell the server to generate new addresses, attach to the multisig you give
// await Channel.getNewBranch(toUse.multisig, toUse.generate)
}
/////////////////////////////////
/// CONSTRUCT BUNDLES
let bundles
let newTansfers
try {
// Check if its closing the channel
if (!close) {
// Prepare the transfer.
newTansfers = transfer.prepare(
user.flash.settlementAddresses,
user.flash.deposit,
user.userIndex,
actions
)
} else {
// Distribute the remaining channel balance amongst the channel users
// NOTE: YOU MUST PASS THE SETTLEMENT ADDRESSES ARRAY as 'actions'
newTansfers = transfer.close(actions, user.flash.deposit)
}
// Compose the transfer bundles
bundles = transfer.compose(
user.flash.balance,
user.flash.deposit,
user.flash.outputs,
toUse.multisig,
user.flash.remainderAddress,
user.flash.transfers,
newTansfers,
close
)
} catch (e) {
console.log("Error: ", e)
return false
}
return bundles
}
const signTransaction = (user, bundles) => {
return transfer.sign(user.flash.root, user.userSeed, bundles)
}
const applyTransfers = (user, bundles) => {
transfer.applyTransfers(
user.flash.root,
user.flash.deposit,
user.flash.outputs,
user.flash.remainderAddress,
user.flash.transfers,
bundles
)
return user
}
module.exports = {
createTransaction: createTransaction,
signTransaction: signTransaction,
applyTransfers: applyTransfers
}