@canton-network/core-ledger-client 0.1.0
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/README.md +1 -0
- package/dist/LedgerClient.d.ts +67 -0
- package/dist/LedgerClient.d.ts.map +1 -0
- package/dist/LedgerClient.js +89 -0
- package/dist/TopologyWriteService.d.ts +20 -0
- package/dist/TopologyWriteService.d.ts.map +1 -0
- package/dist/TopologyWriteService.js +231 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/package.json +46 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# ledger-client
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { paths } from './generated-clients/openapi-3.3.0-SNAPSHOT.js';
|
|
2
|
+
import { Logger } from 'pino';
|
|
3
|
+
type PostEndpoint = {
|
|
4
|
+
[Pathname in keyof paths]: paths[Pathname] extends {
|
|
5
|
+
post: unknown;
|
|
6
|
+
} ? Pathname : never;
|
|
7
|
+
}[keyof paths];
|
|
8
|
+
export type PostRequest<Path extends PostEndpoint> = paths[Path] extends {
|
|
9
|
+
post: {
|
|
10
|
+
requestBody: {
|
|
11
|
+
content: {
|
|
12
|
+
'application/json': infer Req;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
} ? Req : never;
|
|
17
|
+
export type PostResponse<Path extends PostEndpoint> = paths[Path] extends {
|
|
18
|
+
post: {
|
|
19
|
+
responses: {
|
|
20
|
+
200: {
|
|
21
|
+
content: {
|
|
22
|
+
'application/json': infer Res;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
} ? Res : never;
|
|
28
|
+
type GetEndpoint = {
|
|
29
|
+
[Pathname in keyof paths]: paths[Pathname] extends {
|
|
30
|
+
get: unknown;
|
|
31
|
+
} ? Pathname : never;
|
|
32
|
+
}[keyof paths];
|
|
33
|
+
export type GetResponse<Path extends GetEndpoint> = paths[Path] extends {
|
|
34
|
+
get: {
|
|
35
|
+
responses: {
|
|
36
|
+
200: {
|
|
37
|
+
content: {
|
|
38
|
+
'application/json': infer Res;
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
} ? Res : never;
|
|
44
|
+
export declare class LedgerClient {
|
|
45
|
+
private readonly client;
|
|
46
|
+
private readonly logger;
|
|
47
|
+
constructor(baseUrl: string, token: string, _logger: Logger);
|
|
48
|
+
post<Path extends PostEndpoint>(path: Path, body: PostRequest<Path>, params?: {
|
|
49
|
+
path?: Record<string, string>;
|
|
50
|
+
query?: Record<string, string>;
|
|
51
|
+
}): Promise<PostResponse<Path>>;
|
|
52
|
+
get<Path extends GetEndpoint>(path: Path, params?: {
|
|
53
|
+
path?: Record<string, string>;
|
|
54
|
+
query?: Record<string, string>;
|
|
55
|
+
}): Promise<GetResponse<Path>>;
|
|
56
|
+
/**
|
|
57
|
+
* Grants a user the right to act as a party, while ensuring the party exists.
|
|
58
|
+
*
|
|
59
|
+
* @param userId The ID of the user to grant rights to.
|
|
60
|
+
* @param partyId The ID of the party to grant rights for.
|
|
61
|
+
* @returns A promise that resolves when the rights have been granted.
|
|
62
|
+
*/
|
|
63
|
+
grantUserRights(userId: string, partyId: string): Promise<void>;
|
|
64
|
+
private valueOrError;
|
|
65
|
+
}
|
|
66
|
+
export {};
|
|
67
|
+
//# sourceMappingURL=LedgerClient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LedgerClient.d.ts","sourceRoot":"","sources":["../src/LedgerClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,+CAA+C,CAAA;AAErE,OAAO,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAI7B,KAAK,YAAY,GAAG;KACf,QAAQ,IAAI,MAAM,KAAK,GAAG,KAAK,CAAC,QAAQ,CAAC,SAAS;QAC/C,IAAI,EAAE,OAAO,CAAA;KAChB,GACK,QAAQ,GACR,KAAK;CACd,CAAC,MAAM,KAAK,CAAC,CAAA;AAGd,MAAM,MAAM,WAAW,CAAC,IAAI,SAAS,YAAY,IAAI,KAAK,CAAC,IAAI,CAAC,SAAS;IACrE,IAAI,EAAE;QAAE,WAAW,EAAE;YAAE,OAAO,EAAE;gBAAE,kBAAkB,EAAE,MAAM,GAAG,CAAA;aAAE,CAAA;SAAE,CAAA;KAAE,CAAA;CACxE,GACK,GAAG,GACH,KAAK,CAAA;AAGX,MAAM,MAAM,YAAY,CAAC,IAAI,SAAS,YAAY,IAAI,KAAK,CAAC,IAAI,CAAC,SAAS;IACtE,IAAI,EAAE;QAAE,SAAS,EAAE;YAAE,GAAG,EAAE;gBAAE,OAAO,EAAE;oBAAE,kBAAkB,EAAE,MAAM,GAAG,CAAA;iBAAE,CAAA;aAAE,CAAA;SAAE,CAAA;KAAE,CAAA;CAC/E,GACK,GAAG,GACH,KAAK,CAAA;AAGX,KAAK,WAAW,GAAG;KACd,QAAQ,IAAI,MAAM,KAAK,GAAG,KAAK,CAAC,QAAQ,CAAC,SAAS;QAC/C,GAAG,EAAE,OAAO,CAAA;KACf,GACK,QAAQ,GACR,KAAK;CACd,CAAC,MAAM,KAAK,CAAC,CAAA;AAGd,MAAM,MAAM,WAAW,CAAC,IAAI,SAAS,WAAW,IAAI,KAAK,CAAC,IAAI,CAAC,SAAS;IACpE,GAAG,EAAE;QAAE,SAAS,EAAE;YAAE,GAAG,EAAE;gBAAE,OAAO,EAAE;oBAAE,kBAAkB,EAAE,MAAM,GAAG,CAAA;iBAAE,CAAA;aAAE,CAAA;SAAE,CAAA;KAAE,CAAA;CAC9E,GACK,GAAG,GACH,KAAK,CAAA;AAEX,qBAAa,YAAY;IACrB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAe;IACtC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAQ;gBAEnB,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAgB9C,IAAI,CAAC,IAAI,SAAS,YAAY,EACvC,IAAI,EAAE,IAAI,EACV,IAAI,EAAE,WAAW,CAAC,IAAI,CAAC,EACvB,MAAM,CAAC,EAAE;QACL,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;QAC7B,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;KACjC,GACF,OAAO,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;IASjB,GAAG,CAAC,IAAI,SAAS,WAAW,EACrC,IAAI,EAAE,IAAI,EACV,MAAM,CAAC,EAAE;QACL,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;QAC7B,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;KACjC,GACF,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;IAY7B;;;;;;OAMG;IACU,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;YAqD9C,YAAY;CAU7B"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import createClient from 'openapi-fetch';
|
|
2
|
+
export class LedgerClient {
|
|
3
|
+
client;
|
|
4
|
+
logger;
|
|
5
|
+
constructor(baseUrl, token, _logger) {
|
|
6
|
+
this.logger = _logger.child({ component: 'LedgerClient' });
|
|
7
|
+
this.client = createClient({
|
|
8
|
+
baseUrl,
|
|
9
|
+
fetch: async (url, options = {}) => {
|
|
10
|
+
return fetch(url, {
|
|
11
|
+
...options,
|
|
12
|
+
headers: {
|
|
13
|
+
...(options.headers || {}),
|
|
14
|
+
Authorization: `Bearer ${token}`,
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
async post(path, body, params) {
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- (cant align this with openapi-fetch generics :shrug:)
|
|
22
|
+
const options = { body, params };
|
|
23
|
+
const resp = await this.client.POST(path, options);
|
|
24
|
+
this.logger.debug({ requestBody: body, response: resp }, `POST ${path}`);
|
|
25
|
+
return this.valueOrError(resp);
|
|
26
|
+
}
|
|
27
|
+
async get(path, params) {
|
|
28
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- (cant align this with openapi-fetch generics :shrug:)
|
|
29
|
+
const options = { params };
|
|
30
|
+
const resp = await this.client.GET(path, options);
|
|
31
|
+
this.logger.debug({ path: path, params: params, response: resp }, `GET ${path}`);
|
|
32
|
+
return this.valueOrError(resp);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Grants a user the right to act as a party, while ensuring the party exists.
|
|
36
|
+
*
|
|
37
|
+
* @param userId The ID of the user to grant rights to.
|
|
38
|
+
* @param partyId The ID of the party to grant rights for.
|
|
39
|
+
* @returns A promise that resolves when the rights have been granted.
|
|
40
|
+
*/
|
|
41
|
+
async grantUserRights(userId, partyId) {
|
|
42
|
+
// Wait for party to appear on participant
|
|
43
|
+
let partyFound = false;
|
|
44
|
+
let tries = 0;
|
|
45
|
+
const maxTries = 5;
|
|
46
|
+
while (!partyFound || tries >= maxTries) {
|
|
47
|
+
const parties = await this.get('/v2/parties');
|
|
48
|
+
partyFound =
|
|
49
|
+
parties.partyDetails?.some((party) => party.party === partyId) || false;
|
|
50
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
51
|
+
tries++;
|
|
52
|
+
}
|
|
53
|
+
if (tries >= maxTries) {
|
|
54
|
+
throw new Error('timed out waiting for new party to appear');
|
|
55
|
+
}
|
|
56
|
+
// Assign user rights to party
|
|
57
|
+
const result = await this.post('/v2/users/{user-id}/rights', {
|
|
58
|
+
identityProviderId: '',
|
|
59
|
+
userId,
|
|
60
|
+
rights: [
|
|
61
|
+
{
|
|
62
|
+
kind: {
|
|
63
|
+
CanActAs: {
|
|
64
|
+
value: {
|
|
65
|
+
party: partyId,
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
}, {
|
|
72
|
+
path: {
|
|
73
|
+
'user-id': userId,
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
if (!result.newlyGrantedRights) {
|
|
77
|
+
throw new Error('Failed to grant user rights');
|
|
78
|
+
}
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
async valueOrError(response) {
|
|
82
|
+
if (response.data === undefined) {
|
|
83
|
+
return Promise.reject(response.error);
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
return Promise.resolve(response.data);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { LedgerClient } from './LedgerClient';
|
|
2
|
+
import { SigningPublicKey, SignedTopologyTransaction, GenerateTransactionsResponse } from '@canton-network/core-ledger-proto';
|
|
3
|
+
export declare class TopologyWriteService {
|
|
4
|
+
private userAdminToken;
|
|
5
|
+
private topologyClient;
|
|
6
|
+
private topologyReadService;
|
|
7
|
+
private ledgerClient;
|
|
8
|
+
private store;
|
|
9
|
+
constructor(synchronizerId: string, userAdminUrl: string, userAdminToken: string, ledgerClient: LedgerClient);
|
|
10
|
+
static combineHashes(hashes: Buffer[]): string;
|
|
11
|
+
static createFingerprintFromKey: (publicKey: SigningPublicKey | string) => string;
|
|
12
|
+
static toSignedTopologyTransaction(txHashes: Buffer<ArrayBuffer>[], serializedTransaction: Uint8Array<ArrayBufferLike>, signature: string, namespace: string): SignedTopologyTransaction;
|
|
13
|
+
private generateTransactionsRequest;
|
|
14
|
+
submitExternalPartyTopology(signedTopologyTxs: SignedTopologyTransaction[], partyId: string): Promise<void>;
|
|
15
|
+
generateTransactions(publicKey: string, partyId: string): Promise<GenerateTransactionsResponse>;
|
|
16
|
+
private addTransactions;
|
|
17
|
+
waitForPartyToParticipantProposal(partyId: string): Promise<Uint8Array | undefined>;
|
|
18
|
+
private authorizePartyToParticipant;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=TopologyWriteService.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TopologyWriteService.d.ts","sourceRoot":"","sources":["../src/TopologyWriteService.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,EAIH,gBAAgB,EAahB,yBAAyB,EASzB,4BAA4B,EAK/B,MAAM,mCAAmC,CAAA;AAgC1C,qBAAa,oBAAoB;IAUzB,OAAO,CAAC,cAAc;IAT1B,OAAO,CAAC,cAAc,CAAmC;IACzD,OAAO,CAAC,mBAAmB,CAAkC;IAC7D,OAAO,CAAC,YAAY,CAAc;IAElC,OAAO,CAAC,KAAK,CAAS;gBAGlB,cAAc,EAAE,MAAM,EACtB,YAAY,EAAE,MAAM,EACZ,cAAc,EAAE,MAAM,EAC9B,YAAY,EAAE,YAAY;IAuB9B,MAAM,CAAC,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM;IA0B9C,MAAM,CAAC,wBAAwB,GAC3B,WAAW,gBAAgB,GAAG,MAAM,KACrC,MAAM,CAeR;IAED,MAAM,CAAC,2BAA2B,CAC9B,QAAQ,EAAE,MAAM,CAAC,WAAW,CAAC,EAAE,EAC/B,qBAAqB,EAAE,UAAU,CAAC,eAAe,CAAC,EAClD,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,GAClB,yBAAyB;IAqB5B,OAAO,CAAC,2BAA2B;IAiE7B,2BAA2B,CAC7B,iBAAiB,EAAE,yBAAyB,EAAE,EAC9C,OAAO,EAAE,MAAM;IAMb,oBAAoB,CACtB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,GAChB,OAAO,CAAC,4BAA4B,CAAC;YAuB1B,eAAe;IAgBvB,iCAAiC,CACnC,OAAO,EAAE,MAAM,GAChB,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC;YAkCpB,2BAA2B;CAwB5C"}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { CryptoKeyFormat, SigningKeyScheme, SigningKeySpec, Signature, SignatureFormat, SigningAlgorithmSpec, TopologyManagerWriteServiceClient, StoreId, StoreId_Synchronizer, Enums_ParticipantPermission, Enums_TopologyChangeOp, NamespaceDelegation, PartyToKeyMapping, PartyToParticipant, PartyToParticipant_HostingParticipant, SignedTopologyTransaction, TopologyMapping, MultiTransactionSignatures, AddTransactionsRequest, AuthorizeRequest, GenerateTransactionsRequest, GenerateTransactionsRequest_Proposal, BaseQuery, ListPartyToParticipantRequest, Empty, TopologyManagerReadServiceClient, } from '@canton-network/core-ledger-proto';
|
|
2
|
+
import { GrpcTransport } from '@protobuf-ts/grpc-transport';
|
|
3
|
+
import { ChannelCredentials } from '@grpc/grpc-js';
|
|
4
|
+
import { createHash } from 'node:crypto';
|
|
5
|
+
function prefixedInt(value, bytes) {
|
|
6
|
+
const buffer = Buffer.alloc(4 + bytes.length);
|
|
7
|
+
buffer.writeUInt32BE(value, 0);
|
|
8
|
+
Buffer.from(bytes).copy(buffer, 4);
|
|
9
|
+
return buffer;
|
|
10
|
+
}
|
|
11
|
+
function signingPublicKeyFromEd25519(publicKey) {
|
|
12
|
+
return {
|
|
13
|
+
format: CryptoKeyFormat.RAW,
|
|
14
|
+
publicKey: Buffer.from(publicKey, 'base64'),
|
|
15
|
+
scheme: SigningKeyScheme.ED25519,
|
|
16
|
+
keySpec: SigningKeySpec.EC_CURVE25519,
|
|
17
|
+
usage: [],
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function computeSha256CantonHash(purpose, bytes) {
|
|
21
|
+
const hashInput = prefixedInt(purpose, bytes);
|
|
22
|
+
const hash = createHash('sha256').update(hashInput).digest();
|
|
23
|
+
const multiprefix = Buffer.from([0x12, 0x20]);
|
|
24
|
+
return Buffer.concat([multiprefix, hash]).toString('hex');
|
|
25
|
+
}
|
|
26
|
+
// TODO(#180): remove or rewrite after grpc is gone
|
|
27
|
+
export class TopologyWriteService {
|
|
28
|
+
userAdminToken;
|
|
29
|
+
topologyClient;
|
|
30
|
+
topologyReadService;
|
|
31
|
+
ledgerClient;
|
|
32
|
+
store;
|
|
33
|
+
constructor(synchronizerId, userAdminUrl, userAdminToken, ledgerClient) {
|
|
34
|
+
this.userAdminToken = userAdminToken;
|
|
35
|
+
const transport = new GrpcTransport({
|
|
36
|
+
host: userAdminUrl,
|
|
37
|
+
channelCredentials: ChannelCredentials.createInsecure(),
|
|
38
|
+
});
|
|
39
|
+
this.topologyClient = new TopologyManagerWriteServiceClient(transport);
|
|
40
|
+
this.topologyReadService = new TopologyManagerReadServiceClient(transport);
|
|
41
|
+
this.ledgerClient = ledgerClient;
|
|
42
|
+
this.store = StoreId.create({
|
|
43
|
+
store: {
|
|
44
|
+
oneofKind: 'synchronizer',
|
|
45
|
+
synchronizer: StoreId_Synchronizer.create({
|
|
46
|
+
id: synchronizerId,
|
|
47
|
+
}),
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
static combineHashes(hashes) {
|
|
52
|
+
// Sort the hashes by their hex representation
|
|
53
|
+
const sortedHashes = hashes.sort((a, b) => a.toString('hex').localeCompare(b.toString('hex')));
|
|
54
|
+
// Start with the number of hashes encoded as a 4-byte integer in big-endian
|
|
55
|
+
const combinedHashes = Buffer.alloc(4);
|
|
56
|
+
combinedHashes.writeUInt32BE(sortedHashes.length, 0);
|
|
57
|
+
// Concatenate each hash, prefixing them with their size as a 4-byte integer in big-endian
|
|
58
|
+
let concatenatedHashes = combinedHashes;
|
|
59
|
+
for (const h of sortedHashes) {
|
|
60
|
+
const lengthBuffer = Buffer.alloc(4);
|
|
61
|
+
lengthBuffer.writeUInt32BE(h.length, 0);
|
|
62
|
+
concatenatedHashes = Buffer.concat([
|
|
63
|
+
concatenatedHashes,
|
|
64
|
+
lengthBuffer,
|
|
65
|
+
h,
|
|
66
|
+
]);
|
|
67
|
+
}
|
|
68
|
+
// 55 is the hash purpose for multi topology transaction hashes
|
|
69
|
+
return computeSha256CantonHash(55, concatenatedHashes);
|
|
70
|
+
}
|
|
71
|
+
static createFingerprintFromKey = (publicKey) => {
|
|
72
|
+
let key;
|
|
73
|
+
if (typeof publicKey === 'string') {
|
|
74
|
+
key = signingPublicKeyFromEd25519(publicKey);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
key = publicKey;
|
|
78
|
+
}
|
|
79
|
+
// Hash purpose codes can be looked up in the Canton codebase:
|
|
80
|
+
// https://github.com/DACH-NY/canton/blob/62e9ccd3f1743d2c9422d863cfc2ca800405c71b/community/base/src/main/scala/com/digitalasset/canton/crypto/HashPurpose.scala#L52
|
|
81
|
+
const hashPurpose = 12; // For `PublicKeyFingerprint`
|
|
82
|
+
// Implementation for creating a fingerprint from the public key
|
|
83
|
+
return computeSha256CantonHash(hashPurpose, key.publicKey);
|
|
84
|
+
};
|
|
85
|
+
static toSignedTopologyTransaction(txHashes, serializedTransaction, signature, namespace) {
|
|
86
|
+
return SignedTopologyTransaction.create({
|
|
87
|
+
transaction: serializedTransaction,
|
|
88
|
+
proposal: true,
|
|
89
|
+
signatures: [],
|
|
90
|
+
multiTransactionSignatures: [
|
|
91
|
+
MultiTransactionSignatures.create({
|
|
92
|
+
transactionHashes: txHashes,
|
|
93
|
+
signatures: [
|
|
94
|
+
Signature.create({
|
|
95
|
+
format: SignatureFormat.RAW,
|
|
96
|
+
signature: Buffer.from(signature, 'base64'),
|
|
97
|
+
signedBy: namespace,
|
|
98
|
+
signingAlgorithmSpec: SigningAlgorithmSpec.ED25519,
|
|
99
|
+
}),
|
|
100
|
+
],
|
|
101
|
+
}),
|
|
102
|
+
],
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
generateTransactionsRequest(namespace, partyId, participantId, publicKey) {
|
|
106
|
+
// Implementation for generating transactions request
|
|
107
|
+
const namespaceDelegation = TopologyMapping.create({
|
|
108
|
+
mapping: {
|
|
109
|
+
oneofKind: 'namespaceDelegation',
|
|
110
|
+
namespaceDelegation: NamespaceDelegation.create({
|
|
111
|
+
namespace,
|
|
112
|
+
targetKey: publicKey,
|
|
113
|
+
isRootDelegation: true,
|
|
114
|
+
restriction: {
|
|
115
|
+
oneofKind: undefined,
|
|
116
|
+
},
|
|
117
|
+
}),
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
const partyToParticipant = TopologyMapping.create({
|
|
121
|
+
mapping: {
|
|
122
|
+
oneofKind: 'partyToParticipant',
|
|
123
|
+
partyToParticipant: PartyToParticipant.create({
|
|
124
|
+
party: partyId,
|
|
125
|
+
threshold: 1,
|
|
126
|
+
participants: [
|
|
127
|
+
PartyToParticipant_HostingParticipant.create({
|
|
128
|
+
participantUid: participantId,
|
|
129
|
+
permission: Enums_ParticipantPermission.CONFIRMATION,
|
|
130
|
+
}),
|
|
131
|
+
],
|
|
132
|
+
}),
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
const partyToKeyMapping = TopologyMapping.create({
|
|
136
|
+
mapping: {
|
|
137
|
+
oneofKind: 'partyToKeyMapping',
|
|
138
|
+
partyToKeyMapping: PartyToKeyMapping.create({
|
|
139
|
+
party: partyId,
|
|
140
|
+
threshold: 1,
|
|
141
|
+
signingKeys: [publicKey],
|
|
142
|
+
}),
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
return GenerateTransactionsRequest.create({
|
|
146
|
+
proposals: [
|
|
147
|
+
namespaceDelegation,
|
|
148
|
+
partyToParticipant,
|
|
149
|
+
partyToKeyMapping,
|
|
150
|
+
].map((mapping) => GenerateTransactionsRequest_Proposal.create({
|
|
151
|
+
mapping,
|
|
152
|
+
serial: 1,
|
|
153
|
+
store: this.store,
|
|
154
|
+
operation: Enums_TopologyChangeOp.ADD_REPLACE,
|
|
155
|
+
})),
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
async submitExternalPartyTopology(signedTopologyTxs, partyId) {
|
|
159
|
+
await this.addTransactions(signedTopologyTxs);
|
|
160
|
+
await this.authorizePartyToParticipant(partyId);
|
|
161
|
+
}
|
|
162
|
+
async generateTransactions(publicKey, partyId) {
|
|
163
|
+
const signingPublicKey = signingPublicKeyFromEd25519(publicKey);
|
|
164
|
+
const namespace = TopologyWriteService.createFingerprintFromKey(signingPublicKey);
|
|
165
|
+
const { participantId } = await this.ledgerClient.get('/v2/parties/participant-id');
|
|
166
|
+
const req = this.generateTransactionsRequest(namespace, partyId, participantId, signingPublicKey);
|
|
167
|
+
return this.topologyClient.generateTransactions(req, {
|
|
168
|
+
meta: {
|
|
169
|
+
Authorization: `Bearer ${this.userAdminToken}`,
|
|
170
|
+
},
|
|
171
|
+
}).response;
|
|
172
|
+
}
|
|
173
|
+
async addTransactions(signedTopologyTxs) {
|
|
174
|
+
const request = AddTransactionsRequest.create({
|
|
175
|
+
transactions: signedTopologyTxs,
|
|
176
|
+
forceChanges: [],
|
|
177
|
+
store: this.store,
|
|
178
|
+
});
|
|
179
|
+
return this.topologyClient.addTransactions(request, {
|
|
180
|
+
meta: {
|
|
181
|
+
Authorization: `Bearer ${this.userAdminToken}`,
|
|
182
|
+
},
|
|
183
|
+
}).response;
|
|
184
|
+
}
|
|
185
|
+
async waitForPartyToParticipantProposal(partyId) {
|
|
186
|
+
return new Promise((resolve, reject) => {
|
|
187
|
+
const interval = setInterval(async () => {
|
|
188
|
+
let counter = 0;
|
|
189
|
+
const result = await this.topologyReadService.listPartyToParticipant(ListPartyToParticipantRequest.create({
|
|
190
|
+
baseQuery: BaseQuery.create({
|
|
191
|
+
store: this.store,
|
|
192
|
+
proposals: true,
|
|
193
|
+
timeQuery: {
|
|
194
|
+
oneofKind: 'headState',
|
|
195
|
+
headState: Empty.create(),
|
|
196
|
+
},
|
|
197
|
+
}),
|
|
198
|
+
filterParty: partyId,
|
|
199
|
+
}));
|
|
200
|
+
if (result.response.results.length > 0) {
|
|
201
|
+
clearInterval(interval);
|
|
202
|
+
resolve(result.response.results[0].context?.transactionHash);
|
|
203
|
+
}
|
|
204
|
+
counter += 1;
|
|
205
|
+
if (counter > 10) {
|
|
206
|
+
clearInterval(interval);
|
|
207
|
+
reject('Timeout waiting for party to participant proposal');
|
|
208
|
+
}
|
|
209
|
+
}, 1000);
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
async authorizePartyToParticipant(partyId) {
|
|
213
|
+
const hash = await this.waitForPartyToParticipantProposal(partyId);
|
|
214
|
+
if (!hash) {
|
|
215
|
+
throw new Error('No topology transaction found for authorization');
|
|
216
|
+
}
|
|
217
|
+
const request = AuthorizeRequest.create({
|
|
218
|
+
type: {
|
|
219
|
+
oneofKind: 'transactionHash',
|
|
220
|
+
transactionHash: Buffer.from(hash).toString('hex'),
|
|
221
|
+
},
|
|
222
|
+
mustFullyAuthorize: false,
|
|
223
|
+
store: this.store,
|
|
224
|
+
});
|
|
225
|
+
return this.topologyClient.authorize(request, {
|
|
226
|
+
meta: {
|
|
227
|
+
Authorization: `Bearer ${this.userAdminToken}`,
|
|
228
|
+
},
|
|
229
|
+
}).response;
|
|
230
|
+
}
|
|
231
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export * from './LedgerClient.js';
|
|
2
|
+
export * from './TopologyWriteService.js';
|
|
3
|
+
export * from './generated-clients/openapi-3.3.0-SNAPSHOT.js';
|
|
4
|
+
export * from './_proto/com/digitalasset/canton/crypto/v30/crypto.js';
|
|
5
|
+
export * from './_proto/com/digitalasset/canton/protocol/v30/topology.js';
|
|
6
|
+
export * from './_proto/com/digitalasset/canton/topology/admin/v30/topology_manager_write_service.js';
|
|
7
|
+
export * from './_proto/com/digitalasset/canton/topology/admin/v30/topology_manager_read_service.js';
|
|
8
|
+
export * from './_proto/com/digitalasset/canton/topology/admin/v30/common.js';
|
|
9
|
+
export { DamlTransaction, DamlTransaction_Node, DamlTransaction_NodeSeed, HashingSchemeVersion, Metadata, Metadata_InputContract, PreparedTransaction, } from './_proto/com/daml/ledger/api/v2/interactive/interactive_submission_service.js';
|
|
10
|
+
export { Create, Exercise, Fetch, Rollback, } from './_proto/com/daml/ledger/api/v2/interactive/transaction/v1/interactive_submission_data.js';
|
|
11
|
+
export { GenMap_Entry, Identifier, RecordField, TextMap_Entry, Value, } from './_proto/com/daml/ledger/api/v2/value.js';
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAA;AACjC,cAAc,2BAA2B,CAAA;AACzC,cAAc,+CAA+C,CAAA;AAC7D,cAAc,uDAAuD,CAAA;AACrE,cAAc,2DAA2D,CAAA;AACzE,cAAc,uFAAuF,CAAA;AACrG,cAAc,sFAAsF,CAAA;AACpG,cAAc,+DAA+D,CAAA;AAC7E,OAAO,EACH,eAAe,EACf,oBAAoB,EACpB,wBAAwB,EACxB,oBAAoB,EACpB,QAAQ,EACR,sBAAsB,EACtB,mBAAmB,GACtB,MAAM,+EAA+E,CAAA;AAEtF,OAAO,EACH,MAAM,EACN,QAAQ,EACR,KAAK,EACL,QAAQ,GACX,MAAM,2FAA2F,CAAA;AAClG,OAAO,EACH,YAAY,EACZ,UAAU,EACV,WAAW,EACX,aAAa,EACb,KAAK,GACR,MAAM,0CAA0C,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from './LedgerClient.js';
|
|
2
|
+
export * from './TopologyWriteService.js';
|
|
3
|
+
export * from './generated-clients/openapi-3.3.0-SNAPSHOT.js';
|
|
4
|
+
export * from './_proto/com/digitalasset/canton/crypto/v30/crypto.js';
|
|
5
|
+
export * from './_proto/com/digitalasset/canton/protocol/v30/topology.js';
|
|
6
|
+
export * from './_proto/com/digitalasset/canton/topology/admin/v30/topology_manager_write_service.js';
|
|
7
|
+
export * from './_proto/com/digitalasset/canton/topology/admin/v30/topology_manager_read_service.js';
|
|
8
|
+
export * from './_proto/com/digitalasset/canton/topology/admin/v30/common.js';
|
|
9
|
+
export { DamlTransaction, DamlTransaction_Node, DamlTransaction_NodeSeed, HashingSchemeVersion, Metadata, Metadata_InputContract, PreparedTransaction, } from './_proto/com/daml/ledger/api/v2/interactive/interactive_submission_service.js';
|
|
10
|
+
export { Create, Exercise, Fetch, Rollback, } from './_proto/com/daml/ledger/api/v2/interactive/transaction/v1/interactive_submission_data.js';
|
|
11
|
+
export { GenMap_Entry, Identifier, RecordField, TextMap_Entry, Value, } from './_proto/com/daml/ledger/api/v2/value.js';
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@canton-network/core-ledger-client",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Provides ledger client for the wallet kernel.",
|
|
5
|
+
"author": "Phillip Olesen <phillip.olesen@digitalasset.com>",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"packageManager": "yarn@4.9.2",
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"type": "module",
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc -b",
|
|
13
|
+
"dev": "tsc -b --watch",
|
|
14
|
+
"clean": "tsc -b --clean; rm -rf dist",
|
|
15
|
+
"test": "yarn node --experimental-vm-modules $(yarn bin jest) --passWithNoTests"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@grpc/grpc-js": "^1.13.4",
|
|
19
|
+
"@jest/globals": "^29.0.0",
|
|
20
|
+
"@protobuf-ts/grpc-transport": "^2.11.1",
|
|
21
|
+
"@protobuf-ts/runtime": "^2.11.1",
|
|
22
|
+
"@protobuf-ts/runtime-rpc": "^2.11.1",
|
|
23
|
+
"@swc/core": "^1.11.31",
|
|
24
|
+
"@swc/jest": "^0.2.38",
|
|
25
|
+
"@types/jest": "^30.0.0",
|
|
26
|
+
"@types/node": "^22.15.30",
|
|
27
|
+
"jest": "^30.0.0",
|
|
28
|
+
"openapi-typescript": "^7.8.0",
|
|
29
|
+
"ts-jest": "^29.4.0",
|
|
30
|
+
"ts-jest-resolver": "^2.0.1",
|
|
31
|
+
"typescript": "^5.8.3"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@canton-network/core-ledger-proto": "^0.1.0",
|
|
35
|
+
"@protobuf-ts/runtime": "^2.11.1",
|
|
36
|
+
"@protobuf-ts/runtime-rpc": "^2.11.1",
|
|
37
|
+
"openapi-fetch": "^0.14.0",
|
|
38
|
+
"pino": "^9.7.0"
|
|
39
|
+
},
|
|
40
|
+
"files": [
|
|
41
|
+
"dist/*"
|
|
42
|
+
],
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public"
|
|
45
|
+
}
|
|
46
|
+
}
|