Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cityofzion/neon-dappkit",
"comment": "Add network magic as an optional init option",
"type": "minor"
}
],
"packageName": "@cityofzion/neon-dappkit"
}
21 changes: 11 additions & 10 deletions packages/neon-dappkit/src/NeonInvoker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 & {
Expand All @@ -31,6 +32,16 @@ export class NeonInvoker implements Neo3Invoker {

private constructor(public options: Options) {}

static async getMagicOfRpcAddress(rpcAddress: string): Promise<number> {
const resp = await new rpc.RPCClient(rpcAddress).getVersion()
return resp.protocol.network
}

static async init(options: InitOptions): Promise<NeonInvoker> {
const networkMagic = options.networkMagic || (await this.getMagicOfRpcAddress(options.rpcAddress))
return new NeonInvoker({ ...options, validBlocks: options.validBlocks || 100, networkMagic })
}

async testInvoke(cim: ContractInvocationMulti): Promise<InvokeResult> {
const accountArr = this.normalizeAccountArray(this.options.account)
const script = this.buildScriptHex(cim)
Expand Down Expand Up @@ -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<NeonInvoker> {
const networkMagic = await this.getMagicOfRpcAddress(options.rpcAddress)
return new NeonInvoker({ ...options, validBlocks, networkMagic })
}

static async getMagicOfRpcAddress(rpcAddress: string): Promise<number> {
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')
Expand Down
10 changes: 10 additions & 0 deletions packages/neon-dappkit/test/NeonInvoker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
Loading