@libp2p/peer-id 1.1.0 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/index.d.ts +17 -42
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +109 -91
- package/dist/src/index.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +128 -100
package/dist/src/index.d.ts
CHANGED
|
@@ -1,64 +1,39 @@
|
|
|
1
1
|
import { CID } from 'multiformats/cid';
|
|
2
|
+
import { symbol } from '@libp2p/interfaces/peer-id';
|
|
2
3
|
import type { MultibaseDecoder, MultibaseEncoder } from 'multiformats/bases/interface';
|
|
3
4
|
import type { MultihashDigest } from 'multiformats/hashes/interface';
|
|
5
|
+
import type { PeerId } from '@libp2p/interfaces/peer-id';
|
|
4
6
|
interface PeerIdOptions {
|
|
5
7
|
type: 'RSA' | 'Ed25519' | 'secp256k1';
|
|
6
8
|
multihash: MultihashDigest;
|
|
7
9
|
privateKey?: Uint8Array;
|
|
8
10
|
}
|
|
9
|
-
|
|
10
|
-
multihash: MultihashDigest;
|
|
11
|
-
privateKey?: Uint8Array;
|
|
12
|
-
publicKey?: Uint8Array;
|
|
13
|
-
}
|
|
14
|
-
interface Ed25519PeerIdOptions {
|
|
15
|
-
multihash: MultihashDigest;
|
|
16
|
-
privateKey?: Uint8Array;
|
|
17
|
-
}
|
|
18
|
-
interface Secp256k1PeerIdOptions {
|
|
19
|
-
multihash: MultihashDigest;
|
|
20
|
-
privateKey?: Uint8Array;
|
|
21
|
-
}
|
|
22
|
-
export declare class PeerId {
|
|
11
|
+
declare class PeerIdImpl {
|
|
23
12
|
type: 'RSA' | 'Ed25519' | 'secp256k1';
|
|
24
13
|
readonly multihash: MultihashDigest;
|
|
25
14
|
readonly privateKey?: Uint8Array;
|
|
26
15
|
readonly publicKey?: Uint8Array;
|
|
16
|
+
private readonly strings;
|
|
27
17
|
constructor(opts: PeerIdOptions);
|
|
18
|
+
get [Symbol.toStringTag](): string;
|
|
19
|
+
get [symbol](): boolean;
|
|
28
20
|
toString(codec?: MultibaseEncoder<any>): string;
|
|
29
21
|
toCID(): CID;
|
|
30
22
|
toBytes(): Uint8Array;
|
|
31
23
|
/**
|
|
32
|
-
* Checks the equality of `this` peer against a given PeerId
|
|
33
|
-
*
|
|
34
|
-
* @param {Uint8Array|PeerId} id
|
|
35
|
-
* @returns {boolean}
|
|
24
|
+
* Checks the equality of `this` peer against a given PeerId
|
|
36
25
|
*/
|
|
37
26
|
equals(id: any): boolean;
|
|
38
|
-
static fromPeerId(other: any): RSAPeerId | Ed25519PeerId | Secp256k1PeerId;
|
|
39
|
-
static fromString(str: string, decoder?: MultibaseDecoder<any>): RSAPeerId | Ed25519PeerId | Secp256k1PeerId;
|
|
40
|
-
static fromBytes(buf: Uint8Array): RSAPeerId | Ed25519PeerId | Secp256k1PeerId;
|
|
41
|
-
static fromCID(cid: CID): RSAPeerId | Ed25519PeerId | Secp256k1PeerId;
|
|
42
|
-
/**
|
|
43
|
-
* @param publicKey - A marshalled public key
|
|
44
|
-
* @param privateKey - A marshalled private key
|
|
45
|
-
*/
|
|
46
|
-
static fromKeys(publicKey: Uint8Array, privateKey?: Uint8Array): Promise<RSAPeerId | Ed25519PeerId | Secp256k1PeerId>;
|
|
47
|
-
}
|
|
48
|
-
export declare class RSAPeerId extends PeerId {
|
|
49
|
-
readonly type = "RSA";
|
|
50
|
-
readonly publicKey?: Uint8Array;
|
|
51
|
-
constructor(opts: RSAPeerIdOptions);
|
|
52
|
-
}
|
|
53
|
-
export declare class Ed25519PeerId extends PeerId {
|
|
54
|
-
readonly type = "Ed25519";
|
|
55
|
-
readonly publicKey: Uint8Array;
|
|
56
|
-
constructor(opts: Ed25519PeerIdOptions);
|
|
57
|
-
}
|
|
58
|
-
export declare class Secp256k1PeerId extends PeerId {
|
|
59
|
-
readonly type = "secp256k1";
|
|
60
|
-
readonly publicKey: Uint8Array;
|
|
61
|
-
constructor(opts: Secp256k1PeerIdOptions);
|
|
62
27
|
}
|
|
28
|
+
export declare function createPeerId(opts: PeerIdOptions): PeerIdImpl;
|
|
29
|
+
export declare function peerIdFromPeerId(other: any): PeerId;
|
|
30
|
+
export declare function peerIdFromString(str: string, decoder?: MultibaseDecoder<any>): PeerId;
|
|
31
|
+
export declare function peerIdFromBytes(buf: Uint8Array): PeerId;
|
|
32
|
+
export declare function peerIdFromCID(cid: CID): PeerId;
|
|
33
|
+
/**
|
|
34
|
+
* @param publicKey - A marshalled public key
|
|
35
|
+
* @param privateKey - A marshalled private key
|
|
36
|
+
*/
|
|
37
|
+
export declare function peerIdFromKeys(publicKey: Uint8Array, privateKey?: Uint8Array): Promise<PeerId>;
|
|
63
38
|
export {};
|
|
64
39
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAQtC,OAAO,KAAK,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAA;AACtF,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAQtC,OAAO,EAA6C,MAAM,EAAE,MAAM,4BAA4B,CAAA;AAC9F,OAAO,KAAK,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAA;AACtF,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AACpE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAA;AAcxD,UAAU,aAAa;IACrB,IAAI,EAAE,KAAK,GAAG,SAAS,GAAG,WAAW,CAAA;IACrC,SAAS,EAAE,eAAe,CAAA;IAC1B,UAAU,CAAC,EAAE,UAAU,CAAA;CACxB;AAkBD,cAAM,UAAU;IACP,IAAI,EAAE,KAAK,GAAG,SAAS,GAAG,WAAW,CAAA;IAC5C,SAAgB,SAAS,EAAE,eAAe,CAAA;IAC1C,SAAgB,UAAU,CAAC,EAAE,UAAU,CAAA;IACvC,SAAgB,SAAS,CAAC,EAAE,UAAU,CAAA;IACtC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAqB;gBAEhC,IAAI,EAAE,aAAa;IAYhC,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,WAEvB;IAED,IAAI,CAAC,MAAM,CAAC,YAEX;IAED,QAAQ,CAAE,KAAK,CAAC,EAAE,gBAAgB,CAAC,GAAG,CAAC;IAoBvC,KAAK;IAIL,OAAO;IAIP;;OAEG;IACH,MAAM,CAAE,EAAE,EAAE,GAAG,GAAG,OAAO;CAS1B;AAmCD,wBAAgB,YAAY,CAAE,IAAI,EAAE,aAAa,cAEhD;AAED,wBAAgB,gBAAgB,CAAE,KAAK,EAAE,GAAG,GAAG,MAAM,CAgBpD;AAED,wBAAgB,gBAAgB,CAAE,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,gBAAgB,CAAC,GAAG,CAAC,GAAG,MAAM,CAkBtF;AAED,wBAAgB,eAAe,CAAE,GAAG,EAAE,UAAU,UAoB/C;AAED,wBAAgB,aAAa,CAAE,GAAG,EAAE,GAAG,GAAG,MAAM,CAkB/C;AAED;;;GAGG;AACH,wBAAsB,cAAc,CAAE,SAAS,EAAE,UAAU,EAAE,UAAU,CAAC,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC,CAUrG"}
|
package/dist/src/index.js
CHANGED
|
@@ -6,6 +6,7 @@ import { identity } from 'multiformats/hashes/identity';
|
|
|
6
6
|
import { equals as uint8ArrayEquals } from 'uint8arrays/equals';
|
|
7
7
|
import { sha256 } from 'multiformats/hashes/sha2';
|
|
8
8
|
import errcode from 'err-code';
|
|
9
|
+
import { symbol } from '@libp2p/interfaces/peer-id';
|
|
9
10
|
const baseDecoder = Object
|
|
10
11
|
.values(bases)
|
|
11
12
|
.map(codec => codec.decoder)
|
|
@@ -15,17 +16,34 @@ const baseDecoder = Object
|
|
|
15
16
|
const LIBP2P_KEY_CODE = 0x72;
|
|
16
17
|
const MARSHALLED_ED225519_PUBLIC_KEY_LENGTH = 36;
|
|
17
18
|
const MARSHALLED_SECP258K1_PUBLIC_KEY_LENGTH = 37;
|
|
18
|
-
|
|
19
|
+
class PeerIdImpl {
|
|
19
20
|
constructor(opts) {
|
|
20
21
|
this.type = opts.type;
|
|
21
22
|
this.multihash = opts.multihash;
|
|
22
23
|
this.privateKey = opts.privateKey;
|
|
24
|
+
// mark toString cache as non-enumerable
|
|
25
|
+
this.strings = new Map();
|
|
26
|
+
Object.defineProperty(this, 'strings', {
|
|
27
|
+
enumerable: false
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
get [Symbol.toStringTag]() {
|
|
31
|
+
return symbol.toString();
|
|
32
|
+
}
|
|
33
|
+
get [symbol]() {
|
|
34
|
+
return true;
|
|
23
35
|
}
|
|
24
36
|
toString(codec) {
|
|
25
37
|
if (codec == null) {
|
|
26
38
|
codec = base58btc;
|
|
27
39
|
}
|
|
28
|
-
|
|
40
|
+
const cached = this.strings.get(codec.name);
|
|
41
|
+
if (cached != null) {
|
|
42
|
+
return cached;
|
|
43
|
+
}
|
|
44
|
+
const encoded = codec.encode(this.multihash.bytes).slice(1);
|
|
45
|
+
this.strings.set(codec.name, encoded);
|
|
46
|
+
return encoded;
|
|
29
47
|
}
|
|
30
48
|
// return self-describing String representation
|
|
31
49
|
// in default format from RFC 0001: https://github.com/libp2p/specs/pull/209
|
|
@@ -36,10 +54,7 @@ export class PeerId {
|
|
|
36
54
|
return this.multihash.bytes;
|
|
37
55
|
}
|
|
38
56
|
/**
|
|
39
|
-
* Checks the equality of `this` peer against a given PeerId
|
|
40
|
-
*
|
|
41
|
-
* @param {Uint8Array|PeerId} id
|
|
42
|
-
* @returns {boolean}
|
|
57
|
+
* Checks the equality of `this` peer against a given PeerId
|
|
43
58
|
*/
|
|
44
59
|
equals(id) {
|
|
45
60
|
if (id instanceof Uint8Array) {
|
|
@@ -52,108 +67,111 @@ export class PeerId {
|
|
|
52
67
|
throw new Error('not valid Id');
|
|
53
68
|
}
|
|
54
69
|
}
|
|
55
|
-
static fromPeerId(other) {
|
|
56
|
-
const err = errcode(new Error('Not a PeerId'), 'ERR_INVALID_PARAMETERS');
|
|
57
|
-
if (other.type === 'RSA') {
|
|
58
|
-
return new RSAPeerId(other);
|
|
59
|
-
}
|
|
60
|
-
if (other.type === 'Ed25519') {
|
|
61
|
-
return new Ed25519PeerId(other);
|
|
62
|
-
}
|
|
63
|
-
if (other.type === 'secp256k1') {
|
|
64
|
-
return new Secp256k1PeerId(other);
|
|
65
|
-
}
|
|
66
|
-
throw err;
|
|
67
|
-
}
|
|
68
|
-
static fromString(str, decoder) {
|
|
69
|
-
decoder = decoder ?? baseDecoder;
|
|
70
|
-
if (str.charAt(0) === '1' || str.charAt(0) === 'Q') {
|
|
71
|
-
// identity hash ed25519/secp256k1 key or sha2-256 hash of
|
|
72
|
-
// rsa public key - base58btc encoded either way
|
|
73
|
-
const multihash = Digest.decode(base58btc.decode(`z${str}`));
|
|
74
|
-
if (str.startsWith('12D')) {
|
|
75
|
-
return new Ed25519PeerId({ multihash });
|
|
76
|
-
}
|
|
77
|
-
else if (str.startsWith('16U')) {
|
|
78
|
-
return new Secp256k1PeerId({ multihash });
|
|
79
|
-
}
|
|
80
|
-
else {
|
|
81
|
-
return new RSAPeerId({ multihash });
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
return PeerId.fromBytes(baseDecoder.decode(str));
|
|
85
|
-
}
|
|
86
|
-
static fromBytes(buf) {
|
|
87
|
-
try {
|
|
88
|
-
const multihash = Digest.decode(buf);
|
|
89
|
-
if (multihash.code === identity.code) {
|
|
90
|
-
if (multihash.digest.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
|
91
|
-
return new Ed25519PeerId({ multihash });
|
|
92
|
-
}
|
|
93
|
-
else if (multihash.digest.length === MARSHALLED_SECP258K1_PUBLIC_KEY_LENGTH) {
|
|
94
|
-
return new Secp256k1PeerId({ multihash });
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
if (multihash.code === sha256.code) {
|
|
98
|
-
return new RSAPeerId({ multihash });
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
catch {
|
|
102
|
-
return PeerId.fromCID(CID.decode(buf));
|
|
103
|
-
}
|
|
104
|
-
throw new Error('Supplied PeerID CID is invalid');
|
|
105
|
-
}
|
|
106
|
-
static fromCID(cid) {
|
|
107
|
-
if (cid == null || cid.multihash == null || cid.version == null || (cid.version === 1 && cid.code !== LIBP2P_KEY_CODE)) {
|
|
108
|
-
throw new Error('Supplied PeerID CID is invalid');
|
|
109
|
-
}
|
|
110
|
-
const multihash = cid.multihash;
|
|
111
|
-
if (multihash.code === sha256.code) {
|
|
112
|
-
return new RSAPeerId({ multihash: cid.multihash });
|
|
113
|
-
}
|
|
114
|
-
else if (multihash.code === identity.code) {
|
|
115
|
-
if (multihash.bytes.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
|
116
|
-
return new Ed25519PeerId({ multihash: cid.multihash });
|
|
117
|
-
}
|
|
118
|
-
else if (multihash.bytes.length === MARSHALLED_SECP258K1_PUBLIC_KEY_LENGTH) {
|
|
119
|
-
return new Secp256k1PeerId({ multihash: cid.multihash });
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
throw new Error('Supplied PeerID CID is invalid');
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* @param publicKey - A marshalled public key
|
|
126
|
-
* @param privateKey - A marshalled private key
|
|
127
|
-
*/
|
|
128
|
-
static async fromKeys(publicKey, privateKey) {
|
|
129
|
-
if (publicKey.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
|
130
|
-
return new Ed25519PeerId({ multihash: Digest.create(identity.code, publicKey), privateKey });
|
|
131
|
-
}
|
|
132
|
-
if (publicKey.length === MARSHALLED_SECP258K1_PUBLIC_KEY_LENGTH) {
|
|
133
|
-
return new Secp256k1PeerId({ multihash: Digest.create(identity.code, publicKey), privateKey });
|
|
134
|
-
}
|
|
135
|
-
return new RSAPeerId({ multihash: await sha256.digest(publicKey), publicKey, privateKey });
|
|
136
|
-
}
|
|
137
70
|
}
|
|
138
|
-
|
|
71
|
+
class RSAPeerIdImpl extends PeerIdImpl {
|
|
139
72
|
constructor(opts) {
|
|
140
73
|
super({ ...opts, type: 'RSA' });
|
|
141
74
|
this.type = 'RSA';
|
|
142
75
|
this.publicKey = opts.publicKey;
|
|
143
76
|
}
|
|
144
77
|
}
|
|
145
|
-
|
|
78
|
+
class Ed25519PeerIdImpl extends PeerIdImpl {
|
|
146
79
|
constructor(opts) {
|
|
147
80
|
super({ ...opts, type: 'Ed25519' });
|
|
148
81
|
this.type = 'Ed25519';
|
|
149
82
|
this.publicKey = opts.multihash.digest;
|
|
150
83
|
}
|
|
151
84
|
}
|
|
152
|
-
|
|
85
|
+
class Secp256k1PeerIdImpl extends PeerIdImpl {
|
|
153
86
|
constructor(opts) {
|
|
154
87
|
super({ ...opts, type: 'secp256k1' });
|
|
155
88
|
this.type = 'secp256k1';
|
|
156
89
|
this.publicKey = opts.multihash.digest;
|
|
157
90
|
}
|
|
158
91
|
}
|
|
92
|
+
export function createPeerId(opts) {
|
|
93
|
+
return new PeerIdImpl(opts);
|
|
94
|
+
}
|
|
95
|
+
export function peerIdFromPeerId(other) {
|
|
96
|
+
const err = errcode(new Error('Not a PeerId'), 'ERR_INVALID_PARAMETERS');
|
|
97
|
+
if (other.type === 'RSA') {
|
|
98
|
+
return new RSAPeerIdImpl(other);
|
|
99
|
+
}
|
|
100
|
+
if (other.type === 'Ed25519') {
|
|
101
|
+
return new Ed25519PeerIdImpl(other);
|
|
102
|
+
}
|
|
103
|
+
if (other.type === 'secp256k1') {
|
|
104
|
+
return new Secp256k1PeerIdImpl(other);
|
|
105
|
+
}
|
|
106
|
+
throw err;
|
|
107
|
+
}
|
|
108
|
+
export function peerIdFromString(str, decoder) {
|
|
109
|
+
decoder = decoder ?? baseDecoder;
|
|
110
|
+
if (str.charAt(0) === '1' || str.charAt(0) === 'Q') {
|
|
111
|
+
// identity hash ed25519/secp256k1 key or sha2-256 hash of
|
|
112
|
+
// rsa public key - base58btc encoded either way
|
|
113
|
+
const multihash = Digest.decode(base58btc.decode(`z${str}`));
|
|
114
|
+
if (str.startsWith('12D')) {
|
|
115
|
+
return new Ed25519PeerIdImpl({ multihash });
|
|
116
|
+
}
|
|
117
|
+
else if (str.startsWith('16U')) {
|
|
118
|
+
return new Secp256k1PeerIdImpl({ multihash });
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
return new RSAPeerIdImpl({ multihash });
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return peerIdFromBytes(baseDecoder.decode(str));
|
|
125
|
+
}
|
|
126
|
+
export function peerIdFromBytes(buf) {
|
|
127
|
+
try {
|
|
128
|
+
const multihash = Digest.decode(buf);
|
|
129
|
+
if (multihash.code === identity.code) {
|
|
130
|
+
if (multihash.digest.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
|
131
|
+
return new Ed25519PeerIdImpl({ multihash });
|
|
132
|
+
}
|
|
133
|
+
else if (multihash.digest.length === MARSHALLED_SECP258K1_PUBLIC_KEY_LENGTH) {
|
|
134
|
+
return new Secp256k1PeerIdImpl({ multihash });
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (multihash.code === sha256.code) {
|
|
138
|
+
return new RSAPeerIdImpl({ multihash });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
return peerIdFromCID(CID.decode(buf));
|
|
143
|
+
}
|
|
144
|
+
throw new Error('Supplied PeerID CID is invalid');
|
|
145
|
+
}
|
|
146
|
+
export function peerIdFromCID(cid) {
|
|
147
|
+
if (cid == null || cid.multihash == null || cid.version == null || (cid.version === 1 && cid.code !== LIBP2P_KEY_CODE)) {
|
|
148
|
+
throw new Error('Supplied PeerID CID is invalid');
|
|
149
|
+
}
|
|
150
|
+
const multihash = cid.multihash;
|
|
151
|
+
if (multihash.code === sha256.code) {
|
|
152
|
+
return new RSAPeerIdImpl({ multihash: cid.multihash });
|
|
153
|
+
}
|
|
154
|
+
else if (multihash.code === identity.code) {
|
|
155
|
+
if (multihash.bytes.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
|
156
|
+
return new Ed25519PeerIdImpl({ multihash: cid.multihash });
|
|
157
|
+
}
|
|
158
|
+
else if (multihash.bytes.length === MARSHALLED_SECP258K1_PUBLIC_KEY_LENGTH) {
|
|
159
|
+
return new Secp256k1PeerIdImpl({ multihash: cid.multihash });
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
throw new Error('Supplied PeerID CID is invalid');
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* @param publicKey - A marshalled public key
|
|
166
|
+
* @param privateKey - A marshalled private key
|
|
167
|
+
*/
|
|
168
|
+
export async function peerIdFromKeys(publicKey, privateKey) {
|
|
169
|
+
if (publicKey.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
|
170
|
+
return new Ed25519PeerIdImpl({ multihash: Digest.create(identity.code, publicKey), privateKey });
|
|
171
|
+
}
|
|
172
|
+
if (publicKey.length === MARSHALLED_SECP258K1_PUBLIC_KEY_LENGTH) {
|
|
173
|
+
return new Secp256k1PeerIdImpl({ multihash: Digest.create(identity.code, publicKey), privateKey });
|
|
174
|
+
}
|
|
175
|
+
return new RSAPeerIdImpl({ multihash: await sha256.digest(publicKey), publicKey, privateKey });
|
|
176
|
+
}
|
|
159
177
|
//# sourceMappingURL=index.js.map
|
package/dist/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AACtC,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAA;AAC3C,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAA;AACrD,OAAO,KAAK,MAAM,MAAM,4BAA4B,CAAA;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAA;AACvD,OAAO,EAAE,MAAM,IAAI,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAC/D,OAAO,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAA;AACjD,OAAO,OAAO,MAAM,UAAU,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AACtC,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAA;AAC3C,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAA;AACrD,OAAO,KAAK,MAAM,MAAM,4BAA4B,CAAA;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAA;AACvD,OAAO,EAAE,MAAM,IAAI,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAC/D,OAAO,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAA;AACjD,OAAO,OAAO,MAAM,UAAU,CAAA;AAC9B,OAAO,EAA6C,MAAM,EAAE,MAAM,4BAA4B,CAAA;AAK9F,MAAM,WAAW,GAAG,MAAM;KACvB,MAAM,CAAC,KAAK,CAAC;KACb,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC;IAC5B,8EAA8E;KAC7E,MAAM,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,GAAG,CAAC,EAA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AAA;AAChG,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/peer-id",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.4",
|
|
4
4
|
"description": "IPFS Peer Id implementation in Node.js",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p-interfaces/tree/master/packages/libp2p-peer-id#readme",
|
|
@@ -120,10 +120,10 @@
|
|
|
120
120
|
},
|
|
121
121
|
"scripts": {
|
|
122
122
|
"lint": "aegir lint",
|
|
123
|
-
"dep-check": "aegir dep-check dist/src/**/*.js",
|
|
123
|
+
"dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js",
|
|
124
124
|
"build": "tsc",
|
|
125
125
|
"pretest": "npm run build",
|
|
126
|
-
"test": "aegir test -f ./dist/test/**/*.js",
|
|
126
|
+
"test": "aegir test -f ./dist/test/*.js -f ./dist/test/**/*.js",
|
|
127
127
|
"test:chrome": "npm run test -- -t browser",
|
|
128
128
|
"test:chrome-webworker": "npm run test -- -t webworker",
|
|
129
129
|
"test:firefox": "npm run test -- -t browser -- --browser firefox",
|
package/src/index.ts
CHANGED
|
@@ -6,8 +6,10 @@ import { identity } from 'multiformats/hashes/identity'
|
|
|
6
6
|
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
7
7
|
import { sha256 } from 'multiformats/hashes/sha2'
|
|
8
8
|
import errcode from 'err-code'
|
|
9
|
+
import { Ed25519PeerId, RSAPeerId, Secp256k1PeerId, symbol } from '@libp2p/interfaces/peer-id'
|
|
9
10
|
import type { MultibaseDecoder, MultibaseEncoder } from 'multiformats/bases/interface'
|
|
10
11
|
import type { MultihashDigest } from 'multiformats/hashes/interface'
|
|
12
|
+
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
11
13
|
|
|
12
14
|
const baseDecoder = Object
|
|
13
15
|
.values(bases)
|
|
@@ -43,16 +45,31 @@ interface Secp256k1PeerIdOptions {
|
|
|
43
45
|
privateKey?: Uint8Array
|
|
44
46
|
}
|
|
45
47
|
|
|
46
|
-
|
|
48
|
+
class PeerIdImpl {
|
|
47
49
|
public type: 'RSA' | 'Ed25519' | 'secp256k1'
|
|
48
50
|
public readonly multihash: MultihashDigest
|
|
49
51
|
public readonly privateKey?: Uint8Array
|
|
50
52
|
public readonly publicKey?: Uint8Array
|
|
53
|
+
private readonly strings: Map<string, string>
|
|
51
54
|
|
|
52
55
|
constructor (opts: PeerIdOptions) {
|
|
53
56
|
this.type = opts.type
|
|
54
57
|
this.multihash = opts.multihash
|
|
55
58
|
this.privateKey = opts.privateKey
|
|
59
|
+
|
|
60
|
+
// mark toString cache as non-enumerable
|
|
61
|
+
this.strings = new Map()
|
|
62
|
+
Object.defineProperty(this, 'strings', {
|
|
63
|
+
enumerable: false
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
get [Symbol.toStringTag] () {
|
|
68
|
+
return symbol.toString()
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
get [symbol] () {
|
|
72
|
+
return true
|
|
56
73
|
}
|
|
57
74
|
|
|
58
75
|
toString (codec?: MultibaseEncoder<any>) {
|
|
@@ -60,7 +77,17 @@ export class PeerId {
|
|
|
60
77
|
codec = base58btc
|
|
61
78
|
}
|
|
62
79
|
|
|
63
|
-
|
|
80
|
+
const cached = this.strings.get(codec.name)
|
|
81
|
+
|
|
82
|
+
if (cached != null) {
|
|
83
|
+
return cached
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const encoded = codec.encode(this.multihash.bytes).slice(1)
|
|
87
|
+
|
|
88
|
+
this.strings.set(codec.name, encoded)
|
|
89
|
+
|
|
90
|
+
return encoded
|
|
64
91
|
}
|
|
65
92
|
|
|
66
93
|
// return self-describing String representation
|
|
@@ -74,12 +101,9 @@ export class PeerId {
|
|
|
74
101
|
}
|
|
75
102
|
|
|
76
103
|
/**
|
|
77
|
-
* Checks the equality of `this` peer against a given PeerId
|
|
78
|
-
*
|
|
79
|
-
* @param {Uint8Array|PeerId} id
|
|
80
|
-
* @returns {boolean}
|
|
104
|
+
* Checks the equality of `this` peer against a given PeerId
|
|
81
105
|
*/
|
|
82
|
-
equals (id: any) {
|
|
106
|
+
equals (id: any): boolean {
|
|
83
107
|
if (id instanceof Uint8Array) {
|
|
84
108
|
return uint8ArrayEquals(this.multihash.bytes, id)
|
|
85
109
|
} else if (id?.multihash?.bytes != null) {
|
|
@@ -88,133 +112,137 @@ export class PeerId {
|
|
|
88
112
|
throw new Error('not valid Id')
|
|
89
113
|
}
|
|
90
114
|
}
|
|
115
|
+
}
|
|
91
116
|
|
|
92
|
-
|
|
93
|
-
|
|
117
|
+
class RSAPeerIdImpl extends PeerIdImpl implements RSAPeerId {
|
|
118
|
+
public readonly type = 'RSA'
|
|
119
|
+
public readonly publicKey?: Uint8Array
|
|
94
120
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
121
|
+
constructor (opts: RSAPeerIdOptions) {
|
|
122
|
+
super({ ...opts, type: 'RSA' })
|
|
98
123
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
124
|
+
this.publicKey = opts.publicKey
|
|
125
|
+
}
|
|
126
|
+
}
|
|
102
127
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
128
|
+
class Ed25519PeerIdImpl extends PeerIdImpl implements Ed25519PeerId {
|
|
129
|
+
public readonly type = 'Ed25519'
|
|
130
|
+
public readonly publicKey: Uint8Array
|
|
106
131
|
|
|
107
|
-
|
|
108
|
-
|
|
132
|
+
constructor (opts: Ed25519PeerIdOptions) {
|
|
133
|
+
super({ ...opts, type: 'Ed25519' })
|
|
109
134
|
|
|
110
|
-
|
|
111
|
-
|
|
135
|
+
this.publicKey = opts.multihash.digest
|
|
136
|
+
}
|
|
137
|
+
}
|
|
112
138
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
const multihash = Digest.decode(base58btc.decode(`z${str}`))
|
|
139
|
+
class Secp256k1PeerIdImpl extends PeerIdImpl implements Secp256k1PeerId {
|
|
140
|
+
public readonly type = 'secp256k1'
|
|
141
|
+
public readonly publicKey: Uint8Array
|
|
117
142
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
} else if (str.startsWith('16U')) {
|
|
121
|
-
return new Secp256k1PeerId({ multihash })
|
|
122
|
-
} else {
|
|
123
|
-
return new RSAPeerId({ multihash })
|
|
124
|
-
}
|
|
125
|
-
}
|
|
143
|
+
constructor (opts: Secp256k1PeerIdOptions) {
|
|
144
|
+
super({ ...opts, type: 'secp256k1' })
|
|
126
145
|
|
|
127
|
-
|
|
146
|
+
this.publicKey = opts.multihash.digest
|
|
128
147
|
}
|
|
148
|
+
}
|
|
129
149
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
if (multihash.code === identity.code) {
|
|
135
|
-
if (multihash.digest.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
|
136
|
-
return new Ed25519PeerId({ multihash })
|
|
137
|
-
} else if (multihash.digest.length === MARSHALLED_SECP258K1_PUBLIC_KEY_LENGTH) {
|
|
138
|
-
return new Secp256k1PeerId({ multihash })
|
|
139
|
-
}
|
|
140
|
-
}
|
|
150
|
+
export function createPeerId (opts: PeerIdOptions) {
|
|
151
|
+
return new PeerIdImpl(opts)
|
|
152
|
+
}
|
|
141
153
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
} catch {
|
|
146
|
-
return PeerId.fromCID(CID.decode(buf))
|
|
147
|
-
}
|
|
154
|
+
export function peerIdFromPeerId (other: any): PeerId {
|
|
155
|
+
const err = errcode(new Error('Not a PeerId'), 'ERR_INVALID_PARAMETERS')
|
|
148
156
|
|
|
149
|
-
|
|
157
|
+
if (other.type === 'RSA') {
|
|
158
|
+
return new RSAPeerIdImpl(other)
|
|
150
159
|
}
|
|
151
160
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
}
|
|
161
|
+
if (other.type === 'Ed25519') {
|
|
162
|
+
return new Ed25519PeerIdImpl(other)
|
|
163
|
+
}
|
|
156
164
|
|
|
157
|
-
|
|
165
|
+
if (other.type === 'secp256k1') {
|
|
166
|
+
return new Secp256k1PeerIdImpl(other)
|
|
167
|
+
}
|
|
158
168
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
} else if (multihash.code === identity.code) {
|
|
162
|
-
if (multihash.bytes.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
|
163
|
-
return new Ed25519PeerId({ multihash: cid.multihash })
|
|
164
|
-
} else if (multihash.bytes.length === MARSHALLED_SECP258K1_PUBLIC_KEY_LENGTH) {
|
|
165
|
-
return new Secp256k1PeerId({ multihash: cid.multihash })
|
|
166
|
-
}
|
|
167
|
-
}
|
|
169
|
+
throw err
|
|
170
|
+
}
|
|
168
171
|
|
|
169
|
-
|
|
170
|
-
|
|
172
|
+
export function peerIdFromString (str: string, decoder?: MultibaseDecoder<any>): PeerId {
|
|
173
|
+
decoder = decoder ?? baseDecoder
|
|
171
174
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
static async fromKeys (publicKey: Uint8Array, privateKey?: Uint8Array) {
|
|
177
|
-
if (publicKey.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
|
178
|
-
return new Ed25519PeerId({ multihash: Digest.create(identity.code, publicKey), privateKey })
|
|
179
|
-
}
|
|
175
|
+
if (str.charAt(0) === '1' || str.charAt(0) === 'Q') {
|
|
176
|
+
// identity hash ed25519/secp256k1 key or sha2-256 hash of
|
|
177
|
+
// rsa public key - base58btc encoded either way
|
|
178
|
+
const multihash = Digest.decode(base58btc.decode(`z${str}`))
|
|
180
179
|
|
|
181
|
-
if (
|
|
182
|
-
return new
|
|
180
|
+
if (str.startsWith('12D')) {
|
|
181
|
+
return new Ed25519PeerIdImpl({ multihash })
|
|
182
|
+
} else if (str.startsWith('16U')) {
|
|
183
|
+
return new Secp256k1PeerIdImpl({ multihash })
|
|
184
|
+
} else {
|
|
185
|
+
return new RSAPeerIdImpl({ multihash })
|
|
183
186
|
}
|
|
184
|
-
|
|
185
|
-
return new RSAPeerId({ multihash: await sha256.digest(publicKey), publicKey, privateKey })
|
|
186
187
|
}
|
|
188
|
+
|
|
189
|
+
return peerIdFromBytes(baseDecoder.decode(str))
|
|
187
190
|
}
|
|
188
191
|
|
|
189
|
-
export
|
|
190
|
-
|
|
191
|
-
|
|
192
|
+
export function peerIdFromBytes (buf: Uint8Array) {
|
|
193
|
+
try {
|
|
194
|
+
const multihash = Digest.decode(buf)
|
|
192
195
|
|
|
193
|
-
|
|
194
|
-
|
|
196
|
+
if (multihash.code === identity.code) {
|
|
197
|
+
if (multihash.digest.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
|
198
|
+
return new Ed25519PeerIdImpl({ multihash })
|
|
199
|
+
} else if (multihash.digest.length === MARSHALLED_SECP258K1_PUBLIC_KEY_LENGTH) {
|
|
200
|
+
return new Secp256k1PeerIdImpl({ multihash })
|
|
201
|
+
}
|
|
202
|
+
}
|
|
195
203
|
|
|
196
|
-
|
|
204
|
+
if (multihash.code === sha256.code) {
|
|
205
|
+
return new RSAPeerIdImpl({ multihash })
|
|
206
|
+
}
|
|
207
|
+
} catch {
|
|
208
|
+
return peerIdFromCID(CID.decode(buf))
|
|
197
209
|
}
|
|
210
|
+
|
|
211
|
+
throw new Error('Supplied PeerID CID is invalid')
|
|
198
212
|
}
|
|
199
213
|
|
|
200
|
-
export
|
|
201
|
-
|
|
202
|
-
|
|
214
|
+
export function peerIdFromCID (cid: CID): PeerId {
|
|
215
|
+
if (cid == null || cid.multihash == null || cid.version == null || (cid.version === 1 && cid.code !== LIBP2P_KEY_CODE)) {
|
|
216
|
+
throw new Error('Supplied PeerID CID is invalid')
|
|
217
|
+
}
|
|
203
218
|
|
|
204
|
-
|
|
205
|
-
super({ ...opts, type: 'Ed25519' })
|
|
219
|
+
const multihash = cid.multihash
|
|
206
220
|
|
|
207
|
-
|
|
221
|
+
if (multihash.code === sha256.code) {
|
|
222
|
+
return new RSAPeerIdImpl({ multihash: cid.multihash })
|
|
223
|
+
} else if (multihash.code === identity.code) {
|
|
224
|
+
if (multihash.bytes.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
|
225
|
+
return new Ed25519PeerIdImpl({ multihash: cid.multihash })
|
|
226
|
+
} else if (multihash.bytes.length === MARSHALLED_SECP258K1_PUBLIC_KEY_LENGTH) {
|
|
227
|
+
return new Secp256k1PeerIdImpl({ multihash: cid.multihash })
|
|
228
|
+
}
|
|
208
229
|
}
|
|
209
|
-
}
|
|
210
230
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
public readonly publicKey: Uint8Array
|
|
231
|
+
throw new Error('Supplied PeerID CID is invalid')
|
|
232
|
+
}
|
|
214
233
|
|
|
215
|
-
|
|
216
|
-
|
|
234
|
+
/**
|
|
235
|
+
* @param publicKey - A marshalled public key
|
|
236
|
+
* @param privateKey - A marshalled private key
|
|
237
|
+
*/
|
|
238
|
+
export async function peerIdFromKeys (publicKey: Uint8Array, privateKey?: Uint8Array): Promise<PeerId> {
|
|
239
|
+
if (publicKey.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
|
240
|
+
return new Ed25519PeerIdImpl({ multihash: Digest.create(identity.code, publicKey), privateKey })
|
|
241
|
+
}
|
|
217
242
|
|
|
218
|
-
|
|
243
|
+
if (publicKey.length === MARSHALLED_SECP258K1_PUBLIC_KEY_LENGTH) {
|
|
244
|
+
return new Secp256k1PeerIdImpl({ multihash: Digest.create(identity.code, publicKey), privateKey })
|
|
219
245
|
}
|
|
246
|
+
|
|
247
|
+
return new RSAPeerIdImpl({ multihash: await sha256.digest(publicKey), publicKey, privateKey })
|
|
220
248
|
}
|