@@ -8,6 +8,7 @@ chai.use(dirtyChai)
8
8
const expect = chai . expect
9
9
const crypto = require ( 'libp2p-crypto' )
10
10
const mh = require ( 'multihashes' )
11
+ const CID = require ( 'cids' )
11
12
12
13
const PeerId = require ( '../src' )
13
14
@@ -17,6 +18,8 @@ const testId = require('./fixtures/sample-id')
17
18
const testIdHex = testId . id
18
19
const testIdBytes = mh . fromHexString ( testId . id )
19
20
const testIdB58String = mh . toB58String ( testIdBytes )
21
+ const testIdCID = new CID ( 1 , 'libp2p-key' , testIdBytes )
22
+ const testIdCIDString = testIdCID . toBaseEncodedString ( 'base32' )
20
23
21
24
const goId = require ( './fixtures/go-private-key' )
22
25
@@ -63,27 +66,68 @@ describe('PeerId', () => {
63
66
} ) . to . throw ( / i m m u t a b l e / )
64
67
} )
65
68
66
- it ( 'recreate an Id from Hex string' , ( ) => {
69
+ it ( 'recreate from Hex string' , ( ) => {
67
70
const id = PeerId . createFromHexString ( testIdHex )
68
- expect ( testIdBytes ) . to . deep . equal ( id . id )
71
+ expect ( testIdBytes ) . to . deep . equal ( id . toBytes ( ) )
69
72
} )
70
73
71
- it ( 'Recreate an Id from a Buffer' , ( ) => {
74
+ it ( 'recreate from a Buffer' , ( ) => {
72
75
const id = PeerId . createFromBytes ( testIdBytes )
73
76
expect ( testId . id ) . to . equal ( id . toHexString ( ) )
77
+ expect ( testIdBytes ) . to . deep . equal ( id . toBytes ( ) )
74
78
} )
75
79
76
- it ( 'Recreate a B58 String' , ( ) => {
80
+ it ( 'recreate from a B58 String' , ( ) => {
77
81
const id = PeerId . createFromB58String ( testIdB58String )
78
82
expect ( testIdB58String ) . to . equal ( id . toB58String ( ) )
83
+ expect ( testIdBytes ) . to . deep . equal ( id . toBytes ( ) )
79
84
} )
80
85
81
- it ( 'Recreate from a Public Key' , async ( ) => {
86
+ it ( 'recreate from CID object' , ( ) => {
87
+ const id = PeerId . createFromCID ( testIdCID )
88
+ expect ( testIdCIDString ) . to . equal ( id . toCIDString ( ) )
89
+ expect ( testIdBytes ) . to . deep . equal ( id . toBytes ( ) )
90
+ } )
91
+
92
+ it ( 'recreate from Base58 String (CIDv0))' , ( ) => {
93
+ const id = PeerId . createFromCID ( testIdB58String )
94
+ expect ( testIdCIDString ) . to . equal ( id . toCIDString ( ) )
95
+ expect ( testIdBytes ) . to . deep . equal ( id . toBytes ( ) )
96
+ } )
97
+
98
+ it ( 'recreate from CIDv1 Base32' , ( ) => {
99
+ const id = PeerId . createFromCID ( testIdCIDString )
100
+ expect ( testIdCIDString ) . to . equal ( id . toCIDString ( ) )
101
+ expect ( testId . id ) . to . equal ( id . toHexString ( ) )
102
+ } )
103
+
104
+ it ( 'recreate from CID Buffer' , ( ) => {
105
+ const id = PeerId . createFromCID ( testIdCID . buffer )
106
+ expect ( testIdCIDString ) . to . equal ( id . toCIDString ( ) )
107
+ expect ( testIdBytes ) . to . deep . equal ( id . toBytes ( ) )
108
+ } )
109
+
110
+ it ( 'throws on invalid CID value' , async ( ) => {
111
+ const invalidCID = 'QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L'
112
+ expect ( ( ) => {
113
+ PeerId . createFromCID ( invalidCID )
114
+ } ) . to . throw ( / m u l t i h a s h u n k n o w n f u n c t i o n c o d e : 0 x 5 0 / )
115
+ } )
116
+
117
+ it ( 'throws on invalid CID object' , async ( ) => {
118
+ const invalidCID = { }
119
+ expect ( ( ) => {
120
+ PeerId . createFromCID ( invalidCID )
121
+ } ) . to . throw ( / S u p p l i e d c i d v a l u e i s n e i t h e r S t r i n g | C I D | B u f f e r / )
122
+ } )
123
+
124
+ it ( 'recreate from a Public Key' , async ( ) => {
82
125
const id = await PeerId . createFromPubKey ( testId . pubKey )
83
126
expect ( testIdB58String ) . to . equal ( id . toB58String ( ) )
127
+ expect ( testIdBytes ) . to . deep . equal ( id . toBytes ( ) )
84
128
} )
85
129
86
- it ( 'Recreate from a Private Key' , async ( ) => {
130
+ it ( 'recreate from a Private Key' , async ( ) => {
87
131
const id = await PeerId . createFromPrivKey ( testId . privKey )
88
132
expect ( testIdB58String ) . to . equal ( id . toB58String ( ) )
89
133
const encoded = Buffer . from ( testId . privKey , 'base64' )
@@ -92,7 +136,7 @@ describe('PeerId', () => {
92
136
expect ( id . marshalPubKey ( ) ) . to . deep . equal ( id2 . marshalPubKey ( ) )
93
137
} )
94
138
95
- it ( 'Recreate from Protobuf' , async ( ) => {
139
+ it ( 'recreate from Protobuf' , async ( ) => {
96
140
const id = await PeerId . createFromProtobuf ( testId . marshaled )
97
141
expect ( testIdB58String ) . to . equal ( id . toB58String ( ) )
98
142
const encoded = Buffer . from ( testId . privKey , 'base64' )
0 commit comments