Skip to content

Commit

Permalink
fix(event-attestator): add blockhash + txhash
Browse files Browse the repository at this point in the history
  • Loading branch information
gitmp01 committed Jun 21, 2024
1 parent 22517a2 commit 8c399e2
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 241 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
],
"scripts": {
"test": "yarn workspaces run test",
"build": "yarn workspaces run build",
"prettier": "yarn workspaces run prettier"
},
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"eslint": "8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-jest": "^28.6.0",
"prettier": "3.3.2",
"prettier-plugin-solidity": "^1.3.1",
"typescript-eslint": "^7.13.1"
"prettier-plugin-solidity": "^1.3.1"
}
}
49 changes: 12 additions & 37 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,39 +1,14 @@
{
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"incremental": false,
"lib": [
"ES2015",
"ES2016",
"ES2017",
"ES2018",
"ES2019",
"ES2020",
"ES2021",
"DOM"
],
"module": "nodenext",
"moduleResolution": "nodenext",
"noEmitOnError": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"preserveSymlinks": true,
"preserveWatchOutput": true,
"pretty": false,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"target": "es2022",
},
"ts-node": {
"experimentalSpecifierResolution": "node",
"experimentalResolver": true,
"files": true
}
"exclude": [
"**/test/**",
"**/dist/**"
],
"references": [
{
"path": "./solidity",
},
{
"path": "./typescript"
}
]
}
29 changes: 23 additions & 6 deletions typescript/event-attestator/src/ProofcastEventAttestator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { SigningKey, computeAddress } from 'ethers/lib/utils'
import { RlpEncode, RlpList } from 'rlp-stream'

import { Chains } from './Chains'
import { Protocols } from './Protocols'
import { Versions } from './Versions'

const sha256Digest = (_value: crypto.BinaryLike) => {
const sha256 = crypto.createHash('sha256')
Expand All @@ -15,6 +17,8 @@ type Context = {
version: number
protocolId: number
chainId: number
blockHash: string | undefined
txHash: string | undefined
privateKey: string | undefined
}

Expand All @@ -34,17 +38,21 @@ export class ProofcastEventAttestator {
public version: Buffer
public protocolId: Buffer
public chainId: Buffer
public blockHash: Buffer
public txHash: Buffer
public address: string
public publicKey: string
public privateKey: string
private signingKey: SigningKey

constructor(
{ version, protocolId, chainId, privateKey }: Context = {
version: 0x00,
protocolId: 0x00,
{ version, protocolId, chainId, privateKey, blockHash, txHash }: Context = {
version: Versions.V1,
protocolId: Protocols.Evm,
chainId: Chains.Goerli,
privateKey: undefined,
blockHash: undefined,
txHash: undefined,
},
) {
this.version = Buffer.from([version])
Expand All @@ -56,10 +64,19 @@ export class ProofcastEventAttestator {
this.signingKey = new SigningKey(Buffer.from(this.privateKey, 'hex'))
this.publicKey = this.signingKey.publicKey
this.address = computeAddress(this.publicKey)
this.blockHash = fromHex(blockHash)
this.txHash = fromHex(txHash)
}

getEventId(event: Event): Buffer {
return sha256Digest(event.address + event.data + event.topics[0])
getEventId(): Buffer {
const sha256 = crypto.createHash('sha256')
sha256.update(this.version)
sha256.update(this.protocolId)
sha256.update(this.chainId)
sha256.update(this.blockHash)
sha256.update(this.txHash)

return sha256.digest()
}

getStatementBytes(event: Event): Buffer {
Expand All @@ -69,7 +86,7 @@ export class ProofcastEventAttestator {
const eventRLP: RlpList = [address, topics, data]
const eventBytes = RlpEncode(eventRLP)

const eventId = this.getEventId(event)
const eventId = this.getEventId()
const length =
this.version.length +
this.protocolId.length +
Expand Down
6 changes: 6 additions & 0 deletions typescript/event-attestator/src/Protocols.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const Protocols = {
Bitcoin: 0x00,
Evm: 0x01,
Eos: 0x02,
Algorand: 0x03,
}
4 changes: 4 additions & 0 deletions typescript/event-attestator/src/Versions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const Versions = {
V1: 0x01,
V2: 0x02,
}
3 changes: 3 additions & 0 deletions typescript/event-attestator/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export { Chains } from './Chains.js'
export { ProofcastEventAttestator } from './ProofcastEventAttestator.js'
export { Protocols } from './Protocols.js'
export { Versions } from './Versions.js'
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ describe('Proofcast Event Attestator Tests', () => {
protocolId: 0x01,
chainId: Chains.Hardhat,
privateKey,
blockHash: '0x00',
txHash: '0x00',
})

console.log('public key', ea.publicKey)
Expand Down
23 changes: 5 additions & 18 deletions typescript/event-attestator/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,22 @@
{
"compilerOptions": {
"removeComments": true,
"allowJs": true,
"preserveConstEnums": true,
"module": "commonjs",
"target": "es6",
"declaration": true,
"esModuleInterop": true,
"sourceMap": true,
"sourceMap": false,
"lib": [
"esnext"
"es6"
],
"outDir": "dist",
"resolveJsonModule": true,
"moduleResolution": "node",
"moduleResolution": "Node",
"allowSyntheticDefaultImports": true,
"paths": {
"*": [
"types/*"
]
},
"baseUrl": "./",
"typeRoots": [
"node_modules/@types"
],
"alwaysStrict": true
"alwaysStrict": true,
},
"include": [
"src/**/*",
],
"exclude": [
"test/**/*"
"./src"
]
}
5 changes: 5 additions & 0 deletions typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"composite": true
}
}
1 change: 1 addition & 0 deletions typescript/tsconfig.tsbuildinfo

Large diffs are not rendered by default.

Loading

0 comments on commit 8c399e2

Please sign in to comment.