Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: decode more multibase types #152

Merged
merged 2 commits into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions .aegir.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

module.exports = {
bundlesize: {
maxSize: '140kB'
build: {
bundlesizeMax: '121KB'
}
}
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,27 @@
"@types/chai": "^4.2.14",
"@types/dirty-chai": "^2.0.2",
"@types/mocha": "^8.2.0",
"aegir": "^33.0.0",
"multihashes": "^4.0.2",
"aegir": "^34.0.2",
"util": "^0.12.3"
},
"dependencies": {
"class-is": "^1.1.0",
"libp2p-crypto": "^0.19.0",
"minimist": "^1.2.5",
"multiformats": "^9.0.0",
"multiformats": "^9.3.0",
"protobufjs": "^6.10.2",
"uint8arrays": "^2.0.5"
},
"repository": {
"type": "git",
"url": "https://github.com/libp2p/js-peer-id.git"
},
"eslintConfig": {
"extends": "ipfs",
"ignorePatterns": [
"proto.d.ts"
]
},
"contributors": [
"David Dias <daviddias.p@gmail.com>",
"Vasco Santos <vasco.santos@moxy.studio>",
Expand Down
121 changes: 68 additions & 53 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,199 +1,214 @@
import { PrivateKey, PublicKey, KeyType } from "libp2p-crypto";
import { PrivateKey, PublicKey, KeyType } from 'libp2p-crypto'
import { CID } from 'multiformats/cid'

declare namespace PeerId {
/**
* Options for PeerId creation.
*/
type CreateOptions = {
interface CreateOptions {
/**
* The number of bits to use.
*/
bits?: number;
bits?: number
/**
* The type of key to use.
*/
keyType?: KeyType;
};
keyType?: KeyType
}

/**
* PeerId JSON format.
*/
type JSONPeerId = {
interface JSONPeerId {
/**
* String representation of PeerId.
*/
id: string;
id: string
/**
* Public key.
*/
pubKey?: string;
pubKey?: string
/**
* Private key.
*/
privKey?: string;
};
privKey?: string
}

/**
* Checks if a value is an instance of PeerId.
* @param id The value to check.
*
* @param id - The value to check.
*/
function isPeerId(id: any): id is PeerId
function isPeerId (id: any): id is PeerId

/**
* Create a new PeerId.
* @param opts Options.
*
* @param opts - Options.
*/
function create(opts?: PeerId.CreateOptions): Promise<PeerId>;
function create (opts?: PeerId.CreateOptions): Promise<PeerId>

/**
* Create PeerId from hex string.
* @param str The input hex string.
*
* @param str - The input hex string.
*/
function createFromHexString(str: string): PeerId;
function createFromHexString (str: string): PeerId

/**
* Create PeerId from raw bytes.
* @param buf The raw bytes.
*
* @param buf - The raw bytes.
*/
function createFromBytes(buf: Uint8Array): PeerId;
function createFromBytes (buf: Uint8Array): PeerId

/**
* Create PeerId from base58-encoded string.
* @param str The base58-encoded string.
*
* @param str - The base58-encoded string.
*/
function createFromB58String(str: string): PeerId;
function createFromB58String (str: string): PeerId

/**
* Create PeerId from CID.
* @param cid The CID.
*
* @param cid - The CID.
*/
function createFromCID(cid: CID): PeerId;
function createFromCID (cid: CID): PeerId

/**
* Create PeerId from public key.
* @param key Public key, as Uint8Array or base64-encoded string.
*
* @param key - Public key, as Uint8Array or base64-encoded string.
*/
function createFromPubKey(key: Uint8Array | string): Promise<PeerId>;
function createFromPubKey (key: Uint8Array | string): Promise<PeerId>

/**
* Create PeerId from private key.
* @param key Private key, as Uint8Array or base64-encoded string.
*
* @param key - Private key, as Uint8Array or base64-encoded string.
*/
function createFromPrivKey(key: Uint8Array | string): Promise<PeerId>;
function createFromPrivKey (key: Uint8Array | string): Promise<PeerId>

/**
* Create PeerId from PeerId JSON formatted object.
*
* @see {@link PeerId#toJSON}
* @param json PeerId in JSON format.
* @param json - PeerId in JSON format.
*/
function createFromJSON(json: JSONPeerId): Promise<PeerId>;
function createFromJSON (json: JSONPeerId): Promise<PeerId>

/**
* Create PeerId from Protobuf bytes.
* @param buf Protobuf bytes, as Uint8Array or hex-encoded string.
*
* @param buf - Protobuf bytes, as Uint8Array or hex-encoded string.
*/
function createFromProtobuf(buf: Uint8Array | string): Promise<PeerId>;
function createFromProtobuf (buf: Uint8Array | string): Promise<PeerId>

/**
* Parse a PeerId from a string.
* @param str encoded public key string.
*
* @param str - encoded public key string.
*/
function parse(str: string): PeerId;
function parse (str: string): PeerId
}

/**
* PeerId is an object representation of a peer identifier.
*/
declare class PeerId {
constructor(id: Uint8Array, privKey?: PrivateKey, pubKey?: PublicKey);
constructor (id: Uint8Array, privKey?: PrivateKey, pubKey?: PublicKey);

/**
* Raw id.
*/
readonly id: Uint8Array;
readonly id: Uint8Array

/**
* Private key.
*/
privKey: PrivateKey;
privKey: PrivateKey

/**
* Public key.
*/
pubKey: PublicKey;
pubKey: PublicKey

/**
* Return the protobuf version of the public key, matching go ipfs formatting.
*/
marshalPubKey(): Uint8Array;
marshalPubKey (): Uint8Array;

/**
* Return the protobuf version of the private key, matching go ipfs formatting.
*/
marshalPrivKey(): Uint8Array;
marshalPrivKey (): Uint8Array;

/**
* Return the protobuf version of the peer-id.
* @param excludePriv Whether to exclude the private key information from the output.
*
* @param excludePriv - Whether to exclude the private key information from the output.
*/
marshal(excludePriv?: boolean): Uint8Array;
marshal (excludePriv?: boolean): Uint8Array;

/**
* String representation.
*/
toPrint(): string;
toPrint (): string;

/**
* Return the jsonified version of the key.
* Matches the formatting of go-ipfs for its config file.
*
* @see {@link PeerId.createFromJSON}
*/
toJSON(): PeerId.JSONPeerId;
toJSON (): PeerId.JSONPeerId;

/**
* Encode to hex.
*/
toHexString(): string;
toHexString (): string;

/**
* Return raw id bytes.
*/
toBytes(): Uint8Array;
toBytes (): Uint8Array;

/**
* Encode to base58 string.
*/
toB58String(): string;
toB58String (): string;

/**
* Return self-describing string representation.
* Uses default format from RFC 0001: https://github.com/libp2p/specs/pull/209
*/
toString(): string;
toString (): string;

/**
* Checks the equality of `this` peer against a given PeerId.
* @param id The other PeerId.
*
* @param id - The other PeerId.
*/
equals(id: PeerId | Uint8Array): boolean;
equals (id: PeerId | Uint8Array): boolean;

/**
* Checks the equality of `this` peer against a given PeerId.
*
* @deprecated Use {.equals}
* @param id The other PeerId.
* @param id - The other PeerId.
*/
isEqual(id: PeerId | Uint8Array): boolean;
isEqual (id: PeerId | Uint8Array): boolean;

/**
* Check if this PeerId instance is valid (privKey -> pubKey -> Id)
*/
isValid(): boolean;
isValid (): boolean;

/**
* Check if the PeerId has an inline public key.
*/
hasInlinePublicKey(): boolean;
hasInlinePublicKey (): boolean;
}

export = PeerId;
export = PeerId
26 changes: 21 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
'use strict'

const { CID } = require('multiformats/cid')
const b32 = require('multiformats/bases/base32')
const b36 = require('multiformats/bases/base36')
const b58 = require('multiformats/bases/base58')
const b64 = require('multiformats/bases/base64')
const { base58btc } = require('multiformats/bases/base58')
const { base32 } = require('multiformats/bases/base32')
const { base16 } = require('multiformats/bases/base16')
const Digest = require('multiformats/hashes/digest')
const cryptoKeys = require('libp2p-crypto/src/keys')
Expand All @@ -16,6 +21,17 @@ const uint8ArrayFromString = require('uint8arrays/from-string')
const uint8ArrayToString = require('uint8arrays/to-string')
const { identity } = require('multiformats/hashes/identity')

const bases = {
...b32,
...b36,
...b58,
...b64
}
const baseDecoder = Object.keys(bases).reduce(
(acc, curr) => acc.or(bases[curr]),
base32.decoder
)

// these values are from https://github.com/multiformats/multicodec/blob/master/table.csv
const DAG_PB_CODE = 0x70
const LIBP2P_KEY_CODE = 0x72
Expand Down Expand Up @@ -389,13 +405,13 @@ exports.createFromProtobuf = async (buf) => {
}

exports.parse = (str) => {
if (str.charAt(0) === '1') {
// base58btc encoded public key
return exports.createFromBytes(base58btc.decode(`z${str}`))
if (str.charAt(0) === '1' || str.charAt(0) === 'Q') {
// identity hash ed25519 key or sha2-256 hash of rsa public key
// base58btc encoded either way
str = `z${str}`
}

// try to parse it as a regular base58btc multihash or base32 encoded CID
return exports.createFromCID(CID.parse(str))
return exports.createFromBytes(baseDecoder.decode(str))
}

exports.isPeerId = (peerId) => {
Expand Down
Loading