1 | const fastSha256 = require('fast-sha256');
|
2 | const secp256k1 = require('eccrypto-sync/secp256k1');
|
3 | const base64 = require('./base64');
|
4 | const base58 = require('./base58');
|
5 |
|
6 | module.exports = ({random, fetch, Headers, encodeUtf8, crdsUrl}) => {
|
7 |
|
8 | const NULL_PRIVATE_KEY = (() => {
|
9 | const result = new Uint8Array(32);
|
10 | result[0] = 0xFF;
|
11 | return result;
|
12 | })();
|
13 |
|
14 |
|
15 | const getPublicKey = privateKey => Uint8Array.from(secp256k1.keyFromPrivate(privateKey).getPublic('arr'));
|
16 | const getAddress = privateKey => base58.encode(_sha256(getPublicKey(privateKey)));
|
17 | const requestStatus = () => fetch(`${crdsUrl}/status`)
|
18 | .then(_resJson);
|
19 | const requestAssets = () => fetch(`${crdsUrl}/assets`)
|
20 | .then(_resJson);
|
21 | const requestBlockCache = () => fetch(`${crdsUrl}/blockcache`)
|
22 | .then(_resJson);
|
23 | const requestUnconfirmedBalances = address => fetch(`${crdsUrl}/unconfirmedBalances/${encodeURIComponent(address)}`)
|
24 | .then(_resJson);
|
25 | const requestUnconfirmedMinter = asset => fetch(`${crdsUrl}/unconfirmedMinter/${encodeURIComponent(asset)}`)
|
26 | .then(_resJson);
|
27 | const requestUnconfirmedPrice = asset => fetch(`${crdsUrl}/unconfirmedPrice/${encodeURIComponent(asset)}`)
|
28 | .then(_resJson);
|
29 | const requestMempool = () => fetch(`${crdsUrl}/mempool`)
|
30 | .then(_resJson);
|
31 | const requestMine = address => fetch(`${crdsUrl}/mine`, {
|
32 | method: 'POST',
|
33 | headers: (() => {
|
34 | const headers = new Headers();
|
35 | headers.append('Content-Type', 'application/json');
|
36 | return headers;
|
37 | })(),
|
38 | body: JSON.stringify({address}),
|
39 | })
|
40 | .then(_resJson);
|
41 | const requestMinedBlocks = () => fetch(`${crdsUrl}/minedBlocks`)
|
42 | .then(_resJson);
|
43 | const _requestSubmitMessage = message => fetch(`${crdsUrl}/submitMessage`, {
|
44 | method: 'POST',
|
45 | headers: (() => {
|
46 | const headers = new Headers();
|
47 | headers.append('Content-Type', 'application/json');
|
48 | return headers;
|
49 | })(),
|
50 | body: JSON.stringify(message),
|
51 | })
|
52 | .then(_resJson);
|
53 | const requestCreateSend = (asset, quantity, srcAddress, dstAddress, privateKey) => requestStatus()
|
54 | .then(status => {
|
55 | const {startHeight, timestamp} = status;
|
56 | const privateKeyBuffer = base64.decode(privateKey);
|
57 | const publicKey = getPublicKey(privateKeyBuffer);
|
58 | const publicKeyString = base64.encode(publicKey);
|
59 | const payload = JSON.stringify({type: 'send', startHeight, asset, quantity, srcAddress, dstAddress, publicKey: publicKeyString, timestamp});
|
60 | const payloadHash = _sha256(payload);
|
61 | const payloadHashString = _arrayToHex(payloadHash);
|
62 | const signature = Uint8Array.from(secp256k1.sign(payloadHash, privateKeyBuffer).toDER());
|
63 | const signatureString = base64.encode(signature);
|
64 | const message = {
|
65 | payload: payload,
|
66 | hash: payloadHashString,
|
67 | signature: signatureString,
|
68 | };
|
69 |
|
70 | return _requestSubmitMessage(message);
|
71 | });
|
72 | const requestCreateMinter = (asset, privateKey) => requestStatus()
|
73 | .then(status => {
|
74 | const {startHeight, timestamp} = status;
|
75 | const privateKeyBuffer = base64.decode(privateKey);
|
76 | const publicKey = getPublicKey(privateKeyBuffer);
|
77 | const publicKeyString = base64.encode(publicKey);
|
78 | const payload = JSON.stringify({type: 'minter', asset, publicKey: publicKeyString, startHeight, timestamp});
|
79 | const payloadBuffer = encodeUtf8(payload);
|
80 | const payloadHash = _sha256(payloadBuffer);
|
81 | const payloadHashString = _arrayToHex(payloadHash);
|
82 | const signature = Uint8Array.from(secp256k1.sign(payloadHash, privateKeyBuffer).toDER());
|
83 | const signatureString = base64.encode(signature);
|
84 | const message = {
|
85 | payload: payload,
|
86 | hash: payloadHashString,
|
87 | signature: signatureString,
|
88 | };
|
89 |
|
90 | return _requestSubmitMessage(message);
|
91 | });
|
92 | const requestCreateMint = (asset, quantity, privateKey) => requestStatus()
|
93 | .then(status => {
|
94 | const {startHeight, timestamp} = status;
|
95 | const privateKeyBuffer = base64.decode(privateKey);
|
96 | const publicKey = getPublicKey(privateKeyBuffer);
|
97 | const publicKeyString = base64.encode(publicKey);
|
98 | const payload = JSON.stringify({type: 'mint', asset, quantity, publicKey: publicKeyString, startHeight, timestamp});
|
99 | const payloadHash = _sha256(payload);
|
100 | const payloadHashString = _arrayToHex(payloadHash);
|
101 | const signature = Uint8Array.from(secp256k1.sign(payloadHash, privateKeyBuffer).toDER());
|
102 | const signatureString = base64.encode(signature);
|
103 | const message = {
|
104 | payload: payload,
|
105 | hash: payloadHashString,
|
106 | signature: signatureString,
|
107 | };
|
108 |
|
109 | return _requestSubmitMessage(message);
|
110 | });
|
111 | const requestCreateGet = (address, asset, quantity) => requestStatus()
|
112 | .then(status => {
|
113 | const {startHeight, timestamp} = status;
|
114 | const payload = JSON.stringify({type: 'get', address, asset, quantity, startHeight, timestamp});
|
115 | const payloadHash = _sha256(payload);
|
116 | const payloadHashString = _arrayToHex(payloadHash);
|
117 | const signature = Uint8Array.from(secp256k1.sign(payloadHash, NULL_PRIVATE_KEY).toDER());
|
118 | const signatureString = base64.encode(signature);
|
119 | const message = {
|
120 | payload: payload,
|
121 | hash: payloadHashString,
|
122 | signature: signatureString,
|
123 | };
|
124 |
|
125 | return _requestSubmitMessage(message);
|
126 | });
|
127 | const requestCreateBurn = (asset, quantity, privateKey) => requestStatus()
|
128 | .then(status => {
|
129 | const {startHeight, timestamp} = status;
|
130 | const privateKeyBuffer = base64.decode(privateKey);
|
131 | const publicKey = getPublicKey(privateKeyBuffer);
|
132 | const publicKeyString = base64.encode(publicKey);
|
133 | const payload = JSON.stringify({type: 'burn', asset, quantity, publicKey: publicKeyString, startHeight, timestamp});
|
134 | const payloadHash = _sha256(payload);
|
135 | const payloadHashString = _arrayToHex(payloadHash);
|
136 | const signature = Uint8Array.from(secp256k1.sign(payloadHash, privateKeyBuffer).toDER());
|
137 | const signatureString = base64.encode(signature);
|
138 | const message = {
|
139 | payload: payload,
|
140 | hash: payloadHashString,
|
141 | signature: signatureString,
|
142 | };
|
143 |
|
144 | return _requestSubmitMessage(message);
|
145 | });
|
146 | const requestCreateDrop = (address, asset, quantity) => requestStatus()
|
147 | .then(status => {
|
148 | const {startHeight, timestamp} = status;
|
149 | const payload = JSON.stringify({type: 'drop', address, asset, quantity, startHeight, timestamp});
|
150 | const payloadHash = _sha256(payload);
|
151 | const payloadHashString = _arrayToHex(payloadHash);
|
152 | const signature = Uint8Array.from(secp256k1.sign(payloadHash, NULL_PRIVATE_KEY).toDER());
|
153 | const signatureString = base64.encode(signature);
|
154 | const message = {
|
155 | payload: payload,
|
156 | hash: payloadHashString,
|
157 | signature: signatureString,
|
158 | };
|
159 |
|
160 | return _requestSubmitMessage(message);
|
161 | });
|
162 | const requestCreatePrice = (asset, price, privateKey) => requestStatus()
|
163 | .then(status => {
|
164 | const {startHeight, timestamp} = status;
|
165 | const privateKeyBuffer = base64.decode(privateKey);
|
166 | const publicKey = getPublicKey(privateKeyBuffer);
|
167 | const publicKeyString = base64.encode(publicKey);
|
168 | const payload = JSON.stringify({type: 'price', asset, price, publicKey: publicKeyString, startHeight, timestamp});
|
169 | const payloadBuffer = encodeUtf8(payload);
|
170 | const payloadHash = _sha256(payloadBuffer);
|
171 | const payloadHashString = _arrayToHex(payloadHash);
|
172 | const signature = Uint8Array.from(secp256k1.sign(payloadHash, privateKeyBuffer).toDER());
|
173 | const signatureString = base64.encode(signature);
|
174 | const message = {
|
175 | payload: payload,
|
176 | hash: payloadHashString,
|
177 | signature: signatureString,
|
178 | };
|
179 |
|
180 | return _requestSubmitMessage(message);
|
181 | });
|
182 | const requestCreateBuy = (asset, quantity, price, privateKey) => requestStatus()
|
183 | .then(status => {
|
184 | const {startHeight, timestamp} = status;
|
185 | const privateKeyBuffer = base64.decode(privateKey);
|
186 | const publicKey = getPublicKey(privateKeyBuffer);
|
187 | const publicKeyString = base64.encode(publicKey);
|
188 | const payload = JSON.stringify({type: 'buy', asset, quantity, price, publicKey: publicKeyString, startHeight, timestamp});
|
189 | const payloadBuffer = encodeUtf8(payload);
|
190 | const payloadHash = _sha256(payloadBuffer);
|
191 | const payloadHashString = _arrayToHex(payloadHash);
|
192 | const signature = Uint8Array.from(secp256k1.sign(payloadHash, privateKeyBuffer).toDER());
|
193 | const signatureString = base64.encode(signature);
|
194 | const message = {
|
195 | payload: payload,
|
196 | hash: payloadHashString,
|
197 | signature: signatureString,
|
198 | };
|
199 |
|
200 | return _requestSubmitMessage(message);
|
201 | });
|
202 |
|
203 | const _resJson = res => {
|
204 | if (res.status >= 200 && res.status < 300) {
|
205 | return res.json();
|
206 | } else {
|
207 | return Promise.reject({
|
208 | status: res.status,
|
209 | stack: 'API returned failure status code: ' + res.status,
|
210 | });
|
211 | }
|
212 | };
|
213 | const _arrayToHex = array => {
|
214 | let result = '';
|
215 | for (let i = 0; i < array.byteLength; i++) {
|
216 | const n = array[i];
|
217 | result += ('00' + n.toString(16)).slice(-2);
|
218 | }
|
219 | return result;
|
220 | };
|
221 | const _sha256 = o => {
|
222 | if (typeof o === 'string') {
|
223 | o = encodeUtf8(o);
|
224 | }
|
225 | return fastSha256(o);
|
226 | };
|
227 |
|
228 | return {
|
229 | getPublicKey,
|
230 | getAddress,
|
231 | requestStatus,
|
232 | requestAssets,
|
233 | requestBlockCache,
|
234 | requestUnconfirmedBalances,
|
235 | requestUnconfirmedMinter,
|
236 | requestUnconfirmedPrice,
|
237 | requestMempool,
|
238 | requestMine,
|
239 | requestMinedBlocks,
|
240 | requestCreateSend,
|
241 | requestCreateMinter,
|
242 | requestCreateMint,
|
243 | requestCreateGet,
|
244 | requestCreateBurn,
|
245 | requestCreateDrop,
|
246 | requestCreatePrice,
|
247 | requestCreateBuy,
|
248 | };
|
249 |
|
250 | };
|