-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(evm-api): implement
web3_sha3
(#791)
* Add web3_sha3 * Register handler * Add schema * Calculate * Add test
- Loading branch information
1 parent
73552a6
commit 28ce419
Showing
4 changed files
with
81 additions
and
0 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
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,57 @@ | ||
import { Identifiers } from "@mainsail/contracts"; | ||
import { Validator } from "@mainsail/validation"; | ||
|
||
import { describe, Sandbox } from "../../../test-framework/source"; | ||
import { Web3Sha3 } from "./index.js"; | ||
|
||
describe<{ | ||
sandbox: Sandbox; | ||
action: Web3Sha3; | ||
validator: Validator; | ||
}>("Web3Sha3", ({ beforeEach, it, assert }) => { | ||
const version = "0.0.1"; | ||
|
||
beforeEach(async (context) => { | ||
context.sandbox = new Sandbox(); | ||
|
||
context.sandbox.app.bind(Identifiers.Application.Version).toConstantValue(version); | ||
|
||
context.action = context.sandbox.app.resolve(Web3Sha3); | ||
context.validator = context.sandbox.app.resolve(Validator); | ||
}); | ||
|
||
it("should have a name", ({ action }) => { | ||
assert.equal(action.name, "web3_sha3"); | ||
}); | ||
|
||
it("schema should be ok", ({ action, validator }) => { | ||
validator.addSchema({ | ||
$id: "prefixedHex", | ||
pattern: "^0x[0-9a-f]+$", | ||
type: "string", | ||
}); | ||
|
||
assert.equal(action.schema, { | ||
$id: `jsonRpc_web3_sha3`, | ||
maxItems: 1, | ||
minItems: 1, | ||
|
||
prefixItems: [{ $ref: "prefixedHex" }], | ||
type: "array", | ||
}); | ||
|
||
validator.addSchema(action.schema); | ||
|
||
assert.undefined(validator.validate("jsonRpc_web3_sha3", ["0x0"]).errors); | ||
assert.defined(validator.validate("jsonRpc_web3_sha3", ["0x0", ""]).errors); | ||
assert.defined(validator.validate("jsonRpc_web3_sha3", [1]).errors); | ||
assert.defined(validator.validate("jsonRpc_web3_sha3", {}).errors); | ||
}); | ||
|
||
it("should return the keccak256 of provided data", async ({ action }) => { | ||
assert.equal( | ||
await action.handle(["0x68656c6c6f20776f726c64"]), | ||
"0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad", | ||
); | ||
}); | ||
}); |
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,21 @@ | ||
import { injectable } from "@mainsail/container"; | ||
import { Contracts } from "@mainsail/contracts"; | ||
import { keccak256 } from "ethers"; | ||
|
||
@injectable() | ||
export class Web3Sha3 implements Contracts.Api.RPC.Action { | ||
public readonly name: string = "web3_sha3"; | ||
|
||
public readonly schema = { | ||
$id: `jsonRpc_${this.name}`, | ||
maxItems: 1, | ||
minItems: 1, | ||
|
||
prefixItems: [{ $ref: "prefixedHex" }], | ||
type: "array", | ||
}; | ||
|
||
public async handle(parameters: [string]): Promise<string> { | ||
return `0x${keccak256(parameters[0]).slice(2)}`; | ||
} | ||
} |
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