Skip to content

Commit 3b19396

Browse files
authored
refactor(client)!: rename structs and params, extend docs #130 (#231)
Breaking changes are these renamings: - `ApiTelemetry` to `TelemetryAPI` (for consistency with `MainAPI` and `WebSocketAPI`) - in `CreateClientParams`: `accountDomain` and `accountKeyPair` to `authority` and `authorityPrivateKey` (for consistency with data model)
1 parent 06a8c3e commit 3b19396

File tree

13 files changed

+245
-93
lines changed

13 files changed

+245
-93
lines changed

CONTRIBUTING.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,16 @@ You can add this to `.git/hooks/pre-commit`:
5757
#!/bin/sh
5858
deno task ok
5959
```
60+
61+
## Preview Docs
62+
63+
You can `cd` into a package directory (e.g. `packages/core`) and run these commands in parallel (requires `watchexec`
64+
and `jq` tools):
65+
66+
```shell
67+
watchexec -e ts deno doc --html $(cat deno.json* | deno run npm:json5 | jq -r '.exports | .[]')
68+
```
69+
70+
```shell
71+
deno run -A npm:vite serve docs
72+
```

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ const kp = types.KeyPair.random()
3131
const client = new Client({
3232
toriiBaseURL: new URL('http://localhost:8080'),
3333
chain: '000-000',
34-
accountDomain: new types.Name('wonderland'),
35-
accountKeyPair: kp,
34+
authority: new types.AccountId(kp.publicKey(), new types.DomainId('wonderland')),
35+
authorityPrivateKey: kp.privateKey(),
3636
})
3737

3838
async function test() {

deno.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/client/api-ws.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import type { SocketEmitMapBase } from './util.ts'
66
import { setupWebSocket } from './util.ts'
77
import { type IsomorphicWebSocketAdapter, nativeWS } from './web-socket/mod.ts'
88

9+
/**
10+
* Lower-level client
11+
*/
912
export class WebSocketAPI {
1013
public readonly toriiBaseURL: URL
1114
public readonly adapter: IsomorphicWebSocketAdapter

packages/client/api.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
import { urlJoinPath } from './util.ts'
1717

1818
/**
19-
* Peer information returned from {@link ApiTelemetry.peers}
19+
* Peer information returned from {@link TelemetryAPI.peers}
2020
*/
2121
export interface PeerJson {
2222
/**
@@ -78,17 +78,26 @@ export class HttpTransport {
7878
}
7979
}
8080

81+
/**
82+
* Lower-level client to interact with Iroha HTTP APIs.
83+
*
84+
* It is separated from {@linkcode WebSocketAPI}.
85+
*
86+
* It is lower-level in a sense that, for example, {@linkcode MainAPI#transaction} accepts an already signed transaction
87+
* and simply "fire and forget"s it, while {@linkcode Client#transaction} helps to construct a transaction, submit it,
88+
* and verify that it is accepted.
89+
*/
8190
export class MainAPI {
8291
/**
8392
* Works only if Iroha is compiled with `telemetry` feature flag.
8493
*/
85-
public readonly telemetry: ApiTelemetry
94+
public readonly telemetry: TelemetryAPI
8695

8796
private readonly http: HttpTransport
8897

8998
public constructor(http: HttpTransport) {
9099
this.http = http
91-
this.telemetry = new ApiTelemetry(http)
100+
this.telemetry = new TelemetryAPI(http)
92101
}
93102

94103
public async health(): Promise<HealthResult> {
@@ -178,7 +187,7 @@ export class QueryValidationError extends Error {
178187
}
179188

180189
// TODO: handle errors with a hint that Iroha might be not compiled with the needed features
181-
export class ApiTelemetry {
190+
export class TelemetryAPI {
182191
private readonly http: HttpTransport
183192

184193
public constructor(http: HttpTransport) {

packages/client/client.ts

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { KeyPair, PrivateKey } from '@iroha/core/crypto'
1+
import type { PrivateKey } from '@iroha/core/crypto'
22
import * as types from '@iroha/core/data-model'
33
import type { Except } from 'type-fest'
44
import defer from 'p-defer'
@@ -31,9 +31,18 @@ export interface CreateClientParams {
3131
* The base URL of **Torii**, Iroha API Gateway.
3232
*/
3333
toriiBaseURL: URL
34+
/**
35+
* Chain ID.
36+
*/
3437
chain: string
35-
accountDomain: types.DomainId
36-
accountKeyPair: KeyPair
38+
/**
39+
* Authority on which behalf to sign transactions and queries.
40+
*/
41+
authority: types.AccountId
42+
/**
43+
* The private key of {@linkcode CreateClientParams.authority}.
44+
*/
45+
authorityPrivateKey: types.PrivateKey
3746
}
3847

3948
export interface SubmitParams {
@@ -62,6 +71,20 @@ export class TransactionExpiredError extends Error {
6271
}
6372
}
6473

74+
/**
75+
* All-in-one Iroha client.
76+
*
77+
* Through it, it is possible to perform all different kinds of interactions with Iroha, e.g.
78+
* signing and submitting transactions and queries or listening to events through WebSockets.
79+
*
80+
* It is possible to use each layer of functionality separately, through lower-level layers:
81+
*
82+
* - {@linkcode MainAPI}
83+
* - {@linkcode WebSocketAPI}
84+
*
85+
* It could be useful if e.g. you don't need to submit transactions (which requires an account with a key pair),
86+
* but only want to check Iroha status.
87+
*/
6588
export class Client {
6689
public readonly params: CreateClientParams
6790

@@ -83,21 +106,18 @@ export class Client {
83106
const http = new HttpTransport(params.toriiBaseURL, params.fetch)
84107
this.api = new MainAPI(http)
85108

86-
const executor = new QueryExecutor(this.api, this.authority(), this.authorityPrivateKey())
109+
const executor = new QueryExecutor(this.api, this.authority, this.authorityPrivateKey)
87110
this.find = new FindAPI(executor)
88111

89112
this.socket = new WebSocketAPI(params.toriiBaseURL, params.ws)
90113
}
91114

92-
public authority(): types.AccountId {
93-
return new types.AccountId(
94-
this.params.accountKeyPair.publicKey(),
95-
this.params.accountDomain,
96-
)
115+
public get authority(): types.AccountId {
116+
return this.params.authority
97117
}
98118

99-
public authorityPrivateKey(): PrivateKey {
100-
return this.params.accountKeyPair.privateKey()
119+
public get authorityPrivateKey(): PrivateKey {
120+
return this.params.authorityPrivateKey
101121
}
102122

103123
/**
@@ -114,10 +134,10 @@ export class Client {
114134
const tx = signTransaction(
115135
buildTransactionPayload(executable, {
116136
chain: this.params.chain,
117-
authority: this.authority(),
137+
authority: this.authority,
118138
...params,
119139
}),
120-
this.params.accountKeyPair.privateKey(),
140+
this.authorityPrivateKey,
121141
)
122142

123143
return new TransactionHandle(tx, this)

0 commit comments

Comments
 (0)