@aztec/foundation
Advanced tools
Comparing version 0.6.7 to 0.7.0
@@ -22,7 +22,15 @@ import { ABIParameter, FunctionAbi } from '@aztec/foundation/abi'; | ||
private parameters; | ||
constructor(name: string, parameters: ABIParameter[]); | ||
private includeNames; | ||
private separator; | ||
constructor(name: string, parameters: ABIParameter[], includeNames?: boolean); | ||
/** | ||
* Decodes a single function parameter type for the function signature. | ||
* @param param - The parameter type to decode. | ||
* @returns A string representing the parameter type. | ||
*/ | ||
private getParameterType; | ||
/** | ||
* Decodes a single function parameter for the function signature. | ||
* @param param - The parameter to decode. | ||
* @returns A string representing the parameter type. | ||
* @returns A string representing the parameter type and optionally its name. | ||
*/ | ||
@@ -38,3 +46,3 @@ private decodeParameter; | ||
* Decodes a function signature from the name and parameters. | ||
* @param name - The name of the function- | ||
* @param name - The name of the function. | ||
* @param parameters - The parameters of the function. | ||
@@ -44,2 +52,9 @@ * @returns - The function signature. | ||
export declare function decodeFunctionSignature(name: string, parameters: ABIParameter[]): string; | ||
/** | ||
* Decodes a function signature from the name and parameters including parameter names. | ||
* @param name - The name of the function. | ||
* @param parameters - The parameters of the function. | ||
* @returns - The user-friendly function signature. | ||
*/ | ||
export declare function decodeFunctionSignatureWithParameterNames(name: string, parameters: ABIParameter[]): string; | ||
//# sourceMappingURL=decoder.d.ts.map |
@@ -84,12 +84,14 @@ /** | ||
export class FunctionSignatureDecoder { | ||
constructor(name, parameters) { | ||
constructor(name, parameters, includeNames = false) { | ||
this.name = name; | ||
this.parameters = parameters; | ||
this.includeNames = includeNames; | ||
this.separator = includeNames ? ', ' : ','; | ||
} | ||
/** | ||
* Decodes a single function parameter for the function signature. | ||
* @param param - The parameter to decode. | ||
* Decodes a single function parameter type for the function signature. | ||
* @param param - The parameter type to decode. | ||
* @returns A string representing the parameter type. | ||
*/ | ||
decodeParameter(param) { | ||
getParameterType(param) { | ||
switch (param.kind) { | ||
@@ -106,5 +108,5 @@ case 'field': | ||
case 'array': | ||
return `[${this.decodeParameter(param.type)};${param.length}]`; | ||
return `[${this.getParameterType(param.type)};${param.length}]`; | ||
case 'struct': | ||
return `(${param.fields.map(field => `${this.decodeParameter(field.type)}`).join(',')})`; | ||
return `(${param.fields.map(field => `${this.decodeParameter(field)}`).join(this.separator)})`; | ||
default: | ||
@@ -115,2 +117,11 @@ throw new Error(`Unsupported type: ${param.kind}`); | ||
/** | ||
* Decodes a single function parameter for the function signature. | ||
* @param param - The parameter to decode. | ||
* @returns A string representing the parameter type and optionally its name. | ||
*/ | ||
decodeParameter(param) { | ||
const type = this.getParameterType(param.type); | ||
return this.includeNames ? `${param.name}: ${type}` : type; | ||
} | ||
/** | ||
* Decodes all the parameters and build the function signature | ||
@@ -120,3 +131,3 @@ * @returns The function signature. | ||
decode() { | ||
return `${this.name}(${this.parameters.map(param => this.decodeParameter(param.type)).join(',')})`; | ||
return `${this.name}(${this.parameters.map(param => this.decodeParameter(param)).join(this.separator)})`; | ||
} | ||
@@ -126,3 +137,3 @@ } | ||
* Decodes a function signature from the name and parameters. | ||
* @param name - The name of the function- | ||
* @param name - The name of the function. | ||
* @param parameters - The parameters of the function. | ||
@@ -134,2 +145,11 @@ * @returns - The function signature. | ||
} | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGVjb2Rlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9hYmkvZGVjb2Rlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFRQTs7O0dBR0c7QUFDSCxNQUFNLG1CQUFtQjtJQUN2QixZQUFvQixHQUFnQixFQUFVLFNBQWU7UUFBekMsUUFBRyxHQUFILEdBQUcsQ0FBYTtRQUFVLGNBQVMsR0FBVCxTQUFTLENBQU07SUFBRyxDQUFDO0lBRWpFOzs7O09BSUc7SUFDSyxZQUFZLENBQUMsT0FBZ0I7UUFDbkMsUUFBUSxPQUFPLENBQUMsSUFBSSxFQUFFO1lBQ3BCLEtBQ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yxLQUFLLENBQUMsRUFBRSxDQUFDLElBQUksQ0FBQyxlQUFlLENBQUMsS0FBSyxDQUFDLElBQUksQ0FBQyxDQUFDLENBQUMsSUFBSSxDQUFDLEdBQUcsQ0FBQyxHQUFHLENBQUM7SUFDckcsQ0FBQztDQUNGO0FBRUQ7Ozs7O0dBS0c7QUFDSCxNQUFNLFVBQVUsdUJBQXVCLENBQUMsSUFBWSxFQUFFLFVBQTBCO0lBQzlFLE9BQU8sSUFBSSx3QkFBd0IsQ0FBQyxJQUFJLEVBQUUsVUFBVSxDQUFDLENBQUMsTUFBTSxFQUFFLENBQUM7QUFDakUsQ0FBQyJ9 | ||
/** | ||
* Decodes a function signature from the name and parameters including parameter names. | ||
* @param name - The name of the function. | ||
* @param parameters - The parameters of the function. | ||
* @returns - The user-friendly function signature. | ||
*/ | ||
export function decodeFunctionSignatureWithParameterNames(name, parameters) { | ||
return new FunctionSignatureDecoder(name, parameters, true).decode(); | ||
} | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGVjb2Rlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9hYmkvZGVjb2Rlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFRQTs7O0dBR0c7QUFDSCxNQUFNLG1CQUFtQjtJQUN2QixZQUFvQixHQUFnQixFQUFVLFNBQWU7UUFBekMsUUFBRyxHQUFILEdBQUcsQ0FBYTtRQUFVLGNBQVMsR0FBVCxTQUFTLENBQU07SUFBRyxDQUFDO0lBRWpFOzs7O09BSUc7SUFDSyxZQUFZLENBQUMsT0FBZ0I7UUFDbkMsUUFBUSxPQUFPLENBQUMsSUFBSSxFQUFFO1lBQ3BCLEtBQUssT0FBTztnQkFDVixPQUFPLElBQUksQ0FBQyxZQUFZLEVBQUUsQ0FBQyxLQUFLLENBQUM7WUFDbkMsS0FBSyxTQUFTO2dCQUNaLElBQUksT0FBTyxD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SxHQUFHLElBQUksQ0FBQyxnQkFBZ0IsQ0FBQyxLQUFLLENBQUMsSUFBSSxDQUFDLENBQUM7UUFDL0MsT0FBTyxJQUFJLENBQUMsWUFBWSxDQUFDLENBQUMsQ0FBQyxHQUFHLEtBQUssQ0FBQyxJQUFJLEtBQUssSUFBSSxFQUFFLENBQUMsQ0FBQyxDQUFDLElBQUksQ0FBQztJQUM3RCxDQUFDO0lBRUQ7OztPQUdHO0lBQ0ksTUFBTTtRQUNYLE9BQU8sR0FBRyxJQUFJLENBQUMsSUFBSSxJQUFJLElBQUksQ0FBQyxVQUFVLENBQUMsR0FBRyxDQUFDLEtBQUssQ0FBQyxFQUFFLENBQUMsSUFBSSxDQUFDLGVBQWUsQ0FBQyxLQUFLLENBQUMsQ0FBQyxDQUFDLElBQUksQ0FBQyxJQUFJLENBQUMsU0FBUyxDQUFDLEdBQUcsQ0FBQztJQUMzRyxDQUFDO0NBQ0Y7QUFFRDs7Ozs7R0FLRztBQUNILE1BQU0sVUFBVSx1QkFBdUIsQ0FBQyxJQUFZLEVBQUUsVUFBMEI7SUFDOUUsT0FBTyxJQUFJLHdCQUF3QixDQUFDLElBQUksRUFBRSxVQUFVLENBQUMsQ0FBQyxNQUFNLEVBQUUsQ0FBQztBQUNqRSxDQUFDO0FBRUQ7Ozs7O0dBS0c7QUFDSCxNQUFNLFVBQVUseUNBQXlDLENBQUMsSUFBWSxFQUFFLFVBQTBCO0lBQ2hHLE9BQU8sSUFBSSx3QkFBd0IsQ0FBQyxJQUFJLEVBQUUsVUFBVSxFQUFFLElBQUksQ0FBQyxDQUFDLE1BQU0sRUFBRSxDQUFDO0FBQ3ZFLENBQUMifQ== |
{ | ||
"name": "@aztec/foundation", | ||
"version": "0.6.7", | ||
"version": "0.7.0", | ||
"exports": { | ||
@@ -5,0 +5,0 @@ "./eslint": "./.eslintrc.cjs", |
@@ -1,2 +0,2 @@ | ||
import { ABIParameter, ABIType, FunctionAbi } from '@aztec/foundation/abi'; | ||
import { ABIParameter, ABIType, ABIVariable, FunctionAbi } from '@aztec/foundation/abi'; | ||
import { Fr } from '@aztec/foundation/fields'; | ||
@@ -94,10 +94,13 @@ | ||
export class FunctionSignatureDecoder { | ||
constructor(private name: string, private parameters: ABIParameter[]) {} | ||
private separator: string; | ||
constructor(private name: string, private parameters: ABIParameter[], private includeNames = false) { | ||
this.separator = includeNames ? ', ' : ','; | ||
} | ||
/** | ||
* Decodes a single function parameter for the function signature. | ||
* @param param - The parameter to decode. | ||
* Decodes a single function parameter type for the function signature. | ||
* @param param - The parameter type to decode. | ||
* @returns A string representing the parameter type. | ||
*/ | ||
private decodeParameter(param: ABIType): string { | ||
private getParameterType(param: ABIType): string { | ||
switch (param.kind) { | ||
@@ -114,5 +117,5 @@ case 'field': | ||
case 'array': | ||
return `[${this.decodeParameter(param.type)};${param.length}]`; | ||
return `[${this.getParameterType(param.type)};${param.length}]`; | ||
case 'struct': | ||
return `(${param.fields.map(field => `${this.decodeParameter(field.type)}`).join(',')})`; | ||
return `(${param.fields.map(field => `${this.decodeParameter(field)}`).join(this.separator)})`; | ||
default: | ||
@@ -124,2 +127,12 @@ throw new Error(`Unsupported type: ${param.kind}`); | ||
/** | ||
* Decodes a single function parameter for the function signature. | ||
* @param param - The parameter to decode. | ||
* @returns A string representing the parameter type and optionally its name. | ||
*/ | ||
private decodeParameter(param: ABIVariable): string { | ||
const type = this.getParameterType(param.type); | ||
return this.includeNames ? `${param.name}: ${type}` : type; | ||
} | ||
/** | ||
* Decodes all the parameters and build the function signature | ||
@@ -129,3 +142,3 @@ * @returns The function signature. | ||
public decode(): string { | ||
return `${this.name}(${this.parameters.map(param => this.decodeParameter(param.type)).join(',')})`; | ||
return `${this.name}(${this.parameters.map(param => this.decodeParameter(param)).join(this.separator)})`; | ||
} | ||
@@ -136,3 +149,3 @@ } | ||
* Decodes a function signature from the name and parameters. | ||
* @param name - The name of the function- | ||
* @param name - The name of the function. | ||
* @param parameters - The parameters of the function. | ||
@@ -144,1 +157,11 @@ * @returns - The function signature. | ||
} | ||
/** | ||
* Decodes a function signature from the name and parameters including parameter names. | ||
* @param name - The name of the function. | ||
* @param parameters - The parameters of the function. | ||
* @returns - The user-friendly function signature. | ||
*/ | ||
export function decodeFunctionSignatureWithParameterNames(name: string, parameters: ABIParameter[]) { | ||
return new FunctionSignatureDecoder(name, parameters, true).decode(); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1040619
475
17013