fix: multi-repo local dev (file:../ deps, package exports, missing deps)
This commit is contained in:
+21
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2018 Mathias Buus
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
# hypercore-crypto
|
||||
|
||||
The crypto primitives used in hypercore, extracted into a separate module
|
||||
|
||||
```
|
||||
npm install hypercore-crypto
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
const crypto = require('hypercore-crypto')
|
||||
|
||||
const keyPair = crypto.keyPair()
|
||||
console.log(keyPair) // prints a ed25519 keypair
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
#### `keyPair = crypto.keyPair()`
|
||||
|
||||
Returns an `ED25519` keypair that can be used for tree signing.
|
||||
|
||||
#### `signature = crypto.sign(message, secretKey)`
|
||||
|
||||
Signs a message (buffer).
|
||||
|
||||
#### `verified = crypto.verify(message, signature, publicKey)`
|
||||
|
||||
Verifies a signature for a message.
|
||||
|
||||
#### `hash = crypto.data(data)`
|
||||
|
||||
Hashes a leaf node in a merkle tree.
|
||||
|
||||
#### `hash = crypto.parent(left, right)`
|
||||
|
||||
Hash a parent node in a merkle tree. `left` and `right` should look like this:
|
||||
|
||||
```js
|
||||
{
|
||||
index: treeIndex,
|
||||
hash: hashOfThisNode,
|
||||
size: byteSizeOfThisTree
|
||||
}
|
||||
```
|
||||
|
||||
#### `hash = crypto.tree(peaks)`
|
||||
|
||||
Hashes the merkle root of the tree. `peaks` should be an array of the peaks of the tree and should look like above.
|
||||
|
||||
#### `buffer = crypto.randomBytes(size)`
|
||||
|
||||
Returns a buffer containing random bytes of size `size`.
|
||||
|
||||
#### `hash = crypto.discoveryKey(publicKey)`
|
||||
|
||||
Return a hash derived from a `publicKey` that can used for discovery
|
||||
without disclosing the public key.
|
||||
|
||||
#### `list = crypto.namespace(name, count)`
|
||||
|
||||
Make a list of namespaces from a specific publicly known name.
|
||||
Use this to namespace capabilities or hashes / signatures across algorithms.
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
+183
@@ -0,0 +1,183 @@
|
||||
const sodium = require('sodium-universal')
|
||||
const c = require('compact-encoding')
|
||||
const b4a = require('b4a')
|
||||
|
||||
// https://en.wikipedia.org/wiki/Merkle_tree#Second_preimage_attack
|
||||
const LEAF_TYPE = b4a.from([0])
|
||||
const PARENT_TYPE = b4a.from([1])
|
||||
const ROOT_TYPE = b4a.from([2])
|
||||
|
||||
const HYPERCORE = b4a.from('hypercore')
|
||||
|
||||
exports.keyPair = function (seed) {
|
||||
// key pairs might stay around for a while, so better not to use a default slab to avoid retaining it completely
|
||||
const slab = b4a.allocUnsafeSlow(
|
||||
sodium.crypto_sign_PUBLICKEYBYTES + sodium.crypto_sign_SECRETKEYBYTES
|
||||
)
|
||||
const publicKey = slab.subarray(0, sodium.crypto_sign_PUBLICKEYBYTES)
|
||||
const secretKey = slab.subarray(sodium.crypto_sign_PUBLICKEYBYTES)
|
||||
|
||||
if (seed) sodium.crypto_sign_seed_keypair(publicKey, secretKey, seed)
|
||||
else sodium.crypto_sign_keypair(publicKey, secretKey)
|
||||
|
||||
return {
|
||||
publicKey,
|
||||
secretKey
|
||||
}
|
||||
}
|
||||
|
||||
exports.validateKeyPair = function (keyPair) {
|
||||
const pk = b4a.allocUnsafe(sodium.crypto_sign_PUBLICKEYBYTES)
|
||||
sodium.crypto_sign_ed25519_sk_to_pk(pk, keyPair.secretKey)
|
||||
return b4a.equals(pk, keyPair.publicKey)
|
||||
}
|
||||
|
||||
exports.sign = function (message, secretKey) {
|
||||
// Dedicated slab for the signature, to avoid retaining unneeded mem and for security
|
||||
const signature = b4a.allocUnsafeSlow(sodium.crypto_sign_BYTES)
|
||||
sodium.crypto_sign_detached(signature, message, secretKey)
|
||||
return signature
|
||||
}
|
||||
|
||||
exports.verify = function (message, signature, publicKey) {
|
||||
if (signature.byteLength !== sodium.crypto_sign_BYTES) return false
|
||||
if (publicKey.byteLength !== sodium.crypto_sign_PUBLICKEYBYTES) return false
|
||||
return sodium.crypto_sign_verify_detached(signature, message, publicKey)
|
||||
}
|
||||
|
||||
exports.encrypt = function (message, publicKey) {
|
||||
const ciphertext = b4a.alloc(message.byteLength + sodium.crypto_box_SEALBYTES)
|
||||
sodium.crypto_box_seal(ciphertext, message, publicKey)
|
||||
return ciphertext
|
||||
}
|
||||
|
||||
exports.decrypt = function (ciphertext, keyPair) {
|
||||
if (ciphertext.byteLength < sodium.crypto_box_SEALBYTES) return null
|
||||
|
||||
const plaintext = b4a.alloc(ciphertext.byteLength - sodium.crypto_box_SEALBYTES)
|
||||
|
||||
if (!sodium.crypto_box_seal_open(plaintext, ciphertext, keyPair.publicKey, keyPair.secretKey)) {
|
||||
return null
|
||||
}
|
||||
|
||||
return plaintext
|
||||
}
|
||||
|
||||
exports.encryptionKeyPair = function (seed) {
|
||||
const publicKey = b4a.alloc(sodium.crypto_box_PUBLICKEYBYTES)
|
||||
const secretKey = b4a.alloc(sodium.crypto_box_SECRETKEYBYTES)
|
||||
|
||||
if (seed) {
|
||||
sodium.crypto_box_seed_keypair(publicKey, secretKey, seed)
|
||||
} else {
|
||||
sodium.crypto_box_keypair(publicKey, secretKey)
|
||||
}
|
||||
|
||||
return {
|
||||
publicKey,
|
||||
secretKey
|
||||
}
|
||||
}
|
||||
|
||||
exports.data = function (data) {
|
||||
const out = b4a.allocUnsafe(32)
|
||||
|
||||
sodium.crypto_generichash_batch(out, [LEAF_TYPE, c.encode(c.uint64, data.byteLength), data])
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
exports.parent = function (a, b) {
|
||||
if (a.index > b.index) {
|
||||
const tmp = a
|
||||
a = b
|
||||
b = tmp
|
||||
}
|
||||
|
||||
const out = b4a.allocUnsafe(32)
|
||||
|
||||
sodium.crypto_generichash_batch(out, [
|
||||
PARENT_TYPE,
|
||||
c.encode(c.uint64, a.size + b.size),
|
||||
a.hash,
|
||||
b.hash
|
||||
])
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
exports.tree = function (roots, out) {
|
||||
const buffers = new Array(3 * roots.length + 1)
|
||||
let j = 0
|
||||
|
||||
buffers[j++] = ROOT_TYPE
|
||||
|
||||
for (let i = 0; i < roots.length; i++) {
|
||||
const r = roots[i]
|
||||
buffers[j++] = r.hash
|
||||
buffers[j++] = c.encode(c.uint64, r.index)
|
||||
buffers[j++] = c.encode(c.uint64, r.size)
|
||||
}
|
||||
|
||||
if (!out) out = b4a.allocUnsafe(32)
|
||||
sodium.crypto_generichash_batch(out, buffers)
|
||||
return out
|
||||
}
|
||||
|
||||
exports.hash = function (data, out) {
|
||||
if (!out) out = b4a.allocUnsafe(32)
|
||||
if (!Array.isArray(data)) data = [data]
|
||||
|
||||
sodium.crypto_generichash_batch(out, data)
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
exports.randomBytes = function (n) {
|
||||
const buf = b4a.allocUnsafe(n)
|
||||
sodium.randombytes_buf(buf)
|
||||
return buf
|
||||
}
|
||||
|
||||
exports.discoveryKey = function (key) {
|
||||
if (!key || key.byteLength !== 32) throw new Error('Must pass a 32 byte buffer')
|
||||
// Discovery keys might stay around for a while, so better not to use slab memory (for better gc)
|
||||
const digest = b4a.allocUnsafeSlow(32)
|
||||
sodium.crypto_generichash(digest, HYPERCORE, key)
|
||||
return digest
|
||||
}
|
||||
|
||||
if (sodium.sodium_free) {
|
||||
exports.free = function (secureBuf) {
|
||||
if (secureBuf.secure) sodium.sodium_free(secureBuf)
|
||||
}
|
||||
} else {
|
||||
exports.free = function () {}
|
||||
}
|
||||
|
||||
exports.namespace = function (name, count) {
|
||||
const ids = typeof count === 'number' ? range(count) : count
|
||||
|
||||
// Namespaces are long-lived, so better to use a dedicated slab
|
||||
const buf = b4a.allocUnsafeSlow(32 * ids.length)
|
||||
|
||||
const list = new Array(ids.length)
|
||||
|
||||
// ns is ephemeral, so default slab
|
||||
const ns = b4a.allocUnsafe(33)
|
||||
sodium.crypto_generichash(ns.subarray(0, 32), typeof name === 'string' ? b4a.from(name) : name)
|
||||
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
list[i] = buf.subarray(32 * i, 32 * i + 32)
|
||||
ns[32] = ids[i]
|
||||
sodium.crypto_generichash(list[i], ns)
|
||||
}
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
function range(count) {
|
||||
const arr = new Array(count)
|
||||
for (let i = 0; i < count; i++) arr[i] = i
|
||||
return arr
|
||||
}
|
||||
+201
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
+165
@@ -0,0 +1,165 @@
|
||||
# compact-encoding
|
||||
|
||||
A series of compact encoding schemes for building small and fast parsers and serializers
|
||||
|
||||
```
|
||||
npm install compact-encoding
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
const cenc = require('compact-encoding')
|
||||
|
||||
const state = cenc.state()
|
||||
|
||||
// use preencode to figure out how big a buffer is needed
|
||||
cenc.uint.preencode(state, 42)
|
||||
cenc.string.preencode(state, 'hi')
|
||||
|
||||
console.log(state) // { start: 0, end: 4, buffer: null }
|
||||
|
||||
state.buffer = Buffer.allocUnsafe(state.end)
|
||||
|
||||
// then use encode to actually encode it to the buffer
|
||||
cenc.uint.encode(state, 42)
|
||||
cenc.string.encode(state, 'hi')
|
||||
|
||||
// to decode it simply use decode instead
|
||||
|
||||
state.start = 0
|
||||
cenc.uint.decode(state) // 42
|
||||
cenc.string.decode(state) // 'hi'
|
||||
```
|
||||
|
||||
## Encoder API
|
||||
|
||||
#### `state`
|
||||
|
||||
Should be an object that looks like this `{ start, end, buffer }`.
|
||||
|
||||
You can also get a blank state object using `cenc.state()`.
|
||||
|
||||
- `start` is the byte offset to start encoding/decoding at.
|
||||
- `end` is the byte offset indicating the end of the buffer.
|
||||
- `buffer` should be either a Node.js Buffer or Uint8Array.
|
||||
|
||||
#### `enc.preencode(state, val)`
|
||||
|
||||
Does a fast preencode dry-run that only sets state.end.
|
||||
Use this to figure out how big of a buffer you need.
|
||||
|
||||
#### `enc.encode(state, val)`
|
||||
|
||||
Encodes `val` into `state.buffer` at position `state.start`.
|
||||
Updates `state.start` to point after the encoded value when done.
|
||||
|
||||
#### `val = enc.decode(state)`
|
||||
|
||||
Decodes a value from `state.buffer` as position `state.start`.
|
||||
Updates `state.start` to point after the decoded value when done in the buffer.
|
||||
|
||||
## Helpers
|
||||
|
||||
If you are just encoding to a buffer or decoding from one you can use the `encode` and `decode` helpers
|
||||
to reduce your boilerplate
|
||||
|
||||
```js
|
||||
const buf = cenc.encode(cenc.bool, true)
|
||||
const bool = cenc.decode(cenc.bool, buf)
|
||||
```
|
||||
|
||||
## Bundled encodings
|
||||
|
||||
The following encodings are bundled as they are primitives that can be used
|
||||
to build others on top. Feel free to PR more that are missing.
|
||||
|
||||
- `cenc.raw` - Pass through encodes a buffer, i.e. a basic copy.
|
||||
- `cenc.uint` - Encodes a uint using the smallest fixed size encoding with a prefix to signal which one. Useful for uints that can be a wide range of values.
|
||||
- `cenc.uint8` - Encodes a fixed size uint8.
|
||||
- `cenc.uint16` - Encodes a fixed size uint16. Useful for things like ports.
|
||||
- `cenc.uint24` - Encodes a fixed size uint24. Useful for message framing.
|
||||
- `cenc.uint32` - Encodes a fixed size uint32. Useful for very large message framing.
|
||||
- `cenc.uint40` - Encodes a fixed size uint40.
|
||||
- `cenc.uint48` - Encodes a fixed size uint48.
|
||||
- `cenc.uint56` - Encodes a fixed size uint56.
|
||||
- `cenc.uint64` - Encodes a fixed size uint64.
|
||||
- `cenc.int` - Encodes an int using `cenc.uint` with ZigZag encoding.
|
||||
- `cenc.int8` - Encodes a fixed size int8 using `cenc.uint8` with ZigZag encoding.
|
||||
- `cenc.int16` - Encodes a fixed size int16 using `cenc.uint16` with ZigZag encoding.
|
||||
- `cenc.int24` - Encodes a fixed size int24 using `cenc.uint24` with ZigZag encoding.
|
||||
- `cenc.int32` - Encodes a fixed size int32 using `cenc.uint32` with ZigZag encoding.
|
||||
- `cenc.int40` - Encodes a fixed size int40 using `cenc.uint40` with ZigZag encoding.
|
||||
- `cenc.int48` - Encodes a fixed size int48 using `cenc.uint48` with ZigZag encoding.
|
||||
- `cenc.int56` - Encodes a fixed size int56 using `cenc.uint56` with ZigZag encoding.
|
||||
- `cenc.int64` - Encodes a fixed size int64 using `cenc.uint64` with ZigZag encoding.
|
||||
- `cenc.biguint64` - Encodes a fixed size biguint64.
|
||||
- `cenc.bigint64` - Encodes a fixed size bigint64 using `cenc.biguint64` with ZigZag encoding.
|
||||
- `cenc.biguint` - Encodes a biguint with its word count uint prefixed.
|
||||
- `cenc.bigint` - Encodes a bigint using `cenc.biguint` with ZigZag encoding.
|
||||
- `cenc.float32` - Encodes a fixed size float32.
|
||||
- `cenc.float64` - Encodes a fixed size float64.
|
||||
- `cenc.buffer` - Encodes a buffer with its length uint prefixed. When decoding an empty buffer, `null` is returned.
|
||||
- `cenc.raw.buffer` - Encodes a buffer without a length prefixed.
|
||||
- `cenc.arraybuffer` - Encodes an arraybuffer with its length uint prefixed.
|
||||
- `cenc.raw.arraybuffer` - Encodes an arraybuffer without a length prefixed.
|
||||
- `cenc.uint8array` - Encodes a uint8array with its element length uint prefixed.
|
||||
- `cenc.raw.uint8array` - Encodes a uint8array without a length prefixed.
|
||||
- `cenc.uint16array` - Encodes a uint16array with its element length uint prefixed.
|
||||
- `cenc.raw.uint16array` - Encodes a uint16array without a length prefixed.
|
||||
- `cenc.uint32array` - Encodes a uint32array with its element length uint prefixed.
|
||||
- `cenc.raw.uint32array` - Encodes a uint32array without a length prefixed.
|
||||
- `cenc.int8array` - Encodes a int8array with its element length uint prefixed.
|
||||
- `cenc.raw.int8array` - Encodes a int8array without a length prefixed.
|
||||
- `cenc.int16array` - Encodes a int16array with its element length uint prefixed.
|
||||
- `cenc.raw.int16array` - Encodes a int16array without a length prefixed.
|
||||
- `cenc.int32array` - Encodes a int32array with its element length uint prefixed.
|
||||
- `cenc.raw.int32array` - Encodes a int32array without a length prefixed.
|
||||
- `cenc.biguint64array` - Encodes a biguint64array with its element length uint prefixed.
|
||||
- `cenc.raw.biguint64array` - Encodes a biguint64array without a length prefixed.
|
||||
- `cenc.bigint64array` - Encodes a bigint64array with its element length uint prefixed.
|
||||
- `cenc.raw.bigint64array` - Encodes a bigint64array without a length prefixed.
|
||||
- `cenc.float32array` - Encodes a float32array with its element length uint prefixed.
|
||||
- `cenc.raw.float32array` - Encodes a float32array without a length prefixed.
|
||||
- `cenc.float64array` - Encodes a float64array with its element length uint prefixed.
|
||||
- `cenc.raw.float64array` - Encodes a float64array without a length prefixed.
|
||||
- `cenc.bool` - Encodes a boolean as 1 or 0.
|
||||
- `cenc.string`, `cenc.utf8` - Encodes a utf-8 string, similar to buffer.
|
||||
- `cenc.raw.string`, `cenc.raw.utf8` - Encodes a utf-8 string without a length prefixed.
|
||||
- `cenc.string.fixed(n)`, `cenc.utf8.fixed(n)` - Encodes a fixed sized utf-8 string.
|
||||
- `cenc.ascii` - Encodes an ascii string.
|
||||
- `cenc.raw.ascii` - Encodes an ascii string without a length prefixed.
|
||||
- `cenc.ascii.fixed(n)` - Encodes a fixed size ascii string.
|
||||
- `cenc.hex` - Encodes a hex string.
|
||||
- `cenc.raw.hex` - Encodes a hex string without a length prefixed.
|
||||
- `cenc.hex.fixed(n)` - Encodes a fixed size hex string.
|
||||
- `cenc.base64` - Encodes a base64 string.
|
||||
- `cenc.raw.base64` - Encodes a base64 string without a length prefixed.
|
||||
- `cenc.base64.fixed(n)` - Encodes a fixed size base64 string.
|
||||
- `cenc.utf16le`, `cenc.ucs2` - Encodes a utf16le string.
|
||||
- `cenc.raw.utf16le`, `cenc.raw.ucs2` - Encodes a utf16le string without a length prefixed.
|
||||
- `cenc.utf16le.fixed(n)`, `cenc.ucs2.fixed(n)` - Encodes a fixed size utf16le string.
|
||||
- `cenc.fixed32` - Encodes a fixed 32 byte buffer.
|
||||
- `cenc.fixed64` - Encodes a fixed 64 byte buffer.
|
||||
- `cenc.fixed(n)` - Makes a fixed sized encoder.
|
||||
- `cenc.date(d)` - Encodes a date object.
|
||||
- `cenc.array(enc)` - Makes an array encoder from another encoder. Arrays are uint prefixed with their length.
|
||||
- `cenc.raw.array(enc)` - Makes an array encoder from another encoder, without a length prefixed.
|
||||
- `cenc.json` - Encodes a JSON value as utf-8.
|
||||
- `cenc.raw.json` - Encodes a JSON value as utf-8 without a length prefixed.
|
||||
- `cenc.ndjson` - Encodes a JSON value as newline delimited utf-8.
|
||||
- `cenc.raw.ndjson` - Encodes a JSON value as newline delimited utf-8 without a length prefixed.
|
||||
- `cenc.any` - Encodes any JSON representable value into a self described buffer. Like JSON + buffer, but using compact types. Useful for schemaless codecs.
|
||||
- `cenc.port` - Encodes a port number for network addresses.
|
||||
- `cenc.ipv4` - Encodes an IPv4 network address.
|
||||
- `cenc.ipv4Address` Encodes an IPv4 network address and a port number.
|
||||
- `cenc.ipv6` - Encodes an IPv6 network address.
|
||||
- `cenc.ipv6Address` Encodes an IPv6 network address and a port number.
|
||||
- `cenc.ip` - Encodes a dual IPv4/6 network address.
|
||||
- `cenc.ipAddress` Encodes a dual IPv4/6 network address and a port number.
|
||||
- `cenc.from(enc)` - Makes a compact encoder from a [codec](https://github.com/mafintosh/codecs) or [abstract-encoding](https://github.com/mafintosh/abstract-encoding).
|
||||
- `cenc.none` - Helper for when you want to just express nothing
|
||||
|
||||
## License
|
||||
|
||||
Apache 2.0
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
const LE = (exports.LE =
|
||||
new Uint8Array(new Uint16Array([0xff]).buffer)[0] === 0xff)
|
||||
|
||||
exports.BE = !LE
|
||||
+1091
File diff suppressed because it is too large
Load Diff
+117
@@ -0,0 +1,117 @@
|
||||
module.exports = {
|
||||
preencode,
|
||||
encode,
|
||||
decode
|
||||
}
|
||||
|
||||
function preencode(state, num) {
|
||||
if (num < 251) {
|
||||
state.end++
|
||||
} else if (num < 256) {
|
||||
state.end += 2
|
||||
} else if (num < 0x10000) {
|
||||
state.end += 3
|
||||
} else if (num < 0x1000000) {
|
||||
state.end += 4
|
||||
} else if (num < 0x100000000) {
|
||||
state.end += 5
|
||||
} else {
|
||||
state.end++
|
||||
const exp = Math.floor(Math.log(num) / Math.log(2)) - 32
|
||||
preencode(state, exp)
|
||||
state.end += 6
|
||||
}
|
||||
}
|
||||
|
||||
function encode(state, num) {
|
||||
const max = 251
|
||||
const x = num - max
|
||||
|
||||
if (num < max) {
|
||||
state.buffer[state.start++] = num
|
||||
} else if (num < 256) {
|
||||
state.buffer[state.start++] = max
|
||||
state.buffer[state.start++] = x
|
||||
} else if (num < 0x10000) {
|
||||
state.buffer[state.start++] = max + 1
|
||||
state.buffer[state.start++] = (x >> 8) & 0xff
|
||||
state.buffer[state.start++] = x & 0xff
|
||||
} else if (num < 0x1000000) {
|
||||
state.buffer[state.start++] = max + 2
|
||||
state.buffer[state.start++] = x >> 16
|
||||
state.buffer[state.start++] = (x >> 8) & 0xff
|
||||
state.buffer[state.start++] = x & 0xff
|
||||
} else if (num < 0x100000000) {
|
||||
state.buffer[state.start++] = max + 3
|
||||
state.buffer[state.start++] = x >> 24
|
||||
state.buffer[state.start++] = (x >> 16) & 0xff
|
||||
state.buffer[state.start++] = (x >> 8) & 0xff
|
||||
state.buffer[state.start++] = x & 0xff
|
||||
} else {
|
||||
// need to use Math here as bitwise ops are 32 bit
|
||||
const exp = Math.floor(Math.log(x) / Math.log(2)) - 32
|
||||
state.buffer[state.start++] = 0xff
|
||||
|
||||
encode(state, exp)
|
||||
const rem = x / Math.pow(2, exp - 11)
|
||||
|
||||
for (let i = 5; i >= 0; i--) {
|
||||
state.buffer[state.start++] = (rem / Math.pow(2, 8 * i)) & 0xff
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function decode(state) {
|
||||
const max = 251
|
||||
|
||||
if (state.end - state.start < 1) throw new Error('Out of bounds')
|
||||
|
||||
const flag = state.buffer[state.start++]
|
||||
|
||||
if (flag < max) return flag
|
||||
|
||||
if (state.end - state.start < flag - max + 1) {
|
||||
throw new Error('Out of bounds.')
|
||||
}
|
||||
|
||||
if (flag < 252) {
|
||||
return state.buffer[state.start++] + max
|
||||
}
|
||||
|
||||
if (flag < 253) {
|
||||
return (
|
||||
(state.buffer[state.start++] << 8) + state.buffer[state.start++] + max
|
||||
)
|
||||
}
|
||||
|
||||
if (flag < 254) {
|
||||
return (
|
||||
(state.buffer[state.start++] << 16) +
|
||||
(state.buffer[state.start++] << 8) +
|
||||
state.buffer[state.start++] +
|
||||
max
|
||||
)
|
||||
}
|
||||
|
||||
// << 24 result may be interpreted as negative
|
||||
if (flag < 255) {
|
||||
return (
|
||||
state.buffer[state.start++] * 0x1000000 +
|
||||
(state.buffer[state.start++] << 16) +
|
||||
(state.buffer[state.start++] << 8) +
|
||||
state.buffer[state.start++] +
|
||||
max
|
||||
)
|
||||
}
|
||||
|
||||
const exp = decode(state)
|
||||
|
||||
if (state.end - state.start < 6) throw new Error('Out of bounds')
|
||||
|
||||
let rem = 0
|
||||
for (let i = 5; i >= 0; i--) {
|
||||
rem += state.buffer[state.start++] * Math.pow(2, 8 * i)
|
||||
}
|
||||
|
||||
return rem * Math.pow(2, exp - 11) + max
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
{
|
||||
"name": "compact-encoding",
|
||||
"version": "3.1.0",
|
||||
"description": "A series of compact encoding schemes for building small and fast parsers and serializers",
|
||||
"main": "index.js",
|
||||
"files": [
|
||||
"endian.js",
|
||||
"index.js",
|
||||
"lexint.js",
|
||||
"raw.js"
|
||||
],
|
||||
"dependencies": {
|
||||
"b4a": "^1.3.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"brittle": "^3.0.0",
|
||||
"prettier": "^3.6.2",
|
||||
"prettier-config-holepunch": "^1.0.0"
|
||||
},
|
||||
"scripts": {
|
||||
"format": "prettier . --write",
|
||||
"test": "prettier . --check && brittle test.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/holepunchto/compact-encoding.git"
|
||||
},
|
||||
"author": "Mathias Buus (@mafintosh)",
|
||||
"license": "Apache-2.0",
|
||||
"bugs": {
|
||||
"url": "https://github.com/holepunchto/compact-encoding/issues"
|
||||
},
|
||||
"homepage": "https://github.com/holepunchto/compact-encoding"
|
||||
}
|
||||
+170
@@ -0,0 +1,170 @@
|
||||
const b4a = require('b4a')
|
||||
|
||||
const { BE } = require('./endian')
|
||||
|
||||
exports = module.exports = {
|
||||
preencode(state, b) {
|
||||
state.end += b.byteLength
|
||||
},
|
||||
encode(state, b) {
|
||||
state.buffer.set(b, state.start)
|
||||
state.start += b.byteLength
|
||||
},
|
||||
decode(state) {
|
||||
const b = state.buffer.subarray(state.start, state.end)
|
||||
state.start = state.end
|
||||
return b
|
||||
}
|
||||
}
|
||||
|
||||
const buffer = (exports.buffer = {
|
||||
preencode(state, b) {
|
||||
uint8array.preencode(state, b)
|
||||
},
|
||||
encode(state, b) {
|
||||
uint8array.encode(state, b)
|
||||
},
|
||||
decode(state) {
|
||||
const b = state.buffer.subarray(state.start)
|
||||
state.start = state.end
|
||||
return b
|
||||
}
|
||||
})
|
||||
|
||||
exports.binary = {
|
||||
...buffer,
|
||||
preencode(state, b) {
|
||||
if (typeof b === 'string') utf8.preencode(state, b)
|
||||
else buffer.preencode(state, b)
|
||||
},
|
||||
encode(state, b) {
|
||||
if (typeof b === 'string') utf8.encode(state, b)
|
||||
else buffer.encode(state, b)
|
||||
}
|
||||
}
|
||||
|
||||
exports.arraybuffer = {
|
||||
preencode(state, b) {
|
||||
state.end += b.byteLength
|
||||
},
|
||||
encode(state, b) {
|
||||
const view = new Uint8Array(b)
|
||||
|
||||
state.buffer.set(view, state.start)
|
||||
state.start += b.byteLength
|
||||
},
|
||||
decode(state) {
|
||||
const b = new ArrayBuffer(state.end - state.start)
|
||||
const view = new Uint8Array(b)
|
||||
|
||||
view.set(state.buffer.subarray(state.start))
|
||||
|
||||
state.start = state.end
|
||||
|
||||
return b
|
||||
}
|
||||
}
|
||||
|
||||
function typedarray(TypedArray, swap) {
|
||||
const n = TypedArray.BYTES_PER_ELEMENT
|
||||
|
||||
return {
|
||||
preencode(state, b) {
|
||||
state.end += b.byteLength
|
||||
},
|
||||
encode(state, b) {
|
||||
const view = new Uint8Array(b.buffer, b.byteOffset, b.byteLength)
|
||||
|
||||
if (BE && swap) swap(view)
|
||||
|
||||
state.buffer.set(view, state.start)
|
||||
state.start += b.byteLength
|
||||
},
|
||||
decode(state) {
|
||||
let b = state.buffer.subarray(state.start)
|
||||
if (b.byteOffset % n !== 0) b = new Uint8Array(b)
|
||||
|
||||
if (BE && swap) swap(b)
|
||||
|
||||
state.start = state.end
|
||||
|
||||
return new TypedArray(b.buffer, b.byteOffset, b.byteLength / n)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const uint8array = (exports.uint8array = typedarray(Uint8Array))
|
||||
exports.uint16array = typedarray(Uint16Array, b4a.swap16)
|
||||
exports.uint32array = typedarray(Uint32Array, b4a.swap32)
|
||||
|
||||
exports.int8array = typedarray(Int8Array)
|
||||
exports.int16array = typedarray(Int16Array, b4a.swap16)
|
||||
exports.int32array = typedarray(Int32Array, b4a.swap32)
|
||||
|
||||
exports.biguint64array = typedarray(BigUint64Array, b4a.swap64)
|
||||
exports.bigint64array = typedarray(BigInt64Array, b4a.swap64)
|
||||
|
||||
exports.float32array = typedarray(Float32Array, b4a.swap32)
|
||||
exports.float64array = typedarray(Float64Array, b4a.swap64)
|
||||
|
||||
function string(encoding) {
|
||||
return {
|
||||
preencode(state, s) {
|
||||
state.end += b4a.byteLength(s, encoding)
|
||||
},
|
||||
encode(state, s) {
|
||||
state.start += b4a.write(state.buffer, s, state.start, encoding)
|
||||
},
|
||||
decode(state) {
|
||||
const s = b4a.toString(state.buffer, encoding, state.start)
|
||||
state.start = state.end
|
||||
return s
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const utf8 = (exports.string = exports.utf8 = string('utf-8'))
|
||||
exports.ascii = string('ascii')
|
||||
exports.hex = string('hex')
|
||||
exports.base64 = string('base64')
|
||||
exports.ucs2 = exports.utf16le = string('utf16le')
|
||||
|
||||
exports.array = function array(enc) {
|
||||
return {
|
||||
preencode(state, list) {
|
||||
for (const value of list) enc.preencode(state, value)
|
||||
},
|
||||
encode(state, list) {
|
||||
for (const value of list) enc.encode(state, value)
|
||||
},
|
||||
decode(state) {
|
||||
const arr = []
|
||||
while (state.start < state.end) arr.push(enc.decode(state))
|
||||
return arr
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exports.json = {
|
||||
preencode(state, v) {
|
||||
utf8.preencode(state, JSON.stringify(v))
|
||||
},
|
||||
encode(state, v) {
|
||||
utf8.encode(state, JSON.stringify(v))
|
||||
},
|
||||
decode(state) {
|
||||
return JSON.parse(utf8.decode(state))
|
||||
}
|
||||
}
|
||||
|
||||
exports.ndjson = {
|
||||
preencode(state, v) {
|
||||
utf8.preencode(state, JSON.stringify(v) + '\n')
|
||||
},
|
||||
encode(state, v) {
|
||||
utf8.encode(state, JSON.stringify(v) + '\n')
|
||||
},
|
||||
decode(state) {
|
||||
return JSON.parse(utf8.decode(state))
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"name": "hypercore-crypto",
|
||||
"version": "3.7.0",
|
||||
"description": "The crypto primitives used in hypercore, extracted into a separate module",
|
||||
"main": "index.js",
|
||||
"files": [
|
||||
"index.js"
|
||||
],
|
||||
"dependencies": {
|
||||
"b4a": "^1.6.6",
|
||||
"compact-encoding": "^3.0.0",
|
||||
"sodium-universal": "^5.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"brittle": "^3.5.0",
|
||||
"lunte": "^1.0.0",
|
||||
"prettier": "^3.6.2",
|
||||
"prettier-config-holepunch": "^2.0.0"
|
||||
},
|
||||
"scripts": {
|
||||
"format": "prettier --write .",
|
||||
"lint": "prettier --check . && lunte",
|
||||
"test": "npm run test:node && npm run test:bare",
|
||||
"test:node": "brittle-node test.js",
|
||||
"test:bare": "brittle-bare test.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/mafintosh/hypercore-crypto.git"
|
||||
},
|
||||
"author": "Mathias Buus (@mafintosh)",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/mafintosh/hypercore-crypto/issues"
|
||||
},
|
||||
"homepage": "https://github.com/mafintosh/hypercore-crypto"
|
||||
}
|
||||
Reference in New Issue
Block a user