UNPKG

922 BJavaScriptView Raw
1import d from 'debug';
2import { tap } from './utils';
3
4const debug = d('LC:SignatureFactoryRunner');
5
6function _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
22export 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 // eslint-disable-next-line no-param-reassign
32 error.message = `sign error: ${error.message}`;
33 debug(error);
34 throw error;
35 }
36 )
37 .then(_validateSignature);