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

Commit

Permalink
feat(sdk): add user api to fetch aztec account by address
Browse files Browse the repository at this point in the history
  • Loading branch information
LeilaWang committed Feb 26, 2020
1 parent f5222ed commit f7166ed
Show file tree
Hide file tree
Showing 19 changed files with 229 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import AuthService from '~/background/services/AuthService';
import Web3Service from '~/helpers/Web3Service';
import {
fromHexString,
} from '~/utils/crypto';
import decodePrivateKey from '~/background/utils/decodePrivateKey';

export default async function decrypt({
data: {
args: {
address,
message,
},
},
}) {
const {
address: currentAddress,
} = Web3Service.account;
const keyStore = await AuthService.getKeyStore();
const {
pwDerivedKey,
} = await AuthService.getSession() || {};

let decrypted = '';
if (address === currentAddress
&& keyStore
&& pwDerivedKey
) {
try {
const privateKey = decodePrivateKey(keyStore, pwDerivedKey);
decrypted = fromHexString(message).decrypt(privateKey);
} catch (e) {
decrypted = '';
}
}

return {
result: {
decrypted,
},
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {
encryptMessage,
} from '~/utils/crypto';

export default async function encrypt({
data: {
args: {
linkedPublicKey,
message,
},
},
}) {
const encrypted = encryptMessage(linkedPublicKey, message);
return {
message: {
encrypted: encrypted.toHexString(),
},
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import userQuery from '~/background/services/GraphQLService/Queries/userQuery';
import query from '../utils/query';

export default async request => query(request, userQuery(`
address
linkedPublicKey
spendingPublicKey
`));
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const registerExtension = async (request, connection) => {
} = await query(request, userPermissionQuery(`
address
linkedPublicKey
spendingPublicKey
blockNumber
`)) || {};

Expand All @@ -52,6 +53,7 @@ const registerExtension = async (request, connection) => {
const {
address,
linkedPublicKey,
spendingPublicKey,
} = account || {};

return {
Expand All @@ -60,6 +62,7 @@ const registerExtension = async (request, connection) => {
: {
address,
linkedPublicKey,
spendingPublicKey,
},
error,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import registerExtension from './RegisterExtension';
import registerDomain from './RegisterDomain';
import account from './Account';
import user from './Account/user';
import encryptMessage from './Account/encrypt';
import decryptMessage from './Account/decrypt';
import prove from './Prove';
import asset from './Asset';
import assetBalance from './Asset/getBalance';
Expand All @@ -15,6 +18,9 @@ const apis = {
registerExtension,
registerDomain,
account,
user,
encryptMessage,
decryptMessage,
asset,
assetBalance,
fetchNotesFromBalance,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import makeSchema from '~/utils/makeSchema';
import addressType from './types/address';

export default makeSchema({
address: addressType.isRequired,
message: {
type: 'string',
required: true,
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import makeSchema from '~/utils/makeSchema';

export default makeSchema({
linkedPublicKey: {
type: 'string',
required: true,
},
message: {
type: 'string',
required: true,
},
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import emptySchema from './emptySchema';
import user from './user';
import encryptMessage from './encryptMessage';
import decryptMessage from './decryptMessage';
import asset from './asset';
import assetBalance from './assetBalance';
import fetchNotesFromBalance from './fetchNotesFromBalance';
Expand All @@ -14,6 +17,9 @@ export default {
registerExtension: emptySchema,
registerDomain: emptySchema,
account: emptySchema,
user,
encryptMessage,
decryptMessage,
asset,
assetBalance,
fetchNotesFromBalance,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import makeSchema from '~/utils/makeSchema';
import addressType from './types/address';

export default makeSchema({
id: addressType.isRequired,
});

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import gql from 'graphql-tag';

export default function userQuery(requestedFields) {
return gql`
query userQuery(
$id: String!
$domain: String!
$currentAddress: String!
) {
user(
id: $id
domain: $domain
currentAddress: $currentAddress
) {
account {
${requestedFields}
}
error {
type
key
message
response
}
}
}
`;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import ClientSubscriptionService from '~/background/services/ClientSubscriptionService';
import userModel from '~/database/models/user';
import {
ensureKeyvault, // TODO rename this also checks session
ensureDomainPermission,
Expand All @@ -16,10 +15,8 @@ import base from './base';

const backgroundResolvers = {
Query: {
user: ensureDomainPermission(async (_, args) => ({
account: await userModel.get({
id: (args.id || args.currentAddress),
}),
user: ensureDomainPermission(async (_, args) => fetchAztecAccount({
address: args.address || args.id,
})),
asset: ensureDomainPermission(async (_, args) => fetchAsset({
address: args.id || args.address,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import userModel from '~/database/models/user';
import fetchAsset from './utils/fetchAsset';
import fetchAztecAccount from './utils/fetchAztecAccount';
import mergeResolvers from './utils/mergeResolvers';
Expand All @@ -15,8 +14,8 @@ const uiResolvers = {
.call(address),
},
Query: {
user: async (_, { id }) => userModel.get({
id,
user: async (_, { address }) => fetchAztecAccount({
address,
}),
asset: async (_, { id }) => {
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export default gql`
address: String!
linkedPublicKey: String
spendingPublicKey: String
lastSynced: String
blockNumber: BigInt
}
type User {
id: ID!
Expand Down
61 changes: 61 additions & 0 deletions packages/extension/src/client/apis/Account.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import Web3Service from '~/client/services/Web3Service';
import ConnectionService from '~/client/services/ConnectionService';
import ApiError from '~/client/utils/ApiError';

const dataProperties = [
'address',
'linkedPublicKey',
'spendingPublicKey',
];

class Account {
constructor(account) {
dataProperties.forEach((key) => {
this[key] = account[key] || '';
});
this.id = account.address;
}

get registered() {
return !!this.linkedPublicKey;
}

async encryptMessage(message) {
if (!this.registered) {
throw new ApiError('user.unregistered', {
fn: 'encryptMessage',
});
}

const { encrypted } = await ConnectionService.query(
'encryptMessage',
{
address: this.address,
linkedPublicKey: this.linkedPublicKey,
message,
},
) || {};

return encrypted;
}

async decryptMessage(message) {
if (this.address !== Web3Service.account.address) {
throw new ApiError('user.logout', {
fn: 'decryptMessage',
});
}

const { decrypted } = await ConnectionService.query(
'decryptMessage',
{
address: this.address,
message,
},
) || {};

return decrypted;
}
}

export default Account;
15 changes: 15 additions & 0 deletions packages/extension/src/client/apis/accountFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import ConnectionService from '~/client/services/ConnectionService';
import Account from './Account';

export default async function accountFactory(address) {
const { account } = await ConnectionService.query(
'user',
{ id: address },
) || {};

return new Account({
...account,
address,
id: address,
});
}
4 changes: 4 additions & 0 deletions packages/extension/src/client/config/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export default {
linked: 'Address has no linked AZTEC extension account.',
},
},
user: {
unregistered: "Cannot call '%{fn}' on unregistered user.",
logout: "'%{fn}' can only be called on current logged in account.",
},
input: {
contract: {
address: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ export default async function ensurePermission() {
const {
address,
linkedPublicKey,
spendingPublicKey,
} = account;

return {
account: {
address,
linkedPublicKey,
spendingPublicKey,
},
domain,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getNetworkName,
} from '~/utils/network';
import Web3Service from '~/client/services/Web3Service';
import accountFactory from '~/client/apis/accountFactory';
import assetFactory from '~/client/apis/assetFactory';
import noteFactory from '~/client/apis/noteFactory';

Expand Down Expand Up @@ -51,6 +52,7 @@ export default async function generateApis(hasPermission = false) {
// TODO - assign mock modules that show warnings when calling apis before enabled

return {
user: hasPermission ? accountFactory : null,
zkAsset: hasPermission ? assetFactory : null,
zkNote: hasPermission ? noteFactory : null,
web3,
Expand Down

0 comments on commit f7166ed

Please sign in to comment.