mirror of git3://git3.w3q/git3-cli
parent
b6e6286b23
commit
c2956f0c41
@ -0,0 +1,4 @@
|
||||
{
|
||||
"prettier.tabWidth": 4,
|
||||
"vetur.format.options.tabSize": 4
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
import { ethers } from "ethers"
|
||||
import nameServices from "../config/name-services.js"
|
||||
import { ETHStorage } from "../storage/ETHStorage.js"
|
||||
import { SLIStorage } from "../storage/SLIStorage.js"
|
||||
import { getWallet, randomRPC, setupContract } from "./wallet.js"
|
||||
import network from "../config/evm-network.js"
|
||||
import abis from "../config/abis.js"
|
||||
|
||||
export type Git3Protocol = {
|
||||
sender: string
|
||||
senderAddress: string
|
||||
hubAddress: string
|
||||
repoName: string
|
||||
chainId: number
|
||||
netConfig: Record<string, any>
|
||||
wallet: ethers.Wallet
|
||||
contract: ethers.Contract
|
||||
storageClass: any
|
||||
ns?: Record<string, any>
|
||||
nsName?: string
|
||||
nsDomain?: string
|
||||
}
|
||||
|
||||
type Option = {
|
||||
skipRepoName: boolean
|
||||
}
|
||||
|
||||
export function parseGit3URI(
|
||||
uri: string,
|
||||
option: Option = { skipRepoName: false }
|
||||
): Git3Protocol {
|
||||
const url = new URL(uri)
|
||||
let sender = url.username || "default"
|
||||
let chainId = url.port ? parseInt(url.port) : null
|
||||
let hub = url.hostname
|
||||
let hubAddress
|
||||
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 (hub.indexOf(".") < 0) {
|
||||
if (url.hostname.startsWith("0x")) {
|
||||
hubAddress = url.hostname
|
||||
} else {
|
||||
throw new Error("invalid git3 uri, hub must be NS or address")
|
||||
}
|
||||
} else {
|
||||
;[nsName, nsDomain] = url.hostname.split(".")
|
||||
ns = nameServices[nsDomain]
|
||||
if (!ns) throw new Error("invalid name service")
|
||||
chainId = chainId || ns.chainId
|
||||
// Todo: resolve name service
|
||||
// hubAddress = ns.resolver()
|
||||
}
|
||||
|
||||
if (!chainId) throw new Error("invalid git3 uri, no chainId")
|
||||
|
||||
let netConfig = network[chainId]
|
||||
if (!netConfig) throw new Error("invalid chainId")
|
||||
|
||||
if (!hubAddress) hubAddress = netConfig.contracts.git3
|
||||
|
||||
let wallet = getWallet(sender)
|
||||
|
||||
let senderAddress = wallet.address
|
||||
|
||||
// route to different storage
|
||||
let storageClass, abi
|
||||
if (chainId == 3334) {
|
||||
storageClass = ETHStorage
|
||||
abi = abis.ETHStorage
|
||||
} else {
|
||||
storageClass = SLIStorage
|
||||
abi = abis.SLIStorage
|
||||
}
|
||||
let rpc = randomRPC(netConfig.rpc)
|
||||
const provider = new ethers.providers.JsonRpcProvider(rpc)
|
||||
|
||||
let contract = setupContract(provider, hubAddress, abi, wallet)
|
||||
wallet = wallet.connect(contract.provider)
|
||||
|
||||
return {
|
||||
sender,
|
||||
senderAddress,
|
||||
hubAddress,
|
||||
repoName,
|
||||
chainId,
|
||||
netConfig,
|
||||
wallet,
|
||||
contract,
|
||||
storageClass,
|
||||
ns,
|
||||
nsName,
|
||||
nsDomain,
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
import { mkdirSync, readFileSync } from "fs"
|
||||
import { ethers } from "ethers"
|
||||
|
||||
export function getWallet(wallet: string | null = "default"): ethers.Wallet {
|
||||
if (!wallet) wallet = "default"
|
||||
|
||||
// Todo: 0xaddress find wallet
|
||||
const keyPath = process.env.HOME + "/.git3/keys"
|
||||
mkdirSync(keyPath, { recursive: true })
|
||||
|
||||
const content = readFileSync(`${keyPath}/${wallet}`).toString()
|
||||
const [walletType, key] = content.split("\n")
|
||||
|
||||
let etherWallet =
|
||||
walletType === "privateKey"
|
||||
? new ethers.Wallet(key)
|
||||
: ethers.Wallet.fromMnemonic(key)
|
||||
|
||||
return etherWallet
|
||||
}
|
||||
|
||||
export function setupContract(
|
||||
provider: ethers.providers.JsonRpcProvider,
|
||||
hubAddress: string,
|
||||
abi: string,
|
||||
wallet: ethers.Wallet
|
||||
): ethers.Contract {
|
||||
|
||||
let contract = new ethers.Contract(hubAddress, abi, provider)
|
||||
wallet = wallet.connect(provider)
|
||||
contract = contract.connect(wallet)
|
||||
|
||||
return contract
|
||||
}
|
||||
|
||||
export function randomRPC(rpcs: string[]): string {
|
||||
return rpcs[Math.floor(Math.random() * rpcs.length)]
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
import { mkdirSync, readFileSync } from "fs"
|
||||
import { ethers } from 'ethers'
|
||||
|
||||
export function getWallet(sender: string | null): ethers.Wallet {
|
||||
// Todo: according sender address to select wallet, if sender==null then use default wallet
|
||||
const wallet = 'default'
|
||||
|
||||
const keyPath = process.env.HOME + "/.git3/keys"
|
||||
mkdirSync(keyPath, { recursive: true })
|
||||
|
||||
const content = readFileSync(`${keyPath}/${wallet}`).toString()
|
||||
const [walletType, key] = content.split('\n')
|
||||
|
||||
let etherWallet = walletType === 'privateKey'
|
||||
? new ethers.Wallet(key)
|
||||
: ethers.Wallet.fromMnemonic(key)
|
||||
|
||||
return etherWallet
|
||||
}
|
Loading…
Reference in new issue