From fa83a1ab16a3eea725ae390540f865b28eeb310b Mon Sep 17 00:00:00 2001 From: micha vie Date: Thu, 16 Nov 2023 15:29:08 +0100 Subject: [PATCH 1/4] add code instantiation from hex --- src/smartcontracts/code.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/smartcontracts/code.ts b/src/smartcontracts/code.ts index 6a3c3b55..6d9d5861 100644 --- a/src/smartcontracts/code.ts +++ b/src/smartcontracts/code.ts @@ -15,6 +15,13 @@ export class Code { return new Code(code.toString("hex")); } + /** + * Creates a Code object from a hex-encoded string. + */ + static fromHex(hex: string): Code { + return new Code(hex) + } + /** * Returns the bytecode as a hex-encoded string. */ From 78780458dcf343face3fc2ebcb31210f0639f3f1 Mon Sep 17 00:00:00 2001 From: micha vie Date: Thu, 16 Nov 2023 15:29:17 +0100 Subject: [PATCH 2/4] add code hash computation --- src/smartcontracts/code.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/smartcontracts/code.ts b/src/smartcontracts/code.ts index 6d9d5861..bd069387 100644 --- a/src/smartcontracts/code.ts +++ b/src/smartcontracts/code.ts @@ -1,3 +1,8 @@ +import { Hash } from "../hash"; + +const createHasher = require('blake2b') +const CODE_HASH_LENGTH = 32 + /** * Bytecode of a Smart Contract, as an abstraction. */ @@ -32,4 +37,12 @@ export class Code { valueOf(): Buffer { return Buffer.from(this.hex, "hex"); } + + computeHash(): Hash { + const hash = createHasher(CODE_HASH_LENGTH) + .update(this.valueOf()) + .digest('hex') + + return new Hash(hash) + } } From 6a87354b5e19c84259c2fb0775a2dd5f3a8c0782 Mon Sep 17 00:00:00 2001 From: micha vie Date: Thu, 16 Nov 2023 15:29:37 +0100 Subject: [PATCH 3/4] add code tests --- src/smartcontracts/code.spec.ts | 37 +++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/smartcontracts/code.spec.ts diff --git a/src/smartcontracts/code.spec.ts b/src/smartcontracts/code.spec.ts new file mode 100644 index 00000000..0fa090df --- /dev/null +++ b/src/smartcontracts/code.spec.ts @@ -0,0 +1,37 @@ +import { assert } from "chai"; +import { Code } from "./code"; +import { Hash } from "../hash"; + +describe("Code Class Tests", function() { + const sampleHex = "abcdef0123456789"; + const sampleBuffer = Buffer.from(sampleHex, "hex"); + + it("should create Code from buffer", function() { + const code = Code.fromBuffer(sampleBuffer); + + assert.instanceOf(code, Code); + assert.equal(code.toString(), sampleHex); + }); + + it("should create Code from hex string", function() { + const code = Code.fromHex(sampleHex); + + assert.instanceOf(code, Code); + assert.equal(code.toString(), sampleHex); + }); + + it("should return the correct buffer from valueOf", function() { + const code = Code.fromHex(sampleHex); + const buffer = code.valueOf(); + + assert.isTrue(Buffer.isBuffer(buffer)); + assert.equal(buffer.toString("hex"), sampleHex); + }); + + it("should compute hash correctly", function() { + const code = Code.fromHex(sampleHex); + const hash = code.computeHash(); + + assert.instanceOf(hash, Hash); + }); +}); From f4b91cd7ff712de00ab6d01b62d0aa0a540ee318 Mon Sep 17 00:00:00 2001 From: micha vie Date: Fri, 17 Nov 2023 11:22:07 +0100 Subject: [PATCH 4/4] update code hash computation to buffer output --- src/smartcontracts/code.spec.ts | 3 ++- src/smartcontracts/code.ts | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/smartcontracts/code.spec.ts b/src/smartcontracts/code.spec.ts index 0fa090df..1a5f3d94 100644 --- a/src/smartcontracts/code.spec.ts +++ b/src/smartcontracts/code.spec.ts @@ -32,6 +32,7 @@ describe("Code Class Tests", function() { const code = Code.fromHex(sampleHex); const hash = code.computeHash(); - assert.instanceOf(hash, Hash); + assert.instanceOf(hash, Buffer); + assert.equal(hash.toString('hex'), 'ac86b78afd9bdda3641a47a4aff2a7ee26acd40cc534d63655e9dfbf3f890a02') }); }); diff --git a/src/smartcontracts/code.ts b/src/smartcontracts/code.ts index bd069387..54c540a3 100644 --- a/src/smartcontracts/code.ts +++ b/src/smartcontracts/code.ts @@ -38,11 +38,11 @@ export class Code { return Buffer.from(this.hex, "hex"); } - computeHash(): Hash { + computeHash(): Buffer { const hash = createHasher(CODE_HASH_LENGTH) .update(this.valueOf()) - .digest('hex') + .digest(); - return new Hash(hash) + return Buffer.from(hash) } }