Skip to content
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

AzeroID off-chain resolver implementation #1

Merged
merged 14 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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: 2 additions & 0 deletions packages/gateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
"@chainlink/ethers-ccip-read-provider": "^0.2.3",
"@ensdomains/ens-contracts": "^0.0.8",
"@ensdomains/offchain-resolver-contracts": "^0.2.1",
"@polkadot/api": "^12.3.1",
"@polkadot/api-contract": "^12.3.1",
"commander": "^8.3.0",
"dotenv": "^15.0.0",
"ethers": "^5.7.2"
Expand Down
84 changes: 84 additions & 0 deletions packages/gateway/src/azero-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import abi from './metadata.json';
import { ApiPromise, WsProvider } from '@polkadot/api';
import { Database } from './server';
import { ContractPromise } from '@polkadot/api-contract';
import type { WeightV2 } from '@polkadot/types/interfaces';

const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
const EMPTY_CONTENT_HASH = '0x';

export interface GasLimit {
refTime: number,
proofSize: number,
}

export class AzeroId implements Database {
ttl: number;
contract: ContractPromise;
maxGasLimit: WeightV2;

constructor(ttl: number, contract: ContractPromise, maxGasLimit: WeightV2) {
this.ttl = ttl;
this.contract = contract;
this.maxGasLimit = maxGasLimit;
}

static async init(ttl: number, providerURL: string, contractAddress: string, gasLimit: GasLimit) {
const wsProvider = new WsProvider(providerURL);
const api = await ApiPromise.create({ provider: wsProvider });
const contract = new ContractPromise(api, abi, contractAddress);
const maxGasLimit = api.registry.createType('WeightV2', gasLimit) as WeightV2;

return new AzeroId(
ttl,
contract,
maxGasLimit,
);
}

addr(name: string, coinType: number) {
console.log("addr", name, coinType);
return { addr: ZERO_ADDRESS, ttl: this.ttl };
}

async text(name: string, key: string) {
console.log("text", name, key);
const value = await this.fetchRecord(name, key) || '';
return { value, ttl: this.ttl };
}

contenthash(name: string) {
console.log("contenthash", name);
return { contenthash: EMPTY_CONTENT_HASH, ttl: this.ttl };
}

private async fetchRecord(name: string, key: string) {
name = this.processName(name);
const resp: any = await this.contract.query.getRecord(
'',
{
gasLimit: this.maxGasLimit
},
name,
key
);

return resp.output?.toHuman().Ok.Ok;
}

private processName(domain: string) {
// TODO: maybe add it as a class variable
const supportedTLDs = ['azero', 'tzero'];
const labels = domain.split('.');
console.log("Labels:", labels);

const name = labels.shift();
if(labels.length != 0) {
const tld = labels.join('.');
if (!supportedTLDs.includes(tld))
throw new Error(`TLD (.${tld}) not supported`);
}

return name || '';
}
}
26 changes: 19 additions & 7 deletions packages/gateway/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { makeApp } from './server';
import { Command } from 'commander';
import { readFileSync } from 'fs';
import { ethers } from 'ethers';
import { JSONDatabase } from './json';
import { AzeroId, GasLimit } from './azero-id';
const program = new Command();
program
.requiredOption(
'-k --private-key <key>',
'Private key to sign responses with. Prefix with @ to read from a file'
)
.requiredOption('-d --data <file>', 'JSON file to read data from')
.option('--provider-url <string>', 'Provider URL of the substrate chain', 'wss://ws.test.azero.dev')
.option('-c --contract <string>', 'Target contract address', '5FsB91tXSEuMj6akzdPczAtmBaVKToqHmtAwSUzXh49AYzaD')
.option('-t --ttl <number>', 'TTL for signatures', '300')
.option('-p --port <number>', 'Port number to serve on', '8080');
program.parse(process.argv);
Expand All @@ -22,9 +23,20 @@ if (privateKey.startsWith('@')) {
}
const address = ethers.utils.computeAddress(privateKey);
const signer = new ethers.utils.SigningKey(privateKey);
const db = JSONDatabase.fromFilename(options.data, parseInt(options.ttl));
const app = makeApp(signer, '/', db);
console.log(`Serving on port ${options.port} with signing address ${address}`);
app.listen(parseInt(options.port));

module.exports = app;
// TODO: make it configurable
const defaultGasLimit: GasLimit = {
refTime: 10_000_000_000,
proofSize: 1_000_000,
};

AzeroId.init(
parseInt(options.ttl),
options.providerUrl,
options.contract,
defaultGasLimit,
).then(db => {
const app = makeApp(signer, '/', db);
console.log(`Serving on port ${options.port} with signing address ${address}`);
app.listen(parseInt(options.port));
});
Loading
Loading