cyhhao 2 years ago
parent 707d58869c
commit aebbbf1a8d

@ -25,10 +25,10 @@ type Option = {
skipRepoName: boolean skipRepoName: boolean
} }
export function parseGit3URI( export async function parseGit3URI(
uri: string, uri: string,
option: Option = { skipRepoName: false } option: Option = { skipRepoName: false }
): Git3Protocol { ): Promise<Git3Protocol> {
const url = new URL(uri) const url = new URL(uri)
let sender = url.username || "default" let sender = url.username || "default"
let chainId = url.port ? parseInt(url.port) : null let chainId = url.port ? parseInt(url.port) : null
@ -37,8 +37,7 @@ export function parseGit3URI(
let nsName, nsDomain, ns let nsName, nsDomain, ns
if (!hub) throw new Error("invalid git3 uri, no hub address") if (!hub) throw new Error("invalid git3 uri, no hub address")
let repoName = url.pathname.slice(1) let repoName = url.pathname.slice(1)
if (!option.skipRepoName && !repoName) if (!option.skipRepoName && !repoName) throw new Error("invalid git3 uri, no repo name")
throw new Error("invalid git3 uri, no repo name")
if (hub.indexOf(".") < 0) { if (hub.indexOf(".") < 0) {
if (url.hostname.startsWith("0x")) { if (url.hostname.startsWith("0x")) {
@ -51,8 +50,17 @@ export function parseGit3URI(
ns = nameServices[nsDomain] ns = nameServices[nsDomain]
if (!ns) throw new Error("invalid name service") if (!ns) throw new Error("invalid name service")
chainId = chainId || ns.chainId chainId = chainId || ns.chainId
// Todo: resolve name service // Todo: temporary resolve name service
// hubAddress = ns.resolver()
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") if (!chainId) throw new Error("invalid git3 uri, no chainId")

@ -12,9 +12,7 @@ export function getWallet(wallet: string | null = "default"): ethers.Wallet {
const [walletType, key] = content.split("\n") const [walletType, key] = content.split("\n")
let etherWallet = let etherWallet =
walletType === "privateKey" walletType === "privateKey" ? new ethers.Wallet(key) : ethers.Wallet.fromMnemonic(key)
? new ethers.Wallet(key)
: ethers.Wallet.fromMnemonic(key)
return etherWallet return etherWallet
} }
@ -23,13 +21,13 @@ export function setupContract(
provider: ethers.providers.JsonRpcProvider, provider: ethers.providers.JsonRpcProvider,
hubAddress: string, hubAddress: string,
abi: string, abi: string,
wallet: ethers.Wallet wallet: ethers.Wallet | null = null
): ethers.Contract { ): ethers.Contract {
let contract = new ethers.Contract(hubAddress, abi, provider) let contract = new ethers.Contract(hubAddress, abi, provider)
if (wallet) {
wallet = wallet.connect(provider) wallet = wallet.connect(provider)
contract = contract.connect(wallet) contract = contract.connect(wallet)
}
return contract return contract
} }

File diff suppressed because one or more lines are too long

@ -1,23 +1,15 @@
const ns: Record<string, any> = { const ns: Record<string, any> = {
eth: {
chainId: 1,
resolver: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e",
},
w3q: { w3q: {
chainId: 3334, chainId: 3334,
resolver: "0x076B3e04dd300De7db95Ba3F5db1eD31f3139aE0", resolver: "0xF56A1dd941667911896B9B872AC79E56cfc6a3dB",
},
fvm: {
chainId: 3141,
resolver: "",
},
goerli: {
chainId: 5,
resolver: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e",
}, },
arb: { arb: {
chainId: 42170,
resolver: "0xF56A1dd941667911896B9B872AC79E56cfc6a3dB",
},
arbg: {
chainId: 421613, chainId: 421613,
resolver: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e", resolver: "0xF56A1dd941667911896B9B872AC79E56cfc6a3dB",
}, },
} }

@ -10,7 +10,7 @@ GitRemoteHelper({
stdout: process.stdout, stdout: process.stdout,
api: { api: {
init: async (p: ApiBaseParams) => { init: async (p: ApiBaseParams) => {
const protocol = parseGit3URI(p.remoteUrl) const protocol = await parseGit3URI(p.remoteUrl)
const storage = new protocol.storageClass(protocol) const storage = new protocol.storageClass(protocol)
git = new Git(p, storage) git = new Git(p, storage)
return return

@ -143,7 +143,7 @@ program
if (!uri.startsWith("git3://")) { if (!uri.startsWith("git3://")) {
uri = "git3://" + uri uri = "git3://" + uri
} }
const protocol = parseGit3URI(uri) const protocol = await parseGit3URI(uri)
let owner = await protocol.contract.repoNameToOwner( let owner = await protocol.contract.repoNameToOwner(
Buffer.from(protocol.repoName) Buffer.from(protocol.repoName)
) )
@ -215,7 +215,7 @@ program
if (!uri.startsWith("git3://")) { if (!uri.startsWith("git3://")) {
uri = "git3://" + uri uri = "git3://" + uri
} }
const protocol = parseGit3URI(uri, { skipRepoName: true }) const protocol = await parseGit3URI(uri, { skipRepoName: true })
const txManager = new TxManager( const txManager = new TxManager(
protocol.contract, protocol.contract,
protocol.chainId, protocol.chainId,

Loading…
Cancel
Save