-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkinWrapper.js
295 lines (237 loc) · 8.43 KB
/
kinWrapper.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
const KinClient = require('@kinecosystem/kin-sdk-node').KinClient;
const Environment = require('@kinecosystem/kin-sdk-node').Environment;
const Channels = require('@kinecosystem/kin-sdk-node').Channels;
function KinWrapper(seed, salt, production, appId) {
/**
* @param {String} seed - private key of your account
* @param {Boolean} production - true for production environment
* @param {String} appId - unique app id provided by the Kin foundation
*/
//Safety in case user forgets 'new' keyword
if (!(this instanceof KinWrapper)) {
return new KinWrapper(seed);
}
//this can only work with a seed and salt
if (typeof seed === 'undefined') {
throw new Error('seed not defined when instantiating object');
}
if (typeof salt === 'undefined') {
throw new Error('salt not defined when instantiating object');
}
if (typeof appId === 'undefined') {
appId = ''; //will default to anon on the blockchain
}
if (!production) {
this.environment = Environment.Testnet;
} else {
this.environment = Environment.Production;
}
this.seed = seed;
this.salt = salt;
this.client = new KinClient(this.environment);
this.channelKeyPairs = this.getChannelKeyPairs();
this.account = this.getAccount(seed, this.client, appId);
this.channels = Channels;
this.fee = 100; //todo: fetch this dynamically
}
KinWrapper.prototype.CreateChannels = function (callBack) {
/**
*Create and fund our channel wallets and only needs to run once. After that we can use the
*generateSeeds function to re-generate the channel keypairs as long as the seed and salt are the same.
*@param {Function} callback (err, channels)
*/
if (!isFunction(callBack)) callBack = function () { };
this.channels.createChannels({
environment: this.environment,
baseSeed: this.seed,
salt: this.salt,
channelsCount: 100,
startingBalance: 1 //nominal amount. For whitelisted accounts, this does not get used up
}).then(() => {
callBack(null, true);
}).catch(err => {
callBack(err);
});
};
KinWrapper.prototype.friendbot = function (address, amount, callBack) {
/**
*Return the transaction details of a transactionId
* @param {String} address - address of account
* @param {Number} amount - amount to fund
* @param {Function} callBack - callback (err, transactionId)
*/
if (!isFunction(callBack)) callBack = function () { };
this.client.friendbot({
address: address,
amount: amount
}).then(transactionId => {
callBack(null, transactionId);
}).catch((err) => {
callBack(err);
});
};
KinWrapper.prototype.getTransactionData = function (transactionId, callBack) {
/**
*Return the transaction details of a transactionId
* @param {String} transactionId - transactionId of account
* @param {Function} callBack - callback (err, number)
*/
if (!isFunction(callBack)) callBack = function () { };
this.client.getTransactionData(transactionId)
.then(transactionData => {
callBack(null, transactionData);
}).catch((err) => {
callBack(err);
});
};
KinWrapper.prototype.getAccountData = function (address, callBack) {
/**
*Return the account balance
* @param {String} address - address of account
* @param {Function} callBack - callback (err, number)
*/
if (!isFunction(callBack)) callBack = function () { };
this.client.getAccountData(address)
.then(accountData => {
callBack(null, accountData);
}).catch((err) => {
callBack(err);
});
};
KinWrapper.prototype.getAccountBalance = function (address, callBack) {
/**
*Return the account balance
* @param {String} address - address of account
* @param {Function} callBack - callback (err, number)
*/
if (!isFunction(callBack)) callBack = function () { };
this.client.getAccountBalance(address)
.then(balance => {
callBack(null, balance);
}).catch((err) => {
callBack(err);
});
};
KinWrapper.prototype.getMinimumFee = function (callBack) {
/**
*Return the minimum fee
* @param {Function} callBack - callback (err, number)
*/
if (!isFunction(callBack)) callBack = function () { };
this.client.getMinimumFee()
.then(minFee => {
callBack(null, minFee);
}).catch((err) => {
callBack(err);
});
};
KinWrapper.prototype.isAccountExisting = function (address, callBack) {
/**
*Return if an account exists on the blockchain
* @param {String} address - address of account
* @param {Function} callBack - callback (err, bool)
*/
if (!isFunction(callBack)) callBack = function () { };
this.client.isAccountExisting(address)
.then(exist => {
callBack(null, exist);
}).catch((err) => {
callBack(err);
});
};
KinWrapper.prototype.sendKin = function (destination, amount, memoText, callBack) {
/**
* Send Kin to an account
* @param {String} destination - address of account
* @param {Number} amount - amount to send
* @param {String} memoText - optional memo
* @param {Function} callBack - callback (err, bool)
*/
if (!isFunction(callBack)) callBack = function () { };
this.account.channelsPool.acquireChannel(async channel => {
this.account.buildSendKin({
address: destination,
amount: amount,
fee: this.fee,
memoText: memoText,
channel: channel
}).then(builder => {
//use the builder to submit the transaction to the blockchain
this.account.submitTransaction(builder).then(transactionId => {
callBack(null, transactionId);
}).catch((err) => {
callBack(err);
});
}).catch((err) => {
callBack(err);
});
}).catch((err) => {
callBack(err);
});
};
KinWrapper.prototype.createAccount = function (address, startingBalance, memoText, callBack) {
/**
* Create an account on the blockchain
* @param {String} address - address of account
* @param {Number} startingBalance - starting balance of account
* @param {String} memoText - optional memo
* @param {Function} callBack - callback (err, bool)
*/
if (!isFunction(callBack)) callBack = function () { };
if (typeof memoText === 'undefined') memoText = '';
if (memoText.length > 21) callback('memo too long - shorten to 21 characters:' + memoText);
this.account.channelsPool.acquireChannel(async channel => {
this.account.buildCreateAccount({
address: address,
startingBalance: startingBalance,
fee: this.fee,
memoText: memoText, //a text memo can also be added; memos cannot exceed 21 characters
channel: channel
}).then(builder => {
this.account.submitTransaction(builder).then(transactionId => {
callBack(null, transactionId);
}).catch((err) => {
callBack(err);
});
}).catch((err) => {
callBack(err);
});
}).catch((err) => {
callBack(err);
});
};
KinWrapper.prototype.getAccount = function (seed, client, appId) {
/**
* Create an account on the blockchain
* @param {String} seed - seed/ private key
* @param {Object} client - kin client
* @param {String} appId - appId
*/
let account = client.createKinAccount({
seed: seed,
appId: appId,
//Mapping the keypair array to a seed array because we only need the seeds
channelSecretKeys: this.channelKeyPairs.map(function (keypair) {
return (keypair.seed);
})
});
return (account);
};
KinWrapper.prototype.getChannelKeyPairs = function () {
/**
* Return channel key/pairs
*Regenerage created channels for use
*generateSeeds function to re-generate the channel keypairs as long as the seed and salt are the same.
*/
const generatedChannelKeypairs = Channels.generateSeeds({
baseSeed: this.seed,
salt: this.salt,
channelsCount: 100
});
return (generatedChannelKeypairs);
};
//helper functions below
function isFunction(possibleFunction) {
return typeof (possibleFunction) === typeof (Function);
}
module.exports = KinWrapper;