@agoric/internal 0.3.3-dev-b2bff24.0 → 0.3.3-dev-4054171.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/package.json +4 -4
- package/src/hex.d.ts +15 -0
- package/src/hex.d.ts.map +1 -0
- package/src/hex.js +105 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agoric/internal",
|
|
3
|
-
"version": "0.3.3-dev-
|
|
3
|
+
"version": "0.3.3-dev-4054171.0+4054171",
|
|
4
4
|
"description": "Externally unsupported utilities internal to agoric-sdk",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"lint:types": "tsc"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@agoric/base-zone": "0.1.1-dev-
|
|
23
|
+
"@agoric/base-zone": "0.1.1-dev-4054171.0+4054171",
|
|
24
24
|
"@endo/common": "^1.2.10",
|
|
25
25
|
"@endo/compartment-mapper": "^1.6.0",
|
|
26
26
|
"@endo/errors": "^1.2.10",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"jessie.js": "^0.3.4"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@agoric/cosmic-proto": "0.4.1-dev-
|
|
39
|
+
"@agoric/cosmic-proto": "0.4.1-dev-4054171.0+4054171",
|
|
40
40
|
"@endo/exo": "^1.5.9",
|
|
41
41
|
"@endo/init": "^1.1.9",
|
|
42
42
|
"@fast-check/ava": "^2.0.1",
|
|
@@ -63,5 +63,5 @@
|
|
|
63
63
|
"typeCoverage": {
|
|
64
64
|
"atLeast": 92.65
|
|
65
65
|
},
|
|
66
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "4054171a6a562a8bad1824b0151d103b8fe2f502"
|
|
67
67
|
}
|
package/src/hex.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function makePortableHexCodec(): HexCodec;
|
|
2
|
+
export function makeBufferishHexCodec(Bufferish: BufferishConstructor): HexCodec;
|
|
3
|
+
export const encodeHex: (buf: Uint8Array) => string;
|
|
4
|
+
export const decodeHex: (hex: string) => Uint8Array;
|
|
5
|
+
export type HexCodec = {
|
|
6
|
+
encodeHex: (buf: Uint8Array) => string;
|
|
7
|
+
decodeHex: (hex: string) => Uint8Array;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* is the portion of the Node.js Buffer API we need for hex conversion.
|
|
11
|
+
*/
|
|
12
|
+
export type BufferishConstructor = Pick<BufferConstructor, "from" | "isBuffer"> & {
|
|
13
|
+
prototype: Pick<Buffer, "toString"> & Uint8Array;
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=hex.d.ts.map
|
package/src/hex.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hex.d.ts","sourceRoot":"","sources":["hex.js"],"names":[],"mappings":"AAsCO,wCAFM,QAAQ,CAyBpB;AAgBM,iDAJI,oBAAoB,GAElB,QAAQ,CAoBpB;8BA7FmB,UAAU,KAAK,MAAM;8BACrB,MAAM,KAAK,UAAU;;eAD3B,CAAC,GAAG,EAAE,UAAU,KAAK,MAAM;eAC3B,CAAC,GAAG,EAAE,MAAM,KAAK,UAAU;;;;;mCA6D5B,IAAI,CAAC,iBAAiB,EAAE,MAAM,GAAG,UAAU,CAAC,GAAG;IACvD,SAAS,EAAE,IAAI,CAAC,MAAM,EAAE,UAAU,CAAC,GAAG,UAAU,CAAC;CAClD"}
|
package/src/hex.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {object} HexCodec
|
|
3
|
+
* @property {(buf: Uint8Array) => string} encodeHex
|
|
4
|
+
* @property {(hex: string) => Uint8Array} decodeHex
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** @type {string[]} */
|
|
8
|
+
const encodings = Array.from({ length: 256 }, (_, b) =>
|
|
9
|
+
// Write the hex representation of the byte.
|
|
10
|
+
b.toString(16).padStart(2, '0'),
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Create map entries for all four permutations of lowercase and uppercase
|
|
15
|
+
* transformations of the two hex digits per byte. The map is keyed by the hex
|
|
16
|
+
* string and the value is the byte value. This allows for fast lookups when
|
|
17
|
+
* decoding hex strings.
|
|
18
|
+
*
|
|
19
|
+
* @type {Map<string, number>}
|
|
20
|
+
*/
|
|
21
|
+
const decodings = new Map(
|
|
22
|
+
encodings.flatMap((hexdigits, b) => {
|
|
23
|
+
const lo = hexdigits.toLowerCase();
|
|
24
|
+
const UP = hexdigits.toUpperCase();
|
|
25
|
+
return [
|
|
26
|
+
[lo, b],
|
|
27
|
+
[`${lo[0]}${UP[1]}`, b],
|
|
28
|
+
[`${UP[0]}${lo[1]}`, b],
|
|
29
|
+
[UP, b],
|
|
30
|
+
];
|
|
31
|
+
}),
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Create a hex codec that is portable across standard JS environments.
|
|
36
|
+
*
|
|
37
|
+
* @returns {HexCodec}
|
|
38
|
+
*/
|
|
39
|
+
export const makePortableHexCodec = () => {
|
|
40
|
+
/** @type {HexCodec} */
|
|
41
|
+
const portableHexCodec = {
|
|
42
|
+
encodeHex: buf => Array.from(buf, b => encodings[b]).join(''),
|
|
43
|
+
decodeHex: hex => {
|
|
44
|
+
const inputLen = hex.length;
|
|
45
|
+
if (inputLen % 2 !== 0) {
|
|
46
|
+
throw new Error(`Invalid hex string: ${hex}`);
|
|
47
|
+
}
|
|
48
|
+
const buf = new Uint8Array(inputLen / 2);
|
|
49
|
+
for (let i = 0; i < inputLen; i += 2) {
|
|
50
|
+
const b = decodings.get(hex.slice(i, i + 2));
|
|
51
|
+
if (b === undefined) {
|
|
52
|
+
throw new Error(`Invalid hex string: ${hex}`);
|
|
53
|
+
}
|
|
54
|
+
// eslint-disable-next-line no-bitwise
|
|
55
|
+
buf[i >> 1] = b;
|
|
56
|
+
}
|
|
57
|
+
return buf;
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
return portableHexCodec;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @typedef {Pick<BufferConstructor, 'from' | 'isBuffer'> & {
|
|
66
|
+
* prototype: Pick<Buffer, 'toString'> & Uint8Array;
|
|
67
|
+
* }} BufferishConstructor
|
|
68
|
+
* is the portion of the Node.js Buffer API we need for hex conversion.
|
|
69
|
+
*/
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Create a hex codec using parts of the Node.js Buffer API.
|
|
73
|
+
*
|
|
74
|
+
* @param {BufferishConstructor} Bufferish the object that implements the
|
|
75
|
+
* necessary pieces of Buffer
|
|
76
|
+
* @returns {HexCodec}
|
|
77
|
+
*/
|
|
78
|
+
export const makeBufferishHexCodec = Bufferish => {
|
|
79
|
+
/** @type {HexCodec} */
|
|
80
|
+
const attenuatedBufferHexCodec = {
|
|
81
|
+
encodeHex: buf =>
|
|
82
|
+
(Bufferish.isBuffer?.(buf) ? buf : Bufferish.from(buf)).toString('hex'),
|
|
83
|
+
decodeHex: hex => {
|
|
84
|
+
const buf = Bufferish.from(hex, 'hex');
|
|
85
|
+
|
|
86
|
+
// Coerce to Uint8Array to avoid leaking the abstraction.
|
|
87
|
+
const u8a = new Uint8Array(
|
|
88
|
+
buf.buffer,
|
|
89
|
+
buf.byteOffset,
|
|
90
|
+
buf.byteLength / Uint8Array.BYTES_PER_ELEMENT,
|
|
91
|
+
);
|
|
92
|
+
return u8a;
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
return attenuatedBufferHexCodec;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Export a hex codec that can work with standard JS engines, but takes
|
|
100
|
+
* advantage of optimizations on some platforms (like Node.js's Buffer API).
|
|
101
|
+
*/
|
|
102
|
+
export const { encodeHex, decodeHex } =
|
|
103
|
+
typeof Buffer === 'undefined'
|
|
104
|
+
? makePortableHexCodec()
|
|
105
|
+
: makeBufferishHexCodec(Buffer);
|