Skip to content

Commit

Permalink
fix(WorkersTable): fix peerId format
Browse files Browse the repository at this point in the history
  • Loading branch information
MixailE committed Feb 3, 2025
1 parent 5d441d1 commit fc7f5cf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"date-fns": "^3.6.0",
"js-yaml": "^4.1.0",
"lodash.merge": "^4.6.2",
"multiformats": "^13.3.1",
"normalize.css": "^8.0.1",
"react": "^18.2.0",
"react-datepicker": "^4.24.0",
Expand Down
5 changes: 4 additions & 1 deletion src/pages/deal/WorkersTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
TableHeader,
} from '../../components/Table'
import { Text } from '../../components/Text'
import { peerIdContractHexToBase58 } from '../../utils/formatPeerIdHex'

import { DealJoinedWorkerFragment, getSdk } from '../../../generated/graphql'
import { graphQLClient } from '../../constants/config'
Expand Down Expand Up @@ -77,7 +78,9 @@ const PeerRow: React.FC<WorkerRowProps> = ({ worker }) => {
<Text size={12}>{worker.id}</Text>
</Cell>
<Cell>
<A href={`/peer/${worker.peer.id}`}>{worker.peer.id}</A>
<A href={`/peer/${peerIdContractHexToBase58(worker.peer.id)}`}>
{peerIdContractHexToBase58(worker.peer.id)}
</A>
</Cell>
</Row>
{worker.resources && (
Expand Down
36 changes: 36 additions & 0 deletions src/utils/formatPeerIdHex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { base58btc } from 'multiformats/bases/base58'

/**
* Fluence peer ids encoding follows the libp2p spec:
* https://github.com/libp2p/specs/blob/master/peer-ids/peer-ids.md.
*
* - Peer PK is bytes (32 bytes for Ed25519)
* - Peer PK is encoded with key type into protobuf (1 for Ed25519)
* - Peer ID is encoded Peer PK hashed using multihash (identity hash)
* - Peer ID text representation is Peer ID encoded with base58btc
*
* On chain Peer PK is stored as bytes32.
* Off chain Peer ID text representation is used.
*/

const PEER_BYTE58_PREFIX = new Uint8Array([
0, // identity hash
36, // length of the following data in bytes
8, // protobuf varint tag
1, // varint value (1 for Ed25519 key type)
18, // protobuf bytes tag
32, // length of the following data in bytes (Peer PK)
])

const BASE_58_PREFIX = 'z'

export function peerIdContractHexToBase58(peerIdHex: string) {
return base58btc
.encode(
Buffer.concat([
PEER_BYTE58_PREFIX,
Buffer.from(peerIdHex.slice(2), 'hex'),
]),
)
.slice(BASE_58_PREFIX.length)
}
3 changes: 2 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8183,6 +8183,7 @@ __metadata:
js-yaml: "npm:^4.1.0"
lint-staged: "npm:^15.0.1"
lodash.merge: "npm:^4.6.2"
multiformats: "npm:^13.3.1"
normalize.css: "npm:^8.0.1"
prettier: "npm:^3.0.3"
react: "npm:^18.2.0"
Expand Down Expand Up @@ -10407,7 +10408,7 @@ __metadata:
languageName: node
linkType: hard

"multiformats@npm:^13.0.0, multiformats@npm:^13.0.1, multiformats@npm:^13.1.0":
"multiformats@npm:^13.0.0, multiformats@npm:^13.0.1, multiformats@npm:^13.1.0, multiformats@npm:^13.3.1":
version: 13.3.1
resolution: "multiformats@npm:13.3.1"
checksum: 10c0/12a68569a2b74b0b3ed554af866149300ed587aa46ce65ef6539c522fc8d7deb5d38d38e41e004b50bc1109566e2c04bf848f1909c9e687e86f504c6cf586eac
Expand Down

0 comments on commit fc7f5cf

Please sign in to comment.