-
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.
Merge pull request #38 from NillionNetwork/timhm/release-tweaks-and-f…
…ixes-0-2-0
- Loading branch information
Showing
19 changed files
with
516 additions
and
205 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,67 @@ | ||
import { VmClientBuilder, createSignerFromKey } from "@nillion/client-vms"; | ||
import type { VmClientBuilderConfig } from "@nillion/client-vms"; | ||
import { | ||
PrivateKeyBase16, | ||
VmClientBuilder, | ||
createSignerFromKey, | ||
} from "@nillion/client-vms"; | ||
import { z } from "zod"; | ||
|
||
export type Network = "testnet" | "devnet" | "custom"; | ||
|
||
export const ClientConfig = z.object({ | ||
bootnodeUrl: z.string().url(), | ||
chainUrl: z.string().url(), | ||
seed: z.string().min(1), | ||
nilchainPrivateKey0: PrivateKeyBase16, | ||
}); | ||
export type ClientConfig = z.infer<typeof ClientConfig>; | ||
|
||
const NamedConfig = { | ||
// use with `$ nillion-devnet` default seed | ||
devnet: { | ||
bootnodeUrl: "http://127.0.0.1:37939", | ||
chainUrl: "http://127.0.0.1:48102", | ||
seed: "user-devnet-seed", | ||
nilchainPrivateKey0: | ||
"9a975f567428d054f2bf3092812e6c42f901ce07d9711bc77ee2cd81101f42c5", | ||
}, | ||
}; | ||
|
||
export async function createClient( | ||
network: Network, | ||
_overrides?: Partial<VmClientBuilderConfig>, | ||
overrides?: Partial<ClientConfig>, | ||
) { | ||
switch (network) { | ||
const builder = new VmClientBuilder(); | ||
switch (network.toLowerCase()) { | ||
case "devnet": { | ||
const config = { ...NamedConfig.devnet }; | ||
const singer = await createSignerFromKey(config.nilchainPrivateKey0); | ||
builder | ||
.seed(config.seed) | ||
.bootnodeUrl(config.bootnodeUrl) | ||
.chainUrl(config.chainUrl) | ||
.signer(singer); | ||
break; | ||
} | ||
case "custom": { | ||
const { nilchainPrivateKey0, seed, bootnodeUrl, chainUrl } = | ||
ClientConfig.parse(overrides); | ||
|
||
if (!nilchainPrivateKey0 || !seed || !bootnodeUrl || !chainUrl) { | ||
throw new Error("Missing required config"); | ||
} | ||
|
||
const singer = await createSignerFromKey(nilchainPrivateKey0); | ||
builder | ||
.seed(seed) | ||
.bootnodeUrl(bootnodeUrl) | ||
.chainUrl(chainUrl) | ||
.signer(singer); | ||
break; | ||
} | ||
default: { | ||
const singer = await createSignerFromKey( | ||
"9a975f567428d054f2bf3092812e6c42f901ce07d9711bc77ee2cd81101f42c5", | ||
); | ||
return await new VmClientBuilder() | ||
.authTokenTtl(1) | ||
.seed("tests") | ||
.bootnodeUrl("http://127.0.0.1:43207") | ||
.chainUrl("http://127.0.0.1:48102") | ||
.signer(singer) | ||
.build(); | ||
throw new Error(`Unsupported network: ${network}`); | ||
} | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,13 @@ | ||
export { Log } from "./logging"; | ||
|
||
export { NillionProvider } from "./nillion-provider"; | ||
|
||
export { createClient } from "./create-client"; | ||
|
||
export { useNillion } from "./use-nillion"; | ||
export { useNilPoolStatus } from "./use-nil-pool-status"; | ||
export { useNilStoreValues } from "./use-nil-store-values"; | ||
export { useNilRetrieveValues } from "./use-nil-retrieve-values"; | ||
export { useNilDeleteValues } from "./use-nil-delete-values"; | ||
export { useNilRetrievePermissions } from "./use-nil-retrieve-permissions"; | ||
export { useNilUpdatePermissions } from "./use-nil-update-permissions"; | ||
export { useNilOverwritePermissions } from "./use-nil-overwrite-permissions"; | ||
export { useNilStoreProgram } from "./use-nil-store-program"; | ||
export { useNilInvokeCompute } from "./use-nil-invoke-compute"; | ||
export { useNilOverwritePermissions } from "./use-nil-overwrite-permissions"; | ||
export { useNilPoolStatus } from "./use-nil-pool-status"; | ||
export { useNilRetrieveComputeResults } from "./use-nil-retrieve-compute-results"; | ||
export { useNilRetrievePermissions } from "./use-nil-retrieve-permissions"; | ||
export { useNilRetrieveValues } from "./use-nil-retrieve-values"; | ||
export { useNilStoreProgram } from "./use-nil-store-program"; | ||
export { useNilStoreValues } from "./use-nil-store-values"; | ||
export { useNilUpdatePermissions } from "./use-nil-update-permissions"; | ||
export { useNillion } from "./use-nillion"; |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { createLogger } from "@nillion/client-vms/logger"; | ||
|
||
export const Log = createLogger("@nillion/client-react-hooks"); |
This file was deleted.
Oops, something went wrong.
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
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,5 +1,7 @@ | ||
export * from "./logger"; | ||
export * from "./payment"; | ||
export * from "./types"; | ||
export * from "./util"; | ||
export * from "./vm"; | ||
|
||
export { NadaValue } from "@nillion/client-wasm"; |
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,20 +1,74 @@ | ||
import pino, { type Logger } from "pino"; | ||
|
||
export const Log = createLogger(); | ||
export const Log = createLogger("@nillion/client-vm"); | ||
|
||
function createLogger(): Logger<never, boolean> { | ||
type PinoWriteFnArgs = { | ||
time: number; | ||
level: string; | ||
module: string; | ||
msg: string | object; | ||
[index: string]: unknown; | ||
}; | ||
|
||
const levelColors: Record<string, string> = { | ||
debug: "#808080", | ||
info: "#0099ff", | ||
warn: "#ffa500", | ||
error: "#ff0000", | ||
fatal: "#800000", | ||
trace: "#a0a0a0", | ||
}; | ||
|
||
export function createLogger(module: string): Logger<never, boolean> { | ||
const isBrowser = Boolean(globalThis.window); | ||
|
||
if (isBrowser) { | ||
return pino({ | ||
level: "debug", | ||
}); | ||
browser: { | ||
asObject: true, | ||
formatters: { | ||
level(label, _numerical) { | ||
return { level: label }; | ||
}, | ||
}, | ||
// @ts-expect-error args type is customised through `child` and `formatters` which pino doesn't recognize | ||
write: (args: PinoWriteFnArgs) => { | ||
if (!localStorage.debug?.includes("@nillion")) { | ||
return; | ||
} | ||
|
||
const time = new Date(args.time).toLocaleTimeString(); | ||
const { level, module, msg, time: _, ...rest } = args; | ||
const color = levelColors[level.toLowerCase()] || "#000000"; | ||
const style = `color: ${color}; font-weight: bold`; | ||
const baseMsg = `${time} %c${level.toUpperCase().padEnd(5)}%c ${module}:`; | ||
|
||
const logArgs: unknown[] = [baseMsg, style, ""]; | ||
|
||
if (typeof msg === "object") { | ||
logArgs[0] += " %O"; | ||
logArgs.push({ ...msg, ...rest }); | ||
} else { | ||
if (msg) { | ||
logArgs[0] += ` ${msg}`; | ||
} | ||
if (Object.keys(rest).length > 0) { | ||
logArgs[0] += " %O"; | ||
logArgs.push(rest); | ||
} | ||
} | ||
|
||
console.log(...logArgs); | ||
}, | ||
}, | ||
}).child({ module }); | ||
} | ||
|
||
return pino({ | ||
level: "debug", | ||
transport: { | ||
target: "pino-pretty", | ||
}, | ||
}); | ||
}).child({ module }); | ||
} |
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
Oops, something went wrong.