Skip to content

Commit e009189

Browse files
committed
Add ENS and DIDregistry to yargs cli
1 parent ea451bf commit e009189

14 files changed

+527
-175
lines changed

addresses.json

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
{
22
"4": {
3+
"IENS": {
4+
"address": "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e"
5+
},
6+
"IEthereumDIDRegistry": {
7+
"address": "0xdCa7EF03e98e0DC2B855bE647C39ABe984fcF21B"
8+
},
39
"EpochManager": {
410
"address": "0xFd12A3ee8Bf3C417e0cedbc591BA10156724C069",
511
"initArgs": [
@@ -203,6 +209,18 @@
203209
}
204210
},
205211
"42": {
212+
"IENS": {
213+
"address": "0xB66B2f307B6e46a6D038a85997B401aE87455772"
214+
},
215+
"ITestRegistrar": {
216+
"address": "0x327033bA7B23A6E3a3Ca165e44D619E3dd675f8b"
217+
},
218+
"IPublicResolver": {
219+
"address": "0xc30F6CCc48F1eA5374aC618dfe5243ddD1e264E7"
220+
},
221+
"IEthereumDIDRegistry": {
222+
"address": "0xdCa7EF03e98e0DC2B855bE647C39ABe984fcF21B"
223+
},
206224
"EpochManager": {
207225
"address": "0xc56bA09727030e018990089964E8537FDa13a786",
208226
"initArgs": [
@@ -607,21 +625,5 @@
607625
"runtimeCodeHash": "0x962af945f8852d82fe16690798107779ff368624a510e9dbf837bafb6fac557e",
608626
"txHash": "0xc6666d7b1a9731a1797f26c5ac3f203ce8adbff27aedc454f2d0b83e8c01e140"
609627
}
610-
},
611-
"kovan": {
612-
"ens": "0xB66B2f307B6e46a6D038a85997B401aE87455772",
613-
"ensTestRegistrar": "0x327033bA7B23A6E3a3Ca165e44D619E3dd675f8b",
614-
"ensPublicResolver": "0xc30F6CCc48F1eA5374aC618dfe5243ddD1e264E7",
615-
"ethereumDIDRegistry": "0xdCa7EF03e98e0DC2B855bE647C39ABe984fcF21B"
616-
},
617-
"rinkeby": {
618-
"ens": "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e",
619-
"ensTestRegistrar": "0x327033bA7B23A6E3a3Ca165e44D619E3dd675f8b",
620-
"ensPublicResolver": "0xc30F6CCc48F1eA5374aC618dfe5243ddD1e264E7",
621-
"ethereumDIDRegistry": "0xdCa7EF03e98e0DC2B855bE647C39ABe984fcF21B"
622-
},
623-
"mainnet": {
624-
"ens": "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e",
625-
"ethereumDIDRegistry": "0xdCa7EF03e98e0DC2B855bE647C39ABe984fcF21B"
626628
}
627629
}

scripts/cli/commands/contracts/contracts.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@ import yargs, { Argv } from 'yargs'
22

33
import { curationCommand } from './curation'
44
import { serviceRegistryCommand } from './service-registry'
5+
import { ensCommand } from './ens'
6+
import { ethereumDIDRegistryCommand } from './ethereumDIDRegistry'
7+
58
import { CLIArgs } from '../../env'
69

710
export const contractsCommand = {
811
command: 'contracts',
912
describe: 'Contract calls for all contracts',
1013
builder: (yargs: Argv): yargs.Argv => {
11-
return yargs.command(curationCommand).command(serviceRegistryCommand)
14+
return yargs
15+
.command(curationCommand)
16+
.command(serviceRegistryCommand)
17+
.command(ensCommand)
18+
.command(ethereumDIDRegistryCommand)
1219
},
1320
handler: (argv: CLIArgs): void => {
1421
yargs.showHelp()

scripts/cli/commands/contracts/curation.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import consola from 'consola'
22
import yargs, { Argv } from 'yargs'
33
import { parseGRT } from '@graphprotocol/common-ts'
44

5-
import { getContractAt, sendTransaction } from '../../network'
5+
import { sendTransaction } from '../../network'
66
import { loadEnv, CLIArgs, CLIEnvironment } from '../../env'
77

88
const logger = consola.create({})
99

10-
export const mint = async (cli: CLIEnvironment, cliArgs: CLIArgs) => {
10+
export const mint = async (cli: CLIEnvironment, cliArgs: CLIArgs): Promise<void> => {
1111
const subgraphID = cliArgs.subgraphID
1212
const amount = parseGRT(cliArgs.amount)
1313

@@ -19,7 +19,7 @@ export const mint = async (cli: CLIEnvironment, cliArgs: CLIArgs) => {
1919
logger.log(`Signaling on ${subgraphID} with ${cliArgs.amount} tokens...`)
2020
await sendTransaction(cli.wallet, curation, 'mint', ...[subgraphID, amount])
2121
}
22-
export const burn = async (cli: CLIEnvironment, cliArgs: CLIArgs) => {
22+
export const burn = async (cli: CLIEnvironment, cliArgs: CLIArgs): Promise<void> => {
2323
const subgraphID = cliArgs.subgraphID
2424
const amount = parseGRT(cliArgs.amount)
2525
const curation = cli.contracts.Curation

scripts/cli/commands/contracts/ens.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import consola from 'consola'
2+
import yargs, { Argv } from 'yargs'
3+
import { utils } from 'ethers'
4+
5+
import { sendTransaction } from '../../network'
6+
import { loadEnv, CLIArgs, CLIEnvironment } from '../../env'
7+
8+
const logger = consola.create({})
9+
export const registerTestName = async (cli: CLIEnvironment, cliArgs: CLIArgs): Promise<void> => {
10+
const name = cliArgs.name
11+
const testRegistrar = cli.contracts.ITestRegistrar
12+
const normalizedName = name.toLowerCase()
13+
const labelNameFull = `${normalizedName}.${'eth'}`
14+
const labelHashFull = utils.namehash(labelNameFull)
15+
const label = utils.keccak256(utils.toUtf8Bytes(normalizedName))
16+
logger.log(`Namehash for ${labelNameFull}: ${labelHashFull}`)
17+
logger.log(`Registering ${name} with ${cli.walletAddress} on the test registrar`)
18+
await sendTransaction(cli.wallet, testRegistrar, 'register', ...[label, cli.walletAddress])
19+
}
20+
export const checkOwner = async (cli: CLIEnvironment, cliArgs: CLIArgs): Promise<void> => {
21+
const name = cliArgs.name
22+
const ens = cli.contracts.IENS
23+
const node = nameToNode(name)
24+
const res = await ens.owner(node)
25+
logger.success(`owner = ${res}`)
26+
}
27+
28+
export const nameToNode = (name: string): string => {
29+
const node = utils.namehash(`${name}.eth`)
30+
logger.log(`Name: ${name}`)
31+
logger.log(`Node: ${node}`)
32+
return node
33+
}
34+
35+
export const ensCommand = {
36+
command: 'ens',
37+
describe: 'ENS contract calls',
38+
builder: (yargs: Argv): yargs.Argv => {
39+
return yargs
40+
.command({
41+
command: 'registerTestName',
42+
describe: 'Register a name on the test registrar',
43+
builder: (yargs: Argv) => {
44+
return yargs.option('name', {
45+
description: 'Name being registered',
46+
type: 'string',
47+
requiresArg: true,
48+
demandOption: true,
49+
})
50+
},
51+
handler: async (argv: CLIArgs): Promise<void> => {
52+
return registerTestName(await loadEnv(argv), argv)
53+
},
54+
})
55+
.command({
56+
command: 'checkOwner',
57+
describe: 'Check the owner of a name',
58+
builder: (yargs: Argv) => {
59+
return yargs.option('name', {
60+
description: 'Name being checked',
61+
type: 'string',
62+
requiresArg: true,
63+
demandOption: true,
64+
})
65+
},
66+
handler: async (argv: CLIArgs): Promise<void> => {
67+
return checkOwner(await loadEnv(argv), argv)
68+
},
69+
})
70+
},
71+
handler: (argv: CLIArgs): void => {
72+
yargs.showHelp()
73+
},
74+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import * as fs from 'fs'
2+
import consola from 'consola'
3+
import yargs, { Argv } from 'yargs'
4+
5+
import { IPFS } from '../../helpers'
6+
import { sendTransaction } from '../../network'
7+
import { loadEnv, CLIArgs, CLIEnvironment } from '../../env'
8+
import { jsonToAccountMetadata } from './metadataHelpers'
9+
10+
const logger = consola.create({})
11+
12+
const handleAccountMetadata = async (ipfs: string, path: string): Promise<string> => {
13+
const metadata = jsonToAccountMetadata(JSON.parse(fs.readFileSync(__dirname + path).toString()))
14+
console.log('Meta data:')
15+
console.log(' Code Repository: ', metadata.codeRepository || '')
16+
console.log(' Description: ', metadata.description || '')
17+
console.log(' Image: ', metadata.image || '')
18+
console.log(' Name: ', metadata.name || '')
19+
console.log(' Website: ', metadata.website || '')
20+
21+
const ipfsClient = IPFS.createIpfsClient(ipfs)
22+
console.log('\nUpload JSON meta data to IPFS...')
23+
const result = await ipfsClient.add(Buffer.from(JSON.stringify(metadata)))
24+
const metaHash = result[0].hash
25+
try {
26+
const data = JSON.parse(await ipfsClient.cat(metaHash))
27+
if (JSON.stringify(data) !== JSON.stringify(metadata)) {
28+
throw new Error(`Original meta data and uploaded data are not identical`)
29+
}
30+
} catch (e) {
31+
throw new Error(`Failed to retrieve and parse JSON meta data after uploading: ${e.message}`)
32+
}
33+
console.log(`Upload metadata successful: ${metaHash}\n`)
34+
return IPFS.ipfsHashToBytes32(metaHash)
35+
}
36+
37+
export const setAttribute = async (cli: CLIEnvironment, cliArgs: CLIArgs) => {
38+
const metadataPath = cliArgs.metadataPath
39+
const ipfsEndpoint = cliArgs.ipfs
40+
const ethereumDIDRegistry = cli.contracts.IEthereumDIDRegistry
41+
// const name comes from: keccak256("GRAPH NAME SERVICE")
42+
const name = '0x72abcb436eed911d1b6046bbe645c235ec3767c842eb1005a6da9326c2347e4c'
43+
const metaHashBytes = await handleAccountMetadata(ipfsEndpoint, metadataPath)
44+
45+
logger.log(`Setting attribute on ethereum DID registry ...`)
46+
await sendTransaction(
47+
cli.wallet,
48+
ethereumDIDRegistry,
49+
'setAttribute',
50+
...[cli.walletAddress, name, metaHashBytes, 0],
51+
)
52+
}
53+
54+
export const ethereumDIDRegistryCommand = {
55+
command: 'ethereumDIDRegistry',
56+
describe: 'Calls into the Ethereum DID Registry Contract',
57+
builder: (yargs: Argv): yargs.Argv => {
58+
return yargs.command({
59+
command: 'setAttribute',
60+
describe: 'Set metadata for graph account',
61+
builder: (yargs: Argv) => {
62+
return yargs
63+
.option('i', {
64+
alias: 'ipfs',
65+
description: 'IPFS endpoint where file is being uploaded',
66+
type: 'string',
67+
requiresArg: true,
68+
demandOption: true,
69+
})
70+
.option('metadataPath', {
71+
description: `filepath to metadata with the JSON format:\n
72+
"codeRepository": "github.com/davekaj",
73+
"description": "Dave Kajpusts graph account",
74+
"image": "http://localhost:8080/ipfs/QmTFK5DZc58XrTqhuuDTYoaq29ndnwoHX5TAW1bZr5EMpq",
75+
"name": "Dave Kajpust",
76+
"website": "https://kajpust.com/"`,
77+
type: 'string',
78+
requiresArg: true,
79+
demandOption: true,
80+
})
81+
},
82+
handler: async (argv: CLIArgs): Promise<void> => {
83+
return setAttribute(await loadEnv(argv), argv)
84+
},
85+
})
86+
},
87+
handler: (argv: CLIArgs): void => {
88+
yargs.showHelp()
89+
},
90+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
interface AccountMetadata {
2+
codeRepository: string
3+
description: string
4+
image: string
5+
name: string
6+
website: string
7+
}
8+
9+
interface SubgraphMetadata {
10+
description: string
11+
displayName: string
12+
image: string
13+
codeRepository: string
14+
website: string
15+
}
16+
17+
interface VersionMetadata {
18+
label: string
19+
description: string
20+
}
21+
22+
const jsonToSubgraphMetadata = (json): SubgraphMetadata => {
23+
const subgraphMetadata: SubgraphMetadata = {
24+
description: checkString(json.description),
25+
displayName: checkString(json.displayName),
26+
image: checkString(json.image),
27+
codeRepository: checkString(json.codeRepository),
28+
website: checkString(json.website),
29+
}
30+
return subgraphMetadata
31+
}
32+
33+
const jsonToVersionMetadata = (json): VersionMetadata => {
34+
const subgraphMetadata: VersionMetadata = {
35+
label: checkString(json.label),
36+
description: checkString(json.description),
37+
}
38+
return subgraphMetadata
39+
}
40+
41+
const jsonToAccountMetadata = (json): AccountMetadata => {
42+
const accountMetadata: AccountMetadata = {
43+
codeRepository: checkString(json.codeRepository),
44+
description: checkString(json.description),
45+
image: checkString(json.image),
46+
name: checkString(json.name),
47+
website: checkString(json.website),
48+
}
49+
return accountMetadata
50+
}
51+
52+
const checkString = (field): string => {
53+
if (typeof field != 'string') {
54+
throw Error('Metadata field is incorrect for one or more files')
55+
}
56+
return field
57+
}
58+
59+
export {
60+
AccountMetadata,
61+
SubgraphMetadata,
62+
VersionMetadata,
63+
jsonToSubgraphMetadata,
64+
jsonToVersionMetadata,
65+
jsonToAccountMetadata,
66+
}

scripts/cli/commands/contracts/service-registry.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import consola from 'consola'
22
import yargs, { Argv } from 'yargs'
33

4-
import { getContractAt, sendTransaction } from '../../network'
4+
import { sendTransaction } from '../../network'
55
import { loadEnv, CLIArgs, CLIEnvironment } from '../../env'
66

77
const logger = consola.create({})
8-
export const register = async (cli: CLIEnvironment, cliArgs: CLIArgs) => {
8+
export const register = async (cli: CLIEnvironment, cliArgs: CLIArgs): Promise<void> => {
99
const url = cliArgs.url
1010
const geoHash = cliArgs.geoHash
1111
const serviceRegistry = cli.contracts.ServiceRegistry
1212

1313
logger.log(`Registering indexer ${cli.walletAddress} with url ${url} and geoHash ${geoHash}`)
1414
await sendTransaction(cli.wallet, serviceRegistry, 'register', ...[url, geoHash])
1515
}
16-
export const unregister = async (cli: CLIEnvironment, cliArgs: CLIArgs) => {
16+
export const unregister = async (cli: CLIEnvironment, cliArgs: CLIArgs): Promise<void> => {
1717
const serviceRegistry = cli.contracts.ServiceRegistry
1818

1919
logger.log(`Unregistering indexer ${cli.walletAddress}`)

0 commit comments

Comments
 (0)