Skip to content

Commit

Permalink
chore: make tokenRegistry required for decrypt()
Browse files Browse the repository at this point in the history
  • Loading branch information
wanseob committed Nov 16, 2021
1 parent dde42e7 commit 90f0179
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
2 changes: 1 addition & 1 deletion packages/account/src/viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class ZkViewer {
return this.A
}

decrypt(zkTx: ZkTx, tokenRegistry?: TokenRegistry): Utxo[] {
decrypt(zkTx: ZkTx, tokenRegistry: TokenRegistry): Utxo[] {
const tryDecodeNote = (
bytes: Buffer,
outflows: ZkOutflow[],
Expand Down
3 changes: 2 additions & 1 deletion packages/dataset/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"dist"
],
"_moduleAliases": {
"~dataset": "dist"
"~dataset": "dist",
"~transaction": "../transaction/dist"
},
"scripts": {
"prebuild": "shx mkdir -p dist",
Expand Down
4 changes: 3 additions & 1 deletion packages/dataset/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @jest-environment node
*/

import { TokenRegistry } from '~transaction/tokens'
import { getDummyBody } from '../src/testset-block'
import { accounts } from '../src/testset-predefined'
import { loadZkTxs } from '../src/testset-zktxs'
Expand All @@ -15,7 +16,8 @@ describe('index', () => {
it('encrypt-decrypt works', async () => {
const zktxs = await loadZkTxs()
const tx1 = zktxs[0]
const note = accounts.bob.decrypt(tx1)
const tokenRegistry: TokenRegistry = new TokenRegistry()
const note = accounts.bob.decrypt(tx1, tokenRegistry)
expect(note[0]).toBeDefined()
expect(note[0].owner.toString()).toBe(accounts.bob.zkAddress.toString())
}, 60000)
Expand Down
50 changes: 24 additions & 26 deletions packages/transaction/src/utxo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class Utxo extends Note {
memo: Buffer
spendingPubKey: Fp
viewingKey: Fr
tokenRegistry?: TokenRegistry
tokenRegistry: TokenRegistry
}): Utxo | undefined {
const ephemeralPubKey = Point.decode(memo.subarray(0, 32))
const sharedKey = ephemeralPubKey.mul(viewingKey).encode()
Expand All @@ -134,32 +134,30 @@ export class Utxo extends Note {
return etherNote
}
// Try to find ERC20 or ERC721 notes
if (tokenRegistry) {
const erc20Addresses = tokenRegistry.getErc20Addresses(tokenIdentifier)
for (const tokenAddr of erc20Addresses) {
const erc20Note = Utxo.newERC20Note({
owner,
eth: Fp.from(0),
tokenAddr,
erc20Amount: value,
salt,
})
if (utxoHash.eq(erc20Note.hash())) {
return erc20Note
}
const erc20Addresses = tokenRegistry.getErc20Addresses(tokenIdentifier)
for (const tokenAddr of erc20Addresses) {
const erc20Note = Utxo.newERC20Note({
owner,
eth: Fp.from(0),
tokenAddr,
erc20Amount: value,
salt,
})
if (utxoHash.eq(erc20Note.hash())) {
return erc20Note
}
const erc721Addresses = tokenRegistry.getErc721Addresses(tokenIdentifier)
for (const tokenAddr of erc721Addresses) {
const nftNote = Utxo.newNFTNote({
owner,
eth: Fp.from(0),
tokenAddr,
nft: value,
salt,
})
if (utxoHash.eq(nftNote.hash())) {
return nftNote
}
}
const erc721Addresses = tokenRegistry.getErc721Addresses(tokenIdentifier)
for (const tokenAddr of erc721Addresses) {
const nftNote = Utxo.newNFTNote({
owner,
eth: Fp.from(0),
tokenAddr,
nft: value,
salt,
})
if (utxoHash.eq(nftNote.hash())) {
return nftNote
}
}
return undefined
Expand Down

0 comments on commit 90f0179

Please sign in to comment.