-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdk.js
327 lines (287 loc) · 9.49 KB
/
sdk.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
'use strict';
const P = require('bluebird');
const bs58check = require('bs58check');
const crypto = require('crypto');
const elliptic = require('elliptic');
const request = require('request');
const ripemd160 = require('ripemd160');
const urlModule = require('url');
module.exports = {
generate: {
address: generateWalletAddress,
currency: generateCurrencyIssuer,
transaction: generateTransactionRequest
},
register: {
address: registerWalletAddress,
currency: registerCurrencyIssuer,
transaction: registerTransactionRequest
},
check: {
address: {
balance: checkAddressBalance
}
}
};
/*--------------------------------------------------------------------------------*/
/** Generates and new key pair, derives an address and a signs a registration statement
* @param {Object} options - Cryptographic scheme and address type
* @return {Object} wallet - Wallet address, public/private keys and a signed statement
*/
function generateWalletAddress(options) {
options = options || {};
let keys = generateKeyPair(options.scheme || 'ed25519');
let address = deriveWalletAddress(keys.public, options.type);
let statement = createAddressRegistrationStatement(address, keys, options);
let wallet = {
address: address,
keys: keys,
statement: statement
};
return wallet;
}
/** Generates a new key pair, derives a currency issuer address and signs a registration statement
* @param {Object} options - Cryptographic scheme and address type
* @return {Object} - Currency issuer address, public/private keys and a signed statement
*/
function generateCurrencyIssuer(options) {
options = options || {};
options.type = 'issuer';
return generateWalletAddress(options);
}
/** Generates a transaction request document in the form of an IOU
* @param {Object} signer - Wallet whose private key is to be used for signing the IOU
* @param {Object} transaction - Transaction parameters
* @param {Object} options
* @return {Object} - A cryptographically signed IOU
*/
function generateTransactionRequest(signer, transaction, options) {
let iou = {
amt: transaction.amount,
cur: transaction.currency,
sub: signer.address,
aud: transaction.destination,
nce: String(Math.floor(Math.random() * 1000000000))
};
return createTransactionRequestStatement(signer, iou, options);
}
/** Sends a wallet address registration request to the supplied URL
* @param {string} url - The URL of a webwallet server
* @param {Object} body - A wallet address registration statement
* @param {Object} options - Request parameters such as method and headers
* @return {Promise} - Resolves to the response body
*/
function registerWalletAddress(url, body, options) {
options = options || {};
url = resolveURL(url, '/address');
return new P(function (resolve, reject) {
request({
url: url,
method: options.method || 'POST',
body: body,
json: true,
headers: options.headers
}, function (err, res, body) {
if (err) return reject(err);
return resolve(body);
});
});
}
/** Sends a currency issuer registration request to the supplied URL
* @param {string} url - The URL of a webwallet server
* @param {Object} body - A currency issuer registration statement
* @param {Object} options - Request parameters such as method and headers
* @return {Promise} - Resolves to the response body
*/
function registerCurrencyIssuer(url, body, options) {
options = options || {};
url = resolveURL(url, '/currency');
return new P(function (resolve, reject) {
request({
url: url,
method: options.method || 'POST',
body: body,
json: true,
headers: options.headers
}, function (err, res, body) {
if (err) return reject(err);
return resolve(body);
});
});
}
/** Sends a transaction request to the supplied URL
* @param {string} url - The URL of a webwallet server
* @param {Object} body - A transaction request statement
* @param {Object} options - Request parameters such as method and headers
* @return {Promise} - Resolves to the response body
*/
function registerTransactionRequest(url, body, options) {
options = options || {};
url = resolveURL(url, '/transaction');
return new P(function (resolve, reject) {
request({
url: url,
method: options.method || 'POST',
body: body,
json: true,
headers: options.headers
}, function (err, res, body) {
if (err) return reject(err);
return resolve(body);
});
});
}
/** Checks the balance of a wallet address
* @param {string} url - The URL of a webwallet server
* @param {string} address - A wallet address
* @param {Object} options - Request parameters such as headers
* @return {Promise} - Resolves to the response body
*/
function checkAddressBalance(url, address, options) {
options = options || {};
let path = '/address/.../balance'.replace('...', address);
url = resolveURL(url, path);
return new P(function (resolve, reject) {
request({
url: url,
method: 'GET',
headers: options.headers
}, function (err, res, body) {
if (err) return reject(err);
return resolve(body);
});
});
}
/*--------------------------------------------------------------------------------*/
const hashes = ['sha256', 'sha512'];
const schemes = {
'ed25519': new elliptic.ec('ed25519'),
'secp256k1': new elliptic.ec('secp256k1')
};
/** Generates a pair of cryptographic keys
* @param {string} scheme - A cryptographic scheme
* @return {Object} keys - A cryptographic key pair
*/
function generateKeyPair(scheme) {
let keypair;
let keys;
switch (scheme) {
case 'ed25519':
case 'secp256k1':
keypair = schemes[scheme].genKeyPair();
keys = {
scheme: scheme,
private: keypair.getPrivate('hex'),
public: keypair.getPublic('hex')
};
break;
default:
throw new Error('invalid-scheme');
break;
}
return keys;
}
/** Derives a wallet address from a public key
* @param {<type>} publicKey - A public key to derive the address from
* @param {string} type - The type of wallet address to derive
* @return {string} base58public - A base58check encoded wallet address
*/
function deriveWalletAddress(publicKey, type) {
let keyBuffer = new Buffer(publicKey, 'hex');
let firstHash = crypto.createHash('sha256').update(keyBuffer).digest();
let secondHash = ripemd160(firstHash);
let extendedHash = (type === 'issuer' ? '57' : '87') + secondHash.toString('hex');
let base58Public = bs58check.encode(new Buffer(extendedHash, 'hex'));
return base58Public;
}
/** Creates and signs a statement to be sent for registering an address
* @param {string} address - A wallet address
* @param {Object} keys - An object containing a cryptograhic key pair
* @param {Object} options - Parameters such as hash type
* @return {Object} - A cryptographically signed statement
*/
function createAddressRegistrationStatement(address, keys, options) {
options = options || {};
/* Create an extended JSON Web Signatures object */
let jws = {
hash: {
type: (hashes.indexOf(options.hash) > -1) ? options.hash : 'sha256',
value: ''
},
payload: {
address: address,
keys: [
keys.public
],
threshold: 1
},
signatures: [
{
header: {
alg: keys.scheme,
kid: '0'
},
signature: ''
}
]
};
/* Generate a cryptogrphic hash of the payload */
jws.hash.value = crypto.createHash(jws.hash.type)
.update(JSON.stringify(jws.payload)).digest('hex');
/* Sign the hash of the payload */
jws.signatures[0].signature = schemes[keys.scheme]
.sign(jws.hash.value, keys.private, 'hex').toDER('hex');
return jws;
}
/** Creates a transaction request statement in the form of an IOU
* @param {Object} signer - Wallet whose private key is to be used for signing the IOU
* @param {Object} iou - The IOU to sign
* @param {Object} options - Parameters such as hash type
* @return {Object} - A cryptographically signed IOU
*/
function createTransactionRequestStatement(signer, iou, options) {
options = options || {};
/* Create an extended JSON Web Signatures object */
let jws = {
hash: {
type: (hashes.indexOf(options.hash) > -1) ? options.hash : 'sha256',
value: ''
},
payload: iou,
signatures: [
{
header: {
alg: signer.keys.scheme,
kid: signer.address
},
signature: ''
}
]
};
/* Generate a cryptogrphic hash of the payload */
jws.hash.value = crypto.createHash(jws.hash.type)
.update(JSON.stringify(jws.payload)).digest('hex');
/* Sign the hash of the payload */
jws.signatures[0].signature = schemes[signer.keys.scheme]
.sign(jws.hash.value, signer.keys.private, 'hex').toDER('hex');
return jws;
}
const protocols = ['http', 'https', 'http:', 'https:'];
/** Resolves a URL given a path
* @param {string} url - URL to resolve
* @param {string} path - Path to append to base URL
* @return {string} resolvedUrl - A valid URL
*/
function resolveURL(url, path) {
if (typeof url !== 'string' || typeof path !== 'string') {
return new Error('url-and-path-required');
}
/* Remove leading and duplicate slashes */
url = url.replace(/\/{2,}/g, '/').replace(/^\//, '').replace(':/','://');
let parsedUrl = urlModule.parse(url);
if (protocols.indexOf(parsedUrl.protocol) < 0 || !parsedUrl.host) {
return new Error('invalid-url');
}
let resolvedUrl = urlModule.resolve(parsedUrl.href, path);
return resolvedUrl;
}