Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
Merge pull request #427 from PureStake/jan/v1-cleanup
Browse files Browse the repository at this point in the history
Refactor & Remove deprecated code from v1 Signing
  • Loading branch information
PureBrent authored Aug 1, 2022
2 parents e51c995 + 8ec72a4 commit 46419d1
Show file tree
Hide file tree
Showing 35 changed files with 312 additions and 860 deletions.
34 changes: 26 additions & 8 deletions packages/common/src/common.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { extensionBrowser } from './chrome';
import { logging } from './logging';
import { isFromExtension } from './utils';
import { RequestError, Transaction, TAccount, Note, Amount } from './types';
import { Transaction, TAccount, Note, Amount } from './types';
import { RequestError } from './errors';

test('Test chrome reference', () => {
//@ts-ignore
Expand All @@ -17,13 +18,6 @@ test('Test extension parts', () => {
expect(isFromExtension('chrome-extension://12345')).toBe(true);
});

test('Type - RequestErrors undefined test', () => {
expect(RequestError.Undefined).toMatchObject({
message: '[RequestError.Undefined] An undefined error occurred.',
code: 4000,
});
});

test('Type - Transaction', () => {
expect({ amount: 1, from: 'AAA', to: 'BBB' } as Transaction).toBeInstanceOf(Object);
});
Expand All @@ -39,3 +33,27 @@ test('Type - Note', () => {
test('Type - Amount', () => {
expect(12345 as Amount).toBe(12345);
});

test('RequestError - Structure', () => {
const address = 'MTHFSNXBMBD4U46Z2HAYAOLGD2EV6GQBPXVTL727RR3G44AJ3WVFMZGSBE';
const ledger = 'MainNet';
const testError = RequestError.NoAccountMatch(address, ledger);

expect(testError).toMatchObject({
message: `No matching account found on AlgoSigner for address: "${address}" on network ${ledger}.`,
code: 4100,
});

expect(RequestError.SigningError(4000, [testError])).toMatchObject({
message: 'There was a problem signing the transaction(s).',
code: 4000,
data: [testError],
});
});

test('RequestError - Throwing', () => {
function throwError() {
throw RequestError.Undefined;
}
expect(() => throwError()).toThrow(RequestError.Undefined);
});
96 changes: 96 additions & 0 deletions packages/common/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
export class RequestError {
// Maximum amount of transactions supported on a single group
static MAX_GROUP_SIZE = 16;

message: string;
code: number;
name: string;
data?: any;

static None = new RequestError('', 0);
static Undefined = new RequestError(
'[RequestError.Undefined] An undefined error occurred.',
4000
);
static UserRejected = new RequestError(
'[RequestError.UserRejected] The extension user does not authorize the request.',
4001
);
static NotAuthorizedByUser = new RequestError(
'[RequestError.NotAuthorized] The extension user does not authorize the request.',
4100
);
static NoAccountMatch = (address: string, ledger: string): RequestError =>
new RequestError(
`No matching account found on AlgoSigner for address: "${address}" on network ${ledger}.`,
4100
);
static UnsupportedAlgod = new RequestError(
'[RequestError.UnsupportedAlgod] The provided method is not supported.',
4200
);
static UnsupportedLedger = new RequestError(
'[RequestError.UnsupportedLedger] The provided ledger is not supported.',
4200
);
static NotAuthorizedOnChain = new RequestError(
'The user does not possess the required private key to sign with this address.',
4200
);
static MultipleTxsRequireGroup = new RequestError(
'If signing multiple transactions, they need to belong to a same group.',
4200
);
static PendingTransaction = new RequestError('Another query processing', 4201);
static LedgerMultipleTransactions = new RequestError(
'Ledger hardware device signing not available for multiple transactions.',
4201
);
static TooManyTransactions = new RequestError(
`The ledger does not support signing more than ${RequestError.MAX_GROUP_SIZE} transactions at a time.`,
4201
);
static InvalidFields = (data?: any) =>
new RequestError('Validation failed for transaction due to invalid properties.', 4300, data);
static InvalidTransactionStructure = (data?: any) =>
new RequestError('Validation failed for transaction due to invalid structure.', 4300, data);
static InvalidFormat = new RequestError(
'[RequestError.InvalidFormat] Please provide an array of either valid transaction objects or nested arrays of valid transaction objects.',
4300
);
static InvalidSigners = new RequestError(
'Signers array should only be provided for multisigs (at least one signer) or for reference-only transactions belonging to a group (empty array).',
4300
);
static InvalidStructure = new RequestError(
"The provided transaction object doesn't adhere to the correct structure.",
4300
);
static InvalidMsigStructure = new RequestError(
"The provided multisig data doesn't adhere to the correct structure.",
4300
);
static IncompleteOrDisorderedGroup = new RequestError(
'The transaction group is incomplete or presented in a different order than when it was created.',
4300
);
static NonMatchingGroup = new RequestError(
'All transactions need to belong to the same group.',
4300
);
static NoDifferentLedgers = new RequestError(
'All transactions need to belong to the same ledger.',
4300
);
static SigningError = (code: number, data?: any) =>
new RequestError('There was a problem signing the transaction(s).', code, data);

protected constructor(message: string, code: number, data?: any) {
this.name = 'AlgoSignerRequestError';
this.message = message;
this.code = code;
this.data = data;

Error.captureStackTrace(this, RequestError);
}
}
2 changes: 0 additions & 2 deletions packages/common/src/messaging/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export enum JsonRpcMethod {
AuthorizationAllow = 'authorization-allow',
AuthorizationDeny = 'authorization-deny',
SignAllow = 'sign-allow',
SignAllowMultisig = 'sign-allow-multisig',
SignAllowWalletTx = 'sign-allow-wallet-tx',
SignDeny = 'sign-deny',
SignWalletTransaction = 'sign-wallet-transaction',
Expand All @@ -35,7 +34,6 @@ export enum JsonRpcMethod {
AssetDetails = 'asset-details',
AssetsAPIList = 'assets-api-list',
AssetsVerifiedList = 'assets-verified-list',
AssetOptOut = 'asset-opt-out',
SignSendTransaction = 'sign-send-transaction',
ChangeLedger = 'change-ledger',
SaveNetwork = 'save-network',
Expand Down
44 changes: 0 additions & 44 deletions packages/common/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,4 @@
/* eslint-disable no-unused-vars */
export class RequestError {
message: string;
code: number;
name?: string;
data?: any;

static None = new RequestError('', 0);
static Undefined = new RequestError(
'[RequestError.Undefined] An undefined error occurred.',
4000
);
static UserRejected = new RequestError(
'[RequestError.UserRejected] The extension user does not authorize the request.',
4001
);
static NotAuthorizedByUser = new RequestError(
'[RequestError.NotAuthorized] The extension user does not authorize the request.',
4100
);
static UnsupportedAlgod = new RequestError(
'[RequestError.UnsupportedAlgod] The provided method is not supported.',
4200
);
static UnsupportedLedger = new RequestError(
'[RequestError.UnsupportedLedger] The provided ledger is not supported.',
4200
);
static NotAuthorizedOnChain = new RequestError(
'The user does not possess the required private key to sign with this address.',
4200
);
static InvalidFormat = new RequestError(
'[RequestError.InvalidFormat] Please provide an array of either valid transaction objects or nested arrays of valid transaction objects.',
4300
);

protected constructor(message: string, code: number, name?: string, data?: any) {
this.message = message;
this.code = code;
this.name = name;
this.data = data;
}
}

export type Field<T> = string | number;

export type TAccount = Field<string>;
Expand Down
3 changes: 2 additions & 1 deletion packages/dapp/src/fn/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RequestError, WalletTransaction } from '@algosigner/common/types';
import { WalletTransaction } from '@algosigner/common/types';
import { RequestError } from '@algosigner/common/errors';
import { JsonPayload } from '@algosigner/common/messaging/types';

/* eslint-disable no-unused-vars */
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp/src/fn/task.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Task } from './task';
import { MessageBuilder } from '../messaging/builder';
import { RequestError } from '@algosigner/common/types';
import { RequestError } from '@algosigner/common/errors';
import { JsonRpcMethod } from '@algosigner/common/messaging/types';

jest.mock('../messaging/builder');
Expand Down
18 changes: 7 additions & 11 deletions packages/dapp/src/fn/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ import { ITask } from './interfaces';

import { MessageBuilder } from '../messaging/builder';

import {
Transaction,
RequestError,
WalletTransaction,
} from '@algosigner/common/types';
import { Transaction, WalletTransaction } from '@algosigner/common/types';
import { RequestError } from '@algosigner/common/errors';
import { JsonRpcMethod, JsonPayload } from '@algosigner/common/messaging/types';
import { Runtime } from '@algosigner/common/runtime/runtime';

Expand Down Expand Up @@ -53,12 +50,11 @@ export class Task extends Runtime implements ITask {
if (
txOrGroup === null ||
txOrGroup === undefined ||
(!Array.isArray(txOrGroup) && typeof txOrGroup === 'object' &&
(!txOrGroup.txn || (txOrGroup.txn && !txOrGroup.txn.length))
) ||
(Array.isArray(txOrGroup) &&
(!txOrGroup.length || (txOrGroup.length && !txOrGroup.every((tx) => tx !== null)))
)
(!Array.isArray(txOrGroup) &&
typeof txOrGroup === 'object' &&
(!txOrGroup.txn || (txOrGroup.txn && !txOrGroup.txn.length))) ||
(Array.isArray(txOrGroup) &&
(!txOrGroup.length || (txOrGroup.length && !txOrGroup.every((tx) => tx !== null))))
)
throw formatError;
});
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp/src/messaging/builder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RequestError } from '@algosigner/common/types';
import { RequestError } from '@algosigner/common/errors';
import { JsonRpcMethod, JsonPayload } from '@algosigner/common/messaging/types';

import { JsonRpc } from '@algosigner/common/messaging/jsonrpc';
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp/src/messaging/handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OnMessageListener } from './types';
import { RequestError } from '@algosigner/common/types';
import { RequestError } from '@algosigner/common/errors';

export class OnMessageHandler {
static promise(resolve: Function, reject: Function): OnMessageListener {
Expand Down
2 changes: 1 addition & 1 deletion packages/extension/src/background/messaging/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { MessageApi } from './api';
import { Task } from './task';
import encryptionWrap from '../encryptionWrap';
import { isFromExtension } from '@algosigner/common/utils';
import { RequestError } from '@algosigner/common/types';
import { RequestError } from '@algosigner/common/errors';
import { JsonRpcMethod, MessageSource } from '@algosigner/common/messaging/types';
import logging from '@algosigner/common/logging';

Expand Down
Loading

0 comments on commit 46419d1

Please sign in to comment.