Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@libp2p/peer-id

Package Overview
Dependencies
Maintainers
6
Versions
569
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@libp2p/peer-id - npm Package Compare versions

Comparing version 4.2.4-50b897139 to 4.2.4-5214dec4a

dist/src/peer-id.d.ts

28

dist/src/index.d.ts

@@ -16,22 +16,16 @@ /**

*/
import { type Ed25519PeerId, type PeerIdType, type RSAPeerId, type URLPeerId, type Secp256k1PeerId } from '@libp2p/interface';
import { CID } from 'multiformats/cid';
import type { MultibaseDecoder } from 'multiformats/bases/interface';
import { type CID, type MultibaseDecoder } from 'multiformats/cid';
import type { Ed25519PeerId, RSAPeerId, URLPeerId, Secp256k1PeerId, PeerId, PublicKey, Ed25519PublicKey, Secp256k1PublicKey, RSAPublicKey, Ed25519PrivateKey, Secp256k1PrivateKey, RSAPrivateKey, PrivateKey } from '@libp2p/interface';
import type { MultihashDigest } from 'multiformats/hashes/interface';
interface PeerIdInit {
type: PeerIdType;
multihash: MultihashDigest;
privateKey?: Uint8Array;
}
export declare function createPeerId(init: PeerIdInit): Ed25519PeerId | Secp256k1PeerId | RSAPeerId;
export declare function peerIdFromPeerId(other: any): Ed25519PeerId | Secp256k1PeerId | RSAPeerId;
export declare function peerIdFromString(str: string, decoder?: MultibaseDecoder<any>): Ed25519PeerId | Secp256k1PeerId | RSAPeerId | URLPeerId;
export declare function peerIdFromBytes(buf: Uint8Array): Ed25519PeerId | Secp256k1PeerId | RSAPeerId | URLPeerId;
export declare function peerIdFromPublicKey(publicKey: Ed25519PublicKey): Ed25519PeerId;
export declare function peerIdFromPublicKey(publicKey: Secp256k1PublicKey): Secp256k1PeerId;
export declare function peerIdFromPublicKey(publicKey: RSAPublicKey): RSAPeerId;
export declare function peerIdFromPublicKey(publicKey: PublicKey): PeerId;
export declare function peerIdFromPrivateKey(privateKey: Ed25519PrivateKey): Ed25519PeerId;
export declare function peerIdFromPrivateKey(privateKey: Secp256k1PrivateKey): Secp256k1PeerId;
export declare function peerIdFromPrivateKey(privateKey: RSAPrivateKey): RSAPeerId;
export declare function peerIdFromPrivateKey(privateKey: PrivateKey): PeerId;
export declare function peerIdFromMultihash(multihash: MultihashDigest): PeerId;
export declare function peerIdFromCID(cid: CID): Ed25519PeerId | Secp256k1PeerId | RSAPeerId | URLPeerId;
/**
* @param publicKey - A marshalled public key
* @param privateKey - A marshalled private key
*/
export declare function peerIdFromKeys(publicKey: Uint8Array, privateKey?: Uint8Array): Promise<Ed25519PeerId | Secp256k1PeerId | RSAPeerId>;
export {};
//# sourceMappingURL=index.d.ts.map

@@ -16,255 +16,91 @@ /**

*/
import { CodeError } from '@libp2p/interface';
import { peerIdSymbol } from '@libp2p/interface';
import { publicKeyFromMultihash } from '@libp2p/crypto/keys';
import { InvalidCIDError, InvalidMultihashError, InvalidParametersError, UnsupportedKeyTypeError } from '@libp2p/interface';
import { base58btc } from 'multiformats/bases/base58';
import { bases } from 'multiformats/basics';
import { CID } from 'multiformats/cid';
import {} from 'multiformats/cid';
import * as Digest from 'multiformats/hashes/digest';
import { identity } from 'multiformats/hashes/identity';
import { sha256 } from 'multiformats/hashes/sha2';
import { equals as uint8ArrayEquals } from 'uint8arrays/equals';
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
const inspect = Symbol.for('nodejs.util.inspect.custom');
const baseDecoder = Object
.values(bases)
.map(codec => codec.decoder)
// @ts-expect-error https://github.com/multiformats/js-multiformats/issues/141
.reduce((acc, curr) => acc.or(curr), bases.identity.decoder);
import { RSAPeerId as RSAPeerIdClass, Ed25519PeerId as Ed25519PeerIdClass, Secp256k1PeerId as Secp256k1PeerIdClass, URLPeerId as URLPeerIdClass } from './peer-id.js';
// these values are from https://github.com/multiformats/multicodec/blob/master/table.csv
const LIBP2P_KEY_CODE = 0x72;
const MARSHALLED_ED225519_PUBLIC_KEY_LENGTH = 36;
const MARSHALLED_SECP256K1_PUBLIC_KEY_LENGTH = 37;
class PeerIdImpl {
type;
multihash;
privateKey;
publicKey;
string;
constructor(init) {
this.type = init.type;
this.multihash = init.multihash;
this.privateKey = init.privateKey;
// mark string cache as non-enumerable
Object.defineProperty(this, 'string', {
enumerable: false,
writable: true
});
}
get [Symbol.toStringTag]() {
return `PeerId(${this.toString()})`;
}
[peerIdSymbol] = true;
toString() {
if (this.string == null) {
this.string = base58btc.encode(this.multihash.bytes).slice(1);
}
return this.string;
}
// return self-describing String representation
// in default format from RFC 0001: https://github.com/libp2p/specs/pull/209
toCID() {
return CID.createV1(LIBP2P_KEY_CODE, this.multihash);
}
toBytes() {
return this.multihash.bytes;
}
/**
* Returns Multiaddr as a JSON string
*/
toJSON() {
return this.toString();
}
/**
* Checks the equality of `this` peer against a given PeerId
*/
equals(id) {
if (id == null) {
return false;
}
if (id instanceof Uint8Array) {
return uint8ArrayEquals(this.multihash.bytes, id);
}
else if (typeof id === 'string') {
return peerIdFromString(id).equals(this);
}
else if (id?.multihash?.bytes != null) {
return uint8ArrayEquals(this.multihash.bytes, id.multihash.bytes);
}
else {
throw new Error('not valid Id');
}
}
/**
* Returns PeerId as a human-readable string
* https://nodejs.org/api/util.html#utilinspectcustom
*
* @example
* ```TypeScript
* import { peerIdFromString } from '@libp2p/peer-id'
*
* console.info(peerIdFromString('QmFoo'))
* // 'PeerId(QmFoo)'
* ```
*/
[inspect]() {
return `PeerId(${this.toString()})`;
}
}
class RSAPeerIdImpl extends PeerIdImpl {
type = 'RSA';
publicKey;
constructor(init) {
super({ ...init, type: 'RSA' });
this.publicKey = init.publicKey;
}
}
class Ed25519PeerIdImpl extends PeerIdImpl {
type = 'Ed25519';
publicKey;
constructor(init) {
super({ ...init, type: 'Ed25519' });
this.publicKey = init.multihash.digest;
}
}
class Secp256k1PeerIdImpl extends PeerIdImpl {
type = 'secp256k1';
publicKey;
constructor(init) {
super({ ...init, type: 'secp256k1' });
this.publicKey = init.multihash.digest;
}
}
// these values are from https://github.com/multiformats/multicodec/blob/master/table.csv
const TRANSPORT_IPFS_GATEWAY_HTTP_CODE = 0x0920;
class URLPeerIdImpl {
type = 'url';
multihash;
privateKey;
publicKey;
url;
constructor(url) {
this.url = url.toString();
this.multihash = identity.digest(uint8ArrayFromString(this.url));
export function peerIdFromString(str, decoder) {
let multihash;
if (str.charAt(0) === '1' || str.charAt(0) === 'Q') {
// identity hash ed25519/secp256k1 key or sha2-256 hash of
// rsa public key - base58btc encoded either way
multihash = Digest.decode(base58btc.decode(`z${str}`));
}
[inspect]() {
return `PeerId(${this.url})`;
}
[peerIdSymbol] = true;
toString() {
return this.toCID().toString();
}
toCID() {
return CID.createV1(TRANSPORT_IPFS_GATEWAY_HTTP_CODE, this.multihash);
}
toBytes() {
return this.toCID().bytes;
}
equals(other) {
if (other == null) {
return false;
else {
if (decoder == null) {
throw new InvalidParametersError('Please pass a multibase decoder for strings that do not start with "1" or "Q"');
}
if (other instanceof Uint8Array) {
other = uint8ArrayToString(other);
}
return other.toString() === this.toString();
multihash = Digest.decode(decoder.decode(str));
}
return peerIdFromMultihash(multihash);
}
export function createPeerId(init) {
if (init.type === 'RSA') {
return new RSAPeerIdImpl(init);
export function peerIdFromPublicKey(publicKey) {
if (publicKey.type === 'Ed25519') {
return new Ed25519PeerIdClass({
multihash: publicKey.toCID().multihash,
publicKey
});
}
if (init.type === 'Ed25519') {
return new Ed25519PeerIdImpl(init);
else if (publicKey.type === 'secp256k1') {
return new Secp256k1PeerIdClass({
multihash: publicKey.toCID().multihash,
publicKey
});
}
if (init.type === 'secp256k1') {
return new Secp256k1PeerIdImpl(init);
else if (publicKey.type === 'RSA') {
return new RSAPeerIdClass({
multihash: publicKey.toCID().multihash,
publicKey
});
}
throw new CodeError('Type must be "RSA", "Ed25519" or "secp256k1"', 'ERR_INVALID_PARAMETERS');
throw new UnsupportedKeyTypeError();
}
export function peerIdFromPeerId(other) {
if (other.type === 'RSA') {
return new RSAPeerIdImpl(other);
}
if (other.type === 'Ed25519') {
return new Ed25519PeerIdImpl(other);
}
if (other.type === 'secp256k1') {
return new Secp256k1PeerIdImpl(other);
}
throw new CodeError('Not a PeerId', 'ERR_INVALID_PARAMETERS');
export function peerIdFromPrivateKey(privateKey) {
return peerIdFromPublicKey(privateKey.publicKey);
}
export function peerIdFromString(str, decoder) {
decoder = decoder ?? baseDecoder;
if (str.charAt(0) === '1' || str.charAt(0) === 'Q') {
// identity hash ed25519/secp256k1 key or sha2-256 hash of
// rsa public key - base58btc encoded either way
const multihash = Digest.decode(base58btc.decode(`z${str}`));
if (str.startsWith('12D')) {
return new Ed25519PeerIdImpl({ multihash });
}
else if (str.startsWith('16U')) {
return new Secp256k1PeerIdImpl({ multihash });
}
else {
return new RSAPeerIdImpl({ multihash });
}
export function peerIdFromMultihash(multihash) {
if (isSha256Multihash(multihash)) {
return new RSAPeerIdClass({ multihash });
}
return peerIdFromBytes(baseDecoder.decode(str));
}
export function peerIdFromBytes(buf) {
try {
const multihash = Digest.decode(buf);
if (multihash.code === identity.code) {
if (multihash.digest.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
return new Ed25519PeerIdImpl({ multihash });
else if (isIdentityMultihash(multihash)) {
try {
const publicKey = publicKeyFromMultihash(multihash);
if (publicKey.type === 'Ed25519') {
return new Ed25519PeerIdClass({ multihash, publicKey });
}
else if (multihash.digest.length === MARSHALLED_SECP256K1_PUBLIC_KEY_LENGTH) {
return new Secp256k1PeerIdImpl({ multihash });
else if (publicKey.type === 'secp256k1') {
return new Secp256k1PeerIdClass({ multihash, publicKey });
}
}
if (multihash.code === sha256.code) {
return new RSAPeerIdImpl({ multihash });
catch (err) {
// was not Ed or secp key, try URL
const url = uint8ArrayToString(multihash.digest);
return new URLPeerIdClass(new URL(url));
}
}
catch {
return peerIdFromCID(CID.decode(buf));
}
throw new Error('Supplied PeerID CID is invalid');
throw new InvalidMultihashError('Supplied PeerID Multihash is invalid');
}
export function peerIdFromCID(cid) {
if (cid?.multihash == null || cid.version == null || (cid.version === 1 && (cid.code !== LIBP2P_KEY_CODE) && cid.code !== TRANSPORT_IPFS_GATEWAY_HTTP_CODE)) {
throw new Error('Supplied PeerID CID is invalid');
throw new InvalidCIDError('Supplied PeerID CID is invalid');
}
if (cid.code === TRANSPORT_IPFS_GATEWAY_HTTP_CODE) {
const url = uint8ArrayToString(cid.multihash.digest);
return new URLPeerIdImpl(new URL(url));
return new URLPeerIdClass(new URL(url));
}
const multihash = cid.multihash;
if (multihash.code === sha256.code) {
return new RSAPeerIdImpl({ multihash: cid.multihash });
}
else if (multihash.code === identity.code) {
if (multihash.digest.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
return new Ed25519PeerIdImpl({ multihash: cid.multihash });
}
else if (multihash.digest.length === MARSHALLED_SECP256K1_PUBLIC_KEY_LENGTH) {
return new Secp256k1PeerIdImpl({ multihash: cid.multihash });
}
}
throw new Error('Supplied PeerID CID is invalid');
return peerIdFromMultihash(cid.multihash);
}
/**
* @param publicKey - A marshalled public key
* @param privateKey - A marshalled private key
*/
export async function peerIdFromKeys(publicKey, privateKey) {
if (publicKey.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
return new Ed25519PeerIdImpl({ multihash: Digest.create(identity.code, publicKey), privateKey });
}
if (publicKey.length === MARSHALLED_SECP256K1_PUBLIC_KEY_LENGTH) {
return new Secp256k1PeerIdImpl({ multihash: Digest.create(identity.code, publicKey), privateKey });
}
return new RSAPeerIdImpl({ multihash: await sha256.digest(publicKey), publicKey, privateKey });
function isIdentityMultihash(multihash) {
return multihash.code === identity.code;
}
function isSha256Multihash(multihash) {
return multihash.code === sha256.code;
}
//# sourceMappingURL=index.js.map
{
"name": "@libp2p/peer-id",
"version": "4.2.4-50b897139",
"version": "4.2.4-5214dec4a",
"description": "Implementation of @libp2p/interface-peer-id",

@@ -57,3 +57,4 @@ "license": "Apache-2.0 OR MIT",

"dependencies": {
"@libp2p/interface": "1.7.0-50b897139",
"@libp2p/crypto": "4.1.9-5214dec4a",
"@libp2p/interface": "1.7.0-5214dec4a",
"multiformats": "^13.1.0",

@@ -60,0 +61,0 @@ "uint8arrays": "^5.1.0"

@@ -17,289 +17,90 @@ /**

import { CodeError } from '@libp2p/interface'
import { type Ed25519PeerId, type PeerIdType, type RSAPeerId, type URLPeerId, type Secp256k1PeerId, peerIdSymbol, type PeerId } from '@libp2p/interface'
import { publicKeyFromMultihash } from '@libp2p/crypto/keys'
import { InvalidCIDError, InvalidMultihashError, InvalidParametersError, UnsupportedKeyTypeError } from '@libp2p/interface'
import { base58btc } from 'multiformats/bases/base58'
import { bases } from 'multiformats/basics'
import { CID } from 'multiformats/cid'
import { type CID, type MultibaseDecoder } from 'multiformats/cid'
import * as Digest from 'multiformats/hashes/digest'
import { identity } from 'multiformats/hashes/identity'
import { sha256 } from 'multiformats/hashes/sha2'
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import type { MultibaseDecoder } from 'multiformats/bases/interface'
import { RSAPeerId as RSAPeerIdClass, Ed25519PeerId as Ed25519PeerIdClass, Secp256k1PeerId as Secp256k1PeerIdClass, URLPeerId as URLPeerIdClass } from './peer-id.js'
import type { Ed25519PeerId, RSAPeerId, URLPeerId, Secp256k1PeerId, PeerId, PublicKey, Ed25519PublicKey, Secp256k1PublicKey, RSAPublicKey, Ed25519PrivateKey, Secp256k1PrivateKey, RSAPrivateKey, PrivateKey } from '@libp2p/interface'
import type { MultihashDigest } from 'multiformats/hashes/interface'
const inspect = Symbol.for('nodejs.util.inspect.custom')
const baseDecoder = Object
.values(bases)
.map(codec => codec.decoder)
// @ts-expect-error https://github.com/multiformats/js-multiformats/issues/141
.reduce((acc, curr) => acc.or(curr), bases.identity.decoder)
// these values are from https://github.com/multiformats/multicodec/blob/master/table.csv
const LIBP2P_KEY_CODE = 0x72
const MARSHALLED_ED225519_PUBLIC_KEY_LENGTH = 36
const MARSHALLED_SECP256K1_PUBLIC_KEY_LENGTH = 37
interface PeerIdInit {
type: PeerIdType
multihash: MultihashDigest
privateKey?: Uint8Array
}
interface RSAPeerIdInit {
multihash: MultihashDigest
privateKey?: Uint8Array
publicKey?: Uint8Array
}
interface Ed25519PeerIdInit {
multihash: MultihashDigest
privateKey?: Uint8Array
}
interface Secp256k1PeerIdInit {
multihash: MultihashDigest
privateKey?: Uint8Array
}
class PeerIdImpl {
public type: PeerIdType
public readonly multihash: MultihashDigest
public readonly privateKey?: Uint8Array
public readonly publicKey?: Uint8Array
private string?: string
constructor (init: PeerIdInit) {
this.type = init.type
this.multihash = init.multihash
this.privateKey = init.privateKey
// mark string cache as non-enumerable
Object.defineProperty(this, 'string', {
enumerable: false,
writable: true
})
}
get [Symbol.toStringTag] (): string {
return `PeerId(${this.toString()})`
}
readonly [peerIdSymbol] = true
toString (): string {
if (this.string == null) {
this.string = base58btc.encode(this.multihash.bytes).slice(1)
}
return this.string
}
// return self-describing String representation
// in default format from RFC 0001: https://github.com/libp2p/specs/pull/209
toCID (): CID {
return CID.createV1(LIBP2P_KEY_CODE, this.multihash)
}
toBytes (): Uint8Array {
return this.multihash.bytes
}
/**
* Returns Multiaddr as a JSON string
*/
toJSON (): string {
return this.toString()
}
/**
* Checks the equality of `this` peer against a given PeerId
*/
equals (id?: PeerId | Uint8Array | string): boolean {
if (id == null) {
return false
}
if (id instanceof Uint8Array) {
return uint8ArrayEquals(this.multihash.bytes, id)
} else if (typeof id === 'string') {
return peerIdFromString(id).equals(this as PeerId)
} else if (id?.multihash?.bytes != null) {
return uint8ArrayEquals(this.multihash.bytes, id.multihash.bytes)
} else {
throw new Error('not valid Id')
}
}
/**
* Returns PeerId as a human-readable string
* https://nodejs.org/api/util.html#utilinspectcustom
*
* @example
* ```TypeScript
* import { peerIdFromString } from '@libp2p/peer-id'
*
* console.info(peerIdFromString('QmFoo'))
* // 'PeerId(QmFoo)'
* ```
*/
[inspect] (): string {
return `PeerId(${this.toString()})`
}
}
class RSAPeerIdImpl extends PeerIdImpl implements RSAPeerId {
public readonly type = 'RSA'
public readonly publicKey?: Uint8Array
constructor (init: RSAPeerIdInit) {
super({ ...init, type: 'RSA' })
this.publicKey = init.publicKey
}
}
class Ed25519PeerIdImpl extends PeerIdImpl implements Ed25519PeerId {
public readonly type = 'Ed25519'
public readonly publicKey: Uint8Array
constructor (init: Ed25519PeerIdInit) {
super({ ...init, type: 'Ed25519' })
this.publicKey = init.multihash.digest
}
}
class Secp256k1PeerIdImpl extends PeerIdImpl implements Secp256k1PeerId {
public readonly type = 'secp256k1'
public readonly publicKey: Uint8Array
constructor (init: Secp256k1PeerIdInit) {
super({ ...init, type: 'secp256k1' })
this.publicKey = init.multihash.digest
}
}
// these values are from https://github.com/multiformats/multicodec/blob/master/table.csv
const TRANSPORT_IPFS_GATEWAY_HTTP_CODE = 0x0920
class URLPeerIdImpl implements URLPeerId {
readonly type = 'url'
readonly multihash: MultihashDigest
readonly privateKey?: Uint8Array
readonly publicKey?: Uint8Array
readonly url: string
export function peerIdFromString (str: string, decoder?: MultibaseDecoder<any>): Ed25519PeerId | Secp256k1PeerId | RSAPeerId | URLPeerId {
let multihash: MultihashDigest
constructor (url: URL) {
this.url = url.toString()
this.multihash = identity.digest(uint8ArrayFromString(this.url))
}
[inspect] (): string {
return `PeerId(${this.url})`
}
readonly [peerIdSymbol] = true
toString (): string {
return this.toCID().toString()
}
toCID (): CID {
return CID.createV1(TRANSPORT_IPFS_GATEWAY_HTTP_CODE, this.multihash)
}
toBytes (): Uint8Array {
return this.toCID().bytes
}
equals (other?: PeerId | Uint8Array | string): boolean {
if (other == null) {
return false
if (str.charAt(0) === '1' || str.charAt(0) === 'Q') {
// identity hash ed25519/secp256k1 key or sha2-256 hash of
// rsa public key - base58btc encoded either way
multihash = Digest.decode(base58btc.decode(`z${str}`))
} else {
if (decoder == null) {
throw new InvalidParametersError('Please pass a multibase decoder for strings that do not start with "1" or "Q"')
}
if (other instanceof Uint8Array) {
other = uint8ArrayToString(other)
}
return other.toString() === this.toString()
multihash = Digest.decode(decoder.decode(str))
}
}
export function createPeerId (init: PeerIdInit): Ed25519PeerId | Secp256k1PeerId | RSAPeerId {
if (init.type === 'RSA') {
return new RSAPeerIdImpl(init)
}
if (init.type === 'Ed25519') {
return new Ed25519PeerIdImpl(init)
}
if (init.type === 'secp256k1') {
return new Secp256k1PeerIdImpl(init)
}
throw new CodeError('Type must be "RSA", "Ed25519" or "secp256k1"', 'ERR_INVALID_PARAMETERS')
return peerIdFromMultihash(multihash)
}
export function peerIdFromPeerId (other: any): Ed25519PeerId | Secp256k1PeerId | RSAPeerId {
if (other.type === 'RSA') {
return new RSAPeerIdImpl(other)
export function peerIdFromPublicKey (publicKey: Ed25519PublicKey): Ed25519PeerId
export function peerIdFromPublicKey (publicKey: Secp256k1PublicKey): Secp256k1PeerId
export function peerIdFromPublicKey (publicKey: RSAPublicKey): RSAPeerId
export function peerIdFromPublicKey (publicKey: PublicKey): PeerId
export function peerIdFromPublicKey (publicKey: PublicKey): PeerId {
if (publicKey.type === 'Ed25519') {
return new Ed25519PeerIdClass({
multihash: publicKey.toCID().multihash,
publicKey
})
} else if (publicKey.type === 'secp256k1') {
return new Secp256k1PeerIdClass({
multihash: publicKey.toCID().multihash,
publicKey
})
} else if (publicKey.type === 'RSA') {
return new RSAPeerIdClass({
multihash: publicKey.toCID().multihash,
publicKey
})
}
if (other.type === 'Ed25519') {
return new Ed25519PeerIdImpl(other)
}
if (other.type === 'secp256k1') {
return new Secp256k1PeerIdImpl(other)
}
throw new CodeError('Not a PeerId', 'ERR_INVALID_PARAMETERS')
throw new UnsupportedKeyTypeError()
}
export function peerIdFromString (str: string, decoder?: MultibaseDecoder<any>): Ed25519PeerId | Secp256k1PeerId | RSAPeerId | URLPeerId {
decoder = decoder ?? baseDecoder
if (str.charAt(0) === '1' || str.charAt(0) === 'Q') {
// identity hash ed25519/secp256k1 key or sha2-256 hash of
// rsa public key - base58btc encoded either way
const multihash = Digest.decode(base58btc.decode(`z${str}`))
if (str.startsWith('12D')) {
return new Ed25519PeerIdImpl({ multihash })
} else if (str.startsWith('16U')) {
return new Secp256k1PeerIdImpl({ multihash })
} else {
return new RSAPeerIdImpl({ multihash })
}
}
return peerIdFromBytes(baseDecoder.decode(str))
export function peerIdFromPrivateKey (privateKey: Ed25519PrivateKey): Ed25519PeerId
export function peerIdFromPrivateKey (privateKey: Secp256k1PrivateKey): Secp256k1PeerId
export function peerIdFromPrivateKey (privateKey: RSAPrivateKey): RSAPeerId
export function peerIdFromPrivateKey (privateKey: PrivateKey): PeerId
export function peerIdFromPrivateKey (privateKey: PrivateKey): PeerId {
return peerIdFromPublicKey(privateKey.publicKey)
}
export function peerIdFromBytes (buf: Uint8Array): Ed25519PeerId | Secp256k1PeerId | RSAPeerId | URLPeerId {
try {
const multihash = Digest.decode(buf)
export function peerIdFromMultihash (multihash: MultihashDigest): PeerId {
if (isSha256Multihash(multihash)) {
return new RSAPeerIdClass({ multihash })
} else if (isIdentityMultihash(multihash)) {
try {
const publicKey = publicKeyFromMultihash(multihash)
if (multihash.code === identity.code) {
if (multihash.digest.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
return new Ed25519PeerIdImpl({ multihash })
} else if (multihash.digest.length === MARSHALLED_SECP256K1_PUBLIC_KEY_LENGTH) {
return new Secp256k1PeerIdImpl({ multihash })
if (publicKey.type === 'Ed25519') {
return new Ed25519PeerIdClass({ multihash, publicKey })
} else if (publicKey.type === 'secp256k1') {
return new Secp256k1PeerIdClass({ multihash, publicKey })
}
}
} catch (err) {
// was not Ed or secp key, try URL
const url = uint8ArrayToString(multihash.digest)
if (multihash.code === sha256.code) {
return new RSAPeerIdImpl({ multihash })
return new URLPeerIdClass(new URL(url))
}
} catch {
return peerIdFromCID(CID.decode(buf))
}
throw new Error('Supplied PeerID CID is invalid')
throw new InvalidMultihashError('Supplied PeerID Multihash is invalid')
}

@@ -309,3 +110,3 @@

if (cid?.multihash == null || cid.version == null || (cid.version === 1 && (cid.code !== LIBP2P_KEY_CODE) && cid.code !== TRANSPORT_IPFS_GATEWAY_HTTP_CODE)) {
throw new Error('Supplied PeerID CID is invalid')
throw new InvalidCIDError('Supplied PeerID CID is invalid')
}

@@ -316,34 +117,14 @@

return new URLPeerIdImpl(new URL(url))
return new URLPeerIdClass(new URL(url))
}
const multihash = cid.multihash
return peerIdFromMultihash(cid.multihash)
}
if (multihash.code === sha256.code) {
return new RSAPeerIdImpl({ multihash: cid.multihash })
} else if (multihash.code === identity.code) {
if (multihash.digest.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
return new Ed25519PeerIdImpl({ multihash: cid.multihash })
} else if (multihash.digest.length === MARSHALLED_SECP256K1_PUBLIC_KEY_LENGTH) {
return new Secp256k1PeerIdImpl({ multihash: cid.multihash })
}
}
throw new Error('Supplied PeerID CID is invalid')
function isIdentityMultihash (multihash: MultihashDigest): multihash is MultihashDigest<0x0> {
return multihash.code === identity.code
}
/**
* @param publicKey - A marshalled public key
* @param privateKey - A marshalled private key
*/
export async function peerIdFromKeys (publicKey: Uint8Array, privateKey?: Uint8Array): Promise<Ed25519PeerId | Secp256k1PeerId | RSAPeerId> {
if (publicKey.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
return new Ed25519PeerIdImpl({ multihash: Digest.create(identity.code, publicKey), privateKey })
}
if (publicKey.length === MARSHALLED_SECP256K1_PUBLIC_KEY_LENGTH) {
return new Secp256k1PeerIdImpl({ multihash: Digest.create(identity.code, publicKey), privateKey })
}
return new RSAPeerIdImpl({ multihash: await sha256.digest(publicKey), publicKey, privateKey })
function isSha256Multihash (multihash: MultihashDigest): multihash is MultihashDigest<0x12> {
return multihash.code === sha256.code
}

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc