Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { InvalidMultiaddrError } from './errors.ts'
import { registry } from './registry.js'
import type { Multiaddr } from './index.ts'
import type { MultibaseCodec } from 'multiformats'
import type { SupportedEncodings } from 'uint8arrays/to-string'
Expand Down Expand Up @@ -259,3 +260,40 @@ export function convertToIpNet (multiaddr: Multiaddr): IpNet {

return new IpNet(addr, mask)
}

/**
* converts (serializes) addresses
*
* @deprecated Will be removed in a future release
*/
export function convert (proto: string, a: string): Uint8Array
export function convert (proto: string, a: Uint8Array): string
export function convert (proto: string, a: string | Uint8Array): Uint8Array | string {
if (a instanceof Uint8Array) {
return convertToString(proto, a)
} else {
return convertToBytes(proto, a)
}
}

/**
* Convert [code, Uint8Array] to string
*
* @deprecated Will be removed in a future release
*/
export function convertToString (proto: number | string, buf: Uint8Array): string {
const protocol = registry.getCodec(proto)

return protocol.bytesToValue?.(buf) ?? uint8ArrayToString(buf, 'base16') // no clue. convert to hex
}

/**
* Convert [code, string] to Uint8Array
*
* @deprecated Will be removed in a future release
*/
export function convertToBytes (proto: string | number, str: string): Uint8Array {
const protocol = registry.getCodec(proto)

return protocol.valueToBytes?.(str) ?? uint8ArrayFromString(str, 'base16') // no clue. convert from hex
}