@libp2p/peer-id 1.1.2 → 1.1.3
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 +14 -41
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +92 -92
- package/dist/src/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +102 -102
package/dist/src/index.d.ts
CHANGED
|
@@ -1,34 +1,21 @@
|
|
|
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';
|
|
4
|
-
|
|
5
|
+
import type { PeerId } from '@libp2p/interfaces/peer-id';
|
|
5
6
|
interface PeerIdOptions {
|
|
6
7
|
type: 'RSA' | 'Ed25519' | 'secp256k1';
|
|
7
8
|
multihash: MultihashDigest;
|
|
8
9
|
privateKey?: Uint8Array;
|
|
9
10
|
}
|
|
10
|
-
|
|
11
|
-
multihash: MultihashDigest;
|
|
12
|
-
privateKey?: Uint8Array;
|
|
13
|
-
publicKey?: Uint8Array;
|
|
14
|
-
}
|
|
15
|
-
interface Ed25519PeerIdOptions {
|
|
16
|
-
multihash: MultihashDigest;
|
|
17
|
-
privateKey?: Uint8Array;
|
|
18
|
-
}
|
|
19
|
-
interface Secp256k1PeerIdOptions {
|
|
20
|
-
multihash: MultihashDigest;
|
|
21
|
-
privateKey?: Uint8Array;
|
|
22
|
-
}
|
|
23
|
-
export declare class PeerId {
|
|
11
|
+
declare class PeerIdImpl {
|
|
24
12
|
type: 'RSA' | 'Ed25519' | 'secp256k1';
|
|
25
13
|
readonly multihash: MultihashDigest;
|
|
26
14
|
readonly privateKey?: Uint8Array;
|
|
27
15
|
readonly publicKey?: Uint8Array;
|
|
28
16
|
constructor(opts: PeerIdOptions);
|
|
29
|
-
static isPeerId(other: any): other is PeerId;
|
|
30
17
|
get [Symbol.toStringTag](): string;
|
|
31
|
-
get [
|
|
18
|
+
get [symbol](): boolean;
|
|
32
19
|
toString(codec?: MultibaseEncoder<any>): string;
|
|
33
20
|
toCID(): CID;
|
|
34
21
|
toBytes(): Uint8Array;
|
|
@@ -39,30 +26,16 @@ export declare class PeerId {
|
|
|
39
26
|
* @returns {boolean}
|
|
40
27
|
*/
|
|
41
28
|
equals(id: any): boolean;
|
|
42
|
-
static fromPeerId(other: any): RSAPeerId | Ed25519PeerId | Secp256k1PeerId;
|
|
43
|
-
static fromString(str: string, decoder?: MultibaseDecoder<any>): RSAPeerId | Ed25519PeerId | Secp256k1PeerId;
|
|
44
|
-
static fromBytes(buf: Uint8Array): RSAPeerId | Ed25519PeerId | Secp256k1PeerId;
|
|
45
|
-
static fromCID(cid: CID): RSAPeerId | Ed25519PeerId | Secp256k1PeerId;
|
|
46
|
-
/**
|
|
47
|
-
* @param publicKey - A marshalled public key
|
|
48
|
-
* @param privateKey - A marshalled private key
|
|
49
|
-
*/
|
|
50
|
-
static fromKeys(publicKey: Uint8Array, privateKey?: Uint8Array): Promise<RSAPeerId | Ed25519PeerId | Secp256k1PeerId>;
|
|
51
|
-
}
|
|
52
|
-
export declare class RSAPeerId extends PeerId {
|
|
53
|
-
readonly type = "RSA";
|
|
54
|
-
readonly publicKey?: Uint8Array;
|
|
55
|
-
constructor(opts: RSAPeerIdOptions);
|
|
56
|
-
}
|
|
57
|
-
export declare class Ed25519PeerId extends PeerId {
|
|
58
|
-
readonly type = "Ed25519";
|
|
59
|
-
readonly publicKey: Uint8Array;
|
|
60
|
-
constructor(opts: Ed25519PeerIdOptions);
|
|
61
|
-
}
|
|
62
|
-
export declare class Secp256k1PeerId extends PeerId {
|
|
63
|
-
readonly type = "secp256k1";
|
|
64
|
-
readonly publicKey: Uint8Array;
|
|
65
|
-
constructor(opts: Secp256k1PeerIdOptions);
|
|
66
29
|
}
|
|
30
|
+
export declare function createPeerId(opts: PeerIdOptions): PeerIdImpl;
|
|
31
|
+
export declare function peerIdFromPeerId(other: any): PeerId;
|
|
32
|
+
export declare function peerIdFromString(str: string, decoder?: MultibaseDecoder<any>): PeerId;
|
|
33
|
+
export declare function peerIdFromBytes(buf: Uint8Array): PeerId;
|
|
34
|
+
export declare function peerIdFromCID(cid: CID): PeerId;
|
|
35
|
+
/**
|
|
36
|
+
* @param publicKey - A marshalled public key
|
|
37
|
+
* @param privateKey - A marshalled private key
|
|
38
|
+
*/
|
|
39
|
+
export declare function peerIdFromKeys(publicKey: Uint8Array, privateKey?: Uint8Array): Promise<PeerId>;
|
|
67
40
|
export {};
|
|
68
41
|
//# 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;gBAEzB,IAAI,EAAE,aAAa;IAMhC,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,WAEvB;IAED,IAAI,CAAC,MAAM,CAAC,YAEX;IAED,QAAQ,CAAE,KAAK,CAAC,EAAE,gBAAgB,CAAC,GAAG,CAAC;IAUvC,KAAK;IAIL,OAAO;IAIP;;;;;OAKG;IACH,MAAM,CAAE,EAAE,EAAE,GAAG;CAShB;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,7 +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
|
-
|
|
9
|
+
import { symbol } from '@libp2p/interfaces/peer-id';
|
|
10
10
|
const baseDecoder = Object
|
|
11
11
|
.values(bases)
|
|
12
12
|
.map(codec => codec.decoder)
|
|
@@ -16,19 +16,16 @@ const baseDecoder = Object
|
|
|
16
16
|
const LIBP2P_KEY_CODE = 0x72;
|
|
17
17
|
const MARSHALLED_ED225519_PUBLIC_KEY_LENGTH = 36;
|
|
18
18
|
const MARSHALLED_SECP258K1_PUBLIC_KEY_LENGTH = 37;
|
|
19
|
-
|
|
19
|
+
class PeerIdImpl {
|
|
20
20
|
constructor(opts) {
|
|
21
21
|
this.type = opts.type;
|
|
22
22
|
this.multihash = opts.multihash;
|
|
23
23
|
this.privateKey = opts.privateKey;
|
|
24
24
|
}
|
|
25
|
-
static isPeerId(other) {
|
|
26
|
-
return peerIdSymbol in other;
|
|
27
|
-
}
|
|
28
25
|
get [Symbol.toStringTag]() {
|
|
29
|
-
return
|
|
26
|
+
return symbol.toString();
|
|
30
27
|
}
|
|
31
|
-
get [
|
|
28
|
+
get [symbol]() {
|
|
32
29
|
return true;
|
|
33
30
|
}
|
|
34
31
|
toString(codec) {
|
|
@@ -62,108 +59,111 @@ export class PeerId {
|
|
|
62
59
|
throw new Error('not valid Id');
|
|
63
60
|
}
|
|
64
61
|
}
|
|
65
|
-
static fromPeerId(other) {
|
|
66
|
-
const err = errcode(new Error('Not a PeerId'), 'ERR_INVALID_PARAMETERS');
|
|
67
|
-
if (other.type === 'RSA') {
|
|
68
|
-
return new RSAPeerId(other);
|
|
69
|
-
}
|
|
70
|
-
if (other.type === 'Ed25519') {
|
|
71
|
-
return new Ed25519PeerId(other);
|
|
72
|
-
}
|
|
73
|
-
if (other.type === 'secp256k1') {
|
|
74
|
-
return new Secp256k1PeerId(other);
|
|
75
|
-
}
|
|
76
|
-
throw err;
|
|
77
|
-
}
|
|
78
|
-
static fromString(str, decoder) {
|
|
79
|
-
decoder = decoder ?? baseDecoder;
|
|
80
|
-
if (str.charAt(0) === '1' || str.charAt(0) === 'Q') {
|
|
81
|
-
// identity hash ed25519/secp256k1 key or sha2-256 hash of
|
|
82
|
-
// rsa public key - base58btc encoded either way
|
|
83
|
-
const multihash = Digest.decode(base58btc.decode(`z${str}`));
|
|
84
|
-
if (str.startsWith('12D')) {
|
|
85
|
-
return new Ed25519PeerId({ multihash });
|
|
86
|
-
}
|
|
87
|
-
else if (str.startsWith('16U')) {
|
|
88
|
-
return new Secp256k1PeerId({ multihash });
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
return new RSAPeerId({ multihash });
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
return PeerId.fromBytes(baseDecoder.decode(str));
|
|
95
|
-
}
|
|
96
|
-
static fromBytes(buf) {
|
|
97
|
-
try {
|
|
98
|
-
const multihash = Digest.decode(buf);
|
|
99
|
-
if (multihash.code === identity.code) {
|
|
100
|
-
if (multihash.digest.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
|
101
|
-
return new Ed25519PeerId({ multihash });
|
|
102
|
-
}
|
|
103
|
-
else if (multihash.digest.length === MARSHALLED_SECP258K1_PUBLIC_KEY_LENGTH) {
|
|
104
|
-
return new Secp256k1PeerId({ multihash });
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
if (multihash.code === sha256.code) {
|
|
108
|
-
return new RSAPeerId({ multihash });
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
catch {
|
|
112
|
-
return PeerId.fromCID(CID.decode(buf));
|
|
113
|
-
}
|
|
114
|
-
throw new Error('Supplied PeerID CID is invalid');
|
|
115
|
-
}
|
|
116
|
-
static fromCID(cid) {
|
|
117
|
-
if (cid == null || cid.multihash == null || cid.version == null || (cid.version === 1 && cid.code !== LIBP2P_KEY_CODE)) {
|
|
118
|
-
throw new Error('Supplied PeerID CID is invalid');
|
|
119
|
-
}
|
|
120
|
-
const multihash = cid.multihash;
|
|
121
|
-
if (multihash.code === sha256.code) {
|
|
122
|
-
return new RSAPeerId({ multihash: cid.multihash });
|
|
123
|
-
}
|
|
124
|
-
else if (multihash.code === identity.code) {
|
|
125
|
-
if (multihash.bytes.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
|
126
|
-
return new Ed25519PeerId({ multihash: cid.multihash });
|
|
127
|
-
}
|
|
128
|
-
else if (multihash.bytes.length === MARSHALLED_SECP258K1_PUBLIC_KEY_LENGTH) {
|
|
129
|
-
return new Secp256k1PeerId({ multihash: cid.multihash });
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
throw new Error('Supplied PeerID CID is invalid');
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* @param publicKey - A marshalled public key
|
|
136
|
-
* @param privateKey - A marshalled private key
|
|
137
|
-
*/
|
|
138
|
-
static async fromKeys(publicKey, privateKey) {
|
|
139
|
-
if (publicKey.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
|
140
|
-
return new Ed25519PeerId({ multihash: Digest.create(identity.code, publicKey), privateKey });
|
|
141
|
-
}
|
|
142
|
-
if (publicKey.length === MARSHALLED_SECP258K1_PUBLIC_KEY_LENGTH) {
|
|
143
|
-
return new Secp256k1PeerId({ multihash: Digest.create(identity.code, publicKey), privateKey });
|
|
144
|
-
}
|
|
145
|
-
return new RSAPeerId({ multihash: await sha256.digest(publicKey), publicKey, privateKey });
|
|
146
|
-
}
|
|
147
62
|
}
|
|
148
|
-
|
|
63
|
+
class RSAPeerIdImpl extends PeerIdImpl {
|
|
149
64
|
constructor(opts) {
|
|
150
65
|
super({ ...opts, type: 'RSA' });
|
|
151
66
|
this.type = 'RSA';
|
|
152
67
|
this.publicKey = opts.publicKey;
|
|
153
68
|
}
|
|
154
69
|
}
|
|
155
|
-
|
|
70
|
+
class Ed25519PeerIdImpl extends PeerIdImpl {
|
|
156
71
|
constructor(opts) {
|
|
157
72
|
super({ ...opts, type: 'Ed25519' });
|
|
158
73
|
this.type = 'Ed25519';
|
|
159
74
|
this.publicKey = opts.multihash.digest;
|
|
160
75
|
}
|
|
161
76
|
}
|
|
162
|
-
|
|
77
|
+
class Secp256k1PeerIdImpl extends PeerIdImpl {
|
|
163
78
|
constructor(opts) {
|
|
164
79
|
super({ ...opts, type: 'secp256k1' });
|
|
165
80
|
this.type = 'secp256k1';
|
|
166
81
|
this.publicKey = opts.multihash.digest;
|
|
167
82
|
}
|
|
168
83
|
}
|
|
84
|
+
export function createPeerId(opts) {
|
|
85
|
+
return new PeerIdImpl(opts);
|
|
86
|
+
}
|
|
87
|
+
export function peerIdFromPeerId(other) {
|
|
88
|
+
const err = errcode(new Error('Not a PeerId'), 'ERR_INVALID_PARAMETERS');
|
|
89
|
+
if (other.type === 'RSA') {
|
|
90
|
+
return new RSAPeerIdImpl(other);
|
|
91
|
+
}
|
|
92
|
+
if (other.type === 'Ed25519') {
|
|
93
|
+
return new Ed25519PeerIdImpl(other);
|
|
94
|
+
}
|
|
95
|
+
if (other.type === 'secp256k1') {
|
|
96
|
+
return new Secp256k1PeerIdImpl(other);
|
|
97
|
+
}
|
|
98
|
+
throw err;
|
|
99
|
+
}
|
|
100
|
+
export function peerIdFromString(str, decoder) {
|
|
101
|
+
decoder = decoder ?? baseDecoder;
|
|
102
|
+
if (str.charAt(0) === '1' || str.charAt(0) === 'Q') {
|
|
103
|
+
// identity hash ed25519/secp256k1 key or sha2-256 hash of
|
|
104
|
+
// rsa public key - base58btc encoded either way
|
|
105
|
+
const multihash = Digest.decode(base58btc.decode(`z${str}`));
|
|
106
|
+
if (str.startsWith('12D')) {
|
|
107
|
+
return new Ed25519PeerIdImpl({ multihash });
|
|
108
|
+
}
|
|
109
|
+
else if (str.startsWith('16U')) {
|
|
110
|
+
return new Secp256k1PeerIdImpl({ multihash });
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
return new RSAPeerIdImpl({ multihash });
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return peerIdFromBytes(baseDecoder.decode(str));
|
|
117
|
+
}
|
|
118
|
+
export function peerIdFromBytes(buf) {
|
|
119
|
+
try {
|
|
120
|
+
const multihash = Digest.decode(buf);
|
|
121
|
+
if (multihash.code === identity.code) {
|
|
122
|
+
if (multihash.digest.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
|
123
|
+
return new Ed25519PeerIdImpl({ multihash });
|
|
124
|
+
}
|
|
125
|
+
else if (multihash.digest.length === MARSHALLED_SECP258K1_PUBLIC_KEY_LENGTH) {
|
|
126
|
+
return new Secp256k1PeerIdImpl({ multihash });
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (multihash.code === sha256.code) {
|
|
130
|
+
return new RSAPeerIdImpl({ multihash });
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
return peerIdFromCID(CID.decode(buf));
|
|
135
|
+
}
|
|
136
|
+
throw new Error('Supplied PeerID CID is invalid');
|
|
137
|
+
}
|
|
138
|
+
export function peerIdFromCID(cid) {
|
|
139
|
+
if (cid == null || cid.multihash == null || cid.version == null || (cid.version === 1 && cid.code !== LIBP2P_KEY_CODE)) {
|
|
140
|
+
throw new Error('Supplied PeerID CID is invalid');
|
|
141
|
+
}
|
|
142
|
+
const multihash = cid.multihash;
|
|
143
|
+
if (multihash.code === sha256.code) {
|
|
144
|
+
return new RSAPeerIdImpl({ multihash: cid.multihash });
|
|
145
|
+
}
|
|
146
|
+
else if (multihash.code === identity.code) {
|
|
147
|
+
if (multihash.bytes.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
|
148
|
+
return new Ed25519PeerIdImpl({ multihash: cid.multihash });
|
|
149
|
+
}
|
|
150
|
+
else if (multihash.bytes.length === MARSHALLED_SECP258K1_PUBLIC_KEY_LENGTH) {
|
|
151
|
+
return new Secp256k1PeerIdImpl({ multihash: cid.multihash });
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
throw new Error('Supplied PeerID CID is invalid');
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* @param publicKey - A marshalled public key
|
|
158
|
+
* @param privateKey - A marshalled private key
|
|
159
|
+
*/
|
|
160
|
+
export async function peerIdFromKeys(publicKey, privateKey) {
|
|
161
|
+
if (publicKey.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
|
162
|
+
return new Ed25519PeerIdImpl({ multihash: Digest.create(identity.code, publicKey), privateKey });
|
|
163
|
+
}
|
|
164
|
+
if (publicKey.length === MARSHALLED_SECP258K1_PUBLIC_KEY_LENGTH) {
|
|
165
|
+
return new Secp256k1PeerIdImpl({ multihash: Digest.create(identity.code, publicKey), privateKey });
|
|
166
|
+
}
|
|
167
|
+
return new RSAPeerIdImpl({ multihash: await sha256.digest(publicKey), publicKey, privateKey });
|
|
168
|
+
}
|
|
169
169
|
//# 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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,EAAE,CAAC,CAAA;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.3",
|
|
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",
|
package/src/index.ts
CHANGED
|
@@ -6,10 +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'
|
|
11
|
-
|
|
12
|
-
const peerIdSymbol = Symbol.for('@libp2p/peer-id')
|
|
12
|
+
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
13
13
|
|
|
14
14
|
const baseDecoder = Object
|
|
15
15
|
.values(bases)
|
|
@@ -45,7 +45,7 @@ interface Secp256k1PeerIdOptions {
|
|
|
45
45
|
privateKey?: Uint8Array
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
|
|
48
|
+
class PeerIdImpl {
|
|
49
49
|
public type: 'RSA' | 'Ed25519' | 'secp256k1'
|
|
50
50
|
public readonly multihash: MultihashDigest
|
|
51
51
|
public readonly privateKey?: Uint8Array
|
|
@@ -57,15 +57,11 @@ export class PeerId {
|
|
|
57
57
|
this.privateKey = opts.privateKey
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
static isPeerId (other: any): other is PeerId {
|
|
61
|
-
return peerIdSymbol in other
|
|
62
|
-
}
|
|
63
|
-
|
|
64
60
|
get [Symbol.toStringTag] () {
|
|
65
|
-
return
|
|
61
|
+
return symbol.toString()
|
|
66
62
|
}
|
|
67
63
|
|
|
68
|
-
get [
|
|
64
|
+
get [symbol] () {
|
|
69
65
|
return true
|
|
70
66
|
}
|
|
71
67
|
|
|
@@ -102,133 +98,137 @@ export class PeerId {
|
|
|
102
98
|
throw new Error('not valid Id')
|
|
103
99
|
}
|
|
104
100
|
}
|
|
101
|
+
}
|
|
105
102
|
|
|
106
|
-
|
|
107
|
-
|
|
103
|
+
class RSAPeerIdImpl extends PeerIdImpl implements RSAPeerId {
|
|
104
|
+
public readonly type = 'RSA'
|
|
105
|
+
public readonly publicKey?: Uint8Array
|
|
108
106
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}
|
|
107
|
+
constructor (opts: RSAPeerIdOptions) {
|
|
108
|
+
super({ ...opts, type: 'RSA' })
|
|
112
109
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
110
|
+
this.publicKey = opts.publicKey
|
|
111
|
+
}
|
|
112
|
+
}
|
|
116
113
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
114
|
+
class Ed25519PeerIdImpl extends PeerIdImpl implements Ed25519PeerId {
|
|
115
|
+
public readonly type = 'Ed25519'
|
|
116
|
+
public readonly publicKey: Uint8Array
|
|
120
117
|
|
|
121
|
-
|
|
122
|
-
|
|
118
|
+
constructor (opts: Ed25519PeerIdOptions) {
|
|
119
|
+
super({ ...opts, type: 'Ed25519' })
|
|
123
120
|
|
|
124
|
-
|
|
125
|
-
|
|
121
|
+
this.publicKey = opts.multihash.digest
|
|
122
|
+
}
|
|
123
|
+
}
|
|
126
124
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
const multihash = Digest.decode(base58btc.decode(`z${str}`))
|
|
125
|
+
class Secp256k1PeerIdImpl extends PeerIdImpl implements Secp256k1PeerId {
|
|
126
|
+
public readonly type = 'secp256k1'
|
|
127
|
+
public readonly publicKey: Uint8Array
|
|
131
128
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
} else if (str.startsWith('16U')) {
|
|
135
|
-
return new Secp256k1PeerId({ multihash })
|
|
136
|
-
} else {
|
|
137
|
-
return new RSAPeerId({ multihash })
|
|
138
|
-
}
|
|
139
|
-
}
|
|
129
|
+
constructor (opts: Secp256k1PeerIdOptions) {
|
|
130
|
+
super({ ...opts, type: 'secp256k1' })
|
|
140
131
|
|
|
141
|
-
|
|
132
|
+
this.publicKey = opts.multihash.digest
|
|
142
133
|
}
|
|
134
|
+
}
|
|
143
135
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
if (multihash.code === identity.code) {
|
|
149
|
-
if (multihash.digest.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
|
150
|
-
return new Ed25519PeerId({ multihash })
|
|
151
|
-
} else if (multihash.digest.length === MARSHALLED_SECP258K1_PUBLIC_KEY_LENGTH) {
|
|
152
|
-
return new Secp256k1PeerId({ multihash })
|
|
153
|
-
}
|
|
154
|
-
}
|
|
136
|
+
export function createPeerId (opts: PeerIdOptions) {
|
|
137
|
+
return new PeerIdImpl(opts)
|
|
138
|
+
}
|
|
155
139
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
} catch {
|
|
160
|
-
return PeerId.fromCID(CID.decode(buf))
|
|
161
|
-
}
|
|
140
|
+
export function peerIdFromPeerId (other: any): PeerId {
|
|
141
|
+
const err = errcode(new Error('Not a PeerId'), 'ERR_INVALID_PARAMETERS')
|
|
162
142
|
|
|
163
|
-
|
|
143
|
+
if (other.type === 'RSA') {
|
|
144
|
+
return new RSAPeerIdImpl(other)
|
|
164
145
|
}
|
|
165
146
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
}
|
|
147
|
+
if (other.type === 'Ed25519') {
|
|
148
|
+
return new Ed25519PeerIdImpl(other)
|
|
149
|
+
}
|
|
170
150
|
|
|
171
|
-
|
|
151
|
+
if (other.type === 'secp256k1') {
|
|
152
|
+
return new Secp256k1PeerIdImpl(other)
|
|
153
|
+
}
|
|
172
154
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
} else if (multihash.code === identity.code) {
|
|
176
|
-
if (multihash.bytes.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
|
177
|
-
return new Ed25519PeerId({ multihash: cid.multihash })
|
|
178
|
-
} else if (multihash.bytes.length === MARSHALLED_SECP258K1_PUBLIC_KEY_LENGTH) {
|
|
179
|
-
return new Secp256k1PeerId({ multihash: cid.multihash })
|
|
180
|
-
}
|
|
181
|
-
}
|
|
155
|
+
throw err
|
|
156
|
+
}
|
|
182
157
|
|
|
183
|
-
|
|
184
|
-
|
|
158
|
+
export function peerIdFromString (str: string, decoder?: MultibaseDecoder<any>): PeerId {
|
|
159
|
+
decoder = decoder ?? baseDecoder
|
|
185
160
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
static async fromKeys (publicKey: Uint8Array, privateKey?: Uint8Array) {
|
|
191
|
-
if (publicKey.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
|
192
|
-
return new Ed25519PeerId({ multihash: Digest.create(identity.code, publicKey), privateKey })
|
|
193
|
-
}
|
|
161
|
+
if (str.charAt(0) === '1' || str.charAt(0) === 'Q') {
|
|
162
|
+
// identity hash ed25519/secp256k1 key or sha2-256 hash of
|
|
163
|
+
// rsa public key - base58btc encoded either way
|
|
164
|
+
const multihash = Digest.decode(base58btc.decode(`z${str}`))
|
|
194
165
|
|
|
195
|
-
if (
|
|
196
|
-
return new
|
|
166
|
+
if (str.startsWith('12D')) {
|
|
167
|
+
return new Ed25519PeerIdImpl({ multihash })
|
|
168
|
+
} else if (str.startsWith('16U')) {
|
|
169
|
+
return new Secp256k1PeerIdImpl({ multihash })
|
|
170
|
+
} else {
|
|
171
|
+
return new RSAPeerIdImpl({ multihash })
|
|
197
172
|
}
|
|
198
|
-
|
|
199
|
-
return new RSAPeerId({ multihash: await sha256.digest(publicKey), publicKey, privateKey })
|
|
200
173
|
}
|
|
174
|
+
|
|
175
|
+
return peerIdFromBytes(baseDecoder.decode(str))
|
|
201
176
|
}
|
|
202
177
|
|
|
203
|
-
export
|
|
204
|
-
|
|
205
|
-
|
|
178
|
+
export function peerIdFromBytes (buf: Uint8Array) {
|
|
179
|
+
try {
|
|
180
|
+
const multihash = Digest.decode(buf)
|
|
206
181
|
|
|
207
|
-
|
|
208
|
-
|
|
182
|
+
if (multihash.code === identity.code) {
|
|
183
|
+
if (multihash.digest.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
|
184
|
+
return new Ed25519PeerIdImpl({ multihash })
|
|
185
|
+
} else if (multihash.digest.length === MARSHALLED_SECP258K1_PUBLIC_KEY_LENGTH) {
|
|
186
|
+
return new Secp256k1PeerIdImpl({ multihash })
|
|
187
|
+
}
|
|
188
|
+
}
|
|
209
189
|
|
|
210
|
-
|
|
190
|
+
if (multihash.code === sha256.code) {
|
|
191
|
+
return new RSAPeerIdImpl({ multihash })
|
|
192
|
+
}
|
|
193
|
+
} catch {
|
|
194
|
+
return peerIdFromCID(CID.decode(buf))
|
|
211
195
|
}
|
|
196
|
+
|
|
197
|
+
throw new Error('Supplied PeerID CID is invalid')
|
|
212
198
|
}
|
|
213
199
|
|
|
214
|
-
export
|
|
215
|
-
|
|
216
|
-
|
|
200
|
+
export function peerIdFromCID (cid: CID): PeerId {
|
|
201
|
+
if (cid == null || cid.multihash == null || cid.version == null || (cid.version === 1 && cid.code !== LIBP2P_KEY_CODE)) {
|
|
202
|
+
throw new Error('Supplied PeerID CID is invalid')
|
|
203
|
+
}
|
|
217
204
|
|
|
218
|
-
|
|
219
|
-
super({ ...opts, type: 'Ed25519' })
|
|
205
|
+
const multihash = cid.multihash
|
|
220
206
|
|
|
221
|
-
|
|
207
|
+
if (multihash.code === sha256.code) {
|
|
208
|
+
return new RSAPeerIdImpl({ multihash: cid.multihash })
|
|
209
|
+
} else if (multihash.code === identity.code) {
|
|
210
|
+
if (multihash.bytes.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
|
211
|
+
return new Ed25519PeerIdImpl({ multihash: cid.multihash })
|
|
212
|
+
} else if (multihash.bytes.length === MARSHALLED_SECP258K1_PUBLIC_KEY_LENGTH) {
|
|
213
|
+
return new Secp256k1PeerIdImpl({ multihash: cid.multihash })
|
|
214
|
+
}
|
|
222
215
|
}
|
|
223
|
-
}
|
|
224
216
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
public readonly publicKey: Uint8Array
|
|
217
|
+
throw new Error('Supplied PeerID CID is invalid')
|
|
218
|
+
}
|
|
228
219
|
|
|
229
|
-
|
|
230
|
-
|
|
220
|
+
/**
|
|
221
|
+
* @param publicKey - A marshalled public key
|
|
222
|
+
* @param privateKey - A marshalled private key
|
|
223
|
+
*/
|
|
224
|
+
export async function peerIdFromKeys (publicKey: Uint8Array, privateKey?: Uint8Array): Promise<PeerId> {
|
|
225
|
+
if (publicKey.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
|
226
|
+
return new Ed25519PeerIdImpl({ multihash: Digest.create(identity.code, publicKey), privateKey })
|
|
227
|
+
}
|
|
231
228
|
|
|
232
|
-
|
|
229
|
+
if (publicKey.length === MARSHALLED_SECP258K1_PUBLIC_KEY_LENGTH) {
|
|
230
|
+
return new Secp256k1PeerIdImpl({ multihash: Digest.create(identity.code, publicKey), privateKey })
|
|
233
231
|
}
|
|
232
|
+
|
|
233
|
+
return new RSAPeerIdImpl({ multihash: await sha256.digest(publicKey), publicKey, privateKey })
|
|
234
234
|
}
|