@bitgo-beta/sdk-coin-canton 1.0.0-alpha.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/LICENSE +191 -0
- package/README.md +30 -0
- package/dist/resources/hash/hash.d.ts +5 -0
- package/dist/resources/hash/hash.js +297 -0
- package/dist/resources/proto/damlTransaction.js +100 -0
- package/dist/resources/proto/damlTransactionNode.js +71 -0
- package/dist/resources/proto/damlTransactionNodeSeed.js +56 -0
- package/dist/resources/proto/metadata/metadataGlobalKeyMappingEntry.js +53 -0
- package/dist/resources/proto/metadata/metadataInputContract.js +80 -0
- package/dist/resources/proto/metadata/metadataSubmitterInfo.js +62 -0
- package/dist/resources/proto/metadata.js +189 -0
- package/dist/resources/proto/node/empty.js +36 -0
- package/dist/resources/proto/node/globalKey.js +81 -0
- package/dist/resources/proto/node/identifier.js +73 -0
- package/dist/resources/proto/node/node.js +656 -0
- package/dist/resources/proto/node/timestamp.js +145 -0
- package/dist/resources/proto/node/value.js +824 -0
- package/dist/resources/proto/preparedTransaction.d.ts +8 -0
- package/dist/resources/proto/preparedTransaction.js +66 -0
- package/dist/src/canton.d.ts +37 -0
- package/dist/src/canton.d.ts.map +1 -0
- package/dist/src/canton.js +94 -0
- package/dist/src/index.d.ts +4 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +20 -0
- package/dist/src/lib/constant.d.ts +12 -0
- package/dist/src/lib/constant.d.ts.map +1 -0
- package/dist/src/lib/constant.js +15 -0
- package/dist/src/lib/iface.d.ts +58 -0
- package/dist/src/lib/iface.d.ts.map +1 -0
- package/dist/src/lib/iface.js +3 -0
- package/dist/src/lib/index.d.ts +8 -0
- package/dist/src/lib/index.d.ts.map +1 -0
- package/dist/src/lib/index.js +49 -0
- package/dist/src/lib/keyPair.d.ts +18 -0
- package/dist/src/lib/keyPair.d.ts.map +1 -0
- package/dist/src/lib/keyPair.js +44 -0
- package/dist/src/lib/oneStepPreApprovalBuilder.d.ts +49 -0
- package/dist/src/lib/oneStepPreApprovalBuilder.d.ts.map +1 -0
- package/dist/src/lib/oneStepPreApprovalBuilder.js +87 -0
- package/dist/src/lib/resourcesInterface.d.ts +214 -0
- package/dist/src/lib/resourcesInterface.d.ts.map +1 -0
- package/dist/src/lib/resourcesInterface.js +3 -0
- package/dist/src/lib/transaction/transaction.d.ts +18 -0
- package/dist/src/lib/transaction/transaction.d.ts.map +1 -0
- package/dist/src/lib/transaction/transaction.js +79 -0
- package/dist/src/lib/transactionBuilder.d.ts +36 -0
- package/dist/src/lib/transactionBuilder.d.ts.map +1 -0
- package/dist/src/lib/transactionBuilder.js +86 -0
- package/dist/src/lib/transactionBuilderFactory.d.ts +12 -0
- package/dist/src/lib/transactionBuilderFactory.d.ts.map +1 -0
- package/dist/src/lib/transactionBuilderFactory.js +20 -0
- package/dist/src/lib/transferBuilder.d.ts +10 -0
- package/dist/src/lib/transferBuilder.d.ts.map +1 -0
- package/dist/src/lib/transferBuilder.js +18 -0
- package/dist/src/lib/utils.d.ts +107 -0
- package/dist/src/lib/utils.d.ts.map +1 -0
- package/dist/src/lib/utils.js +243 -0
- package/dist/src/lib/walletInitBuilder.d.ts +98 -0
- package/dist/src/lib/walletInitBuilder.d.ts.map +1 -0
- package/dist/src/lib/walletInitBuilder.js +223 -0
- package/dist/src/lib/walletInitialization/walletInitTransaction.d.ts +16 -0
- package/dist/src/lib/walletInitialization/walletInitTransaction.d.ts.map +1 -0
- package/dist/src/lib/walletInitialization/walletInitTransaction.js +59 -0
- package/dist/src/register.d.ts +3 -0
- package/dist/src/register.d.ts.map +1 -0
- package/dist/src/register.js +11 -0
- package/dist/src/tcanton.d.ts +17 -0
- package/dist/src/tcanton.d.ts.map +1 -0
- package/dist/src/tcanton.js +30 -0
- package/dist/test/integration/index.d.ts +1 -0
- package/dist/test/integration/index.d.ts.map +1 -0
- package/dist/test/integration/index.js +1 -0
- package/dist/test/resources.d.ts +40 -0
- package/dist/test/resources.d.ts.map +1 -0
- package/dist/test/resources.js +53 -0
- package/dist/test/unit/builder/oneStepEnablement/oneStepEnablementBuilder.d.ts +2 -0
- package/dist/test/unit/builder/oneStepEnablement/oneStepEnablementBuilder.d.ts.map +1 -0
- package/dist/test/unit/builder/oneStepEnablement/oneStepEnablementBuilder.js +67 -0
- package/dist/test/unit/builder/walletInit/walletInitBuilder.d.ts +2 -0
- package/dist/test/unit/builder/walletInit/walletInitBuilder.d.ts.map +1 -0
- package/dist/test/unit/builder/walletInit/walletInitBuilder.js +66 -0
- package/dist/test/unit/index.d.ts +2 -0
- package/dist/test/unit/index.d.ts.map +1 -0
- package/dist/test/unit/index.js +16 -0
- package/dist/test/unit/keyPair.d.ts +2 -0
- package/dist/test/unit/keyPair.d.ts.map +1 -0
- package/dist/test/unit/keyPair.js +72 -0
- package/dist/test/unit/utils.d.ts +2 -0
- package/dist/test/unit/utils.d.ts.map +1 -0
- package/dist/test/unit/utils.js +35 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +72 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
Copyright 2022 BitGo
|
|
180
|
+
|
|
181
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
182
|
+
you may not use this file except in compliance with the License.
|
|
183
|
+
You may obtain a copy of the License at
|
|
184
|
+
|
|
185
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
186
|
+
|
|
187
|
+
Unless required by applicable law or agreed to in writing, software
|
|
188
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
189
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
190
|
+
See the License for the specific language governing permissions and
|
|
191
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# BitGo sdk-coin-canton
|
|
2
|
+
|
|
3
|
+
SDK coins provide a modular approach to a monolithic architecture. This and all BitGoJS SDK coins allow developers to use only the coins needed for a given project.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
All coins are loaded traditionally through the `bitgo` package. If you are using coins individually, you will be accessing the coin via the `@bitgo/sdk-api` package.
|
|
8
|
+
|
|
9
|
+
In your project install both `@bitgo/sdk-api` and `@bitgo/sdk-coin-canton`.
|
|
10
|
+
|
|
11
|
+
```shell
|
|
12
|
+
npm i @bitgo/sdk-api @bitgo/sdk-coin-canton
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Next, you will be able to initialize an instance of "bitgo" through `@bitgo/sdk-api` instead of `bitgo`.
|
|
16
|
+
|
|
17
|
+
```javascript
|
|
18
|
+
import { BitGoAPI } from '@bitgo/sdk-api';
|
|
19
|
+
import { Canton } from '@bitgo/sdk-coin-canton';
|
|
20
|
+
|
|
21
|
+
const sdk = new BitGoAPI();
|
|
22
|
+
|
|
23
|
+
sdk.register('canton', Canton.createInstance);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Development
|
|
27
|
+
|
|
28
|
+
Most of the coin implementations are derived from `@bitgo/sdk-core`, `@bitgo/statics`, and coin specific packages. These implementations are used to interact with the BitGo API and BitGo platform services.
|
|
29
|
+
|
|
30
|
+
You will notice that the basic version of common class extensions have been provided to you and must be resolved before the package build will succeed. Upon initiation of a given SDK coin, you will need to verify that your coin has been included in the root `tsconfig.packages.json` and that the linting, formatting, and testing succeeds when run both within the coin and from the root of BitGoJS.
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
// This is the static helper file from canton SDK, replicating it here since we won't be using the canton wallet SDK
|
|
2
|
+
// TODO: https://bitgoinc.atlassian.net/browse/COIN-6016
|
|
3
|
+
const PREPARED_TRANSACTION_HASH_PURPOSE = Uint8Array.from([0x00, 0x00, 0x00, 0x30]);
|
|
4
|
+
const NODE_ENCODING_VERSION = Uint8Array.from([0x01]);
|
|
5
|
+
const HASHING_SCHEME_VERSION = Uint8Array.from([2]);
|
|
6
|
+
async function sha256(message) {
|
|
7
|
+
const msg = typeof message === 'string' ? new TextEncoder().encode(message) : message;
|
|
8
|
+
return crypto.subtle.digest('SHA-256', new Uint8Array(msg)).then((hash) => new Uint8Array(hash));
|
|
9
|
+
}
|
|
10
|
+
async function mkByteArray(...args) {
|
|
11
|
+
const normalizedArgs = args.map((arg) => {
|
|
12
|
+
if (typeof arg === 'number') {
|
|
13
|
+
return new Uint8Array([arg]);
|
|
14
|
+
} else {
|
|
15
|
+
return arg;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
let totalLength = 0;
|
|
19
|
+
normalizedArgs.forEach((arg) => {
|
|
20
|
+
totalLength += arg.length;
|
|
21
|
+
});
|
|
22
|
+
const mergedArray = new Uint8Array(totalLength);
|
|
23
|
+
let offset = 0;
|
|
24
|
+
normalizedArgs.forEach((arg) => {
|
|
25
|
+
mergedArray.set(arg, offset);
|
|
26
|
+
offset += arg.length;
|
|
27
|
+
});
|
|
28
|
+
return mergedArray;
|
|
29
|
+
}
|
|
30
|
+
async function encodeBool(value) {
|
|
31
|
+
return new Uint8Array([value ? 1 : 0]);
|
|
32
|
+
}
|
|
33
|
+
async function encodeInt32(value) {
|
|
34
|
+
const buffer = new ArrayBuffer(4);
|
|
35
|
+
const view = new DataView(buffer);
|
|
36
|
+
view.setInt32(0, value, false); // true for little-endian
|
|
37
|
+
return new Uint8Array(buffer);
|
|
38
|
+
}
|
|
39
|
+
async function encodeInt64(value) {
|
|
40
|
+
// eslint-disable-next-line no-undef
|
|
41
|
+
const num = typeof value === 'bigint' ? value : BigInt(value || 0);
|
|
42
|
+
const buffer = new ArrayBuffer(8);
|
|
43
|
+
const view = new DataView(buffer);
|
|
44
|
+
view.setBigInt64(0, num, false); // true for little-endian
|
|
45
|
+
return new Uint8Array(buffer);
|
|
46
|
+
}
|
|
47
|
+
async function encodeString(value = '') {
|
|
48
|
+
const utf8Bytes = new TextEncoder().encode(value);
|
|
49
|
+
return encodeBytes(utf8Bytes);
|
|
50
|
+
}
|
|
51
|
+
async function encodeBytes(value) {
|
|
52
|
+
const length = await encodeInt32(value.length);
|
|
53
|
+
return mkByteArray(length, value);
|
|
54
|
+
}
|
|
55
|
+
async function encodeHash(value) {
|
|
56
|
+
return value;
|
|
57
|
+
}
|
|
58
|
+
function encodeHexString(value = '') {
|
|
59
|
+
// Convert hex string to Uint8Array
|
|
60
|
+
const bytes = new Uint8Array(value.length / 2);
|
|
61
|
+
for (let i = 0; i < value.length; i += 2) {
|
|
62
|
+
bytes[i / 2] = parseInt(value.slice(i, i + 2), 16);
|
|
63
|
+
}
|
|
64
|
+
return encodeBytes(bytes);
|
|
65
|
+
}
|
|
66
|
+
// Maybe suspicious?
|
|
67
|
+
async function encodeOptional(value, encodeFn) {
|
|
68
|
+
if (value === undefined || value === null) {
|
|
69
|
+
return new Uint8Array([0]); // Return empty array for undefined fields
|
|
70
|
+
} else {
|
|
71
|
+
return mkByteArray(1, await encodeFn(value));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
// Maybe suspicious?
|
|
75
|
+
async function encodeProtoOptional(parentValue, fieldName, value, encodeFn) {
|
|
76
|
+
if (parentValue && parentValue[fieldName] !== undefined) {
|
|
77
|
+
return mkByteArray(1, await encodeFn(value));
|
|
78
|
+
} else {
|
|
79
|
+
return new Uint8Array([0]); // Return empty array for undefined fields
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async function encodeRepeated(values = [], encodeFn) {
|
|
83
|
+
const length = await encodeInt32(values.length);
|
|
84
|
+
const encodedValues = await Promise.all(values.map(encodeFn));
|
|
85
|
+
return mkByteArray(length, ...encodedValues);
|
|
86
|
+
}
|
|
87
|
+
function findSeed(nodeId, nodeSeeds) {
|
|
88
|
+
const seed = nodeSeeds.find((seed) => seed.nodeId.toString() === nodeId)?.seed;
|
|
89
|
+
return seed;
|
|
90
|
+
}
|
|
91
|
+
async function encodeIdentifier(identifier) {
|
|
92
|
+
return mkByteArray(
|
|
93
|
+
await encodeString(identifier.packageId),
|
|
94
|
+
await encodeRepeated(identifier.moduleName.split('.'), encodeString),
|
|
95
|
+
await encodeRepeated(identifier.entityName.split('.'), encodeString)
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
async function encodeMetadata(metadata) {
|
|
99
|
+
return mkByteArray(
|
|
100
|
+
Uint8Array.from([0x01]),
|
|
101
|
+
await encodeRepeated(metadata.submitterInfo?.actAs, encodeString),
|
|
102
|
+
await encodeString(metadata.submitterInfo?.commandId),
|
|
103
|
+
await encodeString(metadata.transactionUuid),
|
|
104
|
+
await encodeInt32(metadata.mediatorGroup),
|
|
105
|
+
await encodeString(metadata.synchronizerId),
|
|
106
|
+
await encodeProtoOptional(metadata, 'minLedgerEffectiveTime', metadata.minLedgerEffectiveTime, encodeInt64),
|
|
107
|
+
await encodeProtoOptional(metadata, 'maxLedgerEffectiveTime', metadata.maxLedgerEffectiveTime, encodeInt64),
|
|
108
|
+
await encodeInt64(metadata.preparationTime),
|
|
109
|
+
await encodeRepeated(metadata.inputContracts, encodeInputContract)
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
async function encodeCreateNode(create, nodeId, nodeSeeds) {
|
|
113
|
+
return create
|
|
114
|
+
? mkByteArray(
|
|
115
|
+
NODE_ENCODING_VERSION,
|
|
116
|
+
await encodeString(create.lfVersion),
|
|
117
|
+
0 /** Create node tag */,
|
|
118
|
+
await encodeOptional(findSeed(nodeId, nodeSeeds), encodeHash),
|
|
119
|
+
await encodeHexString(create.contractId),
|
|
120
|
+
await encodeString(create.packageName),
|
|
121
|
+
await encodeIdentifier(create.templateId),
|
|
122
|
+
await encodeValue(create.argument),
|
|
123
|
+
await encodeRepeated(create.signatories, encodeString),
|
|
124
|
+
await encodeRepeated(create.stakeholders, encodeString)
|
|
125
|
+
)
|
|
126
|
+
: mkByteArray();
|
|
127
|
+
}
|
|
128
|
+
async function encodeExerciseNode(exercise, nodeId, nodesDict, nodeSeeds) {
|
|
129
|
+
return mkByteArray(
|
|
130
|
+
NODE_ENCODING_VERSION,
|
|
131
|
+
await encodeString(exercise.lfVersion),
|
|
132
|
+
1 /** Exercise node tag */,
|
|
133
|
+
await encodeHash(findSeed(nodeId, nodeSeeds)),
|
|
134
|
+
await encodeHexString(exercise.contractId),
|
|
135
|
+
await encodeString(exercise.packageName),
|
|
136
|
+
await encodeIdentifier(exercise.templateId),
|
|
137
|
+
await encodeRepeated(exercise.signatories, encodeString),
|
|
138
|
+
await encodeRepeated(exercise.stakeholders, encodeString),
|
|
139
|
+
await encodeRepeated(exercise.actingParties, encodeString),
|
|
140
|
+
await encodeProtoOptional(exercise, 'interfaceId', exercise.interfaceId, encodeIdentifier),
|
|
141
|
+
await encodeString(exercise.choiceId),
|
|
142
|
+
await encodeValue(exercise.chosenValue),
|
|
143
|
+
await encodeBool(exercise.consuming),
|
|
144
|
+
await encodeProtoOptional(exercise, 'exerciseResult', exercise.exerciseResult, encodeValue),
|
|
145
|
+
await encodeRepeated(exercise.choiceObservers, encodeString),
|
|
146
|
+
await encodeRepeated(exercise.children, encodeNodeId(nodesDict, nodeSeeds))
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
async function encodeFetchNode(fetch) {
|
|
150
|
+
return mkByteArray(
|
|
151
|
+
NODE_ENCODING_VERSION,
|
|
152
|
+
await encodeString(fetch.lfVersion),
|
|
153
|
+
2 /** Fetch node tag */,
|
|
154
|
+
await encodeHexString(fetch.contractId),
|
|
155
|
+
await encodeString(fetch.packageName),
|
|
156
|
+
await encodeIdentifier(fetch.templateId),
|
|
157
|
+
await encodeRepeated(fetch.signatories, encodeString),
|
|
158
|
+
await encodeRepeated(fetch.stakeholders, encodeString),
|
|
159
|
+
await encodeProtoOptional(fetch, 'interfaceId', fetch.interfaceId, encodeIdentifier),
|
|
160
|
+
await encodeRepeated(fetch.actingParties, encodeString)
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
async function encodeRollbackNode(rollback, nodesDict, nodeSeeds) {
|
|
164
|
+
return mkByteArray(
|
|
165
|
+
NODE_ENCODING_VERSION,
|
|
166
|
+
3 /** Rollback node tag */,
|
|
167
|
+
await encodeRepeated(rollback.children, encodeNodeId(nodesDict, nodeSeeds))
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
async function encodeInputContract(contract) {
|
|
171
|
+
if (contract.contract.oneofKind === 'v1')
|
|
172
|
+
return mkByteArray(
|
|
173
|
+
await encodeInt64(contract.createdAt),
|
|
174
|
+
await sha256(await encodeCreateNode(contract.contract.v1, 'unused_node_id', []))
|
|
175
|
+
);
|
|
176
|
+
else throw new Error('Unsupported contract version');
|
|
177
|
+
}
|
|
178
|
+
async function encodeValue(value) {
|
|
179
|
+
if (value.sum.oneofKind === 'unit') {
|
|
180
|
+
return Uint8Array.from([0]); // Unit value
|
|
181
|
+
} else if (value.sum.oneofKind === 'bool') {
|
|
182
|
+
return mkByteArray(Uint8Array.from([0x01]), await encodeBool(value.sum.bool));
|
|
183
|
+
} else if (value.sum.oneofKind === 'int64') {
|
|
184
|
+
return mkByteArray(Uint8Array.from([0x02]), await encodeInt64(parseInt(value.sum.int64, 10)));
|
|
185
|
+
} else if (value.sum.oneofKind === 'numeric') {
|
|
186
|
+
return mkByteArray(Uint8Array.from([0x03]), await encodeString(value.sum.numeric));
|
|
187
|
+
} else if (value.sum.oneofKind === 'timestamp') {
|
|
188
|
+
// eslint-disable-next-line no-undef
|
|
189
|
+
return mkByteArray(Uint8Array.from([0x04]), await encodeInt64(BigInt(value.sum.timestamp)));
|
|
190
|
+
} else if (value.sum.oneofKind === 'date') {
|
|
191
|
+
return mkByteArray(Uint8Array.from([0x05]), await encodeInt32(value.sum.date));
|
|
192
|
+
} else if (value.sum.oneofKind === 'party') {
|
|
193
|
+
return mkByteArray(Uint8Array.from([0x06]), await encodeString(value.sum.party));
|
|
194
|
+
} else if (value.sum.oneofKind === 'text') {
|
|
195
|
+
return mkByteArray(Uint8Array.from([0x07]), await encodeString(value.sum.text));
|
|
196
|
+
} else if (value.sum.oneofKind === 'contractId') {
|
|
197
|
+
return mkByteArray(Uint8Array.from([0x08]), await encodeHexString(value.sum.contractId));
|
|
198
|
+
} else if (value.sum.oneofKind === 'optional') {
|
|
199
|
+
return mkByteArray(
|
|
200
|
+
Uint8Array.from([0x09]),
|
|
201
|
+
await encodeProtoOptional(value.sum.optional, 'value', value.sum.optional.value, encodeValue)
|
|
202
|
+
);
|
|
203
|
+
} else if (value.sum.oneofKind === 'list') {
|
|
204
|
+
return mkByteArray(Uint8Array.from([0x0a]), await encodeRepeated(value.sum.list.elements, encodeValue));
|
|
205
|
+
} else if (value.sum.oneofKind === 'textMap') {
|
|
206
|
+
return mkByteArray(Uint8Array.from([0x0b]), await encodeRepeated(value.sum.textMap?.entries, encodeTextMapEntry));
|
|
207
|
+
} else if (value.sum.oneofKind === 'record') {
|
|
208
|
+
return mkByteArray(
|
|
209
|
+
Uint8Array.from([0x0c]),
|
|
210
|
+
await encodeProtoOptional(value.sum.record, 'recordId', value.sum.record.recordId, encodeIdentifier),
|
|
211
|
+
await encodeRepeated(value.sum.record.fields, encodeRecordField)
|
|
212
|
+
);
|
|
213
|
+
} else if (value.sum.oneofKind === 'variant') {
|
|
214
|
+
return mkByteArray(
|
|
215
|
+
Uint8Array.from([0x0d]),
|
|
216
|
+
await encodeProtoOptional(value.sum.variant, 'variantId', value.sum.variant.variantId, encodeIdentifier),
|
|
217
|
+
await encodeString(value.sum.variant.constructor),
|
|
218
|
+
await encodeValue(value.sum.variant.value)
|
|
219
|
+
);
|
|
220
|
+
} else if (value.sum.oneofKind === 'enum') {
|
|
221
|
+
return mkByteArray(
|
|
222
|
+
Uint8Array.from([0x0e]),
|
|
223
|
+
await encodeProtoOptional(value.sum.enum, 'enumId', value.sum.enum.enumId, encodeIdentifier),
|
|
224
|
+
await encodeString(value.sum.enum.constructor)
|
|
225
|
+
);
|
|
226
|
+
} else if (value.sum.oneofKind === 'genMap') {
|
|
227
|
+
return mkByteArray(Uint8Array.from([0x0f]), await encodeRepeated(value.sum.genMap?.entries, encodeGenMapEntry));
|
|
228
|
+
}
|
|
229
|
+
throw new Error('Unsupported value type: ' + JSON.stringify(value));
|
|
230
|
+
}
|
|
231
|
+
async function encodeTextMapEntry(entry) {
|
|
232
|
+
return mkByteArray(await encodeString(entry.key), await encodeValue(entry.value));
|
|
233
|
+
}
|
|
234
|
+
async function encodeRecordField(field) {
|
|
235
|
+
return mkByteArray(await encodeOptional(field.label, encodeString), await encodeValue(field.value));
|
|
236
|
+
}
|
|
237
|
+
async function encodeGenMapEntry(entry) {
|
|
238
|
+
return mkByteArray(await encodeValue(entry.key), await encodeValue(entry.value));
|
|
239
|
+
}
|
|
240
|
+
function encodeNodeId(nodesDict, nodeSeeds) {
|
|
241
|
+
return async (nodeId) => {
|
|
242
|
+
const node = nodesDict[nodeId];
|
|
243
|
+
if (!node) {
|
|
244
|
+
throw new Error(`Node with ID ${nodeId} not found in transaction`);
|
|
245
|
+
}
|
|
246
|
+
const encodedNode = await encodeNode(node, nodesDict, nodeSeeds);
|
|
247
|
+
return sha256(encodedNode);
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
async function encodeNode(node, nodesDict, nodeSeeds) {
|
|
251
|
+
if (node.versionedNode.oneofKind === 'v1') {
|
|
252
|
+
if (node.versionedNode.v1.nodeType.oneofKind === 'create') {
|
|
253
|
+
return encodeCreateNode(node.versionedNode.v1.nodeType.create, node.nodeId, nodeSeeds);
|
|
254
|
+
} else if (node.versionedNode.v1.nodeType.oneofKind === 'exercise') {
|
|
255
|
+
return encodeExerciseNode(node.versionedNode.v1.nodeType.exercise, node.nodeId, nodesDict, nodeSeeds);
|
|
256
|
+
} else if (node.versionedNode.v1.nodeType.oneofKind === 'fetch') {
|
|
257
|
+
return encodeFetchNode(node.versionedNode.v1.nodeType.fetch);
|
|
258
|
+
} else if (node.versionedNode.v1.nodeType.oneofKind === 'rollback') {
|
|
259
|
+
return encodeRollbackNode(node.versionedNode.v1.nodeType.rollback, nodesDict, nodeSeeds);
|
|
260
|
+
}
|
|
261
|
+
throw new Error('Unsupported node type');
|
|
262
|
+
} else {
|
|
263
|
+
throw new Error(`Unsupported node version`);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
function createNodesDict(preparedTransaction) {
|
|
267
|
+
const nodesDict = {};
|
|
268
|
+
const nodes = preparedTransaction.transaction?.nodes || [];
|
|
269
|
+
for (const node of nodes) {
|
|
270
|
+
nodesDict[node.nodeId] = node;
|
|
271
|
+
}
|
|
272
|
+
return nodesDict;
|
|
273
|
+
}
|
|
274
|
+
async function encodeTransaction(transaction, nodesDict, nodeSeeds) {
|
|
275
|
+
return mkByteArray(
|
|
276
|
+
await encodeString(transaction.version),
|
|
277
|
+
await encodeRepeated(transaction.roots, encodeNodeId(nodesDict, nodeSeeds))
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
async function hashTransaction(transaction, nodesDict) {
|
|
281
|
+
const encodedTransaction = await encodeTransaction(transaction, nodesDict, transaction.nodeSeeds);
|
|
282
|
+
const hash = await sha256(await mkByteArray(PREPARED_TRANSACTION_HASH_PURPOSE, encodedTransaction));
|
|
283
|
+
return hash;
|
|
284
|
+
}
|
|
285
|
+
async function hashMetadata(metadata) {
|
|
286
|
+
const hash = await sha256(await mkByteArray(PREPARED_TRANSACTION_HASH_PURPOSE, await encodeMetadata(metadata)));
|
|
287
|
+
return hash;
|
|
288
|
+
}
|
|
289
|
+
async function encodePreparedTransaction(preparedTransaction) {
|
|
290
|
+
const nodesDict = createNodesDict(preparedTransaction);
|
|
291
|
+
const transactionHash = await hashTransaction(preparedTransaction.transaction, nodesDict);
|
|
292
|
+
const metadataHash = await hashMetadata(preparedTransaction.metadata);
|
|
293
|
+
return mkByteArray(PREPARED_TRANSACTION_HASH_PURPOSE, HASHING_SCHEME_VERSION, transactionHash, metadataHash);
|
|
294
|
+
}
|
|
295
|
+
export async function computePreparedTransaction(preparedTransaction) {
|
|
296
|
+
return sha256(await encodePreparedTransaction(preparedTransaction));
|
|
297
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { MessageType, UnknownFieldHandler, reflectionMergePartial, WireType } from '@protobuf-ts/runtime';
|
|
2
|
+
import { DamlTransaction_Node } from './damlTransactionNode.js';
|
|
3
|
+
import { DamlTransaction_NodeSeed } from './damlTransactionNodeSeed.js';
|
|
4
|
+
|
|
5
|
+
class DamlTransaction$Type extends MessageType {
|
|
6
|
+
constructor() {
|
|
7
|
+
super('com.daml.ledger.api.v2.interactive.DamlTransaction', [
|
|
8
|
+
{
|
|
9
|
+
no: 1,
|
|
10
|
+
name: 'version',
|
|
11
|
+
kind: 'scalar',
|
|
12
|
+
T: 9 /*ScalarType.STRING*/,
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
no: 2,
|
|
16
|
+
name: 'roots',
|
|
17
|
+
kind: 'scalar',
|
|
18
|
+
repeat: 2 /*RepeatType.UNPACKED*/,
|
|
19
|
+
T: 9 /*ScalarType.STRING*/,
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
no: 3,
|
|
23
|
+
name: 'nodes',
|
|
24
|
+
kind: 'message',
|
|
25
|
+
repeat: 2 /*RepeatType.UNPACKED*/,
|
|
26
|
+
T: () => DamlTransaction_Node,
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
no: 4,
|
|
30
|
+
name: 'node_seeds',
|
|
31
|
+
kind: 'message',
|
|
32
|
+
repeat: 2 /*RepeatType.UNPACKED*/,
|
|
33
|
+
T: () => DamlTransaction_NodeSeed,
|
|
34
|
+
},
|
|
35
|
+
]);
|
|
36
|
+
}
|
|
37
|
+
create(value) {
|
|
38
|
+
const message = Object.create(this.messagePrototype);
|
|
39
|
+
message.version = '';
|
|
40
|
+
message.roots = [];
|
|
41
|
+
message.nodes = [];
|
|
42
|
+
message.nodeSeeds = [];
|
|
43
|
+
if (value !== undefined) reflectionMergePartial(this, message, value);
|
|
44
|
+
return message;
|
|
45
|
+
}
|
|
46
|
+
internalBinaryRead(reader, length, options, target) {
|
|
47
|
+
const message = target ?? this.create(),
|
|
48
|
+
end = reader.pos + length;
|
|
49
|
+
while (reader.pos < end) {
|
|
50
|
+
const [fieldNo, wireType] = reader.tag();
|
|
51
|
+
switch (fieldNo) {
|
|
52
|
+
case /* string version */ 1:
|
|
53
|
+
message.version = reader.string();
|
|
54
|
+
break;
|
|
55
|
+
case /* repeated string roots */ 2:
|
|
56
|
+
message.roots.push(reader.string());
|
|
57
|
+
break;
|
|
58
|
+
case /* repeated com.daml.ledger.api.v2.interactive.DamlTransaction.Node nodes */ 3:
|
|
59
|
+
message.nodes.push(DamlTransaction_Node.internalBinaryRead(reader, reader.uint32(), options));
|
|
60
|
+
break;
|
|
61
|
+
case /* repeated com.daml.ledger.api.v2.interactive.DamlTransaction.NodeSeed node_seeds */ 4:
|
|
62
|
+
message.nodeSeeds.push(DamlTransaction_NodeSeed.internalBinaryRead(reader, reader.uint32(), options));
|
|
63
|
+
break;
|
|
64
|
+
default:
|
|
65
|
+
const u = options.readUnknownField;
|
|
66
|
+
if (u === 'throw') throw new Error(`Unknown field ${fieldNo} (wire type ${wireType}) for ${this.typeName}`);
|
|
67
|
+
const d = reader.skip(wireType);
|
|
68
|
+
if (u !== false) (u === true ? UnknownFieldHandler.onRead : u)(this.typeName, message, fieldNo, wireType, d);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return message;
|
|
72
|
+
}
|
|
73
|
+
internalBinaryWrite(message, writer, options) {
|
|
74
|
+
/* string version = 1; */
|
|
75
|
+
if (message.version !== '') writer.tag(1, WireType.LengthDelimited).string(message.version);
|
|
76
|
+
/* repeated string roots = 2; */
|
|
77
|
+
for (let i = 0; i < message.roots.length; i++) writer.tag(2, WireType.LengthDelimited).string(message.roots[i]);
|
|
78
|
+
/* repeated com.daml.ledger.api.v2.interactive.DamlTransaction.Node nodes = 3; */
|
|
79
|
+
for (let i = 0; i < message.nodes.length; i++)
|
|
80
|
+
DamlTransaction_Node.internalBinaryWrite(
|
|
81
|
+
message.nodes[i],
|
|
82
|
+
writer.tag(3, WireType.LengthDelimited).fork(),
|
|
83
|
+
options
|
|
84
|
+
).join();
|
|
85
|
+
/* repeated com.daml.ledger.api.v2.interactive.DamlTransaction.NodeSeed node_seeds = 4; */
|
|
86
|
+
for (let i = 0; i < message.nodeSeeds.length; i++)
|
|
87
|
+
DamlTransaction_NodeSeed.internalBinaryWrite(
|
|
88
|
+
message.nodeSeeds[i],
|
|
89
|
+
writer.tag(4, WireType.LengthDelimited).fork(),
|
|
90
|
+
options
|
|
91
|
+
).join();
|
|
92
|
+
const u = options.writeUnknownFields;
|
|
93
|
+
if (u !== false) (u === true ? UnknownFieldHandler.onWrite : u)(this.typeName, message, writer);
|
|
94
|
+
return writer;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* @generated MessageType for protobuf message com.daml.ledger.api.v2.interactive.DamlTransaction
|
|
99
|
+
*/
|
|
100
|
+
export const DamlTransaction = new DamlTransaction$Type();
|