1 | import d from 'debug';
|
2 | import { tap } from './utils';
|
3 |
|
4 | const debug = d('LC:SignatureFactoryRunner');
|
5 |
|
6 | function _validateSignature(signatureResult = {}) {
|
7 | const { signature, timestamp, nonce } = signatureResult;
|
8 | if (
|
9 | typeof signature !== 'string' ||
|
10 | typeof timestamp !== 'number' ||
|
11 | typeof nonce !== 'string'
|
12 | ) {
|
13 | throw new Error('malformed signature');
|
14 | }
|
15 | return {
|
16 | signature,
|
17 | timestamp,
|
18 | nonce,
|
19 | };
|
20 | }
|
21 |
|
22 | export default (signatureFactory, params) =>
|
23 | Promise.resolve()
|
24 | .then(() => {
|
25 | debug('call signatureFactory with %O', params);
|
26 | return signatureFactory(...params);
|
27 | })
|
28 | .then(
|
29 | tap(signatureResult => debug('sign result %O', signatureResult)),
|
30 | error => {
|
31 |
|
32 | error.message = `sign error: ${error.message}`;
|
33 | debug(error);
|
34 | throw error;
|
35 | }
|
36 | )
|
37 | .then(_validateSignature);
|