Skip to content

v3 for horizon #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"packages": [
"packages/common-ts"
],
"version": "2.0.7"
"version": "3.0.1"
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
"devDependencies": {
"@octokit/core": "^3.2.0",
"lerna": "^4.0.0"
}
},
"packageManager": "[email protected]+sha1.ac34549e6aa8e7ead463a7407e1c7390f61a6610"
}
5 changes: 2 additions & 3 deletions packages/common-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@graphprotocol/common-ts",
"version": "2.0.11",
"version": "3.0.1",
"description": "Common TypeScript library for Graph Protocol components",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -16,15 +16,14 @@
"test:watch": "jest --watch --passWithNoTests --detectOpenHandles --verbose"
},
"dependencies": {
"@graphprotocol/contracts": "5.3.3",
"@graphprotocol/pino-sentry-simple": "0.7.1",
"@urql/core": "3.1.0",
"@urql/exchange-execute": "2.1.0",
"body-parser": "1.20.2",
"bs58": "5.0.0",
"cors": "2.8.5",
"cross-fetch": "4.0.0",
"ethers": "5.7.0",
"ethers": "6.13.7",
"express": "4.18.2",
"graphql": "16.8.0",
"graphql-tag": "2.12.6",
Expand Down
12 changes: 6 additions & 6 deletions packages/common-ts/src/attestations/attestations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
recoverAttestation,
} from './attestations'
import { Wallet } from 'ethers'
import { utils } from 'ethers'
import { hexlify } from 'ethers'
import * as bs58 from 'bs58'

describe('Attestations', () => {
Expand All @@ -16,12 +16,12 @@ describe('Attestations', () => {
const receipt = {
requestCID: '0xd902c18a1b3590a3d2a8ae4439db376764fda153ca077e339d0427bf776bd463',
responseCID: '0xbe0b5ae5f598fdf631133571d59ef16b443b2fe02e35ca2cb807158069009db9',
subgraphDeploymentID: utils.hexlify(
subgraphDeploymentID: hexlify(
bs58.decode('QmTXzATwNfgGVukV1fX2T6xw9f6LAYRVWpsdXyRWzUR2H9').slice(2),
),
}

const signer = Wallet.fromMnemonic(mnemonic)
const signer = Wallet.fromPhrase(mnemonic)
const attestation = await createAttestation(
signer.privateKey,
1,
Expand All @@ -44,7 +44,7 @@ describe('Attestations', () => {
const receipt = {
requestCID: '0xd902c18a1b3590a3d2a8ae4439db376764fda153ca077e339d0427bf776bd463',
responseCID: '0xbe0b5ae5f598fdf631133571d59ef16b443b2fe02e35ca2cb807158069009db9',
subgraphDeploymentID: utils.hexlify(
subgraphDeploymentID: hexlify(
bs58.decode('QmTXzATwNfgGVukV1fX2T6xw9f6LAYRVWpsdXyRWzUR2H9').slice(2),
),
}
Expand All @@ -66,12 +66,12 @@ describe('Attestations', () => {
const receipt = {
requestCID: '0xd902c18a1b3590a3d2a8ae4439db376764fda153ca077e339d0427bf776bd463',
responseCID: '0xbe0b5ae5f598fdf631133571d59ef16b443b2fe02e35ca2cb807158069009db9',
subgraphDeploymentID: utils.hexlify(
subgraphDeploymentID: hexlify(
bs58.decode('QmTXzATwNfgGVukV1fX2T6xw9f6LAYRVWpsdXyRWzUR2H9').slice(2),
),
}

const signer = Wallet.fromMnemonic(mnemonic)
const signer = Wallet.fromPhrase(mnemonic)
const chainID = 1
const contractAddress = '0x0000000000000000000000000000000000000000'
const attestation = await createAttestation(
Expand Down
46 changes: 25 additions & 21 deletions packages/common-ts/src/attestations/attestations.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { utils } from 'ethers'
import * as eip712 from './eip712'

const {
defaultAbiCoder: abi,
arrayify,
import {
AbiCoder,
getBytes,
concat,
hexlify,
splitSignature,
joinSignature,
} = utils
BytesLike,
keccak256,
SigningKey,
Signature,
recoverAddress,
} from 'ethers'
import * as eip712 from './eip712'

const SIG_SIZE_BYTES = 161
const RECEIPT_SIZE_BYTES = 96
const RECEIPT_TYPE_HASH = eip712.typeHash(
'Receipt(bytes32 requestCID,bytes32 responseCID,bytes32 subgraphDeploymentID)',
)
const abi = AbiCoder.defaultAbiCoder()

export interface Receipt {
requestCID: string
Expand Down Expand Up @@ -56,7 +58,7 @@ export const getDomainSeparator = (
}

export const createAttestation = async (
signer: utils.BytesLike,
signer: BytesLike,
chainId: number,
disputeManagerAddress: string,
receipt: Receipt,
Expand All @@ -65,9 +67,9 @@ export const createAttestation = async (
const domainSeparator = getDomainSeparator(chainId, disputeManagerAddress, version)
const encodedReceipt = encodeReceipt(receipt)
const message = eip712.encode(domainSeparator, encodedReceipt)
const messageHash = utils.keccak256(message)
const signingKey = new utils.SigningKey(signer)
const { r, s, v } = signingKey.signDigest(messageHash)
const messageHash = keccak256(message)
const signingKey = new SigningKey(signer)
const { r, s, v } = signingKey.sign(messageHash)

return {
requestCID: receipt.requestCID,
Expand All @@ -80,18 +82,18 @@ export const createAttestation = async (
}

export const encodeAttestation = (attestation: Attestation): string => {
const data = arrayify(
const data = getBytes(
abi.encode(
['bytes32', 'bytes32', 'bytes32'],
[attestation.requestCID, attestation.responseCID, attestation.subgraphDeploymentID],
),
)
const sig = joinSignature(attestation)
const sig = Signature.from(attestation).serialized
return hexlify(concat([data, sig]))
}

export const decodeAttestation = (attestationData: string): Attestation => {
const attestationBytes = arrayify(attestationData)
const attestationBytes = getBytes(attestationData)
if (attestationBytes.length !== SIG_SIZE_BYTES) {
throw new Error('Invalid signature length')
}
Expand All @@ -100,8 +102,10 @@ export const decodeAttestation = (attestationData: string): Attestation => {
['bytes32', 'bytes32', 'bytes32'],
attestationBytes,
)
const sig = splitSignature(
attestationBytes.slice(RECEIPT_SIZE_BYTES, RECEIPT_SIZE_BYTES + SIG_SIZE_BYTES),
const sig = Signature.from(
hexlify(
attestationBytes.slice(RECEIPT_SIZE_BYTES, RECEIPT_SIZE_BYTES + SIG_SIZE_BYTES),
),
)

return {
Expand All @@ -128,9 +132,9 @@ export const recoverAttestation = (
}
const encodedReceipt = encodeReceipt(receipt)
const message = eip712.encode(domainSeparator, encodedReceipt)
const messageHash = utils.keccak256(message)
return utils.recoverAddress(
const messageHash = keccak256(message)
return recoverAddress(
messageHash,
joinSignature({ r: attestation.r, s: attestation.s, v: attestation.v }),
Signature.from({ r: attestation.r, s: attestation.s, v: attestation.v }).serialized,
)
}
10 changes: 5 additions & 5 deletions packages/common-ts/src/attestations/eip712.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { utils } from 'ethers'
import { AbiCoder, keccak256, toUtf8Bytes } from 'ethers'

// Hashes a type signature based on the `typeHash` defined on
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-712.md#definition-of-hashstruct.
//
// The type signature is expected to follow the `encodeType` format described on
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-712.md#definition-of-encodetype.
export const typeHash = (typeSignature: string): string =>
utils.keccak256(utils.toUtf8Bytes(typeSignature))
keccak256(toUtf8Bytes(typeSignature))

// Encodes a list of values according to the given types.
//
Expand All @@ -21,14 +21,14 @@ const encodeData = (types: string[], values: any[]): string => {
for (let i = 0; i < types.length; i++) {
if (types[i] === 'string' || types[i] === 'bytes') {
transformedTypes[i] = 'bytes32'
transformedValues[i] = utils.keccak256(utils.toUtf8Bytes(values[i]))
transformedValues[i] = keccak256(toUtf8Bytes(values[i]))
} else {
transformedTypes[i] = types[i]
transformedValues[i] = values[i]
}
}

return utils.defaultAbiCoder.encode(transformedTypes, transformedValues)
return AbiCoder.defaultAbiCoder().encode(transformedTypes, transformedValues)
}

// Hashes a struct based on the hash of a type signature (see `typeHash`),
Expand All @@ -37,7 +37,7 @@ const encodeData = (types: string[], values: any[]): string => {
// NOTE: Does not support recursion yet.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const hashStruct = (typeHash: string, types: string[], values: any[]): string => {
return utils.keccak256(encodeData(['bytes32', ...types], [typeHash, ...values]))
return keccak256(encodeData(['bytes32', ...types], [typeHash, ...values]))
}

const EIP712_DOMAIN_TYPE_HASH = typeHash(
Expand Down
47 changes: 0 additions & 47 deletions packages/common-ts/src/contracts/chain.ts

This file was deleted.

25 changes: 0 additions & 25 deletions packages/common-ts/src/contracts/index.test.ts

This file was deleted.

Loading