diff --git a/package.json b/package.json index 0a0f78b5..b933a6ea 100644 --- a/package.json +++ b/package.json @@ -146,9 +146,9 @@ }, "dependencies": { "@chainsafe/is-ip": "^2.0.1", - "multiformats": "^13.0.0", - "uint8-varint": "^2.0.1", - "uint8arrays": "^5.0.0" + "multiformats": "^14.0.0", + "uint8-varint": "^3.0.0", + "uint8arrays": "^6.1.1" }, "devDependencies": { "aegir": "^48.0.4" diff --git a/src/components.ts b/src/components.ts index f6009e11..924ceea4 100644 --- a/src/components.ts +++ b/src/components.ts @@ -1,4 +1,5 @@ import * as varint from 'uint8-varint' +import { withArrayBuffer } from 'uint8arrays' import { concat as uint8ArrayConcat } from 'uint8arrays/concat' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import { toString as uint8ArrayToString } from 'uint8arrays/to-string' @@ -27,7 +28,7 @@ export function bytesToComponents (bytes: Uint8Array): Component[] { const component: Component = { code, name: codec.name, - bytes: bytes.subarray(i, i + componentLength) + bytes: withArrayBuffer(bytes.subarray(i, i + componentLength)) } if (size > 0) { @@ -45,7 +46,7 @@ export function bytesToComponents (bytes: Uint8Array): Component[] { return components } -export function componentsToBytes (components: Component[]): Uint8Array { +export function componentsToBytes (components: Component[]): Uint8Array { let length = 0 const bytes: Uint8Array[] = [] diff --git a/src/index.ts b/src/index.ts index 86b6bae4..94113baf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -123,11 +123,11 @@ export interface Component { * The bytes that make up the component. This will be set if the multiaddr * was parsed from a `Uint8Array`, or if `.bytes` has been accessed on it. */ - bytes?: Uint8Array + bytes?: Uint8Array } export interface Multiaddr { - bytes: Uint8Array + bytes: Uint8Array /** * Returns Multiaddr as a String @@ -258,7 +258,7 @@ export interface Multiaddr { * // false * ``` */ - equals(addr: { bytes: Uint8Array }): boolean + equals(addr?: any): boolean } /** diff --git a/src/multiaddr.ts b/src/multiaddr.ts index 7a93359a..573cc702 100644 --- a/src/multiaddr.ts +++ b/src/multiaddr.ts @@ -54,7 +54,7 @@ export class Multiaddr implements MultiaddrInterface { // cache string representation #string: string | undefined // cache byte representation - #bytes: Uint8Array | undefined + #bytes: Uint8Array | undefined constructor (addr: MultiaddrInput | Component[] = '/', options: MultiaddrOptions = {}) { this.#components = toComponents(addr) @@ -64,7 +64,7 @@ export class Multiaddr implements MultiaddrInterface { } } - get bytes (): Uint8Array { + get bytes (): Uint8Array { if (this.#bytes == null) { this.#bytes = componentsToBytes(this.#components) } diff --git a/src/registry.ts b/src/registry.ts index 1c345467..de211924 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -48,7 +48,7 @@ export interface ProtocolCodec { /** * To encode the multiaddr as bytes, convert the value to bytes */ - valueToBytes?(value: string): Uint8Array + valueToBytes?(value: string): Uint8Array /** * To decode bytes to a multiaddr, convert the value bytes to a string diff --git a/src/utils.ts b/src/utils.ts index d75f6b71..1b496f21 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -14,7 +14,7 @@ export function bytesToString (base: SupportedEncodings): (buf: Uint8Array) => s } } -export function stringToBytes (base: SupportedEncodings): (value: string) => Uint8Array { +export function stringToBytes (base: SupportedEncodings): (value: string) => Uint8Array { return (buf) => { return uint8ArrayFromString(buf, base) } @@ -25,7 +25,7 @@ export function bytes2port (buf: Uint8Array): string { return view.getUint16(buf.byteOffset).toString() } -export function port2bytes (port: string | number): Uint8Array { +export function port2bytes (port: string | number): Uint8Array { const buf = new ArrayBuffer(2) const view = new DataView(buf) view.setUint16(0, typeof port === 'string' ? parseInt(port) : port) @@ -33,7 +33,7 @@ export function port2bytes (port: string | number): Uint8Array { return new Uint8Array(buf) } -export function onion2bytes (str: string): Uint8Array { +export function onion2bytes (str: string): Uint8Array { const addr = str.split(':') if (addr.length !== 2) { @@ -59,7 +59,7 @@ export function onion2bytes (str: string): Uint8Array { return uint8ArrayConcat([buf, portBuf], buf.length + portBuf.length) } -export function onion32bytes (str: string): Uint8Array { +export function onion32bytes (str: string): Uint8Array { const addr = str.split(':') if (addr.length !== 2) { @@ -95,7 +95,7 @@ export function bytes2onion (buf: Uint8Array): string { // Copied from https://github.com/indutny/node-ip/blob/master/lib/ip.js#L7 // but with buf/offset args removed because we don't use them -export const ip4ToBytes = function (ip: string): Uint8Array { +export const ip4ToBytes = function (ip: string): Uint8Array { ip = ip.toString().trim() const bytes = new Uint8Array(4) @@ -115,7 +115,7 @@ export const ip4ToBytes = function (ip: string): Uint8Array { // Copied from https://github.com/indutny/node-ip/blob/master/lib/ip.js#L7 // but with buf/offset args removed because we don't use them -export const ip6ToBytes = function (ip: string): Uint8Array { +export const ip6ToBytes = function (ip: string): Uint8Array { let offset = 0 ip = ip.toString().trim() @@ -228,7 +228,7 @@ const anybaseDecoder = (function () { return acc })() -export function mb2bytes (mbstr: string): Uint8Array { +export function mb2bytes (mbstr: string): Uint8Array { return anybaseDecoder.decode(mbstr) }