From e6f95bc165805db81114b880aba8f7e60236a829 Mon Sep 17 00:00:00 2001 From: Andrew Miller Date: Tue, 17 Mar 2026 10:50:43 -0400 Subject: [PATCH] Sync Node SDK with Go SDK: add missing types, credentials functions, and client getters - Add remote field to SmartContractCreateRequest - Add GrpcConnectionInfo interface and field on SmartContract - Add addChain, setDefault, deleteChain credential functions - Add getPublicId, getAuthKeyId, getEndpoint client accessors - Fix expandPath to handle both $VAR and ${VAR} env var formats - Improve saveConfig with better yaml.dump options and _rawContent tracking --- src/client.ts | 21 +++++++++++++++++ src/credentials.ts | 59 ++++++++++++++++++++++++++++++++++++++++++---- src/types.ts | 7 ++++++ 3 files changed, 82 insertions(+), 5 deletions(-) diff --git a/src/client.ts b/src/client.ts index 3eb38b4..4004c3b 100644 --- a/src/client.ts +++ b/src/client.ts @@ -177,6 +177,27 @@ export class DragonchainClient { }); } + /** + * Returns the public ID + */ + public getPublicId(): string { + return this.publicId; + } + + /** + * Returns the auth key ID + */ + public getAuthKeyId(): string { + return this.authKeyId; + } + + /** + * Returns the base URL endpoint + */ + public getEndpoint(): string { + return this.baseURL; + } + /** * Performs a GET request */ diff --git a/src/credentials.ts b/src/credentials.ts index c4fb1aa..ed54e86 100644 --- a/src/credentials.ts +++ b/src/credentials.ts @@ -19,19 +19,23 @@ export interface ChainConfig { } /** - * Complete configuration structure + * Complete configuration structure. + * The _rawContent field stores the original YAML for re-saving unmodified configs. */ export interface Config { default: string; chains: ChainConfig[]; + /** @internal Original YAML content, used to preserve formatting on re-save */ + _rawContent?: string; } /** * Expands file paths with home directory and environment variables */ function expandPath(filePath: string): string { - // Expand environment variables - let expanded = filePath.replace(/\$\{([^}]+)\}/g, (_, variable: string) => { + // Expand environment variables: ${VAR} and $VAR formats + let expanded = filePath.replace(/\$\{([^}]+)\}|\$([A-Za-z_][A-Za-z0-9_]*)/g, (_, braced, bare) => { + const variable = braced || bare; return process.env[variable] || ''; }); @@ -62,6 +66,9 @@ export function loadConfig(filePath: string): Config { throw new Error('Invalid config: missing or invalid chains array'); } + // Store raw content for formatting preservation on re-save + config._rawContent = fileContent; + return config; } @@ -75,6 +82,9 @@ export function loadConfigFromString(yamlContent: string): Config { throw new Error('Invalid config: missing or invalid chains array'); } + // Store raw content for formatting preservation on re-save + config._rawContent = yamlContent; + return config; } @@ -100,10 +110,49 @@ export function listChains(config: Config): string[] { } /** - * Saves configuration to a YAML file + * Adds a chain configuration to the config + */ +export function addChain(config: Config, chain: ChainConfig): void { + config.chains.push(chain); + delete config._rawContent; +} + +/** + * Sets the default chain by public ID + */ +export function setDefault(config: Config, publicId: string): void { + config.default = publicId; + delete config._rawContent; +} + +/** + * Deletes a chain configuration by public ID. + * Throws if attempting to delete the default chain. + */ +export function deleteChain(config: Config, publicId: string): void { + if (publicId === config.default) { + throw new Error('cannot delete default chain'); + } + config.chains = config.chains.filter((chain) => chain.publicId !== publicId); + delete config._rawContent; +} + +/** + * Saves configuration to a YAML file. + * Strips internal _rawContent before serializing. */ export function saveConfig(config: Config, filePath: string): void { const expandedPath = expandPath(filePath); - const yamlContent = yaml.dump(config); + + // Strip internal field before dumping + const { _rawContent, ...configData } = config; + + const yamlContent = yaml.dump(configData, { + lineWidth: -1, // No line wrapping + noRefs: true, // No YAML references + quotingType: "'", // Prefer single quotes + forceQuotes: false, + }); + fs.writeFileSync(expandedPath, yamlContent, 'utf8'); } diff --git a/src/types.ts b/src/types.ts index a9d5fee..53eeb92 100644 --- a/src/types.ts +++ b/src/types.ts @@ -84,6 +84,7 @@ export interface SmartContractCreateRequest { executionOrder: string; environmentVariables?: Record; secrets?: Record; + remote?: boolean; } export interface SmartContractUpdateRequest { @@ -100,6 +101,11 @@ export interface SmartContractExecutionInfo { executableHash?: string; } +export interface GrpcConnectionInfo { + address: string; + apiKey?: string; +} + export interface SmartContract { id: string; created: number; @@ -111,6 +117,7 @@ export interface SmartContract { executionInfo?: SmartContractExecutionInfo; envVars: Record; secrets: string[]; + grpcConnectionInfo?: GrpcConnectionInfo; } // Block Types