-
Notifications
You must be signed in to change notification settings - Fork 20
/
SEP10.js
66 lines (54 loc) · 1.89 KB
/
SEP10.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
const fetch = require('node-fetch');
const StellarSdk = require('stellar-sdk');
const querystring = require('querystring');
const networkPassphrase = 'Public Global Stellar Network ; September 2015';
const url = 'https://api.cowrie.exchange/web_auth';
module.exports = {
challenge: (account) => {
var query = querystring.stringify({ account: account.publicKey()});
var api = url + '?' + query;
console.log(api);
var signed =
fetch(api, { method: 'GET' }
).then((response) => {
console.log('HTTP: ' + response.status);
if(response.status == 200) {
return response.json();
}
else {
throw response.ststausText;
}
}).then((body) => {
//console.log('body: ' + JSON.stringify(body));
var transaction = StellarSdk.TransactionBuilder.fromXDR(body.transaction, networkPassphrase);
transaction.sign(account);
return transaction.toXDR();
}).catch((error) => {
console.log(error);
});
return signed;
},
token: (transaction) => {
var post = JSON.stringify({ transaction: transaction });
var api = url;
console.log(api);
console.log(post);
var jwt =
fetch(api, { method: 'POST', headers: {'Content-Type': 'application/json'}, body: post }
).then((response) => {
console.log('HTTP: ' + response.status);
if(response.status == 200) {
return response.json();
}
else {
throw response.ststausText;
}
}).then((body) => {
//console.log('body: ' + JSON.stringify(body));
return body.token;
}).catch((error) => {
console.log(error);
});
return jwt;
}
}