From aebbbf1a8dffdf4e2f09dc3fd87eae314e1208cc Mon Sep 17 00:00:00 2001 From: cyhhao Date: Sun, 19 Feb 2023 22:27:33 +0800 Subject: [PATCH] add --- src/common/git3-protocol.ts | 20 ++++++++++++++------ src/common/wallet.ts | 14 ++++++-------- src/config/abis.ts | 2 ++ src/config/name-services.ts | 20 ++++++-------------- src/git-remote-git3/index.ts | 2 +- src/git3/index.ts | 4 ++-- 6 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/common/git3-protocol.ts b/src/common/git3-protocol.ts index 692a338..4a45914 100644 --- a/src/common/git3-protocol.ts +++ b/src/common/git3-protocol.ts @@ -25,10 +25,10 @@ type Option = { skipRepoName: boolean } -export function parseGit3URI( +export async function parseGit3URI( uri: string, option: Option = { skipRepoName: false } -): Git3Protocol { +): Promise { const url = new URL(uri) let sender = url.username || "default" let chainId = url.port ? parseInt(url.port) : null @@ -37,8 +37,7 @@ export function parseGit3URI( let nsName, nsDomain, ns if (!hub) throw new Error("invalid git3 uri, no hub address") let repoName = url.pathname.slice(1) - if (!option.skipRepoName && !repoName) - throw new Error("invalid git3 uri, no repo name") + if (!option.skipRepoName && !repoName) throw new Error("invalid git3 uri, no repo name") if (hub.indexOf(".") < 0) { if (url.hostname.startsWith("0x")) { @@ -51,8 +50,17 @@ export function parseGit3URI( ns = nameServices[nsDomain] if (!ns) throw new Error("invalid name service") chainId = chainId || ns.chainId - // Todo: resolve name service - // hubAddress = ns.resolver() + // Todo: temporary resolve name service + + let resolverAddress = ns["resolver"] + let nsContract = setupContract( + new ethers.providers.JsonRpcProvider("https://goerli-rollup.arbitrum.io/rpc"), + resolverAddress, + abis.NameService + ) + hubAddress = await nsContract.NameHub([nsName, nsDomain].join(".")) + if (hubAddress == "0x0000000000000000000000000000000000000000") + throw new Error(`${nsName} not found`) } if (!chainId) throw new Error("invalid git3 uri, no chainId") diff --git a/src/common/wallet.ts b/src/common/wallet.ts index 076af9a..cb3257f 100644 --- a/src/common/wallet.ts +++ b/src/common/wallet.ts @@ -12,9 +12,7 @@ export function getWallet(wallet: string | null = "default"): ethers.Wallet { const [walletType, key] = content.split("\n") let etherWallet = - walletType === "privateKey" - ? new ethers.Wallet(key) - : ethers.Wallet.fromMnemonic(key) + walletType === "privateKey" ? new ethers.Wallet(key) : ethers.Wallet.fromMnemonic(key) return etherWallet } @@ -23,13 +21,13 @@ export function setupContract( provider: ethers.providers.JsonRpcProvider, hubAddress: string, abi: string, - wallet: ethers.Wallet + wallet: ethers.Wallet | null = null ): ethers.Contract { - let contract = new ethers.Contract(hubAddress, abi, provider) - wallet = wallet.connect(provider) - contract = contract.connect(wallet) - + if (wallet) { + wallet = wallet.connect(provider) + contract = contract.connect(wallet) + } return contract } diff --git a/src/config/abis.ts b/src/config/abis.ts index 803eead..d6c4461 100644 --- a/src/config/abis.ts +++ b/src/config/abis.ts @@ -3,4 +3,6 @@ export default { '[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes","name":"repoName","type":"bytes"},{"indexed":false,"internalType":"bytes","name":"ref","type":"bytes"}],"name":"PushRef","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes","name":"repoName","type":"bytes"},{"indexed":false,"internalType":"address","name":"owner","type":"address"}],"name":"RepoCreated","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes","name":"repoName","type":"bytes"},{"indexed":false,"internalType":"address","name":"oldOwner","type":"address"},{"indexed":false,"internalType":"address","name":"newOwner","type":"address"}],"name":"RepoOwnerTransfer","type":"event"},{"inputs":[{"internalType":"bytes","name":"repoName","type":"bytes"},{"internalType":"bytes","name":"path","type":"bytes"},{"internalType":"uint256","name":"chunkId","type":"uint256"}],"name":"chunkStakeTokens","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes","name":"repoName","type":"bytes"},{"internalType":"bytes","name":"name","type":"bytes"}],"name":"countChunks","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes","name":"repoName","type":"bytes"}],"name":"createRepo","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes","name":"repoName","type":"bytes"},{"internalType":"bytes","name":"ref","type":"bytes"}],"name":"delRef","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes","name":"repoName","type":"bytes"},{"internalType":"bytes","name":"path","type":"bytes"}],"name":"download","outputs":[{"internalType":"bytes","name":"","type":"bytes"},{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes","name":"repoName","type":"bytes"},{"internalType":"bytes","name":"path","type":"bytes"},{"internalType":"uint256","name":"chunkId","type":"uint256"}],"name":"getChunkAddr","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"isOptimize","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes","name":"repoName","type":"bytes"}],"name":"listRefs","outputs":[{"components":[{"internalType":"bytes20","name":"hash","type":"bytes20"},{"internalType":"bytes","name":"name","type":"bytes"}],"internalType":"struct Git3Hub.refData[]","name":"list","type":"tuple[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes","name":"","type":"bytes"}],"name":"nameToRefInfo","outputs":[{"internalType":"bytes20","name":"hash","type":"bytes20"},{"internalType":"uint96","name":"index","type":"uint96"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes","name":"repoName","type":"bytes"},{"internalType":"bytes","name":"path","type":"bytes"}],"name":"remove","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes","name":"repoName","type":"bytes"},{"internalType":"bytes","name":"path","type":"bytes"},{"internalType":"uint256","name":"chunkId","type":"uint256"}],"name":"removeChunk","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes","name":"","type":"bytes"}],"name":"repoNameToOwner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes","name":"","type":"bytes"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"repoNameToRefs","outputs":[{"internalType":"bytes","name":"","type":"bytes"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes","name":"repoName","type":"bytes"},{"internalType":"bytes","name":"ref","type":"bytes"},{"internalType":"bytes20","name":"refHash","type":"bytes20"}],"name":"setRef","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes","name":"repoName","type":"bytes"},{"internalType":"bytes","name":"name","type":"bytes"}],"name":"size","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes","name":"repoName","type":"bytes"},{"internalType":"bytes","name":"path","type":"bytes"}],"name":"stakeTokens","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes","name":"repoName","type":"bytes"},{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes","name":"repoName","type":"bytes"},{"internalType":"bytes","name":"path","type":"bytes"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"upload","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"bytes","name":"repoName","type":"bytes"},{"internalType":"bytes","name":"path","type":"bytes"},{"internalType":"uint256","name":"chunkId","type":"uint256"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"uploadChunk","outputs":[],"stateMutability":"payable","type":"function"}]', SLIStorage: '[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bytes","name":"repoName","type":"bytes"},{"indexed":false,"internalType":"bytes","name":"ref","type":"bytes"}],"name":"PushRef","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bytes","name":"repoName","type":"bytes"},{"indexed":false,"internalType":"address","name":"owner","type":"address"}],"name":"RepoCreated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bytes","name":"repoName","type":"bytes"},{"indexed":false,"internalType":"address","name":"oldOwner","type":"address"},{"indexed":false,"internalType":"address","name":"newOwner","type":"address"}],"name":"RepoOwnerTransfer","type":"event"},{"inputs":[{"internalType":"bytes","name":"repoName","type":"bytes"},{"internalType":"bytes[]","name":"path","type":"bytes[]"},{"internalType":"bytes[]","name":"data","type":"bytes[]"}],"name":"batchUpload","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"bytes","name":"repoName","type":"bytes"}],"name":"createRepo","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes","name":"repoName","type":"bytes"},{"internalType":"bytes","name":"ref","type":"bytes"}],"name":"delRef","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes","name":"repoName","type":"bytes"},{"internalType":"bytes","name":"path","type":"bytes"}],"name":"download","outputs":[{"internalType":"bytes","name":"","type":"bytes"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes","name":"repoName","type":"bytes"}],"name":"listRefs","outputs":[{"components":[{"internalType":"bytes20","name":"hash","type":"bytes20"},{"internalType":"bytes","name":"name","type":"bytes"}],"internalType":"struct Git3HubStorage_SLI.refData[]","name":"list","type":"tuple[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes","name":"","type":"bytes"}],"name":"nameToRefInfo","outputs":[{"internalType":"bytes20","name":"hash","type":"bytes20"},{"internalType":"uint96","name":"index","type":"uint96"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"pathToHash","outputs":[{"internalType":"bytes","name":"","type":"bytes"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes","name":"repoName","type":"bytes"},{"internalType":"bytes","name":"path","type":"bytes"}],"name":"remove","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes","name":"","type":"bytes"}],"name":"repoNameToOwner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes","name":"","type":"bytes"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"repoNameToRefs","outputs":[{"internalType":"bytes","name":"","type":"bytes"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes","name":"repoName","type":"bytes"},{"internalType":"bytes","name":"ref","type":"bytes"},{"internalType":"bytes20","name":"refHash","type":"bytes20"}],"name":"setRef","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes","name":"repoName","type":"bytes"},{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes","name":"repoName","type":"bytes"},{"internalType":"bytes","name":"path","type":"bytes"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"upload","outputs":[],"stateMutability":"payable","type":"function"}]', + NameService: + '[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"string","name":"","type":"string"}],"name":"HubRecords","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"","type":"string"}],"name":"NameHub","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"","type":"string"}],"name":"NameOwner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"name","type":"string"},{"internalType":"address","name":"hub","type":"address"}],"name":"rebindHubAddress","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"name","type":"string"},{"internalType":"address","name":"hub","type":"address"}],"name":"registerHub","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"name","type":"string"},{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferNameOwner","outputs":[],"stateMutability":"nonpayable","type":"function"}]', } diff --git a/src/config/name-services.ts b/src/config/name-services.ts index efe4866..fe8240a 100644 --- a/src/config/name-services.ts +++ b/src/config/name-services.ts @@ -1,23 +1,15 @@ const ns: Record = { - eth: { - chainId: 1, - resolver: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e", - }, w3q: { chainId: 3334, - resolver: "0x076B3e04dd300De7db95Ba3F5db1eD31f3139aE0", - }, - fvm: { - chainId: 3141, - resolver: "", - }, - goerli: { - chainId: 5, - resolver: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e", + resolver: "0xF56A1dd941667911896B9B872AC79E56cfc6a3dB", }, arb: { + chainId: 42170, + resolver: "0xF56A1dd941667911896B9B872AC79E56cfc6a3dB", + }, + arbg: { chainId: 421613, - resolver: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e", + resolver: "0xF56A1dd941667911896B9B872AC79E56cfc6a3dB", }, } diff --git a/src/git-remote-git3/index.ts b/src/git-remote-git3/index.ts index b38bdf2..91a0e38 100644 --- a/src/git-remote-git3/index.ts +++ b/src/git-remote-git3/index.ts @@ -10,7 +10,7 @@ GitRemoteHelper({ stdout: process.stdout, api: { init: async (p: ApiBaseParams) => { - const protocol = parseGit3URI(p.remoteUrl) + const protocol = await parseGit3URI(p.remoteUrl) const storage = new protocol.storageClass(protocol) git = new Git(p, storage) return diff --git a/src/git3/index.ts b/src/git3/index.ts index bb2c1a0..e1def78 100644 --- a/src/git3/index.ts +++ b/src/git3/index.ts @@ -143,7 +143,7 @@ program if (!uri.startsWith("git3://")) { uri = "git3://" + uri } - const protocol = parseGit3URI(uri) + const protocol = await parseGit3URI(uri) let owner = await protocol.contract.repoNameToOwner( Buffer.from(protocol.repoName) ) @@ -215,7 +215,7 @@ program if (!uri.startsWith("git3://")) { uri = "git3://" + uri } - const protocol = parseGit3URI(uri, { skipRepoName: true }) + const protocol = await parseGit3URI(uri, { skipRepoName: true }) const txManager = new TxManager( protocol.contract, protocol.chainId,