From 778eb3b425b5ab5b23d28e75be92feccd0fc56bc Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Fri, 27 Sep 2019 21:56:42 -0400 Subject: [PATCH] Added less-common, but useful, coding functions to Interface. --- packages/abi/src.ts/interface.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/packages/abi/src.ts/interface.ts b/packages/abi/src.ts/interface.ts index e232d5b896..33d40b8a14 100644 --- a/packages/abi/src.ts/interface.ts +++ b/packages/abi/src.ts/interface.ts @@ -192,6 +192,10 @@ export class Interface { } + _decodeParams(params: Array, data: BytesLike): Array { + return this._abiCoder.decode(params, data) + } + _encodeParams(params: Array, values: Array): string { return this._abiCoder.encode(params, values) } @@ -200,6 +204,20 @@ export class Interface { return this._encodeParams(this.deploy.inputs, values || [ ]); } + decodeFunctionData(functionFragment: FunctionFragment | string, data: BytesLike): Array { + if (typeof(functionFragment) === "string") { + functionFragment = this.getFunction(functionFragment); + } + + const bytes = arrayify(data); + + if (hexlify(bytes.slice(0, 4)) !== this.getSighash(functionFragment)) { + logger.throwArgumentError(`data signature does not match function ${ functionFragment.name }.`, "data", hexlify(bytes)); + } + + return this._decodeParams(functionFragment.inputs, bytes.slice(4)); + } + encodeFunctionData(functionFragment: FunctionFragment | string, values?: Array): string { if (typeof(functionFragment) === "string") { functionFragment = this.getFunction(functionFragment); @@ -243,6 +261,14 @@ export class Interface { }); } + encodeFunctionResult(functionFragment: FunctionFragment | string, values?: Array): string { + if (typeof(functionFragment) === "string") { + functionFragment = this.getFunction(functionFragment); + } + + return hexlify(this._abiCoder.encode(functionFragment.outputs, values || [ ])); + } + encodeFilterTopics(eventFragment: EventFragment, values: Array): Array> { if (typeof(eventFragment) === "string") { eventFragment = this.getEvent(eventFragment);