Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/tx local state logs #436

Merged
merged 16 commits into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package-lock.json
# tarball from `yarn pack`
*.tgz

.DS_STORE
coverage
dist
docs
15 changes: 14 additions & 1 deletion src/transaction/TransactionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ export enum TransactionStatus {
unapproved = 'unapproved',
}

/**
* Options for wallet device.
*/
export enum WalletDevice {
MM_MOBILE = 'metamask_mobile',
MM_EXTENSION = 'metamask_extension',
OTHER = 'other_device',
}

type TransactionMetaBase = {
isTransfer?: boolean;
transferInformation?: {
Expand All @@ -101,6 +110,7 @@ type TransactionMetaBase = {
transaction: Transaction;
transactionHash?: string;
blockNumber?: string;
deviceConfirmedOn?: WalletDevice;
};

/**
Expand All @@ -112,6 +122,7 @@ type TransactionMetaBase = {
* @property id - Generated UUID associated with this transaction
* @property networkID - Network code as per EIP-155 for this transaction
* @property origin - Origin this transaction was sent from
* @property deviceConfirmedOn - string to indicate what device the transaction was confirmed
* @property rawTransaction - Hex representation of the underlying transaction
* @property status - String status of this transaction
* @property time - Timestamp associated with this transaction
Expand Down Expand Up @@ -411,9 +422,10 @@ export class TransactionController extends BaseController<TransactionConfig, Tra
*
* @param transaction - Transaction object to add
* @param origin - Domain origin to append to the generated TransactionMeta
* @param deviceConfirmedOn - enum to indicate what device the transaction was confirmed to append to the generated TransactionMeta
* @returns - Object containing a promise resolving to the transaction hash if approved
*/
async addTransaction(transaction: Transaction, origin?: string): Promise<Result> {
async addTransaction(transaction: Transaction, origin?: string, deviceConfirmedOn?: WalletDevice): Promise<Result> {
const network = this.context.NetworkController as NetworkController;
const { transactions } = this.state;
transaction = normalizeTransaction(transaction);
Expand All @@ -434,6 +446,7 @@ export class TransactionController extends BaseController<TransactionConfig, Tra
status: TransactionStatus.unapproved as TransactionStatus.unapproved,
time: Date.now(),
transaction,
deviceConfirmedOn,
};

try {
Expand Down
2 changes: 2 additions & 0 deletions src/user/AddressBookController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import BaseController, { BaseConfig, BaseState } from '../BaseController';
*
* @property address - Hex address of a recipient account
* @property name - Nickname associated with this address
* @property importTime - Data time when an account as created/imported
*/
export interface ContactEntry {
address: string;
name: string;
importTime?: number;
sethkfman marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
44 changes: 23 additions & 21 deletions src/user/PreferencesController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ describe('PreferencesController', () => {
const controller = new PreferencesController();
controller.addIdentities(['foo']);
controller.addIdentities(['foo']);
expect(controller.state.identities).toEqual({
'0xfoO': {
address: '0xfoO',
name: 'Account 1',
},
});
expect(controller.state.identities['0xfoO'].address).toEqual('0xfoO');
expect(controller.state.identities['0xfoO'].name).toEqual('Account 1');
expect(controller.state.identities['0xfoO'].importTime).toBeLessThanOrEqual(Date.now());
});

it('should remove identity', () => {
Expand All @@ -49,24 +46,27 @@ describe('PreferencesController', () => {
const controller = new PreferencesController();
controller.addIdentities(['foo', 'bar']);
controller.syncIdentities(['foo', 'bar']);
expect(controller.state.identities).toEqual({
'0xbar': { address: '0xbar', name: 'Account 2' },
'0xfoO': { address: '0xfoO', name: 'Account 1' },
});
expect(controller.state.identities['0xfoO'].address).toEqual('0xfoO');
expect(controller.state.identities['0xfoO'].name).toEqual('Account 1');
expect(controller.state.identities['0xfoO'].importTime).toBeLessThanOrEqual(Date.now());
expect(controller.state.identities['0xbar'].address).toEqual('0xbar');
expect(controller.state.identities['0xbar'].name).toEqual('Account 2');
expect(controller.state.identities['0xbar'].importTime).toBeLessThanOrEqual(Date.now());
controller.syncIdentities(['foo']);
expect(controller.state.identities).toEqual({
'0xfoO': { address: '0xfoO', name: 'Account 1' },
});
expect(controller.state.identities['0xfoO'].address).toEqual('0xfoO');
expect(controller.state.identities['0xfoO'].name).toEqual('Account 1');
expect(controller.state.selectedAddress).toBe('0xfoO');
});

it('should add new identities', () => {
const controller = new PreferencesController();
controller.updateIdentities(['foo', 'bar']);
expect(controller.state.identities).toEqual({
'0xbar': { address: '0xbar', name: 'Account 2' },
'0xfoO': { address: '0xfoO', name: 'Account 1' },
});
expect(controller.state.identities['0xfoO'].address).toEqual('0xfoO');
expect(controller.state.identities['0xfoO'].name).toEqual('Account 1');
expect(controller.state.identities['0xfoO'].importTime).toBeLessThanOrEqual(Date.now());
expect(controller.state.identities['0xbar'].address).toEqual('0xbar');
expect(controller.state.identities['0xbar'].name).toEqual('Account 2');
expect(controller.state.identities['0xbar'].importTime).toBeLessThanOrEqual(Date.now());
});

it('should not update existing identities', () => {
Expand All @@ -75,10 +75,12 @@ describe('PreferencesController', () => {
{ identities: { '0xbar': { address: '0xbar', name: 'Custom name' } } },
);
controller.updateIdentities(['foo', 'bar']);
expect(controller.state.identities).toEqual({
'0xbar': { address: '0xbar', name: 'Custom name' },
'0xfoO': { address: '0xfoO', name: 'Account 1' },
});
expect(controller.state.identities['0xfoO'].address).toEqual('0xfoO');
expect(controller.state.identities['0xfoO'].name).toEqual('Account 1');
expect(controller.state.identities['0xfoO'].importTime).toBeLessThanOrEqual(Date.now());
expect(controller.state.identities['0xbar'].address).toEqual('0xbar');
expect(controller.state.identities['0xbar'].name).toEqual('Custom name');
expect(controller.state.identities['0xbar'].importTime).toBeUndefined();
});

it('should remove identities', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/user/PreferencesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class PreferencesController extends BaseController<BaseConfig, Preference
}
const identityCount = Object.keys(identities).length;

identities[address] = { name: `Account ${identityCount + 1}`, address };
identities[address] = { name: `Account ${identityCount + 1}`, address, importTime: Date.now() };
});
this.update({ identities: { ...identities } });
}
Expand Down Expand Up @@ -187,6 +187,7 @@ export class PreferencesController extends BaseController<BaseConfig, Preference
ids[address] = oldIdentities[address] || {
address,
name: `Account ${index + 1}`,
importTime: Date.now(),
};
return ids;
}, {});
Expand Down