| 1 | import { isPeerId } from '@libp2p/interface' |
| 2 | import { peerIdFromMultihash } from '@libp2p/peer-id' |
| 3 | import * as Digest from 'multiformats/hashes/digest' |
| 4 | import type { PeerId } from '@libp2p/interface' |
| 5 | import type { ValueCodec } from 'it-rpc' |
| 6 | |
| 7 | export const peerIdCodec: ValueCodec<PeerId> = { |
| 8 | type: 4098, |
| 9 | canEncode: (val) => isPeerId(val), |
| 10 | encode: (val) => val.toMultihash().bytes, |
| 11 | decode: (buf) => peerIdFromMultihash(Digest.decode(buf)) |
| 12 | } |