-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
117 additions
and
123 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,9 +1,5 @@ | ||
import { FUNCTION_SELECTOR_NUM_BYTES } from '@aztec/circuits.js'; | ||
import { computeFunctionSelector } from '@aztec/foundation/abi'; | ||
import { FunctionSelector } from '@aztec/circuits.js'; | ||
|
||
export const computeNoteHashAndNullifierSignature = 'compute_note_hash_and_nullifier(field,field,field,array)'; | ||
|
||
export const computeNoteHashAndNullifierSelector = computeFunctionSelector( | ||
computeNoteHashAndNullifierSignature, | ||
FUNCTION_SELECTOR_NUM_BYTES, | ||
); | ||
export const computeNoteHashAndNullifierSelector = FunctionSelector.fromSignature(computeNoteHashAndNullifierSignature); |
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
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,75 @@ | ||
import { ABIParameter } from '@aztec/foundation/abi'; | ||
import { keccak } from '@aztec/foundation/crypto'; | ||
import { BufferReader } from '@aztec/foundation/serialize'; | ||
|
||
import { FUNCTION_SELECTOR_NUM_BYTES } from '../cbind/constants.gen.js'; | ||
|
||
/** | ||
* A function selector is the first 4 bytes of the hash of a function signature. | ||
*/ | ||
export class FunctionSelector { | ||
/** | ||
* The size of the hash in bytes. | ||
*/ | ||
public static SIZE = FUNCTION_SELECTOR_NUM_BYTES; | ||
|
||
constructor(/** buffer containing the function selector */ public value: Buffer) { | ||
if (value.length !== FunctionSelector.SIZE) { | ||
throw new Error(`Function selector must be ${FunctionSelector.SIZE} bytes long, got ${value.length} bytes.`); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the function selector is empty (all bytes are 0). | ||
* @returns True if the function selector is empty (all bytes are 0). | ||
*/ | ||
public isEmpty(): boolean { | ||
return this.value.equals(Buffer.alloc(FunctionSelector.SIZE)); | ||
} | ||
|
||
/** | ||
* Serialize as a buffer. | ||
* @returns The buffer. | ||
*/ | ||
toBuffer(): Buffer { | ||
return this.value; | ||
} | ||
|
||
/** | ||
* Deserializes from a buffer or reader, corresponding to a write in cpp. | ||
* @param buffer - Buffer or BufferReader to read from. | ||
* @returns The FunctionSelector. | ||
*/ | ||
static fromBuffer(buffer: Buffer | BufferReader): FunctionSelector { | ||
const reader = BufferReader.asReader(buffer); | ||
return new FunctionSelector(reader.readBytes(FunctionSelector.SIZE)); | ||
} | ||
|
||
/** | ||
* Creates a function selector from a signature. | ||
* @param signature - Signature of the function to generate the selector for (e.g. "transfer(field,field)"). | ||
* @returns Function selector. | ||
*/ | ||
static fromSignature(signature: string): FunctionSelector { | ||
return new FunctionSelector(keccak(Buffer.from(signature)).subarray(0, FunctionSelector.SIZE)); | ||
} | ||
|
||
/** | ||
* Creates a function selector for a given function name and parameters. | ||
* @param name - The name of the function. | ||
* @param parameters - An array of ABIParameter objects, each containing the type information of a function parameter. | ||
* @returns A Buffer containing the 4-byte function selector. | ||
*/ | ||
static fromNameAndParameters(name: string, parameters: ABIParameter[]) { | ||
const signature = name === 'constructor' ? name : `${name}(${parameters.map(p => p.type.kind).join(',')})`; | ||
return FunctionSelector.fromSignature(signature); | ||
} | ||
|
||
/** | ||
* Creates an empty function selector. | ||
* @returns An empty function selector. | ||
*/ | ||
static empty(): FunctionSelector { | ||
return new FunctionSelector(Buffer.alloc(FunctionSelector.SIZE)); | ||
} | ||
} |
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
Oops, something went wrong.