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

Commit

Permalink
feat(sdk): allow to send to a recipient who has no aztec account
Browse files Browse the repository at this point in the history
  • Loading branch information
LeilaWang committed Feb 26, 2020
1 parent 346da28 commit b3d8fed
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import {
argsError,
} from '~/utils/error';
import ensureInputNotes from '../utils/ensureInputNotes';
import validateAccounts from '../utils/validateAccounts';

Expand All @@ -19,7 +22,16 @@ export default async function verifyTransferRequest({
return noteError;
}

const addresses = transactions.map(({ to }) => to);
const addresses = transactions
.filter(({ aztecAccountNotRequired }) => !aztecAccountNotRequired)
.map(({ to }) => to);

if (addresses.length !== transactions.length
&& (!userAccess || !userAccess.length)
) {
return argsError('note.viewingKey.noAccess');
}

if (userAccess && userAccess.length > 0) {
userAccess.forEach((address) => {
if (addresses.indexOf(address) < 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const transactionsType = {
gte: 1,
},
},
aztecAccountNotRequired: {
type: 'boolean',
},
},
};

Expand Down
19 changes: 10 additions & 9 deletions packages/extension/src/client/apis/ZkAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,16 +240,17 @@ export default class ZkAsset {
* Send
*
* - transactions ([Transaction!]!)
* amount (Int!): The note value to send.
* to (Address!): The output note owner.
* numberOfOutputNotes (Int): Number of output notes of this transaction.
* amount (Int!): The note value to send.
* to (Address!): The output note owner.
* numberOfOutputNotes (Int): Number of output notes of this transaction.
* aztecAccountNotRequired (Bool): Not to enforce receipient to have an aztec account if set to true.
* - options (Object)
* numberOfInputNotes (Int): Number of notes to be destroyed.
* Will use default value in setting if undefined.
* numberOfOutputNotes (Int): Number of new notes for each transaction.
* Unless numberOfOutputNotes is defined in that transaction.
* Will use default value in setting if undefined.
* userAccess ([Address!]): The addresses that are able to see the real note value.
* numberOfInputNotes (Int): Number of notes to be destroyed.
* Will use default value in setting if undefined.
* numberOfOutputNotes (Int): Number of new notes for each transaction.
* Unless numberOfOutputNotes is defined in that transaction.
* Will use default value in setting if undefined.
* userAccess ([Address!]): The addresses that are able to see the real note value.
*
* @returns (Object)
* - success (Boolean)
Expand Down
11 changes: 9 additions & 2 deletions packages/extension/src/client/utils/parseInputTransactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ export default function parseInputTransactions(transactions) {
return transactions;
}

return transactions.map(tx => ({
return transactions.map(({
amount,
numberOfOutputNotes,
aztecAccountNotRequired,
...tx
}) => ({
...tx,
amount: parseInputInteger(tx.amount),
amount: parseInputInteger(amount),
numberOfOutputNotes: parseInputInteger(numberOfOutputNotes),
aztecAccountNotRequired: aztecAccountNotRequired || false,
}));
}
1 change: 1 addition & 0 deletions packages/extension/src/config/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export default {
viewingKey: {
encrypt: 'Failed to encrypt viewing key.',
recover: 'Failed to recover note from its viewing key.',
noAccess: 'Please allow at least one user to have access to the note.',
},
pick: {
sum: {
Expand Down
22 changes: 12 additions & 10 deletions packages/extension/src/ui/apis/proof/createNoteFromBalance.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,23 @@ export default async function createNoteFromBalance({
const extraAmount = sum - inputAmount;

const addresses = (transactions || []).map(({ to }) => to);
if (extraAmount > 0) {
addresses.push(currentAddress);
}
addresses.push(currentAddress);
const accountMapping = {};
const accounts = await batchGetExtensionAccount(addresses);
accounts.forEach((account) => {
accountMapping[account.address] = account;
});

const currentAccount = accountMapping[currentAddress];

const outputValues = [];
const outputNotes = [];
let remainderNote;
if (extraAmount > 0) {
const {
spendingPublicKey,
linkedPublicKey,
} = accountMapping[currentAddress];
} = currentAccount;
remainderNote = await createNote(
extraAmount,
spendingPublicKey,
Expand Down Expand Up @@ -161,10 +161,12 @@ export default async function createNoteFromBalance({
spendingPublicKey,
} = accountMapping[to];
outputValues.push(...values);
const ownerAccess = {
address: to,
linkedPublicKey,
};
const ownerAccess = !linkedPublicKey
? null
: {
address: to,
linkedPublicKey,
};
const userAccess = !userAccessAccounts.length
? ownerAccess
: uniqBy(
Expand All @@ -173,10 +175,10 @@ export default async function createNoteFromBalance({
ownerAccess,
],
'address',
);
).filter(a => a);
const newNotes = await createNotes(
values,
spendingPublicKey,
spendingPublicKey || currentAccount.spendingPublicKey,
to,
userAccess,
);
Expand Down
2 changes: 2 additions & 0 deletions packages/extension/src/ui/utils/parseInputTransactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ export default function parseInputTransactions(transactions) {
return transactions.map(({
amount,
to,
...rest
}) => ({
...rest,
amount: parseInputAmount(amount),
to,
}));
Expand Down

0 comments on commit b3d8fed

Please sign in to comment.