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

Commit

Permalink
Merge pull request #466 from AztecProtocol/sdk-fixes
Browse files Browse the repository at this point in the history
Sdk fixes
  • Loading branch information
ArnSch authored Feb 4, 2020
2 parents e727cd9 + 6300cfd commit e2696f9
Show file tree
Hide file tree
Showing 47 changed files with 404 additions and 361 deletions.
2 changes: 1 addition & 1 deletion packages/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"**/*.scss"
],
"license": "LGPL-3.0",
"version": "0.2.3",
"version": "1.3.0",
"author": "AZTEC",
"bugs": {
"url": "https://github.com/AztecProtocol/AZTEC/issues"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import async from 'async';
import NoteService from '~/background/services/NoteService';
import {
subscription as NoteSubscription,
saveNotes,
Expand Down Expand Up @@ -210,6 +211,23 @@ class Watcher {
return;
}

const {
createNotes,
updateNotes,
destroyNotes,
} = groupedNotes;
const newNotes = [
...createNotes,
...updateNotes,
...destroyNotes,
].filter(({ owner }) => owner === address);

NoteService.addNotes(
networkId,
address,
newNotes,
);

this.saveQueue.push({
name: 'Save Notes',
groupedNotes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {
warnLog,
} from '~/utils/log';
import Note from '~/background/database/models/note';
import NoteService from '~/background/services/NoteService';
import Web3Service from '~/helpers/Web3Service';

export default async function createBulkNotes(notes, networkId) {
let created;
Expand All @@ -15,17 +13,5 @@ export default async function createBulkNotes(notes, networkId) {
return null;
}

if (created && created.length) {
const {
address,
} = Web3Service.account;

NoteService.addNotes(
networkId,
address,
notes.filter(({ owner }) => owner === address),
);
}

return created;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {
warnLog,
} from '~/utils/log';
import Note from '~/background/database/models/note';
import NoteService from '~/background/services/NoteService';
import Web3Service from '~/helpers/Web3Service';

export default async function createNote(note, networkId) {
let newNote;
Expand All @@ -14,19 +12,5 @@ export default async function createNote(note, networkId) {
return null;
}

if (newNote) {
const {
address,
} = Web3Service.account;

if (newNote.owner === address) {
NoteService.addNotes(
networkId,
address,
[note],
);
}
}

return newNote;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {
warnLog,
} from '~/utils/log';
import Note from '~/background/database/models/note';
import NoteService from '~/background/services/NoteService';
import Web3Service from '~/helpers/Web3Service';

export default async function updateBulkNotes(notes, networkId) {
const updatedNotes = [];
Expand All @@ -21,17 +19,5 @@ export default async function updateBulkNotes(notes, networkId) {
}),
);

if (updatedNotes.length) {
const {
address,
} = Web3Service.account;

NoteService.addNotes(
networkId,
address,
updatedNotes.filter(({ owner }) => owner === address),
);
}

return updatedNotes;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {
warnLog,
} from '~/utils/log';
import Note from '~/background/database/models/note';
import NoteService from '~/background/services/NoteService';
import Web3Service from '~/helpers/Web3Service';

export default async function updateNote(note, networkId) {
let updated;
Expand All @@ -14,17 +12,5 @@ export default async function updateNote(note, networkId) {
return null;
}

if (updated) {
const {
address,
} = Web3Service.account;

NoteService.addNotes(
networkId,
address,
[note],
);
}

return updated;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import EthCrypto from 'eth-crypto';
import userModel from '~/database/models/user';
import fetchAsset from './utils/fetchAsset';
import fetchAztecAccount from './utils/fetchAztecAccount';
Expand Down Expand Up @@ -27,23 +26,9 @@ const uiResolvers = {
});
return asset;
},
account: async (_, { address }) => {
const { spendingPublicKey, ...rest } = await fetchAztecAccount({
address,
});
if (spendingPublicKey) {
const compressedPublicKey = EthCrypto.publicKey.compress(
spendingPublicKey.slice(2),
);
return {
...rest,
spendingPublicKey: `0x${compressedPublicKey}`,
};
}
return {
...rest,
};
},
account: async (_, { address }) => fetchAztecAccount({
address,
}),
note: async (_, args) => {
const {
note: noteResponse,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export default async function getAccounts(args) {
})));

accounts = accounts
.map(({ account }) => account)
.map(({ account }) => ({
...account,
}))
.filter(a => a);
const onChainAccounts = accounts || [];
const invalidAddresses = addresses
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ describe('Asset.decryptNotes', () => {
valueFromViewingKeySpy.mockClear();
});

it('will not process note that is already in storage', async () => {
it('will still process note that is already in storage but will not affect balance', async () => {
const valueFromViewingKeySpy = jest.spyOn(valueFromViewingKey, 'default');
let balance = 0;
let maxBlockNumber = 0;
Expand All @@ -275,6 +275,7 @@ describe('Asset.decryptNotes', () => {

expect(asset.balance).toBe(testNotes[duplicatedIndex].value);
expect(valueFromViewingKeySpy).toHaveBeenCalledTimes(1);
valueFromViewingKeySpy.mockClear();

await asset.decryptNotes(notes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export default class Asset {
const destroyed = isDestroyed(status);
let key = await get(noteHash);

if (destroyed ^ !!key) { // eslint-disable-line no-bitwise
if (destroyed && !key) {
if (blockNumber > this.lastSynced) {
this.lastSynced = blockNumber;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ export default async function createNoteKey(noteHash) {
await lock(
counterKey,
async () => {
const prevNoteKey = await get(noteHash);
if (prevNoteKey) {
key = prevNoteKey;
return;
}

const count = await get(counterKey) || 0;
key = dataKey('note', { count: count + 1 });
await set({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {
} from '@openzeppelin/gsn-provider';
import Web3Service from '~/helpers/Web3Service';
import retrieveSigningInfo from '~/utils/retrieveSigningInfo';
import approveFunction from '~/utils/approveGSNFunction';
import { getProviderUrl } from '~/utils/network';
import makeApproveFunction from './makeApproveFunction';

const sendTransaction = async (query, connection) => {
const {
Expand All @@ -25,7 +25,7 @@ const sendTransaction = async (query, connection) => {
const gsnProvider = new GSNProvider(providerUrl, {
pollInterval: 1 * 1000,
signKey: signingInfo.privateKey,
approveFunction: approveFunction(query, connection),
approveFunction: makeApproveFunction(query, connection),
});
const receipt = await Web3Service
.useContract(contract)
Expand All @@ -43,7 +43,14 @@ const sendTransaction = async (query, connection) => {
},
responseId,
};
} catch (error) {
} catch (e) {
let error = e;
if (error.message.match(/relayer/)) {
error = {
key: 'transaction.gsn.error.relayer',
fetal: true,
};
}
return {
...query,
data: {
Expand All @@ -53,8 +60,6 @@ const sendTransaction = async (query, connection) => {
}
};

// TODO change this to use the gas station network

export default {
sendTransaction,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import ClientActionService from '~/background/services/ClientActionService';
import {
actionRequestEvent,
} from '~/config/event';

export default function makeApproveFunction(query, connection) {
return async ({
from,
to,
encodedFunctionCall,
txFee,
gasPrice,
gas,
nonce,
relayerAddress,
relayHubAddress,
}) => {
const params = {
from,
to,
encodedFunctionCall,
txFee,
gasPrice,
gas,
nonce,
relayerAddress,
relayHubAddress,
};

const response = await ClientActionService.triggerClientAction({
...query,
data: {
type: actionRequestEvent,
action: 'apiKeyApproval',
params,
},
}, connection);

return response.data.approvalData;
};
}
Loading

0 comments on commit e2696f9

Please sign in to comment.