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 #462 from AztecProtocol/fix/sdk-event-service
Browse files Browse the repository at this point in the history
Fix sdk EventService and some minor issues
  • Loading branch information
ArnSch authored Jan 31, 2020
2 parents e0d1349 + 517762d commit 3cb88c7
Show file tree
Hide file tree
Showing 20 changed files with 172 additions and 76 deletions.
6 changes: 3 additions & 3 deletions packages/extension/demo/1_apis-usage/create-asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
const erc20Address = deployedERC20.address;
addAssetStatus(`✓ ERC20 deployed - ${erc20Address}`, true);

addAssetStatus('Deploying ZkAsset...');
addAssetStatus('Deploying zkAsset...');
const scalingFactor = document.getElementById('new-asset-scaling-factor').value;
const aceAddress = window.aztec.web3.getAddress('ACE');
const ZkAsset = await fetchContract('ZkAssetOwnable');
Expand All @@ -60,7 +60,7 @@
],
);
const zkAssetAddress = deployedZkAsset.address;
addAssetStatus(`✓ ZkAsset deployed - ${zkAssetAddress}`, true);
addAssetStatus(`✓ zkAsset deployed - ${zkAssetAddress}`, true);

if (value) {
const account = window.aztec.web3.account();
Expand All @@ -76,7 +76,7 @@
addAssetStatus(`✓ ERC20 balance = ${value}`, true);
}

addAssetStatus(`✓ ZkAsset created with initial ERC20 balance = ${value}`, true);
addAssetStatus(`✓ zkAsset created with initial ERC20 balance = ${value}`, true);

await sleep(500);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import async from 'async';
import NoteService from '~/background/services/NoteService';
import {
subscription as NoteSubscription,
saveNotes,
Expand Down Expand Up @@ -210,20 +209,7 @@ class Watcher {
if (groupedNotes.isEmpty()) {
return;
}
const {
createNotes,
destroyNotes,
updateNotes,
} = groupedNotes;
NoteService.addNotes(
networkId,
address,
[
...createNotes,
...destroyNotes,
...updateNotes,
].filter(({ owner }) => owner === address),
);

this.saveQueue.push({
name: 'Save Notes',
groupedNotes,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
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) {
return Note.bulkAdd(notes, { networkId });
let created;
try {
created = await Note.bulkAdd(notes, { networkId });
} catch (e) {
// TODO - some of the notes might be valid, create them individually
warnLog('Failed to create notes in indexedDB', e);
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
@@ -1,6 +1,32 @@
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) {
return Note.add(note, { networkId });
let newNote;
try {
newNote = await Note.add(note, { networkId });
} catch (e) {
warnLog('Failed to create a note in indexedDB', e);
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
@@ -1,6 +1,37 @@
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) {
return Promise.all(notes.map(note => Note.update(note, { networkId })));
const updatedNotes = [];

await Promise.all(
notes.map(async (note) => {
let updated;
try {
updated = await Note.update(note, { networkId });
updatedNotes.push(note);
} catch (e) {
warnLog('Failed to update note in indexedDB', e);
}
return updated;
}),
);

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
@@ -1,6 +1,30 @@
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) {
return Note.update(note, { networkId });
let updated;
try {
updated = await Note.update(note, { networkId });
} catch (e) {
warnLog('Failed to update note in indexedDB', e);
return null;
}

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

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

return updated;
}
11 changes: 5 additions & 6 deletions packages/extension/src/client/apis/burn/prove.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import * as aztec from 'aztec.js';
import {
BurnProof,
note as noteUtils,
} from 'aztec.js';
import {
createNote,
fromViewingKey,
Expand All @@ -12,10 +15,6 @@ import ApiError from '~/client/utils/ApiError';
// import validateExtensionAccount from '../utils/validateExtensionAccount';
import toAztecNote from '../utils/toAztecNote';

const {
BurnProof,
} = aztec;

export default async function proveBurn({
assetAddress,
notes,
Expand Down Expand Up @@ -43,7 +42,7 @@ export default async function proveBurn({
let balance;
let oldBurnedCounterNote;

const zeroNote = await aztec.note.createZeroValueNote();
const zeroNote = await noteUtils.createZeroValueNote();
if (confidentialTotalBurned === zeroNote.noteHash) {
balance = 0;
oldBurnedCounterNote = zeroNote;
Expand Down
11 changes: 5 additions & 6 deletions packages/extension/src/client/apis/privateRange/prove.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import * as aztec from 'aztec.js';
import {
PrivateRangeProof,
note as noteUtils,
} from 'aztec.js';
import {
createNote,
valueOf,
Expand All @@ -8,10 +11,6 @@ import ConnectionService from '~/client/services/ConnectionService';
import ApiError from '~/client/utils/ApiError';
import toAztecNote from '../utils/toAztecNote';

const {
PrivateRangeProof,
} = aztec;

const valuesValidators = {
eq: diff => diff === 0,
gt: diff => diff > 0,
Expand Down Expand Up @@ -61,7 +60,7 @@ export default async function provePrivateRange({
notesSender.address,
notesSender.linkedPublicKey,
);
} else if (!(utilityNote instanceof aztec.note.Note)) {
} else if (!(utilityNote instanceof noteUtils.Note)) {
throw new ApiError('input.utilityNote.wrong.type', {
note: utilityNote,
});
Expand Down
8 changes: 3 additions & 5 deletions packages/extension/src/client/apis/swap/prove.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as aztec from 'aztec.js';
import {
SwapProof,
} from 'aztec.js';

export default async function proveSwap({
swap: {
Expand All @@ -9,10 +11,6 @@ export default async function proveSwap({
},
sender,
}) {
const {
SwapProof,
} = aztec;

// const taker = await validateAccount(takerBid.owner, true);
// const maker = await validateAccount(takerAsk.owner, true);

Expand Down
6 changes: 4 additions & 2 deletions packages/extension/src/client/apis/utils/toAztecNote.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as aztec from 'aztec.js';
import {
note as noteUtils,
} from 'aztec.js';

export default async function toAztecNote(note) {
if (note instanceof aztec.note.Note) {
if (note instanceof noteUtils.Note) {
return note;
}
if ('export' in note
Expand Down
4 changes: 2 additions & 2 deletions packages/extension/src/ui/apis/asset/deposit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ export default async function deposit({
currentAccount: {
address: currentAddress,
},
erc20Amount,
assetAddress,
proof,
amount,
isGSNAvailable,
}) {
const proofHash = proof.hash;
Expand All @@ -20,7 +20,7 @@ export default async function deposit({
currentAddress,
proofHash,
proofData,
amount,
erc20Amount.toString(),
],
};

Expand Down
9 changes: 4 additions & 5 deletions packages/extension/src/ui/apis/proof/createNoteFromBalance.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import * as aztec from 'aztec.js';
import {
JoinSplitProof,
ProofUtils,
} from 'aztec.js';
import { keccak256 } from 'web3-utils';
import {
METADATA_AZTEC_DATA_LENGTH,
Expand Down Expand Up @@ -179,10 +182,6 @@ export default async function createNoteFromBalance({
});
}

const {
JoinSplitProof,
ProofUtils,
} = aztec;
const publicValue = ProofUtils.getPublicValue(
inputValues,
outputValues,
Expand Down
10 changes: 4 additions & 6 deletions packages/extension/src/ui/apis/proof/deposit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import * as aztec from 'aztec.js';
import {
JoinSplitProof,
ProofUtils,
} from 'aztec.js';
import {
randomSumArray,
} from '~/utils/random';
Expand All @@ -10,11 +13,6 @@ import {
batchGetExtensionAccount,
} from '~/ui/apis/account';

const {
JoinSplitProof,
ProofUtils,
} = aztec;

export default async function deposit({
transactions,
publicOwner,
Expand Down
4 changes: 2 additions & 2 deletions packages/extension/src/ui/locales/en/note.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ export default {
title: 'Grant Access',
step: 'Granting Access',
description: `This will grant another user view access to a portion of your balance.
It will not allow the ZkTokens to be spent without a signature from your MetaMask account.
It will not allow the zkTokens to be spent without a signature from your MetaMask account.
`,
submit: 'Grant Access',
},
sign: {
description: `A MetaMask signature is required to grant access to ZkTokens.
description: `A MetaMask signature is required to grant access to zkTokens.
The signature should contain the following values:
`,
},
Expand Down
8 changes: 4 additions & 4 deletions packages/extension/src/ui/locales/en/send.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
export default {
title: 'Send ZkTokens',
title: 'Send zkTokens',
approve: {
description: `A signature is required to send ZkTokens.
description: `A signature is required to send zkTokens.
The SDK will pick the most suitable notes for the transaction.
Check the transaction details are correct before proceeding.
`,
submit: 'Looks Good!',
},
approved: 'Approved',
sign: {
description: `A MetaMask signature is required to send ZkTokens.
description: `A MetaMask signature is required to send zkTokens.
The signature should contain the following values:
`,
},
Expand All @@ -19,7 +19,7 @@ export default {
`,
},
send: {
step: 'Sending ZkTokens',
step: 'Sending zkTokens',
description: `The SDK has picked the most suitable notes for this transaction.
If everything looks good hit send!
`,
Expand Down
Loading

0 comments on commit 3cb88c7

Please sign in to comment.