Skip to content

Commit f47d684

Browse files
tmigoneMaikol
andauthored
v3 for horizon (#126)
Co-authored-by: Miguel de Elias <miguel@edgeandnode.com>
1 parent c5fac3e commit f47d684

16 files changed

Lines changed: 117 additions & 783 deletions

File tree

lerna.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"packages": [
33
"packages/common-ts"
44
],
5-
"version": "2.0.7"
5+
"version": "3.0.1"
66
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
"devDependencies": {
1515
"@octokit/core": "^3.2.0",
1616
"lerna": "^4.0.0"
17-
}
17+
},
18+
"packageManager": "yarn@1.22.22+sha1.ac34549e6aa8e7ead463a7407e1c7390f61a6610"
1819
}

packages/common-ts/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@graphprotocol/common-ts",
3-
"version": "2.0.11",
3+
"version": "3.0.1",
44
"description": "Common TypeScript library for Graph Protocol components",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -16,15 +16,14 @@
1616
"test:watch": "jest --watch --passWithNoTests --detectOpenHandles --verbose"
1717
},
1818
"dependencies": {
19-
"@graphprotocol/contracts": "5.3.3",
2019
"@graphprotocol/pino-sentry-simple": "0.7.1",
2120
"@urql/core": "3.1.0",
2221
"@urql/exchange-execute": "2.1.0",
2322
"body-parser": "1.20.2",
2423
"bs58": "5.0.0",
2524
"cors": "2.8.5",
2625
"cross-fetch": "4.0.0",
27-
"ethers": "5.7.0",
26+
"ethers": "6.13.7",
2827
"express": "4.18.2",
2928
"graphql": "16.8.0",
3029
"graphql-tag": "2.12.6",

packages/common-ts/src/attestations/attestations.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
recoverAttestation,
66
} from './attestations'
77
import { Wallet } from 'ethers'
8-
import { utils } from 'ethers'
8+
import { hexlify } from 'ethers'
99
import * as bs58 from 'bs58'
1010

1111
describe('Attestations', () => {
@@ -16,12 +16,12 @@ describe('Attestations', () => {
1616
const receipt = {
1717
requestCID: '0xd902c18a1b3590a3d2a8ae4439db376764fda153ca077e339d0427bf776bd463',
1818
responseCID: '0xbe0b5ae5f598fdf631133571d59ef16b443b2fe02e35ca2cb807158069009db9',
19-
subgraphDeploymentID: utils.hexlify(
19+
subgraphDeploymentID: hexlify(
2020
bs58.decode('QmTXzATwNfgGVukV1fX2T6xw9f6LAYRVWpsdXyRWzUR2H9').slice(2),
2121
),
2222
}
2323

24-
const signer = Wallet.fromMnemonic(mnemonic)
24+
const signer = Wallet.fromPhrase(mnemonic)
2525
const attestation = await createAttestation(
2626
signer.privateKey,
2727
1,
@@ -44,7 +44,7 @@ describe('Attestations', () => {
4444
const receipt = {
4545
requestCID: '0xd902c18a1b3590a3d2a8ae4439db376764fda153ca077e339d0427bf776bd463',
4646
responseCID: '0xbe0b5ae5f598fdf631133571d59ef16b443b2fe02e35ca2cb807158069009db9',
47-
subgraphDeploymentID: utils.hexlify(
47+
subgraphDeploymentID: hexlify(
4848
bs58.decode('QmTXzATwNfgGVukV1fX2T6xw9f6LAYRVWpsdXyRWzUR2H9').slice(2),
4949
),
5050
}
@@ -66,12 +66,12 @@ describe('Attestations', () => {
6666
const receipt = {
6767
requestCID: '0xd902c18a1b3590a3d2a8ae4439db376764fda153ca077e339d0427bf776bd463',
6868
responseCID: '0xbe0b5ae5f598fdf631133571d59ef16b443b2fe02e35ca2cb807158069009db9',
69-
subgraphDeploymentID: utils.hexlify(
69+
subgraphDeploymentID: hexlify(
7070
bs58.decode('QmTXzATwNfgGVukV1fX2T6xw9f6LAYRVWpsdXyRWzUR2H9').slice(2),
7171
),
7272
}
7373

74-
const signer = Wallet.fromMnemonic(mnemonic)
74+
const signer = Wallet.fromPhrase(mnemonic)
7575
const chainID = 1
7676
const contractAddress = '0x0000000000000000000000000000000000000000'
7777
const attestation = await createAttestation(

packages/common-ts/src/attestations/attestations.ts

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
import { utils } from 'ethers'
2-
import * as eip712 from './eip712'
3-
4-
const {
5-
defaultAbiCoder: abi,
6-
arrayify,
1+
import {
2+
AbiCoder,
3+
getBytes,
74
concat,
85
hexlify,
9-
splitSignature,
10-
joinSignature,
11-
} = utils
6+
BytesLike,
7+
keccak256,
8+
SigningKey,
9+
Signature,
10+
recoverAddress,
11+
} from 'ethers'
12+
import * as eip712 from './eip712'
1213

1314
const SIG_SIZE_BYTES = 161
1415
const RECEIPT_SIZE_BYTES = 96
1516
const RECEIPT_TYPE_HASH = eip712.typeHash(
1617
'Receipt(bytes32 requestCID,bytes32 responseCID,bytes32 subgraphDeploymentID)',
1718
)
19+
const abi = AbiCoder.defaultAbiCoder()
1820

1921
export interface Receipt {
2022
requestCID: string
@@ -56,7 +58,7 @@ export const getDomainSeparator = (
5658
}
5759

5860
export const createAttestation = async (
59-
signer: utils.BytesLike,
61+
signer: BytesLike,
6062
chainId: number,
6163
disputeManagerAddress: string,
6264
receipt: Receipt,
@@ -65,9 +67,9 @@ export const createAttestation = async (
6567
const domainSeparator = getDomainSeparator(chainId, disputeManagerAddress, version)
6668
const encodedReceipt = encodeReceipt(receipt)
6769
const message = eip712.encode(domainSeparator, encodedReceipt)
68-
const messageHash = utils.keccak256(message)
69-
const signingKey = new utils.SigningKey(signer)
70-
const { r, s, v } = signingKey.signDigest(messageHash)
70+
const messageHash = keccak256(message)
71+
const signingKey = new SigningKey(signer)
72+
const { r, s, v } = signingKey.sign(messageHash)
7173

7274
return {
7375
requestCID: receipt.requestCID,
@@ -80,18 +82,18 @@ export const createAttestation = async (
8082
}
8183

8284
export const encodeAttestation = (attestation: Attestation): string => {
83-
const data = arrayify(
85+
const data = getBytes(
8486
abi.encode(
8587
['bytes32', 'bytes32', 'bytes32'],
8688
[attestation.requestCID, attestation.responseCID, attestation.subgraphDeploymentID],
8789
),
8890
)
89-
const sig = joinSignature(attestation)
91+
const sig = Signature.from(attestation).serialized
9092
return hexlify(concat([data, sig]))
9193
}
9294

9395
export const decodeAttestation = (attestationData: string): Attestation => {
94-
const attestationBytes = arrayify(attestationData)
96+
const attestationBytes = getBytes(attestationData)
9597
if (attestationBytes.length !== SIG_SIZE_BYTES) {
9698
throw new Error('Invalid signature length')
9799
}
@@ -100,8 +102,10 @@ export const decodeAttestation = (attestationData: string): Attestation => {
100102
['bytes32', 'bytes32', 'bytes32'],
101103
attestationBytes,
102104
)
103-
const sig = splitSignature(
104-
attestationBytes.slice(RECEIPT_SIZE_BYTES, RECEIPT_SIZE_BYTES + SIG_SIZE_BYTES),
105+
const sig = Signature.from(
106+
hexlify(
107+
attestationBytes.slice(RECEIPT_SIZE_BYTES, RECEIPT_SIZE_BYTES + SIG_SIZE_BYTES),
108+
),
105109
)
106110

107111
return {
@@ -128,9 +132,9 @@ export const recoverAttestation = (
128132
}
129133
const encodedReceipt = encodeReceipt(receipt)
130134
const message = eip712.encode(domainSeparator, encodedReceipt)
131-
const messageHash = utils.keccak256(message)
132-
return utils.recoverAddress(
135+
const messageHash = keccak256(message)
136+
return recoverAddress(
133137
messageHash,
134-
joinSignature({ r: attestation.r, s: attestation.s, v: attestation.v }),
138+
Signature.from({ r: attestation.r, s: attestation.s, v: attestation.v }).serialized,
135139
)
136140
}

packages/common-ts/src/attestations/eip712.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { utils } from 'ethers'
1+
import { AbiCoder, keccak256, toUtf8Bytes } from 'ethers'
22

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

1111
// Encodes a list of values according to the given types.
1212
//
@@ -21,14 +21,14 @@ const encodeData = (types: string[], values: any[]): string => {
2121
for (let i = 0; i < types.length; i++) {
2222
if (types[i] === 'string' || types[i] === 'bytes') {
2323
transformedTypes[i] = 'bytes32'
24-
transformedValues[i] = utils.keccak256(utils.toUtf8Bytes(values[i]))
24+
transformedValues[i] = keccak256(toUtf8Bytes(values[i]))
2525
} else {
2626
transformedTypes[i] = types[i]
2727
transformedValues[i] = values[i]
2828
}
2929
}
3030

31-
return utils.defaultAbiCoder.encode(transformedTypes, transformedValues)
31+
return AbiCoder.defaultAbiCoder().encode(transformedTypes, transformedValues)
3232
}
3333

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

4343
const EIP712_DOMAIN_TYPE_HASH = typeHash(

packages/common-ts/src/contracts/chain.ts

Lines changed: 0 additions & 47 deletions
This file was deleted.

packages/common-ts/src/contracts/index.test.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)