@canton-network/core-tx-visualizer 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/dist/Tx.test.d.ts +2 -0
- package/dist/Tx.test.d.ts.map +1 -0
- package/dist/Tx.test.js +16 -0
- package/dist/hashing_scheme_v2.d.ts +4 -0
- package/dist/hashing_scheme_v2.d.ts.map +1 -0
- package/dist/hashing_scheme_v2.js +218 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +90 -0
- package/dist/utils.d.ts +10 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +55 -0
- package/package.json +41 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Tx.test.d.ts","sourceRoot":"","sources":["../src/Tx.test.ts"],"names":[],"mappings":""}
|
package/dist/Tx.test.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { expect, test } from '@jest/globals';
|
|
2
|
+
import { PreparedTransaction } from '@canton-network/core-ledger-proto';
|
|
3
|
+
import * as path from 'path';
|
|
4
|
+
import { readFileSync } from 'fs';
|
|
5
|
+
import { decodePreparedTransaction } from '.';
|
|
6
|
+
import camelcaseKeys from 'camelcase-keys';
|
|
7
|
+
test('decode a base 64 encoded prepared tx', async () => {
|
|
8
|
+
const base64EncodedPreparedTx = 'Cp8GCgMyLjESATAa8AUKATDCPukFCuYFCgMyLjESQjAwNTUwODAyZGRiMTYzNzFmNWZjNmQzNjRlNmNkNGIzMjgzYTllYjVjMGNjYjEyMWFlMzY4Y2RlYmJhZDBmYmY1NhoOQWRtaW5Xb3JrZmxvd3MiXgpAMmEzOGI5NjNmNmFiZjQ1Yjc2YzcwMmY5NzAwYmZkOTA2MDU1NTg3MmFmOTE1ZWY3ZjhmNjg3OTVlMmM4MzFiZBIUQ2FudG9uLkludGVybmFsLlBpbmcaBFBpbmcqtgJyswIKXgpAMmEzOGI5NjNmNmFiZjQ1Yjc2YzcwMmY5NzAwYmZkOTA2MDU1NTg3MmFmOTE1ZWY3ZjhmNjg3OTVlMmM4MzFiZBIUQ2FudG9uLkludGVybmFsLlBpbmcaBFBpbmcSDwoCaWQSCUIHcGluZ19pZBJdCglpbml0aWF0b3ISUDpOb3BlcmF0b3I6OjEyMjBkNDRmYzFjM2JhMGI1YmRmN2I5NTZlZTcxYmM5NGViZTJkMjMyNThkYzI2OGZkZjA4MjRmYmFlZmYyYzYxNDI0EmEKCXJlc3BvbmRlchJUOlJwYXJ0aWNpcGFudDE6OjEyMjBkNDRmYzFjM2JhMGI1YmRmN2I5NTZlZTcxYmM5NGViZTJkMjMyNThkYzI2OGZkZjA4MjRmYmFlZmYyYzYxNDI0Mk5vcGVyYXRvcjo6MTIyMGQ0NGZjMWMzYmEwYjViZGY3Yjk1NmVlNzFiYzk0ZWJlMmQyMzI1OGRjMjY4ZmRmMDgyNGZiYWVmZjJjNjE0MjQ6Tm9wZXJhdG9yOjoxMjIwZDQ0ZmMxYzNiYTBiNWJkZjdiOTU2ZWU3MWJjOTRlYmUyZDIzMjU4ZGMyNjhmZGYwODI0ZmJhZWZmMmM2MTQyNDpScGFydGljaXBhbnQxOjoxMjIwZDQ0ZmMxYzNiYTBiNWJkZjdiOTU2ZWU3MWJjOTRlYmUyZDIzMjU4ZGMyNjhmZGYwODI0ZmJhZWZmMmM2MTQyNCIiEiBr2qJTURRRnXWp6y1EyXQiB69cfe50kJw0eYN1UF39nhL1ARJ2Ck5vcGVyYXRvcjo6MTIyMGQ0NGZjMWMzYmEwYjViZGY3Yjk1NmVlNzFiYzk0ZWJlMmQyMzI1OGRjMjY4ZmRmMDgyNGZiYWVmZjJjNjE0MjQSJGYyZWM0ZDhmLWNjYzEtNDAyYi1iMjc4LTc1NTZmZGQyYjQxMhpMd2FsbGV0OjoxMjIwZTdiMjNlYTUyZWI1YzY3MmZiMGIxY2RiYzkxNjkyMmZmZWQzZGQ3Njc2YzIyM2E2MDU2NjQzMTVlMmQ0M2VkZCokYTMyODE2MmUtNzI4ZS00ZTA1LWFjNzgtYjM0ZjA3MDk4M2JhMK7Y9/LU944D';
|
|
9
|
+
const preparedTx = decodePreparedTransaction(base64EncodedPreparedTx);
|
|
10
|
+
const __dirname = path.resolve();
|
|
11
|
+
const resolvedFilePath = path.join(__dirname, '../tx-visualizer/create_ping_prepared_response.json');
|
|
12
|
+
const preapredTXJson = JSON.parse(readFileSync(resolvedFilePath, 'utf-8'));
|
|
13
|
+
const camelCasePreparedTx = camelcaseKeys(preapredTXJson, { deep: true });
|
|
14
|
+
const message = PreparedTransaction.fromJson(camelCasePreparedTx);
|
|
15
|
+
expect(message === preparedTx);
|
|
16
|
+
});
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { PreparedTransaction } from '@canton-network/core-ledger-proto';
|
|
2
|
+
export declare function encodeString(value?: string): Promise<Uint8Array>;
|
|
3
|
+
export declare function computePreparedTransaction(preparedTransaction: PreparedTransaction): Promise<Uint8Array>;
|
|
4
|
+
//# sourceMappingURL=hashing_scheme_v2.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hashing_scheme_v2.d.ts","sourceRoot":"","sources":["../src/hashing_scheme_v2.ts"],"names":[],"mappings":"AAAA,OAAO,EAOH,mBAAmB,EACtB,MAAM,mCAAmC,CAAA;AA6C1C,wBAAsB,YAAY,CAAC,KAAK,GAAE,MAAW,GAAG,OAAO,CAAC,UAAU,CAAC,CAG1E;AAqbD,wBAAsB,0BAA0B,CAC5C,mBAAmB,EAAE,mBAAmB,GACzC,OAAO,CAAC,UAAU,CAAC,CAErB"}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { HashingSchemeVersion, } from '@canton-network/core-ledger-proto';
|
|
2
|
+
import { mkByteArray, sha256 } from './utils.js';
|
|
3
|
+
// Hash purpose reserved for prepared transaction
|
|
4
|
+
const PREPARED_TRANSACTION_HASH_PURPOSE = Uint8Array.from([
|
|
5
|
+
0x00, 0x00, 0x00, 0x30,
|
|
6
|
+
]);
|
|
7
|
+
const NODE_ENCODING_VERSION = Uint8Array.from([0x01]);
|
|
8
|
+
const HASHING_SCHEME_VERSION = Uint8Array.from([HashingSchemeVersion.V2]);
|
|
9
|
+
async function encodeBool(value) {
|
|
10
|
+
return new Uint8Array([value ? 1 : 0]);
|
|
11
|
+
}
|
|
12
|
+
async function encodeInt32(value) {
|
|
13
|
+
const buffer = new ArrayBuffer(4);
|
|
14
|
+
const view = new DataView(buffer);
|
|
15
|
+
view.setInt32(0, value, false); // true for little-endian
|
|
16
|
+
return new Uint8Array(buffer);
|
|
17
|
+
}
|
|
18
|
+
async function encodeInt64(value) {
|
|
19
|
+
const num = typeof value === 'bigint' ? value : BigInt(value || 0);
|
|
20
|
+
const buffer = new ArrayBuffer(8);
|
|
21
|
+
const view = new DataView(buffer);
|
|
22
|
+
view.setBigInt64(0, num, false); // true for little-endian
|
|
23
|
+
return new Uint8Array(buffer);
|
|
24
|
+
}
|
|
25
|
+
export async function encodeString(value = '') {
|
|
26
|
+
const utf8Bytes = new TextEncoder().encode(value);
|
|
27
|
+
return encodeBytes(utf8Bytes);
|
|
28
|
+
}
|
|
29
|
+
async function encodeBytes(value) {
|
|
30
|
+
const length = await encodeInt32(value.length);
|
|
31
|
+
return mkByteArray(length, value);
|
|
32
|
+
}
|
|
33
|
+
async function encodeHash(value) {
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
function encodeHexString(value = '') {
|
|
37
|
+
// Convert hex string to Uint8Array
|
|
38
|
+
const bytes = new Uint8Array(value.length / 2);
|
|
39
|
+
for (let i = 0; i < value.length; i += 2) {
|
|
40
|
+
bytes[i / 2] = parseInt(value.slice(i, i + 2), 16);
|
|
41
|
+
}
|
|
42
|
+
return encodeBytes(bytes);
|
|
43
|
+
}
|
|
44
|
+
// Maybe suspicious?
|
|
45
|
+
async function encodeOptional(value, encodeFn) {
|
|
46
|
+
if (value === undefined || value === null) {
|
|
47
|
+
return new Uint8Array([0]); // Return empty array for undefined fields
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
return mkByteArray(1, await encodeFn(value));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// Maybe suspicious?
|
|
54
|
+
async function encodeProtoOptional(parentValue, fieldName, value, encodeFn) {
|
|
55
|
+
if (parentValue && parentValue[fieldName] !== undefined) {
|
|
56
|
+
return mkByteArray(1, await encodeFn(value));
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
return new Uint8Array([0]); // Return empty array for undefined fields
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async function encodeRepeated(values = [], encodeFn) {
|
|
63
|
+
const length = await encodeInt32(values.length);
|
|
64
|
+
const encodedValues = await Promise.all(values.map(encodeFn));
|
|
65
|
+
return mkByteArray(length, ...encodedValues);
|
|
66
|
+
}
|
|
67
|
+
function findSeed(nodeId, nodeSeeds) {
|
|
68
|
+
const seed = nodeSeeds.find((seed) => seed.nodeId.toString() === nodeId)?.seed;
|
|
69
|
+
if (!seed) {
|
|
70
|
+
throw new Error(`Seed for node ID ${nodeId} not found`);
|
|
71
|
+
}
|
|
72
|
+
return seed;
|
|
73
|
+
}
|
|
74
|
+
async function encodeIdentifier(identifier) {
|
|
75
|
+
return mkByteArray(await encodeString(identifier.packageId), await encodeRepeated(identifier.moduleName.split('.'), encodeString), await encodeRepeated(identifier.entityName.split('.'), encodeString));
|
|
76
|
+
}
|
|
77
|
+
async function encodeMetadata(metadata) {
|
|
78
|
+
return mkByteArray(1, await encodeRepeated(metadata.submitterInfo?.actAs, encodeString), await encodeString(metadata.submitterInfo?.commandId), await encodeString(metadata.transactionUuid), await encodeInt32(metadata.mediatorGroup), await encodeString(metadata.synchronizerId), await encodeProtoOptional(metadata, 'minLedgerEffectiveTime', metadata.minLedgerEffectiveTime, encodeInt64), await encodeProtoOptional(metadata, 'maxLedgerEffectiveTime', metadata.maxLedgerEffectiveTime, encodeInt64), await encodeRepeated(metadata.inputContracts, encodeInputContract));
|
|
79
|
+
}
|
|
80
|
+
async function encodeCreateNode(create, nodeId, nodeSeeds) {
|
|
81
|
+
return create
|
|
82
|
+
? mkByteArray(NODE_ENCODING_VERSION, await encodeString(create.lfVersion), 0 /** Create node tag */, await encodeOptional(findSeed(nodeId, nodeSeeds), encodeHash), await encodeHexString(create.contractId), await encodeString(create.packageName), await encodeIdentifier(create.templateId), await encodeValue(create.argument), await encodeRepeated(create.signatories, encodeString), await encodeRepeated(create.stakeholders, encodeString))
|
|
83
|
+
: mkByteArray();
|
|
84
|
+
}
|
|
85
|
+
async function encodeExerciseNode(exercise, nodeId, nodesDict, nodeSeeds) {
|
|
86
|
+
return mkByteArray(NODE_ENCODING_VERSION, await encodeString(exercise.lfVersion), 1 /** Exercise node tag */, await encodeHash(findSeed(nodeId, nodeSeeds)), await encodeHexString(exercise.contractId), await encodeString(exercise.packageName), await encodeIdentifier(exercise.templateId), await encodeRepeated(exercise.signatories, encodeString), await encodeRepeated(exercise.actingParties, encodeString), await encodeProtoOptional(exercise, 'interfaceId', exercise.interfaceId, encodeIdentifier), await encodeString(exercise.choiceId), await encodeValue(exercise.chosenValue), await encodeBool(exercise.consuming), await encodeProtoOptional(exercise, 'exerciseResult', exercise.exerciseResult, encodeValue), await encodeRepeated(exercise.choiceObservers, encodeString), await encodeRepeated(exercise.children, encodeNodeId(nodesDict, nodeSeeds)));
|
|
87
|
+
}
|
|
88
|
+
async function encodeFetchNode(fetch) {
|
|
89
|
+
return mkByteArray(NODE_ENCODING_VERSION, await encodeString(fetch.lfVersion), 2 /** Fetch node tag */, await encodeHexString(fetch.contractId), await encodeString(fetch.packageName), await encodeIdentifier(fetch.templateId), await encodeRepeated(fetch.signatories, encodeString), await encodeRepeated(fetch.stakeholders, encodeString), await encodeProtoOptional(fetch, 'interfaceId', fetch.interfaceId, encodeIdentifier), await encodeRepeated(fetch.actingParties, encodeString));
|
|
90
|
+
}
|
|
91
|
+
async function encodeRollbackNode(rollback, nodesDict, nodeSeeds) {
|
|
92
|
+
return mkByteArray(NODE_ENCODING_VERSION, 3 /** Rollback node tag */, await encodeRepeated(rollback.children, encodeNodeId(nodesDict, nodeSeeds)));
|
|
93
|
+
}
|
|
94
|
+
async function encodeInputContract(contract) {
|
|
95
|
+
if (contract.contract.oneofKind === 'v1')
|
|
96
|
+
return mkByteArray(await encodeInt64(contract.createdAt), await sha256(await encodeCreateNode(contract.contract.v1, 'unused_node_id', [])));
|
|
97
|
+
else
|
|
98
|
+
throw new Error('Unsupported contract version');
|
|
99
|
+
}
|
|
100
|
+
async function encodeValue(value) {
|
|
101
|
+
if (value.sum.oneofKind === 'unit') {
|
|
102
|
+
return Uint8Array.from([0]); // Unit value
|
|
103
|
+
}
|
|
104
|
+
else if (value.sum.oneofKind === 'bool') {
|
|
105
|
+
return mkByteArray(0x01, await encodeBool(value.sum.bool));
|
|
106
|
+
}
|
|
107
|
+
else if (value.sum.oneofKind === 'int64') {
|
|
108
|
+
return mkByteArray(0x02, await encodeInt64(parseInt(value.sum.int64)));
|
|
109
|
+
}
|
|
110
|
+
else if (value.sum.oneofKind === 'numeric') {
|
|
111
|
+
return mkByteArray(0x03, await encodeString(value.sum.numeric));
|
|
112
|
+
}
|
|
113
|
+
else if (value.sum.oneofKind === 'timestamp') {
|
|
114
|
+
return mkByteArray(0x04, await encodeString(value.sum.timestamp));
|
|
115
|
+
}
|
|
116
|
+
else if (value.sum.oneofKind === 'date') {
|
|
117
|
+
return mkByteArray(0x05, await encodeInt32(value.sum.date));
|
|
118
|
+
}
|
|
119
|
+
else if (value.sum.oneofKind === 'party') {
|
|
120
|
+
return mkByteArray(0x06, await encodeString(value.sum.party));
|
|
121
|
+
}
|
|
122
|
+
else if (value.sum.oneofKind === 'text') {
|
|
123
|
+
return mkByteArray(0x07, await encodeString(value.sum.text));
|
|
124
|
+
}
|
|
125
|
+
else if (value.sum.oneofKind === 'contractId') {
|
|
126
|
+
return mkByteArray(0x08, await encodeHexString(value.sum.contractId));
|
|
127
|
+
}
|
|
128
|
+
else if (value.sum.oneofKind === 'optional') {
|
|
129
|
+
return mkByteArray(0x09, await encodeProtoOptional(value.sum.optional, 'value', value.sum.optional.value, encodeValue));
|
|
130
|
+
}
|
|
131
|
+
else if (value.sum.oneofKind === 'list') {
|
|
132
|
+
return mkByteArray(0x0a, await encodeRepeated(value.sum.list.elements, encodeValue));
|
|
133
|
+
}
|
|
134
|
+
else if (value.sum.oneofKind === 'textMap') {
|
|
135
|
+
return mkByteArray(0x0b, await encodeRepeated(value.sum.textMap?.entries, encodeTextMapEntry));
|
|
136
|
+
}
|
|
137
|
+
else if (value.sum.oneofKind === 'record') {
|
|
138
|
+
return mkByteArray(0x0c, await encodeProtoOptional(value.sum.record, 'recordId', value.sum.record.recordId, encodeIdentifier), await encodeRepeated(value.sum.record.fields, encodeRecordField));
|
|
139
|
+
}
|
|
140
|
+
else if (value.sum.oneofKind === 'variant') {
|
|
141
|
+
return mkByteArray(0x0d, await encodeProtoOptional(value.sum.variant, 'variantId', value.sum.variant.variantId, encodeIdentifier), await encodeString(value.sum.variant.constructor), await encodeValue(value.sum.variant.value));
|
|
142
|
+
}
|
|
143
|
+
else if (value.sum.oneofKind === 'enum') {
|
|
144
|
+
return mkByteArray(0x0e, await encodeProtoOptional(value.sum.enum, 'enumId', value.sum.enum.enumId, encodeIdentifier), await encodeString(value.sum.enum.constructor));
|
|
145
|
+
}
|
|
146
|
+
else if (value.sum.oneofKind === 'genMap') {
|
|
147
|
+
return mkByteArray(0x0f, await encodeRepeated(value.sum.genMap?.entries, encodeGenMapEntry));
|
|
148
|
+
}
|
|
149
|
+
throw new Error('Unsupported value type: ' + JSON.stringify(value));
|
|
150
|
+
}
|
|
151
|
+
async function encodeTextMapEntry(entry) {
|
|
152
|
+
return mkByteArray(await encodeString(entry.key), await encodeValue(entry.value));
|
|
153
|
+
}
|
|
154
|
+
async function encodeRecordField(field) {
|
|
155
|
+
return mkByteArray(await encodeOptional(field.label, encodeString), await encodeValue(field.value));
|
|
156
|
+
}
|
|
157
|
+
async function encodeGenMapEntry(entry) {
|
|
158
|
+
return mkByteArray(await encodeValue(entry.key), await encodeValue(entry.value));
|
|
159
|
+
}
|
|
160
|
+
function encodeNodeId(nodesDict, nodeSeeds) {
|
|
161
|
+
return async (nodeId) => {
|
|
162
|
+
const node = nodesDict[nodeId];
|
|
163
|
+
if (!node) {
|
|
164
|
+
throw new Error(`Node with ID ${nodeId} not found in transaction`);
|
|
165
|
+
}
|
|
166
|
+
const encodedNode = await encodeNode(node, nodesDict, nodeSeeds);
|
|
167
|
+
return sha256(encodedNode);
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
async function encodeNode(node, nodesDict, nodeSeeds) {
|
|
171
|
+
if (node.versionedNode.oneofKind === 'v1') {
|
|
172
|
+
if (node.versionedNode.v1.nodeType.oneofKind === 'create') {
|
|
173
|
+
return encodeCreateNode(node.versionedNode.v1.nodeType.create, node.nodeId, nodeSeeds);
|
|
174
|
+
}
|
|
175
|
+
else if (node.versionedNode.v1.nodeType.oneofKind === 'exercise') {
|
|
176
|
+
return encodeExerciseNode(node.versionedNode.v1.nodeType.exercise, node.nodeId, nodesDict, nodeSeeds);
|
|
177
|
+
}
|
|
178
|
+
else if (node.versionedNode.v1.nodeType.oneofKind === 'fetch') {
|
|
179
|
+
return encodeFetchNode(node.versionedNode.v1.nodeType.fetch);
|
|
180
|
+
}
|
|
181
|
+
else if (node.versionedNode.v1.nodeType.oneofKind === 'rollback') {
|
|
182
|
+
return encodeRollbackNode(node.versionedNode.v1.nodeType.rollback, nodesDict, nodeSeeds);
|
|
183
|
+
}
|
|
184
|
+
throw new Error('Unsupported node type');
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
throw new Error(`Unsupported node version`);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
function createNodesDict(preparedTransaction) {
|
|
191
|
+
const nodesDict = {};
|
|
192
|
+
const nodes = preparedTransaction.transaction?.nodes || [];
|
|
193
|
+
for (const node of nodes) {
|
|
194
|
+
nodesDict[node.nodeId] = node;
|
|
195
|
+
}
|
|
196
|
+
return nodesDict;
|
|
197
|
+
}
|
|
198
|
+
async function encodeTransaction(transaction, nodesDict, nodeSeeds) {
|
|
199
|
+
return mkByteArray(await encodeString(transaction.version), await encodeRepeated(transaction.roots, encodeNodeId(nodesDict, nodeSeeds)));
|
|
200
|
+
}
|
|
201
|
+
async function hashTransaction(transaction, nodesDict) {
|
|
202
|
+
const encodedTransaction = await encodeTransaction(transaction, nodesDict, transaction.nodeSeeds);
|
|
203
|
+
const hash = await sha256(await mkByteArray(PREPARED_TRANSACTION_HASH_PURPOSE, encodedTransaction));
|
|
204
|
+
return hash;
|
|
205
|
+
}
|
|
206
|
+
async function hashMetadata(metadata) {
|
|
207
|
+
const hash = await sha256(await mkByteArray(PREPARED_TRANSACTION_HASH_PURPOSE, await encodeMetadata(metadata)));
|
|
208
|
+
return hash;
|
|
209
|
+
}
|
|
210
|
+
async function encodePreparedTransaction(preparedTransaction) {
|
|
211
|
+
const nodesDict = createNodesDict(preparedTransaction);
|
|
212
|
+
const transactionHash = await hashTransaction(preparedTransaction.transaction, nodesDict);
|
|
213
|
+
const metadataHash = await hashMetadata(preparedTransaction.metadata);
|
|
214
|
+
return mkByteArray(PREPARED_TRANSACTION_HASH_PURPOSE, HASHING_SCHEME_VERSION, transactionHash, metadataHash);
|
|
215
|
+
}
|
|
216
|
+
export async function computePreparedTransaction(preparedTransaction) {
|
|
217
|
+
return sha256(await encodePreparedTransaction(preparedTransaction));
|
|
218
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { PreparedTransaction } from '@canton-network/core-ledger-proto';
|
|
2
|
+
/**
|
|
3
|
+
* Decodes a base64 encoded prepared transaction into a well-typed data model, generated directly from Protobuf definitions.
|
|
4
|
+
*
|
|
5
|
+
* @param preparedTransaction - The prepared transaction in base64 format
|
|
6
|
+
* @returns The decoded prepared transaction
|
|
7
|
+
*/
|
|
8
|
+
export declare const decodePreparedTransaction: (preparedTransaction: string) => PreparedTransaction;
|
|
9
|
+
/**
|
|
10
|
+
* Computes the hash of a prepared transaction.
|
|
11
|
+
*
|
|
12
|
+
* @param preparedTransaction - The prepared transaction to hash
|
|
13
|
+
* @param format - The format of the output hash (base64 or hex)
|
|
14
|
+
* @returns The computed hash in the specified format
|
|
15
|
+
*/
|
|
16
|
+
export declare const hashPreparedTransaction: (preparedTransaction: string | PreparedTransaction, format?: "base64" | "hex") => Promise<string>;
|
|
17
|
+
type ValidationResult = Record<string, {
|
|
18
|
+
isAuthorized: boolean;
|
|
19
|
+
locations: string[];
|
|
20
|
+
}>;
|
|
21
|
+
export declare const validateAuthorizedPartyIds: (preparedTransaction: string | PreparedTransaction, authorizedPartyIds: string[]) => ValidationResult;
|
|
22
|
+
export {};
|
|
23
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,mCAAmC,CAAA;AAIvE;;;;;GAKG;AACH,eAAO,MAAM,yBAAyB,GAClC,qBAAqB,MAAM,KAC5B,mBAGF,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,uBAAuB,GAChC,qBAAqB,MAAM,GAAG,mBAAmB,EACjD,SAAQ,QAAQ,GAAG,KAAgB,KACpC,OAAO,CAAC,MAAM,CAiBhB,CAAA;AAED,KAAK,gBAAgB,GAAG,MAAM,CAC1B,MAAM,EACN;IACI,YAAY,EAAE,OAAO,CAAA;IACrB,SAAS,EAAE,MAAM,EAAE,CAAA;CACtB,CACJ,CAAA;AAED,eAAO,MAAM,0BAA0B,GACnC,qBAAqB,MAAM,GAAG,mBAAmB,EACjD,oBAAoB,MAAM,EAAE,KAC7B,gBAgEF,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { PreparedTransaction } from '@canton-network/core-ledger-proto';
|
|
2
|
+
import { computePreparedTransaction } from './hashing_scheme_v2.js';
|
|
3
|
+
import { fromBase64, toBase64, toHex } from './utils.js';
|
|
4
|
+
/**
|
|
5
|
+
* Decodes a base64 encoded prepared transaction into a well-typed data model, generated directly from Protobuf definitions.
|
|
6
|
+
*
|
|
7
|
+
* @param preparedTransaction - The prepared transaction in base64 format
|
|
8
|
+
* @returns The decoded prepared transaction
|
|
9
|
+
*/
|
|
10
|
+
export const decodePreparedTransaction = (preparedTransaction) => {
|
|
11
|
+
const bytes = fromBase64(preparedTransaction);
|
|
12
|
+
return PreparedTransaction.fromBinary(bytes);
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Computes the hash of a prepared transaction.
|
|
16
|
+
*
|
|
17
|
+
* @param preparedTransaction - The prepared transaction to hash
|
|
18
|
+
* @param format - The format of the output hash (base64 or hex)
|
|
19
|
+
* @returns The computed hash in the specified format
|
|
20
|
+
*/
|
|
21
|
+
export const hashPreparedTransaction = async (preparedTransaction, format = 'base64') => {
|
|
22
|
+
let preparedTx;
|
|
23
|
+
if (typeof preparedTransaction === 'string') {
|
|
24
|
+
preparedTx = decodePreparedTransaction(preparedTransaction);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
preparedTx = preparedTransaction;
|
|
28
|
+
}
|
|
29
|
+
const hash = await computePreparedTransaction(preparedTx);
|
|
30
|
+
switch (format) {
|
|
31
|
+
case 'base64':
|
|
32
|
+
return toBase64(hash);
|
|
33
|
+
case 'hex':
|
|
34
|
+
return toHex(hash);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
export const validateAuthorizedPartyIds = (preparedTransaction, authorizedPartyIds) => {
|
|
38
|
+
let preparedTx;
|
|
39
|
+
if (typeof preparedTransaction === 'string') {
|
|
40
|
+
preparedTx = decodePreparedTransaction(preparedTransaction);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
preparedTx = preparedTransaction;
|
|
44
|
+
}
|
|
45
|
+
const results = {};
|
|
46
|
+
preparedTx.metadata?.submitterInfo?.actAs.forEach((party) => {
|
|
47
|
+
results[party] = {
|
|
48
|
+
isAuthorized: authorizedPartyIds.includes(party),
|
|
49
|
+
locations: [
|
|
50
|
+
...results[party].locations,
|
|
51
|
+
'metadata.submitterInfo.actAs',
|
|
52
|
+
],
|
|
53
|
+
};
|
|
54
|
+
});
|
|
55
|
+
// then check transaction nodes
|
|
56
|
+
preparedTx.transaction?.nodes.forEach((node) => {
|
|
57
|
+
if (node.versionedNode.oneofKind === 'v1') {
|
|
58
|
+
if (node.versionedNode.v1.nodeType.oneofKind === 'create') {
|
|
59
|
+
node.versionedNode.v1.nodeType.create.signatories.forEach((party) => {
|
|
60
|
+
results[party] = {
|
|
61
|
+
isAuthorized: authorizedPartyIds.includes(party),
|
|
62
|
+
locations: [
|
|
63
|
+
...results[party].locations,
|
|
64
|
+
`transaction.nodes.${node.nodeId}.create.signatories`,
|
|
65
|
+
],
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
node.versionedNode.v1.nodeType.create.stakeholders.forEach((party) => {
|
|
69
|
+
results[party] = {
|
|
70
|
+
isAuthorized: authorizedPartyIds.includes(party),
|
|
71
|
+
locations: [
|
|
72
|
+
...results[party].locations,
|
|
73
|
+
`transaction.nodes.${node.nodeId}.create.stakeholders`,
|
|
74
|
+
],
|
|
75
|
+
};
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
if (node.versionedNode.v1.nodeType.oneofKind === 'exercise') {
|
|
79
|
+
throw new Error('Unsupported');
|
|
80
|
+
}
|
|
81
|
+
if (node.versionedNode.v1.nodeType.oneofKind === 'fetch') {
|
|
82
|
+
throw new Error('Unsupported');
|
|
83
|
+
}
|
|
84
|
+
if (node.versionedNode.v1.nodeType.oneofKind === 'rollback') {
|
|
85
|
+
// do we need to check these nodes?
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
return results;
|
|
90
|
+
};
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Take a base64 encoded string and return a Uint8Array of the decoded bytes */
|
|
2
|
+
export declare function fromBase64(b64: string): Uint8Array;
|
|
3
|
+
/** Take a byte buffer (Uint8Array) and encode it into a base64 string */
|
|
4
|
+
export declare function toBase64(data: Uint8Array): string;
|
|
5
|
+
/** Take a byte buffer (Uint8Array) and encode it into a hex string */
|
|
6
|
+
export declare function toHex(bytes: Uint8Array): string;
|
|
7
|
+
export declare function sha256(message: string | Uint8Array): Promise<Uint8Array>;
|
|
8
|
+
/** Utility to concatenate byte arrays or single bytes */
|
|
9
|
+
export declare function mkByteArray(...args: (number | Uint8Array)[]): Promise<Uint8Array>;
|
|
10
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAQlD;AAED,yEAAyE;AACzE,wBAAgB,QAAQ,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM,CAOjD;AAED,sEAAsE;AACtE,wBAAgB,KAAK,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAI/C;AAED,wBAAsB,MAAM,CACxB,OAAO,EAAE,MAAM,GAAG,UAAU,GAC7B,OAAO,CAAC,UAAU,CAAC,CASrB;AAED,yDAAyD;AACzD,wBAAsB,WAAW,CAC7B,GAAG,IAAI,EAAE,CAAC,MAAM,GAAG,UAAU,CAAC,EAAE,GACjC,OAAO,CAAC,UAAU,CAAC,CAuBrB"}
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/** Take a base64 encoded string and return a Uint8Array of the decoded bytes */
|
|
2
|
+
export function fromBase64(b64) {
|
|
3
|
+
const binaryString = atob(b64);
|
|
4
|
+
const len = binaryString.length;
|
|
5
|
+
const bytes = new Uint8Array(len);
|
|
6
|
+
for (let i = 0; i < len; i++) {
|
|
7
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
8
|
+
}
|
|
9
|
+
return bytes;
|
|
10
|
+
}
|
|
11
|
+
/** Take a byte buffer (Uint8Array) and encode it into a base64 string */
|
|
12
|
+
export function toBase64(data) {
|
|
13
|
+
let binaryString = '';
|
|
14
|
+
const len = data.byteLength;
|
|
15
|
+
for (let i = 0; i < len; i++) {
|
|
16
|
+
binaryString += String.fromCharCode(data[i]);
|
|
17
|
+
}
|
|
18
|
+
return btoa(binaryString);
|
|
19
|
+
}
|
|
20
|
+
/** Take a byte buffer (Uint8Array) and encode it into a hex string */
|
|
21
|
+
export function toHex(bytes) {
|
|
22
|
+
return Array.from(bytes)
|
|
23
|
+
.map((byte) => byte.toString(16).padStart(2, '0'))
|
|
24
|
+
.join('');
|
|
25
|
+
}
|
|
26
|
+
export async function sha256(message) {
|
|
27
|
+
const msg = typeof message === 'string'
|
|
28
|
+
? new TextEncoder().encode(message)
|
|
29
|
+
: message;
|
|
30
|
+
return crypto.subtle
|
|
31
|
+
.digest('SHA-256', new Uint8Array(msg))
|
|
32
|
+
.then((hash) => new Uint8Array(hash));
|
|
33
|
+
}
|
|
34
|
+
/** Utility to concatenate byte arrays or single bytes */
|
|
35
|
+
export async function mkByteArray(...args) {
|
|
36
|
+
const normalizedArgs = args.map((arg) => {
|
|
37
|
+
if (typeof arg === 'number') {
|
|
38
|
+
return new Uint8Array([arg]);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
return arg;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
let totalLength = 0;
|
|
45
|
+
normalizedArgs.forEach((arg) => {
|
|
46
|
+
totalLength += arg.length;
|
|
47
|
+
});
|
|
48
|
+
const mergedArray = new Uint8Array(totalLength);
|
|
49
|
+
let offset = 0;
|
|
50
|
+
normalizedArgs.forEach((arg) => {
|
|
51
|
+
mergedArray.set(arg, offset);
|
|
52
|
+
offset += arg.length;
|
|
53
|
+
});
|
|
54
|
+
return mergedArray;
|
|
55
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@canton-network/core-tx-visualizer",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"packageManager": "yarn@4.9.2",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "tsc -b",
|
|
11
|
+
"dev": "tsc -b --watch",
|
|
12
|
+
"test": "yarn node --experimental-vm-modules $(yarn bin jest)",
|
|
13
|
+
"clean": "tsc -b --clean; rm -rf dist"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@canton-network/core-ledger-proto": "^0.1.0",
|
|
17
|
+
"@jest/globals": "^29.0.0",
|
|
18
|
+
"@swc/jest": "^0.2.38",
|
|
19
|
+
"@types/jest": "^30.0.0",
|
|
20
|
+
"globals": "^16.0.0",
|
|
21
|
+
"jest": "^30.0.0",
|
|
22
|
+
"ts-jest": "^29.4.0",
|
|
23
|
+
"ts-jest-resolver": "^2.0.1",
|
|
24
|
+
"ts-proto": "^2.7.5",
|
|
25
|
+
"typescript": "^5.8.3"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@protobuf-ts/runtime": "^2.11.1",
|
|
29
|
+
"@protobuf-ts/runtime-rpc": "^2.11.1",
|
|
30
|
+
"@swc/core": "^1.13.3",
|
|
31
|
+
"@types/node": "^22.15.30",
|
|
32
|
+
"camelcase-keys": "^9.1.3",
|
|
33
|
+
"rxjs": "^7.8.2"
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist/*"
|
|
37
|
+
],
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
}
|
|
41
|
+
}
|