-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstellation.js
141 lines (118 loc) · 5.09 KB
/
constellation.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
/* global require, module */
(function () {
'use strict';
const axios = require('axios');
const EventSource = require('eventsource');
const network = {
live: '7ac33997',
testnet: 'cee0302d'
};
/**
*
* @module constellation
* @type {{Server: Server}}
*/
module.exports = {
Network: network,
/**
*
* @param {String} [url] - url to a constellation server
* @returns {{submitSignatures: submitSignatures, submitTransaction: submitTransaction, subscribe: subscribe}}
* @constructor
*/
Server: function (url) {
const baseUrl = url || 'https://constellation.futuretense.io/api/v1';
return {
submitSignatures: submitSignatures,
submitTransaction: submitTransaction,
subscribe: subscribe
};
/**
* Submit signatures to the signature server
*
* @param {String} hash - transaction hash for the transaction to sign
* @param {DecoratedSignature[]} sigs - an array of DecoratedSignatures
* @returns {Promise}
*/
function submitSignatures(hash, sigs) {
const data = {
sig: sigs.map(encodeSignature)
};
return axios.put(`${baseUrl}/transaction/${hash}`, data);
}
/**
* Submit a transaction to the signature server
*
* @param {Transaction} tx - transaction
* @param {String} [networkId] - network
* @returns {Promise}
*/
function submitTransaction(tx, networkId) {
const data = {
txenv: encodeTransaction(tx),
network: networkId || network.live
};
return axios.post(`${baseUrl}/transaction`, data);
}
/**
* Subscribe to push notifications for a list of public keys.
*
* @param {String[]} pubkeys - an array of public keys
* @param {requestFunc} requestFunc - handler of request events
* @param {progressFunc} progressFunc - handler of progress events
* @param {addSignerFunc} addSignerFunc - handler of add account events
* @param {removeSignerFunc} removeSignerFunc - handler of remove account events
* @returns {EventSource}
*/
function subscribe(pubkeys, requestFunc, progressFunc, addSignerFunc, removeSignerFunc) {
const eventSource = new EventSource(`${baseUrl}/events/${pubkeys}`);
eventSource.addEventListener('request', handler(requestFunc), false);
eventSource.addEventListener('progress', handler(progressFunc), false);
eventSource.addEventListener('add_signer', handler(addSignerFunc), false);
eventSource.addEventListener('remove_signer', handler(removeSignerFunc), false);
return eventSource;
function handler(func) {
if (func) {
return event => func(JSON.parse(event.data));
} else {
return event => {};
}
}
}
/**
* @callback requestFunc
* @param {Object} payload
* @param {String[]} payload.id - a list of recipient pubkeys for this request
* @param {String} payload.txenv - the transaction to be signed
* @param {String} payload.network - the Stellar network that the transaction is on
* @param {Object} payload.progress - signing progress
*/
/**
* @callback progressFunc
* @param {Object} payload
* @paran {String} payload.hash - the transaction hash of the transaction being signed
* @param {Object} payload.progress - signing progress
*/
/**
* @callback addSignerFunc
* @param {Object} payload
* @param {String} payload.id - the recipient pubkey for this request
* @param {String} payload.account - the account that recipient has been added as a signer to
* @param {String} payload.network - the Stellar network that the account is on
*/
/**
* @callback removeSignerFunc
* @param {Object} payload
* @param {String} payload.id - the recipient pubkey for this request
* @param {String} payload.account - the account that recipient has been removed as a signer from
* @param {String} payload.network - the Stellar network that the account is on
*/
}
};
function encodeSignature(sig) {
return sig.toXDR().toString('base64');
}
function encodeTransaction(tx) {
return tx.toEnvelope().toXDR().toString('base64');
}
})();