@c7-digital/ledger 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/BUILD.md +125 -0
- package/LICENSE +17 -0
- package/README.md +97 -0
- package/lib/scripts/build.d.ts +9 -0
- package/lib/scripts/build.js +366 -0
- package/lib/scripts/enhance-types.d.ts +3 -0
- package/lib/scripts/enhance-types.js +174 -0
- package/lib/scripts/generate-asyncapi-types.d.ts +9 -0
- package/lib/scripts/generate-asyncapi-types.js +129 -0
- package/lib/scripts/generate-schema-modules.d.ts +3 -0
- package/lib/scripts/generate-schema-modules.js +33 -0
- package/lib/src/TemplateEmitterMap.d.ts +40 -0
- package/lib/src/TemplateEmitterMap.js +57 -0
- package/lib/src/client.d.ts +55 -0
- package/lib/src/client.js +62 -0
- package/lib/src/generated/api.d.ts +7147 -0
- package/lib/src/generated/api.js +1 -0
- package/lib/src/generated/async-api.d.ts +1665 -0
- package/lib/src/generated/async-api.js +1 -0
- package/lib/src/generated/asyncapi-schema.d.ts +1 -0
- package/lib/src/generated/asyncapi-schema.js +2233 -0
- package/lib/src/generated/openapi-schema.d.ts +1 -0
- package/lib/src/generated/openapi-schema.js +7321 -0
- package/lib/src/generated/sdk-version.d.ts +1 -0
- package/lib/src/generated/sdk-version.js +3 -0
- package/lib/src/index.d.ts +8 -0
- package/lib/src/index.js +8 -0
- package/lib/src/ledger.d.ts +188 -0
- package/lib/src/ledger.js +849 -0
- package/lib/src/ledger.test.d.ts +1 -0
- package/lib/src/ledger.test.js +23 -0
- package/lib/src/logger.d.ts +41 -0
- package/lib/src/logger.js +56 -0
- package/lib/src/multistream.d.ts +47 -0
- package/lib/src/multistream.js +123 -0
- package/lib/src/translate.d.ts +5 -0
- package/lib/src/translate.js +30 -0
- package/lib/src/types.d.ts +201 -0
- package/lib/src/types.js +1 -0
- package/lib/src/util.d.ts +3 -0
- package/lib/src/util.js +7 -0
- package/lib/src/validation.d.ts +27 -0
- package/lib/src/validation.js +182 -0
- package/lib/src/valueTypes.d.ts +34 -0
- package/lib/src/valueTypes.js +76 -0
- package/lib/src/websocket.d.ts +69 -0
- package/lib/src/websocket.js +125 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/lib-lite/scripts/build.d.ts +9 -0
- package/lib-lite/scripts/build.js +366 -0
- package/lib-lite/scripts/enhance-types.d.ts +3 -0
- package/lib-lite/scripts/enhance-types.js +174 -0
- package/lib-lite/scripts/generate-asyncapi-types.d.ts +9 -0
- package/lib-lite/scripts/generate-asyncapi-types.js +129 -0
- package/lib-lite/scripts/generate-schema-modules.d.ts +3 -0
- package/lib-lite/scripts/generate-schema-modules.js +33 -0
- package/lib-lite/src/TemplateEmitterMap.d.ts +40 -0
- package/lib-lite/src/TemplateEmitterMap.js +57 -0
- package/lib-lite/src/client.d.ts +55 -0
- package/lib-lite/src/client.js +62 -0
- package/lib-lite/src/generated/api.d.ts +7147 -0
- package/lib-lite/src/generated/api.js +1 -0
- package/lib-lite/src/generated/async-api.d.ts +1665 -0
- package/lib-lite/src/generated/async-api.js +1 -0
- package/lib-lite/src/generated/asyncapi-schema.d.ts +1 -0
- package/lib-lite/src/generated/asyncapi-schema.js +2 -0
- package/lib-lite/src/generated/openapi-schema.d.ts +1 -0
- package/lib-lite/src/generated/openapi-schema.js +2 -0
- package/lib-lite/src/generated/sdk-version.d.ts +1 -0
- package/lib-lite/src/generated/sdk-version.js +3 -0
- package/lib-lite/src/index.d.ts +8 -0
- package/lib-lite/src/index.js +8 -0
- package/lib-lite/src/ledger.d.ts +188 -0
- package/lib-lite/src/ledger.js +849 -0
- package/lib-lite/src/ledger.test.d.ts +1 -0
- package/lib-lite/src/ledger.test.js +23 -0
- package/lib-lite/src/logger.d.ts +41 -0
- package/lib-lite/src/logger.js +56 -0
- package/lib-lite/src/multistream.d.ts +47 -0
- package/lib-lite/src/multistream.js +123 -0
- package/lib-lite/src/translate.d.ts +5 -0
- package/lib-lite/src/translate.js +30 -0
- package/lib-lite/src/types.d.ts +201 -0
- package/lib-lite/src/types.js +1 -0
- package/lib-lite/src/util.d.ts +3 -0
- package/lib-lite/src/util.js +7 -0
- package/lib-lite/src/validation.d.ts +14 -0
- package/lib-lite/src/validation.js +31 -0
- package/lib-lite/src/valueTypes.d.ts +34 -0
- package/lib-lite/src/valueTypes.js +76 -0
- package/lib-lite/src/websocket.d.ts +69 -0
- package/lib-lite/src/websocket.js +125 -0
- package/lib-lite/tsconfig.temp.tsbuildinfo +1 -0
- package/package.json +72 -0
- package/scripts/build.ts +456 -0
- package/scripts/enhance-types.ts +223 -0
- package/scripts/generate-asyncapi-types.ts +158 -0
- package/scripts/generate-schema-modules.ts +52 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Basic tests for the new ledger implementation
|
|
2
|
+
import { Ledger } from "./ledger.js";
|
|
3
|
+
// Valid JWT token for testing with sub field
|
|
4
|
+
// Header: {"alg":"HS256","typ":"JWT"}
|
|
5
|
+
// Payload: {"sub":"test-user","iat":1516239022}
|
|
6
|
+
const TEST_TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0ZXN0LXVzZXIiLCJpYXQiOjE1MTYyMzkwMjJ9.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";
|
|
7
|
+
describe("Ledger", () => {
|
|
8
|
+
it("should create a ledger instance with default options", () => {
|
|
9
|
+
const ledger = new Ledger({
|
|
10
|
+
token: TEST_TOKEN,
|
|
11
|
+
httpBaseUrl: "http://localhost:7575",
|
|
12
|
+
});
|
|
13
|
+
expect(ledger).toBeInstanceOf(Ledger);
|
|
14
|
+
});
|
|
15
|
+
it("should create a ledger instance with custom options", () => {
|
|
16
|
+
const ledger = new Ledger({
|
|
17
|
+
token: TEST_TOKEN,
|
|
18
|
+
httpBaseUrl: "http://example.com:8080",
|
|
19
|
+
wsBaseUrl: "ws://example.com:8080",
|
|
20
|
+
});
|
|
21
|
+
expect(ledger).toBeInstanceOf(Ledger);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Logger interface for injecting custom logging behavior into the ledger package
|
|
3
|
+
*/
|
|
4
|
+
export interface Logger {
|
|
5
|
+
debug(message: string, ...args: any[]): void;
|
|
6
|
+
info(message: string, ...args: any[]): void;
|
|
7
|
+
warn(message: string, ...args: any[]): void;
|
|
8
|
+
error(message: string, ...args: any[]): void;
|
|
9
|
+
log(message: string, ...args: any[]): void;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Default console-based logger implementation
|
|
13
|
+
*/
|
|
14
|
+
export declare class ConsoleLogger implements Logger {
|
|
15
|
+
private usePrefix;
|
|
16
|
+
constructor(usePrefix?: boolean);
|
|
17
|
+
private addPrefix;
|
|
18
|
+
debug(message: string, ...args: any[]): void;
|
|
19
|
+
info(message: string, ...args: any[]): void;
|
|
20
|
+
warn(message: string, ...args: any[]): void;
|
|
21
|
+
error(message: string, ...args: any[]): void;
|
|
22
|
+
log(message: string, ...args: any[]): void;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* No-op logger that discards all log messages
|
|
26
|
+
*/
|
|
27
|
+
export declare class NoOpLogger implements Logger {
|
|
28
|
+
debug(): void;
|
|
29
|
+
info(): void;
|
|
30
|
+
warn(): void;
|
|
31
|
+
error(): void;
|
|
32
|
+
log(): void;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Set the global logger instance used by the ledger package
|
|
36
|
+
*/
|
|
37
|
+
export declare function setLogger(newLogger: Logger): void;
|
|
38
|
+
/**
|
|
39
|
+
* Global logger proxy that delegates to the current logger instance
|
|
40
|
+
*/
|
|
41
|
+
export declare const logger: Logger;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default console-based logger implementation
|
|
3
|
+
*/
|
|
4
|
+
export class ConsoleLogger {
|
|
5
|
+
constructor(usePrefix = true) {
|
|
6
|
+
this.usePrefix = usePrefix;
|
|
7
|
+
}
|
|
8
|
+
addPrefix(message) {
|
|
9
|
+
return this.usePrefix ? `ledger: ${message}` : message;
|
|
10
|
+
}
|
|
11
|
+
debug(message, ...args) {
|
|
12
|
+
console.debug(this.addPrefix(message), ...args);
|
|
13
|
+
}
|
|
14
|
+
info(message, ...args) {
|
|
15
|
+
console.info(this.addPrefix(message), ...args);
|
|
16
|
+
}
|
|
17
|
+
warn(message, ...args) {
|
|
18
|
+
console.warn(this.addPrefix(message), ...args);
|
|
19
|
+
}
|
|
20
|
+
error(message, ...args) {
|
|
21
|
+
console.error(this.addPrefix(message), ...args);
|
|
22
|
+
}
|
|
23
|
+
log(message, ...args) {
|
|
24
|
+
console.log(this.addPrefix(message), ...args);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* No-op logger that discards all log messages
|
|
29
|
+
*/
|
|
30
|
+
export class NoOpLogger {
|
|
31
|
+
debug() { }
|
|
32
|
+
info() { }
|
|
33
|
+
warn() { }
|
|
34
|
+
error() { }
|
|
35
|
+
log() { }
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Internal logger instance holder
|
|
39
|
+
*/
|
|
40
|
+
let globalLoggerInstance = new ConsoleLogger(true);
|
|
41
|
+
/**
|
|
42
|
+
* Set the global logger instance used by the ledger package
|
|
43
|
+
*/
|
|
44
|
+
export function setLogger(newLogger) {
|
|
45
|
+
globalLoggerInstance = newLogger;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Global logger proxy that delegates to the current logger instance
|
|
49
|
+
*/
|
|
50
|
+
export const logger = {
|
|
51
|
+
debug: (message, ...args) => globalLoggerInstance.debug(message, ...args),
|
|
52
|
+
info: (message, ...args) => globalLoggerInstance.info(message, ...args),
|
|
53
|
+
warn: (message, ...args) => globalLoggerInstance.warn(message, ...args),
|
|
54
|
+
error: (message, ...args) => globalLoggerInstance.error(message, ...args),
|
|
55
|
+
log: (message, ...args) => globalLoggerInstance.log(message, ...args),
|
|
56
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { ArchiveEvent, CantonError, CreateEvent, MultiStream, Stream, StreamState, TemplateMapping } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Adapts a Stream instance to the MultiStream interface
|
|
4
|
+
* Provides template-specific event handlers with proper typing
|
|
5
|
+
*/
|
|
6
|
+
export declare class MultiStreamAdapter<TM extends TemplateMapping> implements MultiStream<TM> {
|
|
7
|
+
private stream;
|
|
8
|
+
private templateEmitters;
|
|
9
|
+
private errorEmitter;
|
|
10
|
+
private stateEmitter;
|
|
11
|
+
/**
|
|
12
|
+
* Create a new MultiStream adapter
|
|
13
|
+
* @param stream The underlying Stream instance
|
|
14
|
+
*/
|
|
15
|
+
constructor(stream: Stream<object, unknown>);
|
|
16
|
+
/**
|
|
17
|
+
* Get or create an EventEmitter for a specific template
|
|
18
|
+
*/
|
|
19
|
+
private getEmitterForTemplate;
|
|
20
|
+
/**
|
|
21
|
+
* Handle create events from the underlying stream
|
|
22
|
+
* and route them to the appropriate template emitter
|
|
23
|
+
*/
|
|
24
|
+
private handleCreateEvent;
|
|
25
|
+
/**
|
|
26
|
+
* Handle archive events from the underlying stream
|
|
27
|
+
* and route them to the appropriate template emitter
|
|
28
|
+
*/
|
|
29
|
+
private handleArchiveEvent;
|
|
30
|
+
/**
|
|
31
|
+
* Handle error events from the underlying stream
|
|
32
|
+
* and emit them to the dedicated error emitter
|
|
33
|
+
*/
|
|
34
|
+
private handleError;
|
|
35
|
+
private handleStateEvent;
|
|
36
|
+
onCreate<TID extends keyof TM>(templateId: TID, listener: (event: CreateEvent<TM[TID]["contractType"] & object, TM[TID]["keyType"]>) => void): void;
|
|
37
|
+
onArchive<TID extends keyof TM>(templateId: TID, listener: (event: ArchiveEvent<TM[TID]["contractType"]>) => void): void;
|
|
38
|
+
onError(listener: (error: CantonError) => void): void;
|
|
39
|
+
onState(listener: (state: StreamState) => void): void;
|
|
40
|
+
offCreate<TID extends keyof TM>(templateId: TID, listener: (event: CreateEvent<TM[TID]["contractType"], TM[TID]["keyType"]>) => void): void;
|
|
41
|
+
offArchive<TID extends keyof TM>(templateId: TID, listener: (event: ArchiveEvent<TM[TID]["contractType"]>) => void): void;
|
|
42
|
+
offError(listener: (error: CantonError) => void): void;
|
|
43
|
+
offState(listener: (state: StreamState) => void): void;
|
|
44
|
+
start(): void;
|
|
45
|
+
state(): StreamState;
|
|
46
|
+
close(): void;
|
|
47
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { EventEmitter } from "eventemitter3";
|
|
2
|
+
import { logger } from "./logger.js";
|
|
3
|
+
import { TemplateEmitterMap } from "./TemplateEmitterMap.js";
|
|
4
|
+
/**
|
|
5
|
+
* Adapts a Stream instance to the MultiStream interface
|
|
6
|
+
* Provides template-specific event handlers with proper typing
|
|
7
|
+
*/
|
|
8
|
+
export class MultiStreamAdapter {
|
|
9
|
+
/**
|
|
10
|
+
* Create a new MultiStream adapter
|
|
11
|
+
* @param stream The underlying Stream instance
|
|
12
|
+
*/
|
|
13
|
+
constructor(stream) {
|
|
14
|
+
this.templateEmitters = new TemplateEmitterMap();
|
|
15
|
+
this.errorEmitter = new EventEmitter(); // Dedicated error emitter
|
|
16
|
+
this.stateEmitter = new EventEmitter(); // Dedicated state emitter
|
|
17
|
+
this.stream = stream;
|
|
18
|
+
// Set up global listeners that route events to template-specific emitters
|
|
19
|
+
this.stream.on("create", this.handleCreateEvent.bind(this));
|
|
20
|
+
this.stream.on("archive", this.handleArchiveEvent.bind(this));
|
|
21
|
+
this.stream.on("error", this.handleError.bind(this));
|
|
22
|
+
this.stream.on("state", this.handleStateEvent.bind(this));
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Get or create an EventEmitter for a specific template
|
|
26
|
+
*/
|
|
27
|
+
getEmitterForTemplate(templateId) {
|
|
28
|
+
if (!this.templateEmitters.has(templateId)) {
|
|
29
|
+
this.templateEmitters.set(templateId, new EventEmitter());
|
|
30
|
+
}
|
|
31
|
+
return this.templateEmitters.get(templateId);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Handle create events from the underlying stream
|
|
35
|
+
* and route them to the appropriate template emitter
|
|
36
|
+
*/
|
|
37
|
+
handleCreateEvent(event) {
|
|
38
|
+
const templateId = event.templateId;
|
|
39
|
+
if (this.templateEmitters.has(templateId)) {
|
|
40
|
+
this.templateEmitters.get(templateId).emit("create", event);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
logger.warn(`Received create event for unknown template ${templateId}`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Handle archive events from the underlying stream
|
|
48
|
+
* and route them to the appropriate template emitter
|
|
49
|
+
*/
|
|
50
|
+
handleArchiveEvent(event) {
|
|
51
|
+
const templateId = event.templateId;
|
|
52
|
+
if (this.templateEmitters.has(templateId)) {
|
|
53
|
+
this.templateEmitters.get(templateId).emit("archive", event);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
logger.warn(`Received archive event for unknown template ${templateId}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Handle error events from the underlying stream
|
|
61
|
+
* and emit them to the dedicated error emitter
|
|
62
|
+
*/
|
|
63
|
+
handleError(error) {
|
|
64
|
+
// Emit to the dedicated error emitter instead of all template emitters
|
|
65
|
+
this.errorEmitter.emit("error", error);
|
|
66
|
+
}
|
|
67
|
+
handleStateEvent(state) {
|
|
68
|
+
this.stateEmitter.emit("state", state);
|
|
69
|
+
}
|
|
70
|
+
// MultiStream interface implementation
|
|
71
|
+
onCreate(templateId, listener) {
|
|
72
|
+
const emitter = this.getEmitterForTemplate(templateId);
|
|
73
|
+
emitter.on("create", (event) => {
|
|
74
|
+
// Type assertion: we know the event has the correct type based on templateId
|
|
75
|
+
listener(event);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
onArchive(templateId, listener) {
|
|
79
|
+
const emitter = this.getEmitterForTemplate(templateId);
|
|
80
|
+
emitter.on("archive", (event) => {
|
|
81
|
+
// Type assertion: we know the event has the correct type based on templateId
|
|
82
|
+
listener(event);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
onError(listener) {
|
|
86
|
+
this.errorEmitter.on("error", listener);
|
|
87
|
+
}
|
|
88
|
+
onState(listener) {
|
|
89
|
+
this.stream.on("state", listener);
|
|
90
|
+
}
|
|
91
|
+
offCreate(templateId, listener) {
|
|
92
|
+
if (this.templateEmitters.has(templateId)) {
|
|
93
|
+
this.templateEmitters.get(templateId).off("create", listener);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
offArchive(templateId, listener) {
|
|
97
|
+
if (this.templateEmitters.has(templateId)) {
|
|
98
|
+
this.templateEmitters.get(templateId).off("archive", listener);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
offError(listener) {
|
|
102
|
+
this.errorEmitter.off("error", listener);
|
|
103
|
+
}
|
|
104
|
+
offState(listener) {
|
|
105
|
+
this.stream.off("state", listener);
|
|
106
|
+
}
|
|
107
|
+
start() {
|
|
108
|
+
this.stream.start();
|
|
109
|
+
}
|
|
110
|
+
state() {
|
|
111
|
+
return this.stream.state();
|
|
112
|
+
}
|
|
113
|
+
close() {
|
|
114
|
+
this.stream.close();
|
|
115
|
+
for (const emitter of this.templateEmitters.values()) {
|
|
116
|
+
emitter.removeAllListeners();
|
|
117
|
+
}
|
|
118
|
+
this.templateEmitters.clear();
|
|
119
|
+
// Clean up the error emitter too
|
|
120
|
+
this.errorEmitter.removeAllListeners();
|
|
121
|
+
this.stateEmitter.removeAllListeners();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export function userRights(right) {
|
|
2
|
+
const kind = right.kind;
|
|
3
|
+
if ("CanActAs" in kind) {
|
|
4
|
+
return {
|
|
5
|
+
type: "canActAs",
|
|
6
|
+
party: kind.CanActAs.value.party,
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
else if ("CanReadAs" in kind) {
|
|
10
|
+
return {
|
|
11
|
+
type: "canReadAs",
|
|
12
|
+
party: kind.CanReadAs.value.party,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
else if ("CanReadAsAnyParty" in kind) {
|
|
16
|
+
return { type: "canReadAsAnyParty" };
|
|
17
|
+
}
|
|
18
|
+
else if ("Empty" in kind) {
|
|
19
|
+
return { type: "empty" };
|
|
20
|
+
}
|
|
21
|
+
else if ("IdentityProviderAdmin" in kind) {
|
|
22
|
+
return { type: "identityProviderAdmin" };
|
|
23
|
+
}
|
|
24
|
+
else if ("ParticipantAdmin" in kind) {
|
|
25
|
+
return { type: "participantAdmin" };
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
throw new Error(`Unknown right kind: ${JSON.stringify(right)}`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { ContractId, Party, Choice, Template, InterfaceCompanion } from "@daml/types";
|
|
2
|
+
import { PartyIdString, UserIdString, PackageIdString } from "./valueTypes.js";
|
|
3
|
+
import { JsCantonError } from "./websocket.js";
|
|
4
|
+
export type CreateEvent<T extends object, K = unknown> = {
|
|
5
|
+
type: "create";
|
|
6
|
+
templateId: PackageIdString;
|
|
7
|
+
contractId: ContractId<T>;
|
|
8
|
+
payload: T;
|
|
9
|
+
signatories: Party[];
|
|
10
|
+
observers: Party[];
|
|
11
|
+
key?: K;
|
|
12
|
+
createdEventBlob: string;
|
|
13
|
+
/**
|
|
14
|
+
* Package version string (e.g., "0.0.6")
|
|
15
|
+
* Only present if using VersionedRegistry
|
|
16
|
+
*/
|
|
17
|
+
packageVersion?: string;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* An ArchiveEvent of a given template
|
|
21
|
+
*/
|
|
22
|
+
export type ArchiveEvent<T extends object> = {
|
|
23
|
+
type: "archive";
|
|
24
|
+
templateId: PackageIdString;
|
|
25
|
+
contractId: ContractId<T>;
|
|
26
|
+
witnessParties: Party[];
|
|
27
|
+
offset: number;
|
|
28
|
+
};
|
|
29
|
+
export type Event<T extends object, K = unknown> = CreateEvent<T, K> | ArchiveEvent<T>;
|
|
30
|
+
/**
|
|
31
|
+
* The Daml interface of the subscribed 'interfaceId'
|
|
32
|
+
*
|
|
33
|
+
* We use 'templateId' here because the name specified in the codegen field,
|
|
34
|
+
* though this is specified as 'interfaceId' in the OpenAPI spec.
|
|
35
|
+
*/
|
|
36
|
+
export type Interface<I extends object> = {
|
|
37
|
+
type: "interface";
|
|
38
|
+
templateId: PackageIdString;
|
|
39
|
+
contractId: ContractId<I>;
|
|
40
|
+
payload?: any;
|
|
41
|
+
signatories: Party[];
|
|
42
|
+
observers: Party[];
|
|
43
|
+
key?: any;
|
|
44
|
+
createdEventBlob: string;
|
|
45
|
+
interfaceView: I;
|
|
46
|
+
/**
|
|
47
|
+
* Package version string (e.g., "0.0.6")
|
|
48
|
+
* Only present if using VersionedRegistry
|
|
49
|
+
*/
|
|
50
|
+
packageVersion?: string;
|
|
51
|
+
};
|
|
52
|
+
export type VersionedLookupResult = {
|
|
53
|
+
type: "template";
|
|
54
|
+
template: Template<object, unknown, string>;
|
|
55
|
+
version: string;
|
|
56
|
+
} | {
|
|
57
|
+
type: "interface";
|
|
58
|
+
interface_: InterfaceCompanion<object, unknown, string>;
|
|
59
|
+
version: string;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* By default the codegen models use @daml/types registerTemplate function to
|
|
63
|
+
* add the Template instance to a map. This is insufficient on two counts; it
|
|
64
|
+
* does not track the packageId of the containing dar, which is what the stream
|
|
65
|
+
* returns. Furthermore, it does not register the InterfaceView instance which
|
|
66
|
+
* one can also get from the stream.
|
|
67
|
+
*
|
|
68
|
+
* This type can be passed into our Ledger so that we can use it instead. We
|
|
69
|
+
* overload the name of the lookup parameter 'templateId' even though in the
|
|
70
|
+
* interface we really mean the interfaceId as in the spec OpenAPI spec.
|
|
71
|
+
*/
|
|
72
|
+
export type VersionedRegistry = (templateId: string) => VersionedLookupResult | undefined;
|
|
73
|
+
export type LedgerOffset = "start" | "end" | number;
|
|
74
|
+
export type CantonError = JsCantonError;
|
|
75
|
+
export interface StreamCloseEvent {
|
|
76
|
+
code: number;
|
|
77
|
+
reason: string;
|
|
78
|
+
}
|
|
79
|
+
export type StreamState = "start" | "init" | "live" | "stop";
|
|
80
|
+
/**
|
|
81
|
+
* The return interface of streamQuery and streamQueries.
|
|
82
|
+
*/
|
|
83
|
+
export interface Stream<T extends object, K> {
|
|
84
|
+
on(type: "create", listener: (event: CreateEvent<T, K>) => void): void;
|
|
85
|
+
on(type: "archive", listener: (event: ArchiveEvent<T>) => void): void;
|
|
86
|
+
on(type: "error", listener: (event: CantonError) => void): void;
|
|
87
|
+
on(type: "state", listener: (event: StreamState) => void): void;
|
|
88
|
+
off(type: "create", listener: (event: CreateEvent<T, K>) => void): void;
|
|
89
|
+
off(type: "archive", listener: (event: ArchiveEvent<T>) => void): void;
|
|
90
|
+
off(type: "error", listener: (event: CantonError) => void): void;
|
|
91
|
+
off(type: "state", listener: (event: StreamState) => void): void;
|
|
92
|
+
start(): void;
|
|
93
|
+
state(): StreamState;
|
|
94
|
+
close(): void;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Template mapping type for MultiStream:
|
|
98
|
+
* template IDs to their corresponding contract and key types
|
|
99
|
+
*/
|
|
100
|
+
export type TemplateMapping = Record<string, {
|
|
101
|
+
contractType: object;
|
|
102
|
+
keyType: unknown;
|
|
103
|
+
}>;
|
|
104
|
+
/**
|
|
105
|
+
* Provides template-specific event handlers.
|
|
106
|
+
*/
|
|
107
|
+
export interface MultiStream<TM extends TemplateMapping> {
|
|
108
|
+
/**
|
|
109
|
+
* Register a listener for create events for a specific template
|
|
110
|
+
* @param templateId The template ID to listen for
|
|
111
|
+
* @param listener The callback function that will receive properly typed events
|
|
112
|
+
*/
|
|
113
|
+
onCreate<TID extends keyof TM>(templateId: TID, listener: (event: CreateEvent<TM[TID]["contractType"], TM[TID]["keyType"]>) => void): void;
|
|
114
|
+
/**
|
|
115
|
+
* Register a listener for archive events for a specific template
|
|
116
|
+
* @param templateId The template ID to listen for
|
|
117
|
+
* @param listener The callback function that will receive properly typed events
|
|
118
|
+
*/
|
|
119
|
+
onArchive<TID extends keyof TM>(templateId: TID, listener: (event: ArchiveEvent<TM[TID]["contractType"]>) => void): void;
|
|
120
|
+
/**
|
|
121
|
+
* Register a listener for error events
|
|
122
|
+
* @param listener The callback function that will receive error events
|
|
123
|
+
*/
|
|
124
|
+
onError(listener: (error: CantonError) => void): void;
|
|
125
|
+
onState(listener: (state: StreamState) => void): void;
|
|
126
|
+
/**
|
|
127
|
+
* Remove a create event listener for a specific template
|
|
128
|
+
* @param templateId The template ID to stop listening for
|
|
129
|
+
* @param listener The callback function to remove
|
|
130
|
+
*/
|
|
131
|
+
offCreate<TID extends keyof TM>(templateId: TID, listener: (event: CreateEvent<TM[TID]["contractType"], TM[TID]["keyType"]>) => void): void;
|
|
132
|
+
/**
|
|
133
|
+
* Remove an archive event listener for a specific template
|
|
134
|
+
* @param templateId The template ID to stop listening for
|
|
135
|
+
* @param listener The callback function to remove
|
|
136
|
+
*/
|
|
137
|
+
offArchive<TID extends keyof TM>(templateId: TID, listener: (event: ArchiveEvent<TM[TID]["contractType"]>) => void): void;
|
|
138
|
+
/**
|
|
139
|
+
* Remove an error event listener
|
|
140
|
+
* @param listener The callback function to remove
|
|
141
|
+
*/
|
|
142
|
+
offError(listener: (error: CantonError) => void): void;
|
|
143
|
+
offState(listener: (state: StreamState) => void): void;
|
|
144
|
+
start(): void;
|
|
145
|
+
state(): StreamState;
|
|
146
|
+
close(): void;
|
|
147
|
+
}
|
|
148
|
+
export interface Query<T = unknown> {
|
|
149
|
+
[key: string]: unknown;
|
|
150
|
+
}
|
|
151
|
+
export type CreateCommand<T extends object, K = unknown> = {
|
|
152
|
+
type: 'create';
|
|
153
|
+
template: Template<T, K, string>;
|
|
154
|
+
payload: T;
|
|
155
|
+
};
|
|
156
|
+
export type CreateAndExerciseCommand<T extends object, C, R, K = unknown> = {
|
|
157
|
+
type: 'createAndExercise';
|
|
158
|
+
template: Template<T, K, string>;
|
|
159
|
+
payload: T;
|
|
160
|
+
choice: Choice<T, C, R, K>;
|
|
161
|
+
argument: C;
|
|
162
|
+
};
|
|
163
|
+
export type ExerciseCommand<T extends object, C, R, K = unknown> = {
|
|
164
|
+
type: 'exercise';
|
|
165
|
+
choice: Choice<T, C, R, K>;
|
|
166
|
+
contractId: ContractId<T>;
|
|
167
|
+
argument: C;
|
|
168
|
+
};
|
|
169
|
+
export type Command<T extends object, C, R = unknown, K = unknown> = CreateCommand<T, K> | CreateAndExerciseCommand<T, C, R, K> | ExerciseCommand<T, C, R, K>;
|
|
170
|
+
export interface AllocatePartyRequest {
|
|
171
|
+
partyIdHint?: PartyIdString;
|
|
172
|
+
displayName?: string;
|
|
173
|
+
}
|
|
174
|
+
export interface AllocatePartyResponse {
|
|
175
|
+
partyDetails: PartyDetails;
|
|
176
|
+
}
|
|
177
|
+
export interface PartyDetails {
|
|
178
|
+
party: Party;
|
|
179
|
+
displayName?: string;
|
|
180
|
+
isLocal: boolean;
|
|
181
|
+
}
|
|
182
|
+
export type UserRight = {
|
|
183
|
+
type: "canActAs";
|
|
184
|
+
party: Party;
|
|
185
|
+
} | {
|
|
186
|
+
type: "canReadAs";
|
|
187
|
+
party: Party;
|
|
188
|
+
} | {
|
|
189
|
+
type: "canReadAsAnyParty";
|
|
190
|
+
} | {
|
|
191
|
+
type: "empty";
|
|
192
|
+
} | {
|
|
193
|
+
type: "identityProviderAdmin";
|
|
194
|
+
} | {
|
|
195
|
+
type: "participantAdmin";
|
|
196
|
+
};
|
|
197
|
+
export interface User {
|
|
198
|
+
userId: UserIdString;
|
|
199
|
+
primaryParty?: Party;
|
|
200
|
+
rights: UserRight[];
|
|
201
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type ValidationMode = "throwOnError" | "logErrors";
|
|
2
|
+
export declare class SchemaValidator {
|
|
3
|
+
private instanceId;
|
|
4
|
+
private schemaReady;
|
|
5
|
+
constructor(schemaType?: "openapi" | "asyncapi", validation?: ValidationMode);
|
|
6
|
+
/**
|
|
7
|
+
* Validate an array of items against a schema (lite: pass-through)
|
|
8
|
+
*/
|
|
9
|
+
validateArraySchema<T>(data: unknown, _itemSchemaName: string): Promise<T[]>;
|
|
10
|
+
/**
|
|
11
|
+
* Validate data against a schema and return typed result (lite: pass-through)
|
|
12
|
+
*/
|
|
13
|
+
validateSchema<T>(data: unknown, _schemaName: string): Promise<T>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lite runtime validation utilities - validation is disabled in lite builds
|
|
3
|
+
*
|
|
4
|
+
* This module provides stub implementations that pass data through without validation.
|
|
5
|
+
* Use the full @c7-digital/ledger package if you need runtime schema validation.
|
|
6
|
+
*/
|
|
7
|
+
import { logger } from "./logger.js";
|
|
8
|
+
export class SchemaValidator {
|
|
9
|
+
constructor(schemaType = "openapi", validation) {
|
|
10
|
+
this.instanceId = Math.random().toString(36).substring(2, 8);
|
|
11
|
+
if (validation) {
|
|
12
|
+
logger.warn(`SchemaValidator instance ${this.instanceId}: Validation requested but using lite build. ` +
|
|
13
|
+
`Install the full @c7-digital/ledger package for runtime validation support.`);
|
|
14
|
+
}
|
|
15
|
+
this.schemaReady = Promise.resolve();
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Validate an array of items against a schema (lite: pass-through)
|
|
19
|
+
*/
|
|
20
|
+
async validateArraySchema(data, _itemSchemaName) {
|
|
21
|
+
await this.schemaReady;
|
|
22
|
+
return data;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Validate data against a schema and return typed result (lite: pass-through)
|
|
26
|
+
*/
|
|
27
|
+
async validateSchema(data, _schemaName) {
|
|
28
|
+
await this.schemaReady;
|
|
29
|
+
return data;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
declare const __brand: unique symbol;
|
|
2
|
+
type Brand<T, TBrand> = T & {
|
|
3
|
+
[__brand]: TBrand;
|
|
4
|
+
};
|
|
5
|
+
export type NameString = Brand<string, "NameString">;
|
|
6
|
+
export type PackageIdString = Brand<string, "PackageIdString">;
|
|
7
|
+
export type PartyIdString = Brand<string, "PartyIdString">;
|
|
8
|
+
export type LedgerString = Brand<string, "LedgerString">;
|
|
9
|
+
export type UserIdString = Brand<string, "UserIdString">;
|
|
10
|
+
export declare function isValidNameString(value: string): value is NameString;
|
|
11
|
+
export declare function isValidPackageIdString(value: string): value is PackageIdString;
|
|
12
|
+
export declare function isValidPartyIdString(value: string): value is PartyIdString;
|
|
13
|
+
export declare function isValidLedgerString(value: string): value is LedgerString;
|
|
14
|
+
export declare function isValidUserIdString(value: string): value is UserIdString;
|
|
15
|
+
export declare function createNameString(value: string): NameString;
|
|
16
|
+
export declare function createPackageIdString(value: string): PackageIdString;
|
|
17
|
+
export declare function createPartyIdString(value: string): PartyIdString;
|
|
18
|
+
export declare function createLedgerString(value: string): LedgerString;
|
|
19
|
+
export declare function createUserIdString(value: string): UserIdString;
|
|
20
|
+
export declare const ValueStringValidators: {
|
|
21
|
+
readonly isValidNameString: typeof isValidNameString;
|
|
22
|
+
readonly isValidPackageIdString: typeof isValidPackageIdString;
|
|
23
|
+
readonly isValidPartyIdString: typeof isValidPartyIdString;
|
|
24
|
+
readonly isValidLedgerString: typeof isValidLedgerString;
|
|
25
|
+
readonly isValidUserIdString: typeof isValidUserIdString;
|
|
26
|
+
};
|
|
27
|
+
export declare const ValueStringCreators: {
|
|
28
|
+
readonly createNameString: typeof createNameString;
|
|
29
|
+
readonly createPackageIdString: typeof createPackageIdString;
|
|
30
|
+
readonly createPartyIdString: typeof createPartyIdString;
|
|
31
|
+
readonly createLedgerString: typeof createLedgerString;
|
|
32
|
+
readonly createUserIdString: typeof createUserIdString;
|
|
33
|
+
};
|
|
34
|
+
export {};
|