Skip to content

Commit

Permalink
fix #741 and #646
Browse files Browse the repository at this point in the history
  • Loading branch information
rahul-kothari committed Jun 8, 2023
1 parent 596d52d commit 53fd463
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 25 deletions.
1 change: 1 addition & 0 deletions yarn-project/aztec.js/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './contract/index.js';
export * from './contract_deployer/index.js';
export * from './utils/index.js';

// TODO - only export necessary stuffs
export * from '@aztec/aztec-rpc';
1 change: 1 addition & 0 deletions yarn-project/aztec.js/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './utils.js';
14 changes: 14 additions & 0 deletions yarn-project/aztec.js/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { CircuitsWasm, Fr } from '@aztec/circuits.js';
import { computeSecretMessageHash } from '@aztec/circuits.js/abis';

/**
* Given a secret, it computes its pederson hash - used to send l1 to l2 messages
* @param secret - the secret to hash (defaults to a random field element)
* @returns secret and its pederson hash (in hex).
*/
export async function createMessageSecretAndHash(secret = Fr.random()) {
const wasm = await CircuitsWasm.get();
const secretHash = computeSecretMessageHash(wasm, secret);
const secretHashHex = `0x${secretHash.toBuffer().toString('hex')}` as `0x${string}`;
return secretHashHex;
}
11 changes: 3 additions & 8 deletions yarn-project/end-to-end/src/e2e_cross_chain_messaging.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { AztecNodeService } from '@aztec/aztec-node';
import { AztecAddress, AztecRPCServer, Contract, TxStatus } from '@aztec/aztec.js';
import { AztecAddress, AztecRPCServer, Contract, TxStatus, createMessageSecretAndHash } from '@aztec/aztec.js';
import { EthAddress } from '@aztec/foundation/eth-address';

import { CircuitsWasm } from '@aztec/circuits.js';
import { computeSecretMessageHash } from '@aztec/circuits.js/abis';
import { DeployL1Contracts } from '@aztec/ethereum';
import { toBigIntBE, toBufferBE } from '@aztec/foundation/bigint-buffer';
import { Fr } from '@aztec/foundation/fields';
Expand Down Expand Up @@ -88,12 +86,10 @@ describe('e2e_cross_chain_messaging', () => {

it('Milestone 2: Deposit funds from L1 -> L2 and withdraw back to L1', async () => {
// Generate a claim secret using pedersen
// TODO (#741): make this into an aztec.js utility function
logger("Generating a claim secret using pedersen's hash function");
const wasm = await CircuitsWasm.get();
const secret = Fr.random();
const claimSecretHash = computeSecretMessageHash(wasm, secret);
logger('Generated claim secret: ', claimSecretHash.toString());
const secretString = await createMessageSecretAndHash(secret);
logger('Generated claim secret: ', secretString);

logger('Minting tokens on L1');
await underlyingERC20.write.mint([ethAccount.toString(), 1000000n], {} as any);
Expand All @@ -102,7 +98,6 @@ describe('e2e_cross_chain_messaging', () => {
expect(await underlyingERC20.read.balanceOf([ethAccount.toString()])).toBe(1000000n);

// Deposit tokens to the TokenPortal
const secretString = `0x${claimSecretHash.toBuffer().toString('hex')}` as `0x${string}`;
const deadline = 2 ** 32 - 1; // max uint32 - 1

const mintAmount = 100n;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { AztecNodeConfig, AztecNodeService } from '@aztec/aztec-node';
import { AztecAddress, AztecRPCServer, Contract } from '@aztec/aztec.js';
import { AztecAddress, AztecRPCServer, Contract, createMessageSecretAndHash } from '@aztec/aztec.js';
import { EthAddress } from '@aztec/foundation/eth-address';

import { CircuitsWasm } from '@aztec/circuits.js';
import { computeSecretMessageHash } from '@aztec/circuits.js/abis';
import { DeployL1Contracts } from '@aztec/ethereum';
import { Fr } from '@aztec/foundation/fields';
import { DebugLogger } from '@aztec/foundation/log';
Expand Down Expand Up @@ -84,12 +82,10 @@ describe('archiver integration with l1 to l2 messages', () => {
// create a message, then cancel it

// Generate a claim secret using pedersen
// TODO (#741): make this into an aztec.js utility function
logger("Generating a claim secret using pedersen's hash function");
const wasm = await CircuitsWasm.get();
const secret = Fr.random();
const claimSecretHash = computeSecretMessageHash(wasm, secret);
logger('Generated claim secret: ', claimSecretHash.toString());
const secretString = await createMessageSecretAndHash(secret);
logger('Generated claim secret: ', secretString);

logger('Minting tokens on L1');
await underlyingERC20.write.mint([ethAccount.toString(), 1000000n], {} as any);
Expand All @@ -98,7 +94,6 @@ describe('archiver integration with l1 to l2 messages', () => {
expect(await underlyingERC20.read.balanceOf([ethAccount.toString()])).toBe(1000000n);

// Deposit tokens to the TokenPortal
const secretString = `0x${claimSecretHash.toBuffer().toString('hex')}` as `0x${string}`;
const deadline = Number((await publicClient.getBlock()).timestamp + 1000n);
const mintAmount = 100n;

Expand Down
9 changes: 0 additions & 9 deletions yarn-project/types/src/l1_to_l2_message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { EthAddress } from '@aztec/foundation/eth-address';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { Fr } from '@aztec/foundation/fields';
import { BufferReader, serializeToBuffer } from '@aztec/circuits.js/utils';
import { sha256 } from '@aztec/foundation/crypto';
import { toBigIntBE, toBufferBE } from '@aztec/foundation/bigint-buffer';

/**
* Interface of classes allowing for the retrieval of L1 to L2 messages.
Expand Down Expand Up @@ -74,13 +72,6 @@ export class L1ToL2Message {
public readonly entryKey?: Fr,
) {}

// TODO: (#646) - sha256 hash of the message packed the same as solidity
hash(): Fr {
const buf = this.toBuffer();
const temp = toBigIntBE(sha256(buf));
return Fr.fromBuffer(toBufferBE(temp % Fr.MODULUS, 32));
}

/**
* Returns each element within its own field so that it can be consumed by an acvm oracle call.
* @returns The message as an array of fields (in order).
Expand Down

0 comments on commit 53fd463

Please sign in to comment.