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 Jan 6, 2022
1 parent e7836ab commit e908e81
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import { PROTOCOL } from './constants';
import type { PROTOCOL_SCHEME } from '@suite-constants/protocol';
import type { SendFormState } from '@suite-reducers/protocolReducer';
import type { SendFormState, AoppState } 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: SendFormState;
}
| {
type: typeof PROTOCOL.SAVE_AOPP_PROTOCOL;
payload: AoppState;
}
| { type: typeof PROTOCOL.RESET };

export const fillSendForm = (shouldFill: boolean): ProtocolAction => ({
Expand All @@ -27,6 +35,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,
});
20 changes: 20 additions & 0 deletions packages/suite/src/reducers/suite/protocolReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,33 @@ import produce from 'immer';
import { PROTOCOL } from '@suite-actions/constants';
import type { Action } from '@suite-types';
import type { PROTOCOL_SCHEME } from '@suite-constants/protocol';
import type { Network } from '@wallet-types';

export interface SendFormState {
scheme: PROTOCOL_SCHEME;
address: string;
amount?: number;
}

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

type Autofill<T> = Partial<T> & {
shouldFill?: boolean;
};

export interface State {
sendForm: Autofill<SendFormState>;
aopp: Autofill<AoppState>;
}

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

const protocolReducer = (state: State = initialState, action: Action): State =>
Expand All @@ -33,6 +43,16 @@ const protocolReducer = (state: State = initialState, action: Action): State =>
draft.sendForm.amount = action.payload.amount;
draft.sendForm.shouldFill = 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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { PROTOCOL_SCHEME } from '@suite-constants/protocol';
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 @@ -28,7 +30,16 @@ export type CoinProtocolInfo = {
amount?: number;
};

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

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

Expand All @@ -48,5 +59,19 @@ export const getProtocolInfo = (uri: string): CoinProtocolInfo | 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
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,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 e908e81

Please sign in to comment.