diff --git a/common/changes/@cityofzion/neon-dappkit/magic-in-init_2026-05-08-00-03.json b/common/changes/@cityofzion/neon-dappkit/magic-in-init_2026-05-08-00-03.json new file mode 100644 index 0000000..624c644 --- /dev/null +++ b/common/changes/@cityofzion/neon-dappkit/magic-in-init_2026-05-08-00-03.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@cityofzion/neon-dappkit", + "comment": "Add network magic as an optional init option", + "type": "minor" + } + ], + "packageName": "@cityofzion/neon-dappkit" +} \ No newline at end of file diff --git a/packages/neon-dappkit/src/NeonInvoker.ts b/packages/neon-dappkit/src/NeonInvoker.ts index 7cc0bb6..df3b58e 100644 --- a/packages/neon-dappkit/src/NeonInvoker.ts +++ b/packages/neon-dappkit/src/NeonInvoker.ts @@ -19,6 +19,7 @@ export type InitOptions = { account?: NeonTypes.wallet.Account | NeonTypes.wallet.Account[] validBlocks?: number signingCallback?: api.SigningFunction + networkMagic?: number } export type Options = InitOptions & { @@ -31,6 +32,16 @@ export class NeonInvoker implements Neo3Invoker { private constructor(public options: Options) {} + static async getMagicOfRpcAddress(rpcAddress: string): Promise { + const resp = await new rpc.RPCClient(rpcAddress).getVersion() + return resp.protocol.network + } + + static async init(options: InitOptions): Promise { + const networkMagic = options.networkMagic || (await this.getMagicOfRpcAddress(options.rpcAddress)) + return new NeonInvoker({ ...options, validBlocks: options.validBlocks || 100, networkMagic }) + } + async testInvoke(cim: ContractInvocationMulti): Promise { const accountArr = this.normalizeAccountArray(this.options.account) const script = this.buildScriptHex(cim) @@ -91,16 +102,6 @@ export class NeonInvoker implements Neo3Invoker { return result.map((item): RpcResponseStackItem => ({ value: item.value as any, type: item.type as any })) } - static async init({ validBlocks = 100, ...options }: InitOptions): Promise { - const networkMagic = await this.getMagicOfRpcAddress(options.rpcAddress) - return new NeonInvoker({ ...options, validBlocks, networkMagic }) - } - - static async getMagicOfRpcAddress(rpcAddress: string): Promise { - const resp = await new rpc.RPCClient(rpcAddress).getVersion() - return resp.protocol.network - } - static convertParams(args: ExtendedArg[] | undefined): NeonTypes.sc.ContractParam[] { return (args ?? []).map((a) => { if (a.type === undefined) throw new Error('Invalid argument type') diff --git a/packages/neon-dappkit/test/NeonInvoker.spec.ts b/packages/neon-dappkit/test/NeonInvoker.spec.ts index 3231103..787b4bb 100644 --- a/packages/neon-dappkit/test/NeonInvoker.spec.ts +++ b/packages/neon-dappkit/test/NeonInvoker.spec.ts @@ -927,4 +927,14 @@ describe('NeonInvoker', function () { `Invalid bytearray value '${invalidValue}' should throw an error`, ) }) + + it('Should be able to set a custom network magic in init', async () => { + const invoker = await NeonInvoker.init({ + rpcAddress, + account: account1, + networkMagic: 1, + }) + + assert.equal(invoker.options.networkMagic, 1, 'network magic is not 1') + }) })