Skip to content

Commit

Permalink
feat(suite): #4512 add AOPP support to reducers
Browse files Browse the repository at this point in the history
  • Loading branch information
marekrjpolak committed Nov 29, 2021
1 parent ba24f33 commit fb0064d
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 2 deletions.
@@ -1,3 +1,5 @@
export const FILL_SEND_FORM = '@protocol/fill-send-form';
export const SAVE_COIN_PROTOCOL = '@protocol/save-coin-protocol';
export const FILL_AOPP = '@protocol/fill-aopp';
export const SAVE_AOPP_PROTOCOL = '@protocol/save-aopp-protocol';
export const RESET = '@protocol/reset';
20 changes: 19 additions & 1 deletion packages/suite/src/actions/suite/protocolActions.ts
@@ -1,15 +1,23 @@
import { PROTOCOL } from './constants';
import { PROTOCOL_SCHEME } from '@suite-reducers/protocolReducer';
import { AoppState, PROTOCOL_SCHEME } from '@suite-reducers/protocolReducer';

export type ProtocolAction =
| {
type: typeof PROTOCOL.FILL_SEND_FORM;
payload: boolean;
}
| {
type: typeof PROTOCOL.FILL_AOPP;
payload: boolean;
}
| {
type: typeof PROTOCOL.SAVE_COIN_PROTOCOL;
payload: { scheme: PROTOCOL_SCHEME; address: string; amount?: number };
}
| {
type: typeof PROTOCOL.SAVE_AOPP_PROTOCOL;
payload: AoppState;
}
| { type: typeof PROTOCOL.RESET };

export const fillSendForm = (shouldFillSendForm: boolean): ProtocolAction => ({
Expand All @@ -26,6 +34,16 @@ export const saveCoinProtocol = (
payload: { scheme, address, amount },
});

export const fillAopp = (shouldFill: boolean): ProtocolAction => ({
type: PROTOCOL.FILL_AOPP,
payload: shouldFill,
});

export const saveAoppProtocol = (payload: AoppState): ProtocolAction => ({
type: PROTOCOL.SAVE_AOPP_PROTOCOL,
payload,
});

export const resetProtocol = (): ProtocolAction => ({
type: PROTOCOL.RESET,
});
22 changes: 22 additions & 0 deletions packages/suite/src/reducers/suite/protocolReducer.ts
Expand Up @@ -5,6 +5,7 @@ import type { Network } from '@wallet-types';

export enum PROTOCOL_SCHEME {
BITCOIN = 'bitcoin',
AOPP = 'aopp',
}

export const PROTOCOL_TO_SYMBOL: { [key: string]: Network['symbol'] } = {
Expand All @@ -18,12 +19,23 @@ export interface SendFormState {
shouldFillSendForm?: boolean;
}

export interface AoppState {
message: string;
asset: Network['symbol'];
callback?: string;
format?: string;
}

export interface State {
sendForm: SendFormState;
aopp: Partial<AoppState> & {
shouldFill?: boolean;
};
}

export const initialState: State = {
sendForm: {},
aopp: {},
};

const protocolReducer = (state: State = initialState, action: Action): State =>
Expand All @@ -38,6 +50,16 @@ const protocolReducer = (state: State = initialState, action: Action): State =>
draft.sendForm.amount = action.payload.amount;
draft.sendForm.shouldFillSendForm = false;
break;
case PROTOCOL.FILL_AOPP:
draft.aopp.shouldFill = action.payload;
break;
case PROTOCOL.SAVE_AOPP_PROTOCOL:
draft.aopp.message = action.payload.message;
draft.aopp.callback = action.payload.callback;
draft.aopp.asset = action.payload.asset;
draft.aopp.format = action.payload.format;
draft.aopp.shouldFill = false;
break;
case PROTOCOL.RESET:
return initialState;
// no default
Expand Down
36 changes: 36 additions & 0 deletions packages/suite/src/utils/suite/__fixtures__/parseUri.ts
Expand Up @@ -130,6 +130,42 @@ export const getProtocolInfo = [
amount: undefined,
},
},
{
description: 'valid AOPP uri',
uri: 'aopp:?v=0&msg=MESSAGE&asset=btc&format=any&callback=https%3A%2F%2Ftesting.21analytics.ch%2Fproofs%2Fc220a28e-0e99-4be6-8578-a886f628ee20',
result: {
scheme: 'aopp',
v: '0',
msg: 'MESSAGE',
asset: 'btc',
format: 'any',
callback: 'https://testing.21analytics.ch/proofs/c220a28e-0e99-4be6-8578-a886f628ee20',
},
},
{
description: 'valid AOPP uri with invalid callback',
uri: 'aopp:?v=0&msg=MESSAGE&asset=btc&format=any&callback=a',
result: {
scheme: 'aopp',
v: '0',
msg: 'MESSAGE',
asset: 'btc',
format: 'any',
callback: undefined,
},
},
{
description: 'valid AOPP uri, callback with slashes',
uri: 'aopp:?v=0&msg=MESSAGE&asset=btc&format=any&callback=https://foo.bar',
result: {
scheme: 'aopp',
v: '0',
msg: 'MESSAGE',
asset: 'btc',
format: 'any',
callback: 'https://foo.bar',
},
},
{
description: 'invalid uri',
uri: 'gibberish',
Expand Down
27 changes: 26 additions & 1 deletion packages/suite/src/utils/suite/parseUri.ts
@@ -1,4 +1,6 @@
import { PROTOCOL_SCHEME } from '@suite-reducers/protocolReducer';
import { isNetworkSymbol } from '@wallet-utils/accountUtils';
import type { Network } from '@wallet-types';

// Parse URL query string (like 'foo=bar&baz=1337) into an object
export const parseQuery = (uri: string) => {
Expand Down Expand Up @@ -29,7 +31,16 @@ export type BitcoinProtocolInfo = {
amount?: number;
};

export const getProtocolInfo = (uri: string): BitcoinProtocolInfo | null => {
export type AoppProtocolInfo = {
scheme: PROTOCOL_SCHEME.AOPP;
msg: string;
asset: Network['symbol'];
v?: string;
format?: string;
callback?: string;
};

export const getProtocolInfo = (uri: string): BitcoinProtocolInfo | AoppProtocolInfo | null => {
const url = parseUri(uri);
if (!url) return null;

Expand All @@ -49,5 +60,19 @@ export const getProtocolInfo = (uri: string): BitcoinProtocolInfo | null => {
};
}

if (scheme === PROTOCOL_SCHEME.AOPP) {
if (!params.msg) return null;
if (!params.asset || !isNetworkSymbol(params.asset)) return null;
const validCallback = parseUri(params.callback ?? '');
return {
scheme,
v: params.v,
asset: params.asset,
format: params.format,
msg: params.msg,
callback: validCallback ? params.callback : undefined,
};
}

return null;
};
3 changes: 3 additions & 0 deletions packages/suite/src/utils/wallet/accountUtils.ts
Expand Up @@ -245,6 +245,9 @@ export const getSelectedAccount = (

export const getNetwork = (symbol: string) => NETWORKS.find(c => c.symbol === symbol) || null;

export const isNetworkSymbol = (symbol: string): symbol is Network['symbol'] =>
!!getNetwork(symbol);

/**
* Returns a string used as an index to separate txs for given account inside a transactions reducer
*
Expand Down

0 comments on commit fb0064d

Please sign in to comment.