@bitgo-beta/deser-lib 1.0.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/.eslintignore +5 -0
- package/.mocharc.json +9 -0
- package/.prettierignore +2 -0
- package/.prettierrc.yml +3 -0
- package/LICENSE +191 -0
- package/README.md +3 -0
- package/package.json +46 -0
- package/src/cbor.ts +216 -0
- package/src/index.ts +1 -0
- package/test/cbor/fixtures.json +96 -0
- package/test/unit/deser-lib.ts +326 -0
- package/tsconfig.json +8 -0
package/.mocharc.json
ADDED
package/.prettierignore
ADDED
package/.prettierrc.yml
ADDED
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
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bitgo-beta/deser-lib",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "BitGo serialization and deseralization library",
|
|
5
|
+
"main": "./dist/src/index.js",
|
|
6
|
+
"types": "./dist/src/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "yarn unit-test",
|
|
9
|
+
"unit-test": "nyc -- mocha --recursive test",
|
|
10
|
+
"build": "yarn tsc --build --incremental --verbose .",
|
|
11
|
+
"fmt": "prettier --write .",
|
|
12
|
+
"check-fmt": "prettier --check .",
|
|
13
|
+
"clean": "rm -r ./dist",
|
|
14
|
+
"lint": "eslint --quiet .",
|
|
15
|
+
"prepare": "npm run build"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/BitGo/BitGoJS.git",
|
|
20
|
+
"directory": "modules/deser-lib"
|
|
21
|
+
},
|
|
22
|
+
"author": "BitGo SDK Team <sdkteam@bitgo.com>",
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/bitgo/bitgojs/issues"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/bitgo/bitgojs#readme",
|
|
28
|
+
"nyc": {
|
|
29
|
+
"extension": [
|
|
30
|
+
".ts"
|
|
31
|
+
]
|
|
32
|
+
},
|
|
33
|
+
"lint-staged": {
|
|
34
|
+
"*.{js,ts}": [
|
|
35
|
+
"yarn prettier --write",
|
|
36
|
+
"yarn eslint --fix"
|
|
37
|
+
]
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"cbor": "^9.0.1"
|
|
44
|
+
},
|
|
45
|
+
"gitHead": "689fe50b412b5de986ab341fe9418d1b8b9eebbb"
|
|
46
|
+
}
|
package/src/cbor.ts
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { decodeFirstSync, encodeCanonical } from 'cbor';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Return a string describing value as a type.
|
|
5
|
+
* @param value - Any javascript value to type.
|
|
6
|
+
* @returns String describing value type.
|
|
7
|
+
*/
|
|
8
|
+
function getType(value: unknown): string {
|
|
9
|
+
if (value === null || value === undefined) {
|
|
10
|
+
return 'null';
|
|
11
|
+
}
|
|
12
|
+
if (value instanceof Array) {
|
|
13
|
+
const types = value.map(getType);
|
|
14
|
+
if (!types.slice(1).every((value) => value === types[0])) {
|
|
15
|
+
throw new Error('Array elements are not of the same type');
|
|
16
|
+
}
|
|
17
|
+
return JSON.stringify([types[0]]);
|
|
18
|
+
}
|
|
19
|
+
if (value instanceof Object) {
|
|
20
|
+
const properties = Object.getOwnPropertyNames(value);
|
|
21
|
+
properties.sort();
|
|
22
|
+
return JSON.stringify(
|
|
23
|
+
properties.reduce((acc, name) => {
|
|
24
|
+
acc[name] = getType(value[name]);
|
|
25
|
+
return acc;
|
|
26
|
+
}, {})
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
if (typeof value === 'string') {
|
|
30
|
+
if (value.startsWith('0x')) {
|
|
31
|
+
return 'bytes';
|
|
32
|
+
}
|
|
33
|
+
return 'string';
|
|
34
|
+
}
|
|
35
|
+
return JSON.stringify(typeof value);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Compare two buffers for sorting.
|
|
40
|
+
* @param a - left buffer to compare to right buffer.
|
|
41
|
+
* @param b - right buffer to compare to left buffer.
|
|
42
|
+
* @returns Negative if a < b, positive if b > a, 0 if equal.
|
|
43
|
+
*/
|
|
44
|
+
function bufferCompare(a: Buffer, b: Buffer): number {
|
|
45
|
+
let i = 0;
|
|
46
|
+
while (i < a.length && i < b.length && a[i] == b[i]) {
|
|
47
|
+
i++;
|
|
48
|
+
}
|
|
49
|
+
if (i === a.length && i === b.length) {
|
|
50
|
+
return 0;
|
|
51
|
+
}
|
|
52
|
+
if (i === a.length || i === b.length) {
|
|
53
|
+
return a.length - b.length;
|
|
54
|
+
}
|
|
55
|
+
return a[i] - b[i];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** A sortable array element. */
|
|
59
|
+
type Sortable = {
|
|
60
|
+
weight: number;
|
|
61
|
+
value?: unknown;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Type check for sortable array element.
|
|
66
|
+
* @param value - Value to type check.
|
|
67
|
+
* @returns True if value is a sortable array element.
|
|
68
|
+
*/
|
|
69
|
+
function isSortable(value: unknown): value is Sortable {
|
|
70
|
+
return value instanceof Object && 'weight' in value && typeof (value as Sortable).weight === 'number';
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Convert number to base 256 and return as a big-endian Buffer.
|
|
75
|
+
* @param value - Value to convert.
|
|
76
|
+
* @returns Buffer representation of the number.
|
|
77
|
+
*/
|
|
78
|
+
function numberToBufferBE(value: number): Buffer {
|
|
79
|
+
// Normalize value so that negative numbers aren't compared higher
|
|
80
|
+
// than positive numbers when accounting for two's complement.
|
|
81
|
+
value += Math.pow(2, 52);
|
|
82
|
+
const byteCount = Math.floor((value.toString(2).length + 7) / 8);
|
|
83
|
+
const buffer = Buffer.alloc(byteCount);
|
|
84
|
+
let i = 0;
|
|
85
|
+
while (value) {
|
|
86
|
+
buffer[i++] = value % 256;
|
|
87
|
+
value = Math.floor(value / 256);
|
|
88
|
+
}
|
|
89
|
+
return buffer.reverse();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Compare two array elements for sorting.
|
|
94
|
+
* @param a - left element to compare to right element.
|
|
95
|
+
* @param b - right element to compare to left element.
|
|
96
|
+
* @returns Negative if a < b, positive if b > a, 0 if equal.
|
|
97
|
+
*/
|
|
98
|
+
function elementCompare(a: unknown, b: unknown): number {
|
|
99
|
+
if (!isSortable(a) || !isSortable(b)) {
|
|
100
|
+
throw new Error('Array elements must be sortable');
|
|
101
|
+
}
|
|
102
|
+
if (a.weight === b.weight) {
|
|
103
|
+
if (a.value === undefined && b.value === undefined) {
|
|
104
|
+
throw new Error('Array elements must be sortable');
|
|
105
|
+
}
|
|
106
|
+
const aVal = transform(a.value);
|
|
107
|
+
const bVal = transform(b.value);
|
|
108
|
+
if (
|
|
109
|
+
(!Buffer.isBuffer(aVal) && typeof aVal !== 'string' && typeof aVal !== 'number') ||
|
|
110
|
+
(!Buffer.isBuffer(bVal) && typeof bVal !== 'string' && typeof bVal !== 'number')
|
|
111
|
+
) {
|
|
112
|
+
throw new Error('Array element value cannot be compared');
|
|
113
|
+
}
|
|
114
|
+
let aBuf, bBuf;
|
|
115
|
+
if (typeof aVal === 'number') {
|
|
116
|
+
aBuf = numberToBufferBE(aVal);
|
|
117
|
+
} else {
|
|
118
|
+
aBuf = Buffer.from(aVal);
|
|
119
|
+
}
|
|
120
|
+
if (typeof bVal === 'number') {
|
|
121
|
+
bBuf = numberToBufferBE(bVal);
|
|
122
|
+
} else {
|
|
123
|
+
bBuf = Buffer.from(bVal);
|
|
124
|
+
}
|
|
125
|
+
return bufferCompare(aBuf, bBuf);
|
|
126
|
+
}
|
|
127
|
+
return a.weight - b.weight;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Transform value into its canonical, serializable form.
|
|
132
|
+
* @param value - Value to transform.
|
|
133
|
+
* @returns Canonical, serializable form of value.
|
|
134
|
+
*/
|
|
135
|
+
export function transform<T>(value: T): T | Buffer {
|
|
136
|
+
if (value === null || value === undefined) {
|
|
137
|
+
return value;
|
|
138
|
+
}
|
|
139
|
+
if (typeof value === 'string') {
|
|
140
|
+
// Transform hex strings to buffers.
|
|
141
|
+
if (value.startsWith('0x')) {
|
|
142
|
+
if (!value.match(/^0x([0-9a-fA-F]{2})*$/)) {
|
|
143
|
+
throw new Error('0x prefixed string contains non-hex characters.');
|
|
144
|
+
}
|
|
145
|
+
return Buffer.from(value.slice(2), 'hex');
|
|
146
|
+
} else if (value.startsWith('\\0x')) {
|
|
147
|
+
return value.slice(1) as unknown as T;
|
|
148
|
+
}
|
|
149
|
+
} else if (value instanceof Array) {
|
|
150
|
+
// Enforce array elements are same type.
|
|
151
|
+
getType(value);
|
|
152
|
+
value = [...value] as unknown as T;
|
|
153
|
+
(value as unknown as Array<unknown>).sort(elementCompare);
|
|
154
|
+
return (value as unknown as Array<unknown>).map(transform) as unknown as T;
|
|
155
|
+
} else if (value instanceof Object) {
|
|
156
|
+
const properties = Object.getOwnPropertyNames(value);
|
|
157
|
+
properties.sort();
|
|
158
|
+
return properties.reduce((acc, name) => {
|
|
159
|
+
acc[name] = transform(value[name]);
|
|
160
|
+
return acc;
|
|
161
|
+
}, {}) as unknown as T;
|
|
162
|
+
}
|
|
163
|
+
return value;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Untransform value into its human readable form.
|
|
168
|
+
* @param value - Value to untransform.
|
|
169
|
+
* @returns Untransformed, human readable form of value.
|
|
170
|
+
*/
|
|
171
|
+
export function untransform<T>(value: T): T | string {
|
|
172
|
+
if (Buffer.isBuffer(value)) {
|
|
173
|
+
return '0x' + value.toString('hex');
|
|
174
|
+
} else if (typeof value === 'string') {
|
|
175
|
+
if (value.startsWith('0x')) {
|
|
176
|
+
return '\\' + value;
|
|
177
|
+
}
|
|
178
|
+
} else if (value instanceof Array && value.length > 1) {
|
|
179
|
+
for (let i = 1; i < value.length; i++) {
|
|
180
|
+
if (value[i - 1].weight > value[i].weight) {
|
|
181
|
+
throw new Error('Array elements are not in canonical order');
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return value.map(untransform) as unknown as T;
|
|
185
|
+
} else if (value instanceof Object) {
|
|
186
|
+
const properties = Object.getOwnPropertyNames(value);
|
|
187
|
+
for (let i = 1; i < properties.length; i++) {
|
|
188
|
+
if (properties[i - 1].localeCompare(properties[i]) > 0) {
|
|
189
|
+
throw new Error('Object properties are not in caonical order');
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return properties.reduce((acc, name) => {
|
|
193
|
+
acc[name] = untransform(value[name]);
|
|
194
|
+
return acc;
|
|
195
|
+
}, {}) as unknown as T;
|
|
196
|
+
}
|
|
197
|
+
return value;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Serialize a value.
|
|
202
|
+
* @param value - Value to serialize.
|
|
203
|
+
* @returns Buffer representing serialized value.
|
|
204
|
+
*/
|
|
205
|
+
export function serialize<T>(value: T): Buffer {
|
|
206
|
+
return encodeCanonical(transform(value));
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Deserialize a value.
|
|
211
|
+
* @param value - Buffer to deserialize.
|
|
212
|
+
* @returns Deserialized value.
|
|
213
|
+
*/
|
|
214
|
+
export function deserialize(value: Buffer): unknown {
|
|
215
|
+
return untransform(decodeFirstSync(value));
|
|
216
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as Cbor from './cbor';
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"deserialized": {
|
|
4
|
+
"keys": [
|
|
5
|
+
{
|
|
6
|
+
"key": "0x010203",
|
|
7
|
+
"weight": 0
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
"key": "0x040506",
|
|
11
|
+
"weight": 1
|
|
12
|
+
}
|
|
13
|
+
]
|
|
14
|
+
},
|
|
15
|
+
"serialized": "a1646b65797382a2636b6579430102036677656967687400a2636b6579430405066677656967687401"
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"deserialized": {
|
|
19
|
+
"a": "0xffffffff",
|
|
20
|
+
"b": "0x00000000",
|
|
21
|
+
"c": "0xffffffff",
|
|
22
|
+
"d": [
|
|
23
|
+
{
|
|
24
|
+
"weight": 0
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
"weight": 1
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
"weight": 2
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
"weight": 3
|
|
34
|
+
}
|
|
35
|
+
]
|
|
36
|
+
},
|
|
37
|
+
"serialized": "a4616144ffffffff61624400000000616344ffffffff616484a16677656967687400a16677656967687401a16677656967687402a16677656967687403"
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"deserialized": {
|
|
41
|
+
"a": [
|
|
42
|
+
{
|
|
43
|
+
"value": "a",
|
|
44
|
+
"weight": 0
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
"value": "b",
|
|
48
|
+
"weight": 0
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
"value": "c",
|
|
52
|
+
"weight": 0
|
|
53
|
+
}
|
|
54
|
+
]
|
|
55
|
+
},
|
|
56
|
+
"serialized": "a1616183a26576616c756561616677656967687400a26576616c756561626677656967687400a26576616c756561636677656967687400"
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
"deserialized": {
|
|
60
|
+
"a": [
|
|
61
|
+
{
|
|
62
|
+
"value": "0x0a",
|
|
63
|
+
"weight": 0
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
"value": "0x0b",
|
|
67
|
+
"weight": 0
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
"value": "0x0c",
|
|
71
|
+
"weight": 0
|
|
72
|
+
}
|
|
73
|
+
]
|
|
74
|
+
},
|
|
75
|
+
"serialized": "a1616183a26576616c7565410a6677656967687400a26576616c7565410b6677656967687400a26576616c7565410c6677656967687400"
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
"deserialized": {
|
|
79
|
+
"a": [
|
|
80
|
+
{
|
|
81
|
+
"value": 1,
|
|
82
|
+
"weight": 0
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
"value": 2,
|
|
86
|
+
"weight": 0
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
"value": 3,
|
|
90
|
+
"weight": 0
|
|
91
|
+
}
|
|
92
|
+
]
|
|
93
|
+
},
|
|
94
|
+
"serialized": "a1616183a26576616c7565016677656967687400a26576616c7565026677656967687400a26576616c7565036677656967687400"
|
|
95
|
+
}
|
|
96
|
+
]
|
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
import { Cbor } from '../..';
|
|
2
|
+
import * as cborFixtures from '../cbor/fixtures.json';
|
|
3
|
+
|
|
4
|
+
describe('deser-lib', function () {
|
|
5
|
+
describe('cbor', function () {
|
|
6
|
+
describe('transform', function () {
|
|
7
|
+
it('orders object properties canonically', function () {
|
|
8
|
+
const res = Cbor.transform({ b: 'second', a: 'first' }) as any;
|
|
9
|
+
const properties = Object.getOwnPropertyNames(res);
|
|
10
|
+
properties[0].should.equal('a');
|
|
11
|
+
properties[1].should.equal('b');
|
|
12
|
+
res.a.should.equal('first');
|
|
13
|
+
res.b.should.equal('second');
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
describe('canonical ordering', function () {
|
|
17
|
+
it('orders by weight', function () {
|
|
18
|
+
const res = Cbor.transform([
|
|
19
|
+
{ weight: 2, value: null },
|
|
20
|
+
{ weight: 1, value: null },
|
|
21
|
+
]) as any;
|
|
22
|
+
res[0].weight.should.equal(1);
|
|
23
|
+
res[1].weight.should.equal(2);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('groups equal elements', function () {
|
|
27
|
+
const res = Cbor.transform([
|
|
28
|
+
{
|
|
29
|
+
weight: 2,
|
|
30
|
+
value: 'b',
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
weight: 1,
|
|
34
|
+
value: 'a',
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
weight: 3,
|
|
38
|
+
value: 'c',
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
weight: 2,
|
|
42
|
+
value: 'b',
|
|
43
|
+
},
|
|
44
|
+
]) as any;
|
|
45
|
+
res[0].weight.should.equal(1);
|
|
46
|
+
res[1].weight.should.equal(2);
|
|
47
|
+
res[2].weight.should.equal(2);
|
|
48
|
+
res[3].weight.should.equal(3);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('orders number values', function () {
|
|
52
|
+
const res = Cbor.transform([
|
|
53
|
+
{ weight: 1, value: 2 },
|
|
54
|
+
{ weight: 1, value: 1 },
|
|
55
|
+
]) as any;
|
|
56
|
+
res[0].value.should.equal(1);
|
|
57
|
+
res[1].value.should.equal(2);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('orders string values', function () {
|
|
61
|
+
const res = Cbor.transform([
|
|
62
|
+
{ weight: 1, value: 'ab' },
|
|
63
|
+
{ weight: 1, value: 'aa' },
|
|
64
|
+
]) as any;
|
|
65
|
+
res[0].value.should.equal('aa');
|
|
66
|
+
res[1].value.should.equal('ab');
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('orders byte values', function () {
|
|
70
|
+
const res = Cbor.transform([
|
|
71
|
+
{ weight: 1, value: '0x0b' },
|
|
72
|
+
{ weight: 1, value: '0x0a' },
|
|
73
|
+
]) as any;
|
|
74
|
+
res[0].value.equals(Buffer.from([0x0a])).should.equal(true);
|
|
75
|
+
res[1].value.equals(Buffer.from([0x0b])).should.equal(true);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('orders string values of different lengths', function () {
|
|
79
|
+
const res = Cbor.transform([
|
|
80
|
+
{ weight: 1, value: 'ab' },
|
|
81
|
+
{ weight: 1, value: 'a' },
|
|
82
|
+
]) as any;
|
|
83
|
+
res[0].value.should.equal('a');
|
|
84
|
+
res[1].value.should.equal('ab');
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('throws for elements without weight', function () {
|
|
88
|
+
(() => Cbor.transform([{}, {}])).should.throw();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('throws for elements without value', function () {
|
|
92
|
+
(() => Cbor.transform([{ weight: 1 }, { weight: 1 }])).should.throw();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('throws for values that cannot be compared', function () {
|
|
96
|
+
(() =>
|
|
97
|
+
Cbor.transform([
|
|
98
|
+
{ weight: 1, value: {} },
|
|
99
|
+
{ weight: 1, value: 1 },
|
|
100
|
+
])).should.throw();
|
|
101
|
+
(() =>
|
|
102
|
+
Cbor.transform([
|
|
103
|
+
{ weight: 1, value: undefined },
|
|
104
|
+
{ weight: 1, value: null },
|
|
105
|
+
])).should.throw();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('throws for elements of mixed type', function () {
|
|
109
|
+
(() =>
|
|
110
|
+
Cbor.transform([
|
|
111
|
+
{ weight: 0, value: '0' },
|
|
112
|
+
{ weight: 0, value: 0 },
|
|
113
|
+
])).should.throw();
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('preserves null values', function () {
|
|
118
|
+
const res = Cbor.transform({ value: null }) as any;
|
|
119
|
+
res.should.have.property('value').which.is.null();
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('replaces prefixed hex strings with Buffers', function () {
|
|
123
|
+
const hex = '00010203';
|
|
124
|
+
const res = Cbor.transform({ value: '0x' + hex }) as any;
|
|
125
|
+
Buffer.isBuffer(res.value).should.equal(true);
|
|
126
|
+
res.value.equals(Buffer.from(hex, 'hex')).should.equal(true);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('preserves non-prefixed hex strings', function () {
|
|
130
|
+
const string = '00010203';
|
|
131
|
+
const res = Cbor.transform({ value: string }) as any;
|
|
132
|
+
res.value.should.equal(string);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('preserves escaped strings', function () {
|
|
136
|
+
const string = '0xPrefixedString';
|
|
137
|
+
const res = Cbor.transform({ value: '\\' + string }) as any;
|
|
138
|
+
res.value.should.equal(string);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('transforms object recursively', function () {
|
|
142
|
+
const res = Cbor.transform({ value: { b: 'second', a: 'first' } }) as any;
|
|
143
|
+
const properties = Object.getOwnPropertyNames(res.value);
|
|
144
|
+
properties[0].should.equal('a');
|
|
145
|
+
properties[1].should.equal('b');
|
|
146
|
+
res.value.a.should.equal('first');
|
|
147
|
+
res.value.b.should.equal('second');
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('transforms array recursively', function () {
|
|
151
|
+
const res = Cbor.transform([{ weight: 0, value: { b: 'second', a: 'first' } }]) as any;
|
|
152
|
+
const properties = Object.getOwnPropertyNames(res[0].value);
|
|
153
|
+
properties[0].should.equal('a');
|
|
154
|
+
properties[1].should.equal('b');
|
|
155
|
+
res[0].value.a.should.equal('first');
|
|
156
|
+
res[0].value.b.should.equal('second');
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('throws for invalid hex strings', function () {
|
|
160
|
+
(() => Cbor.transform('0x0g')).should.throw();
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
describe('untransform', function () {
|
|
165
|
+
it('untransforms object', function () {
|
|
166
|
+
const res = Cbor.untransform({ a: 'first', b: 'second' }) as any;
|
|
167
|
+
const properties = Object.getOwnPropertyNames(res);
|
|
168
|
+
properties[0].should.equal('a');
|
|
169
|
+
properties[1].should.equal('b');
|
|
170
|
+
res.a.should.equal('first');
|
|
171
|
+
res.b.should.equal('second');
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it('enforces canonical object property order', function () {
|
|
175
|
+
(() => Cbor.untransform({ b: 'second', a: 'first' })).should.throw();
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('enforces canonical array element order', function () {
|
|
179
|
+
(() => Cbor.untransform([{ weight: 2 }, { weight: 1 }])).should.throw();
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('replaces Buffers with prefixed hex strings', function () {
|
|
183
|
+
const hex = '00010203';
|
|
184
|
+
const res = Cbor.untransform({ value: Buffer.from(hex, 'hex') }) as any;
|
|
185
|
+
res.value.should.equal('0x' + hex);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('preserves non-prefixed hex strings', function () {
|
|
189
|
+
const string = '00010203';
|
|
190
|
+
const res = Cbor.untransform({ value: string }) as any;
|
|
191
|
+
res.value.should.equal(string);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it('escapes prefixed string', function () {
|
|
195
|
+
const string = '0xPrefixedString';
|
|
196
|
+
const res = Cbor.untransform({ value: string }) as any;
|
|
197
|
+
res.value.should.equal('\\' + string);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('untransforms object recursively', function () {
|
|
201
|
+
const hex = '00010203';
|
|
202
|
+
const res = Cbor.untransform({ value: { value: Buffer.from(hex, 'hex') } }) as any;
|
|
203
|
+
res.value.value.should.equal('0x' + hex);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('untransforms array recursively', function () {
|
|
207
|
+
const hex = '00010203';
|
|
208
|
+
const res = Cbor.untransform([{ value: Buffer.from(hex, 'hex'), weight: 0 }]) as any;
|
|
209
|
+
res[0].value.should.equal('0x' + hex);
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
describe('fixtures', function () {
|
|
214
|
+
xit('creates test vectors', function () {
|
|
215
|
+
const { writeFileSync } = require('fs');
|
|
216
|
+
const deserialized = [
|
|
217
|
+
{
|
|
218
|
+
keys: [
|
|
219
|
+
{
|
|
220
|
+
key: '0x010203',
|
|
221
|
+
weight: 0,
|
|
222
|
+
},
|
|
223
|
+
{
|
|
224
|
+
key: '0x040506',
|
|
225
|
+
weight: 1,
|
|
226
|
+
},
|
|
227
|
+
],
|
|
228
|
+
},
|
|
229
|
+
{
|
|
230
|
+
a: '0xffffffff',
|
|
231
|
+
b: '0x00000000',
|
|
232
|
+
c: '0xffffffff',
|
|
233
|
+
d: [
|
|
234
|
+
{
|
|
235
|
+
weight: 0,
|
|
236
|
+
},
|
|
237
|
+
{
|
|
238
|
+
weight: 1,
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
weight: 2,
|
|
242
|
+
},
|
|
243
|
+
{
|
|
244
|
+
weight: 3,
|
|
245
|
+
},
|
|
246
|
+
],
|
|
247
|
+
},
|
|
248
|
+
{
|
|
249
|
+
a: [
|
|
250
|
+
{
|
|
251
|
+
value: 'a',
|
|
252
|
+
weight: 0,
|
|
253
|
+
},
|
|
254
|
+
{
|
|
255
|
+
value: 'b',
|
|
256
|
+
weight: 0,
|
|
257
|
+
},
|
|
258
|
+
{
|
|
259
|
+
value: 'c',
|
|
260
|
+
weight: 0,
|
|
261
|
+
},
|
|
262
|
+
],
|
|
263
|
+
},
|
|
264
|
+
{
|
|
265
|
+
a: [
|
|
266
|
+
{
|
|
267
|
+
weight: 0,
|
|
268
|
+
value: '0x0a',
|
|
269
|
+
},
|
|
270
|
+
{
|
|
271
|
+
weight: 0,
|
|
272
|
+
value: '0x0b',
|
|
273
|
+
},
|
|
274
|
+
{
|
|
275
|
+
weight: 0,
|
|
276
|
+
value: '0x0c',
|
|
277
|
+
},
|
|
278
|
+
],
|
|
279
|
+
},
|
|
280
|
+
{
|
|
281
|
+
a: [
|
|
282
|
+
{
|
|
283
|
+
weight: 0,
|
|
284
|
+
value: 1,
|
|
285
|
+
},
|
|
286
|
+
{
|
|
287
|
+
weight: 0,
|
|
288
|
+
value: 2,
|
|
289
|
+
},
|
|
290
|
+
{
|
|
291
|
+
weight: 0,
|
|
292
|
+
value: 3,
|
|
293
|
+
},
|
|
294
|
+
],
|
|
295
|
+
},
|
|
296
|
+
];
|
|
297
|
+
const serialized = deserialized.map((x) => Cbor.serialize(x).toString('hex'));
|
|
298
|
+
writeFileSync(
|
|
299
|
+
'test/cbor/fixtures.json',
|
|
300
|
+
JSON.stringify(
|
|
301
|
+
deserialized.map((deserialized, i) => ({
|
|
302
|
+
deserialized: Cbor.untransform(Cbor.transform(deserialized)),
|
|
303
|
+
serialized: serialized[i],
|
|
304
|
+
})),
|
|
305
|
+
null,
|
|
306
|
+
2
|
|
307
|
+
)
|
|
308
|
+
);
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
for (let i = 0; i < cborFixtures.length; i++) {
|
|
312
|
+
it(`deserializes vector[${i}]`, function () {
|
|
313
|
+
const { deserialized, serialized } = cborFixtures[i];
|
|
314
|
+
Cbor.serialize(deserialized).equals(Buffer.from(serialized, 'hex')).should.equal(true);
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
for (let i = 0; i < cborFixtures.length; i++) {
|
|
319
|
+
it(`serializes vector[${i}]`, function () {
|
|
320
|
+
const { deserialized, serialized } = cborFixtures[i];
|
|
321
|
+
JSON.stringify(Cbor.deserialize(Buffer.from(serialized, 'hex'))).should.equal(JSON.stringify(deserialized));
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
});
|
|
326
|
+
});
|