This repository has been archived by the owner on Mar 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk): add user api to fetch aztec account by address
- Loading branch information
Showing
19 changed files
with
229 additions
and
41 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
packages/extension/src/background/services/ApiService/Account/decrypt.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}; | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/extension/src/background/services/ApiService/Account/encrypt.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}, | ||
}; | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/extension/src/background/services/ApiService/Account/user.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
`)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
packages/extension/src/background/services/ApiService/schemas/decryptMessage.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}); |
12 changes: 12 additions & 0 deletions
12
packages/extension/src/background/services/ApiService/schemas/encryptMessage.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
packages/extension/src/background/services/ApiService/schemas/user.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); |
33 changes: 0 additions & 33 deletions
33
packages/extension/src/background/services/ApiService/utils/validateExtensionAccount.js
This file was deleted.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
packages/extension/src/background/services/GraphQLService/Queries/userQuery.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters