From 6c7b4877240dd834be1d88c7aa340c4d3cf4662b Mon Sep 17 00:00:00 2001 From: apanizo Date: Wed, 22 Aug 2018 13:39:08 +0200 Subject: [PATCH] WA-521 Mock signatures service in localstorage --- src/utils/localStorage/signatures.js | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/utils/localStorage/signatures.js diff --git a/src/utils/localStorage/signatures.js b/src/utils/localStorage/signatures.js new file mode 100644 index 00000000..ab1fea62 --- /dev/null +++ b/src/utils/localStorage/signatures.js @@ -0,0 +1,32 @@ +// @flow +import { Map } from 'immutable' +import { load } from '~/utils/localStorage' + +const getSignaturesKeyFrom = (safeAddress: string) => `TXS-SIGNATURES-${safeAddress}` + +export const storeSignature = (safeAddress: string, nonce: number, signature: string) => { + const signaturesKey = getSignaturesKeyFrom(safeAddress) + const subjects = Map(load(signaturesKey)) || Map() + + try { + const key = `${nonce}` + const existingSignatures = subjects.get(key) + const signatures = existingSignatures ? existingSignatures + signature : signature + const updatedSubjects = subjects.set(key, signatures) + const serializedState = JSON.stringify(updatedSubjects) + localStorage.setItem(signaturesKey, serializedState) + } catch (err) { + // eslint-disable-next-line + console.log('Error storing signatures in localstorage') + } +} + +export const getSignaturesFrom = (safeAddress: string, nonce: number) => { + const key = getSignaturesKeyFrom(safeAddress) + const data: any = load(key) + + const signatures = data ? Map(data) : Map() + const txSigs = signatures.get(String(nonce)) || '' + + return `0x${txSigs}` +}