Skip to content

Commit 6446baf

Browse files
committed
typings correct
Signed-off-by: Alex Matson <alex.matson@digitalasset.com>
1 parent 7ed5f76 commit 6446baf

File tree

22 files changed

+435
-340
lines changed

22 files changed

+435
-340
lines changed

api-specs/openrpc-dapp-api.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@
222222
"title": "JsPrepareSubmissionRequest",
223223
"type": "object",
224224
"description": "Structure representing the request for prepare and execute calls",
225+
"additionalProperties": false,
225226
"properties": {
226227
"commandId": {
227228
"$ref": "api-specs/openrpc-dapp-api.json#/components/schemas/CommandId"

core/rpc-generator/src/components/client.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ import { RpcTransport } from '@canton-network/core-rpc-transport'
2121
2222
<%= methodTypings.toString("typescript").replace(/export type AnyOf[A-Za-z0-9]+ =(?:[\\r\\n]|.)*?;/gm, "") %>
2323
24+
type AsyncReturnType<T> = T extends (...args: any[]) => Promise<infer R>
25+
? R
26+
: never
27+
28+
export type RpcMethods = {
29+
<% openrpcDocument.methods.forEach((method) => { %>
30+
<%= method.name %>: {
31+
params: Parameters<<%= _.upperFirst(method.name) %>>
32+
result: AsyncReturnType<<%= _.upperFirst(method.name) %>>
33+
}
34+
<% }); %>
35+
}
36+
37+
export type RpcClientRequest<M extends keyof RpcMethods> = (method: M, params?: RpcMethods[M]['params']) => Promise<RpcMethods[M]['result']>
38+
2439
export class <%= className %> {
2540
public transport: RpcTransport;
2641
@@ -33,15 +48,21 @@ export class <%= className %> {
3348
* <%= method.summary %>
3449
*/
3550
// tslint:disable-next-line:max-line-length
36-
public async request(method: "<%= method.name %>", ...params: Parameters<<%= _.upperFirst(method.name) %>>): ReturnType<<%= _.upperFirst(method.name) %>>
51+
// public async request(method: "<%= method.name %>", ...params: Parameters<<%= _.upperFirst(method.name) %>>): ReturnType<<%= _.upperFirst(method.name) %>>
3752
<% }); %>
38-
public async request(method: string, params?: RequestPayload['params']): Promise<unknown> {
39-
const response = await this.transport.submit({ method, params });
53+
54+
public async request<M extends keyof RpcMethods>(method: M, params?: RpcMethods[M]['params'][0]): Promise<RpcMethods[M]['result']> {
55+
const submitParams = params ? { method, params } : { method }
56+
57+
const response = await this.transport.submit({
58+
method,
59+
params: submitParams,
60+
})
4061
4162
if ('error' in response) {
4263
throw new Error('RPC error: ' + response.error.code + ' - ' + response.error.message);
4364
} else {
44-
return response.result;
65+
return response.result as RpcMethods[M]['result'];
4566
}
4667
}
4768
}

core/splice-provider/src/SpliceProvider.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
// Copyright (c) 2025-2026 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import { RequestPayload } from '@canton-network/core-types'
4+
import SpliceWalletJSONRPCDAppAPI, {
5+
RpcMethods,
6+
} from '@canton-network/core-wallet-dapp-rpc-client'
57

68
export type EventListener<T> = (...args: T[]) => void
79

810
export interface SpliceProvider {
9-
request<T>(args: RequestPayload): Promise<T>
11+
request<M extends keyof RpcMethods>(
12+
method: M,
13+
params?: RpcMethods[M]['params'][0]
14+
): Promise<RpcMethods[M]['result']>
1015
on<T>(event: string, listener: EventListener<T>): SpliceProvider
1116
emit<T>(event: string, ...args: T[]): boolean
1217
removeListener<T>(
@@ -17,12 +22,19 @@ export interface SpliceProvider {
1722

1823
export abstract class SpliceProviderBase implements SpliceProvider {
1924
listeners: { [event: string]: EventListener<unknown>[] }
25+
_client: SpliceWalletJSONRPCDAppAPI
2026

21-
constructor() {
27+
constructor(client: SpliceWalletJSONRPCDAppAPI) {
2228
this.listeners = {} // Event listeners
29+
this._client = client
2330
}
2431

25-
abstract request<T>(args: RequestPayload): Promise<T>
32+
public async request(
33+
method: keyof RpcMethods,
34+
params?: RpcMethods[typeof method]['params'][0]
35+
): Promise<RpcMethods[typeof method]['result']> {
36+
return this._client.request(method, params)
37+
}
2638

2739
// Event handling
2840
public on<T>(event: string, listener: EventListener<T>): SpliceProvider {

core/splice-provider/src/SpliceProviderHttp.ts

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
// Copyright (c) 2025-2026 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import {
5-
isSpliceMessageEvent,
6-
RequestPayload,
7-
WalletEvent,
8-
} from '@canton-network/core-types'
4+
import { isSpliceMessageEvent, WalletEvent } from '@canton-network/core-types'
95
import { HttpTransport } from '@canton-network/core-rpc-transport'
106
import SpliceWalletJSONRPCDAppAPI from '@canton-network/core-wallet-dapp-rpc-client'
117
import { SpliceProviderBase } from './SpliceProvider'
@@ -23,7 +19,6 @@ let connection: GatewaySocket = null
2319

2420
export class SpliceProviderHttp extends SpliceProviderBase {
2521
private sessionToken?: string
26-
private client: SpliceWalletJSONRPCDAppAPI
2722

2823
private createClient(sessionToken?: string): SpliceWalletJSONRPCDAppAPI {
2924
const transport = new HttpTransport(this.url, sessionToken)
@@ -61,18 +56,17 @@ export class SpliceProviderHttp extends SpliceProviderBase {
6156
}
6257

6358
constructor(
59+
client: SpliceWalletJSONRPCDAppAPI,
6460
private url: URL,
6561
sessionToken?: string
6662
) {
67-
super()
63+
super(client)
6864

6965
if (sessionToken) {
7066
this.sessionToken = sessionToken
7167
this.openSocket(url, sessionToken)
7268
}
7369

74-
this.client = this.createClient(sessionToken)
75-
7670
// Listen for the auth success event sent from the WK UI popup to the SDK running in the parent window.
7771
window.addEventListener('message', async (event) => {
7872
if (!isSpliceMessageEvent(event)) return
@@ -81,13 +75,13 @@ export class SpliceProviderHttp extends SpliceProviderBase {
8175
event.data.type === WalletEvent.SPLICE_WALLET_IDP_AUTH_SUCCESS
8276
) {
8377
this.sessionToken = event.data.token
84-
this.client = this.createClient(this.sessionToken)
78+
this._client = this.createClient(this.sessionToken)
8579
this.openSocket(this.url, event.data.token)
8680

8781
// We requery the status explicitly here, as it's not guaranteed that the socket will be open & authenticated
8882
// before the `onConnected` event is fired from the `addSession` RPC call. The dappApi.StatusResult and
8983
// dappApi.OnConnectedEvent are mapped manually to avoid dependency.
90-
this.request({ method: 'status' })
84+
this.request('status')
9185
.then((status) => {
9286
this.emit('onConnected', status)
9387
})
@@ -100,13 +94,4 @@ export class SpliceProviderHttp extends SpliceProviderBase {
10094
}
10195
})
10296
}
103-
104-
public async request<T>({ method, params }: RequestPayload): Promise<T> {
105-
return (await (
106-
this.client.request as (
107-
method: string,
108-
params?: RequestPayload['params']
109-
) => Promise<unknown>
110-
)(method, params)) as T
111-
}
11297
}
Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
11
// Copyright (c) 2025-2026 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import { RequestPayload } from '@canton-network/core-types'
5-
import { WindowTransport } from '@canton-network/core-rpc-transport'
4+
// import { RequestPayload } from '@canton-network/core-types'
5+
// import { WindowTransport } from '@canton-network/core-rpc-transport'
66
import SpliceWalletJSONRPCDAppAPI from '@canton-network/core-wallet-dapp-rpc-client'
77
import { SpliceProviderBase } from './SpliceProvider.js'
88

99
export class SpliceProviderWindow extends SpliceProviderBase {
10-
private client: SpliceWalletJSONRPCDAppAPI
10+
// private client: SpliceWalletJSONRPCDAppAPI
1111

12-
constructor() {
13-
super()
14-
const transport = new WindowTransport(window)
15-
this.client = new SpliceWalletJSONRPCDAppAPI(transport)
16-
}
17-
18-
public async request<T>({ method, params }: RequestPayload): Promise<T> {
19-
return (await (
20-
this.client.request as (
21-
method: string,
22-
params?: RequestPayload['params']
23-
) => Promise<unknown>
24-
)(method, params)) as T
12+
constructor(client: SpliceWalletJSONRPCDAppAPI) {
13+
super(client)
2514
}
2615
}

core/types/src/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ export type PartyId = z.infer<typeof PartyId>
1919
*/
2020
export const RequestPayload = z.object({
2121
method: z.string(),
22-
params: z.optional(
23-
z.union([z.array(z.unknown()), z.record(z.string(), z.unknown())])
24-
),
22+
params: z.optional(z.union([z.array(z.unknown()), z.looseObject({})])),
2523
})
2624
export type RequestPayload = z.infer<typeof RequestPayload>
2725

0 commit comments

Comments
 (0)