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