-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: clean-up the network configuration
- Loading branch information
Showing
3 changed files
with
125 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,145 @@ | ||
import type { OfflineSigner } from "@cosmjs/proto-signing"; | ||
import type { Keplr } from "@keplr-wallet/types"; | ||
import { VmClientBuilder } from "@nillion/client-vms"; | ||
import { VmClientBuilder, type VmClient } from "@nillion/client-vms"; | ||
import { createSignerFromKey } from "@nillion/client-vms"; | ||
import type { PaymentMode } from "@nillion/client-vms"; | ||
|
||
export type TestnetOptions = { | ||
network: "testnet"; | ||
seed: string; | ||
keplr: Keplr; | ||
paymentMode?: PaymentMode; | ||
config?: TestnetConfig; | ||
}; | ||
const DEFAULT_TESTNET_BOOTNODE_URL = | ||
"https://node-1.nilvm-testnet-1.nillion-network.testnet.nillion.network:14311"; | ||
const DEFAULT_TESTNET_CHAIN_URL = | ||
"https://rpc.testnet.nilchain-rpc-proxy.nilogy.xyz"; | ||
const DEFAULT_TESTNET_CHAIN_ID = "nillion-chain-testnet-1"; | ||
|
||
const DEFAULT_DEVNET_BOOTNODE_URL = "http://127.0.0.1:43207"; | ||
const DEFAULT_DEVNET_CHAIN_URL = "http://127.0.0.1:48102"; | ||
const DEFAULT_DEVNET_CHAIN_ID = "nillion-chain-devnet"; | ||
const DEFAULT_PRIVATE_KEY = | ||
"9a975f567428d054f2bf3092812e6c42f901ce07d9711bc77ee2cd81101f42c5"; | ||
|
||
enum NetworkType { | ||
Testnet = 0, | ||
Devnet = 1, | ||
} | ||
|
||
export type TestnetConfig = { | ||
bootnodeUrl: string; | ||
chainUrl: string; | ||
chainId: string; | ||
}; | ||
|
||
const TestnetDefaultConfig: TestnetConfig = { | ||
bootnodeUrl: | ||
"https://node-1.nilvm-testnet-1.nillion-network.testnet.nillion.network:14311", | ||
chainUrl: "https://rpc.testnet.nilchain-rpc-proxy.nilogy.xyz", | ||
chainId: "nillion-chain-testnet-1", | ||
type: NetworkType; | ||
keplr: Keplr; | ||
}; | ||
|
||
export type DevnetOptions = { | ||
network: "devnet"; | ||
seed?: string; | ||
export type DevnetConfig = { | ||
type: NetworkType; | ||
privateKey: string; | ||
signer?: Keplr | OfflineSigner; | ||
paymentMode?: PaymentMode; | ||
config?: DevnetConfig; | ||
}; | ||
|
||
export type DevnetConfig = { | ||
bootnodeUrl: string; | ||
chainUrl: string; | ||
chainId: string; | ||
seed: string; | ||
nilchainPrivateKey0: string; | ||
}; | ||
type NetworkConfig = DevnetConfig | TestnetConfig; | ||
|
||
export class ClientBuilder { | ||
private _config: NetworkConfig; | ||
private _seed: string; | ||
private _bootnodeUrl: string; | ||
private _chainUrl: string; | ||
private _chainId: string; | ||
private _paymentMode?: PaymentMode; | ||
|
||
private constructor( | ||
config: NetworkConfig, | ||
seed: string, | ||
bootnodeUrl: string, | ||
chainUrl: string, | ||
chainId: string, | ||
paymentMode?: PaymentMode, | ||
) { | ||
this._config = config; | ||
this._seed = seed; | ||
this._bootnodeUrl = bootnodeUrl; | ||
this._chainUrl = chainUrl; | ||
this._chainId = chainId; | ||
this._paymentMode = paymentMode; | ||
} | ||
|
||
const DevnetDefaultConfig: DevnetConfig = { | ||
bootnodeUrl: "http://127.0.0.1:43207", | ||
chainUrl: "http://127.0.0.1:48102", | ||
chainId: "nillion-chain-devnet", | ||
seed: "user-devnet-seed", | ||
nilchainPrivateKey0: | ||
"9a975f567428d054f2bf3092812e6c42f901ce07d9711bc77ee2cd81101f42c5", | ||
}; | ||
static testnet(seed: string, keplr: Keplr): ClientBuilder { | ||
return new ClientBuilder( | ||
{ type: NetworkType.Testnet, keplr }, | ||
seed, | ||
DEFAULT_TESTNET_BOOTNODE_URL, | ||
DEFAULT_TESTNET_CHAIN_URL, | ||
DEFAULT_TESTNET_CHAIN_ID, | ||
); | ||
} | ||
|
||
type Options = DevnetOptions | TestnetOptions; | ||
static devnet( | ||
seed: string, | ||
privateKey?: string, | ||
signer?: Keplr | OfflineSigner, | ||
): ClientBuilder { | ||
return new ClientBuilder( | ||
{ | ||
type: NetworkType.Devnet, | ||
privateKey: privateKey ? privateKey : DEFAULT_PRIVATE_KEY, | ||
signer, | ||
}, | ||
seed, | ||
DEFAULT_DEVNET_BOOTNODE_URL, | ||
DEFAULT_DEVNET_CHAIN_URL, | ||
DEFAULT_DEVNET_CHAIN_ID, | ||
); | ||
} | ||
|
||
export async function createClient(options: Options) { | ||
const builder = new VmClientBuilder(options.paymentMode); | ||
switch (options.network.toLowerCase()) { | ||
case "devnet": { | ||
const network = options as DevnetOptions; | ||
let config = DevnetDefaultConfig; | ||
if (network.config !== undefined) { | ||
config = network.config; | ||
} | ||
bootnodeUrl(bootnodeUrl: string): this { | ||
this._bootnodeUrl = bootnodeUrl; | ||
return this; | ||
} | ||
|
||
chainUrl(chainUrl: string): this { | ||
this._chainUrl = chainUrl; | ||
return this; | ||
} | ||
|
||
let signer: OfflineSigner; | ||
chainId(chainId: string): this { | ||
this._chainId = chainId; | ||
return this; | ||
} | ||
|
||
if (network.signer) { | ||
if ("getOfflineSigner" in network.signer) { | ||
signer = network.signer.getOfflineSigner(config.chainId); | ||
paymentMode(paymentMode: PaymentMode): this { | ||
this._paymentMode = paymentMode; | ||
return this; | ||
} | ||
|
||
async build(): Promise<VmClient> { | ||
const builder = new VmClientBuilder(this._paymentMode) | ||
.seed(this._seed) | ||
.bootnodeUrl(this._bootnodeUrl) | ||
.chainUrl(this._chainUrl); | ||
|
||
switch (this._config.type) { | ||
case NetworkType.Devnet: { | ||
const config = this._config as DevnetConfig; | ||
let signer: OfflineSigner; | ||
if (config.signer) { | ||
if ("getOfflineSigner" in config.signer) { | ||
signer = config.signer.getOfflineSigner(this._chainId); | ||
} else { | ||
signer = config.signer; | ||
} | ||
} else { | ||
signer = network.signer; | ||
signer = await createSignerFromKey(config.privateKey); | ||
} | ||
} else { | ||
signer = await createSignerFromKey(config.nilchainPrivateKey0); | ||
builder.signer(signer); | ||
break; | ||
} | ||
|
||
const seed = network.seed ? network.seed : config.seed; | ||
|
||
builder | ||
.seed(seed) | ||
.bootnodeUrl(config.bootnodeUrl) | ||
.chainUrl(config.chainUrl) | ||
.signer(signer); | ||
|
||
break; | ||
} | ||
case "testnet": { | ||
const network = options as TestnetOptions; | ||
let config = TestnetDefaultConfig; | ||
if (network.config !== undefined) { | ||
config = network.config; | ||
case NetworkType.Testnet: { | ||
const config = this._config as TestnetConfig; | ||
const signer = config.keplr.getOfflineSigner(this._chainId); | ||
builder.signer(signer); | ||
break; | ||
} | ||
|
||
const signer = network.keplr.getOfflineSigner(config.chainId); | ||
|
||
builder | ||
.seed(network.seed) | ||
.bootnodeUrl(config.bootnodeUrl) | ||
.chainUrl(config.chainUrl) | ||
.signer(signer); | ||
|
||
break; | ||
} | ||
default: { | ||
throw new Error(`Unknown network: ${options.network}`); | ||
default: { | ||
throw new Error("Unknown network"); | ||
} | ||
} | ||
return builder.build(); | ||
} | ||
|
||
return builder.build(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters