11import { Multiaddr , multiaddr , protocols } from "@multiformats/multiaddr" ;
2- import base64url from "base64url" ;
3- import { toBigIntBE } from "bigint-buffer" ;
42import * as RLP from "rlp" ;
53import { KeyType , PeerId } from "@libp2p/interface" ;
64import { convertToString , convertToBytes } from "@multiformats/multiaddr/convert" ;
@@ -9,8 +7,9 @@ import { encode as varintEncode } from "uint8-varint";
97import { ERR_INVALID_ID , MAX_RECORD_SIZE } from "./constants.js" ;
108import { ENRKey , ENRValue , SequenceNumber , NodeId } from "./types.js" ;
119import { createPeerIdFromPublicKey , createPrivateKeyFromPeerId } from "./peerId.js" ;
12- import { toNewUint8Array } from "./util.js" ;
10+ import { fromBase64url , toBase64url , toBigInt , toNewUint8Array } from "./util.js" ;
1311import { getV4Crypto } from "./crypto.js" ;
12+ import { compare , fromString , toString } from "uint8arrays" ;
1413
1514/** ENR identity scheme */
1615export enum IDScheme {
@@ -34,7 +33,7 @@ export type SignableENRData = {
3433export function id ( kvs : ReadonlyMap < ENRKey , ENRValue > ) : IDScheme {
3534 const idBuf = kvs . get ( "id" ) ;
3635 if ( ! idBuf ) throw new Error ( "id not found" ) ;
37- const id = Buffer . from ( idBuf ) . toString ( "utf8" ) as IDScheme ;
36+ const id = toString ( idBuf , "utf8" ) as IDScheme ;
3837 if ( IDScheme [ id ] == null ) {
3938 throw new Error ( "Unknown enr id scheme: " + id ) ;
4039 }
@@ -143,7 +142,7 @@ export function decodeFromValues(decoded: Uint8Array[]): ENRData {
143142 }
144143 return {
145144 kvs,
146- seq : toBigIntBE ( Buffer . from ( seq ) ) ,
145+ seq : toBigInt ( seq ) ,
147146 signature,
148147 } ;
149148}
@@ -154,7 +153,7 @@ export function txtToBuf(encoded: string): Uint8Array {
154153 if ( ! encoded . startsWith ( "enr:" ) ) {
155154 throw new Error ( "string encoded ENR must start with 'enr:'" ) ;
156155 }
157- return base64url . toBuffer ( encoded . slice ( 4 ) ) ;
156+ return fromBase64url ( encoded . slice ( 4 ) ) ;
158157}
159158export function decodeTxt ( encoded : string ) : ENRData {
160159 return decode ( txtToBuf ( encoded ) ) ;
@@ -176,17 +175,19 @@ export function getIPValue(kvs: ReadonlyMap<ENRKey, ENRValue>, key: string, mult
176175export function getProtocolValue ( kvs : ReadonlyMap < ENRKey , ENRValue > , key : string ) : number | undefined {
177176 const raw = kvs . get ( key ) ;
178177 if ( raw ) {
179- const view = new DataView ( raw . buffer , raw . byteOffset , raw . byteLength ) ;
180- return view . getUint16 ( 0 ) ;
178+ if ( raw . length < 2 ) {
179+ throw new Error ( "Encoded protocol length should be 2" ) ;
180+ }
181+ return ( raw [ 0 ] << 8 ) + raw [ 1 ] ;
181182 } else {
182183 return undefined ;
183184 }
184185}
185186
186187export function portToBuf ( port : number ) : Uint8Array {
187188 const buf = new Uint8Array ( 2 ) ;
188- const view = new DataView ( buf . buffer , buf . byteOffset , buf . byteLength ) ;
189- view . setUint16 ( 0 , port ) ;
189+ buf [ 0 ] = port >> 8 ;
190+ buf [ 1 ] = port ;
190191 return buf ;
191192}
192193
@@ -296,7 +297,7 @@ export abstract class BaseENR {
296297 abstract encodeToValues ( ) : ( ENRKey | ENRValue | number ) [ ] ;
297298 abstract encode ( ) : Uint8Array ;
298299 encodeTxt ( ) : string {
299- return "enr:" + base64url . encode ( Buffer . from ( this . encode ( ) ) ) ;
300+ return "enr:" + toBase64url ( this . encode ( ) ) ;
300301 }
301302}
302303/**
@@ -397,7 +398,7 @@ export class SignableENR extends BaseENR {
397398 this . _signature = signature ;
398399
399400 if ( this . id === IDScheme . v4 ) {
400- if ( Buffer . compare ( getV4Crypto ( ) . publicKey ( this . privateKey ) , this . publicKey ) !== 0 ) {
401+ if ( compare ( getV4Crypto ( ) . publicKey ( this . privateKey ) , this . publicKey ) !== 0 ) {
401402 throw new Error ( "Provided keypair doesn't match kv pubkey" ) ;
402403 }
403404 }
@@ -411,7 +412,7 @@ export class SignableENR extends BaseENR {
411412 return new SignableENR (
412413 {
413414 ...kvs ,
414- id : Buffer . from ( "v4" ) ,
415+ id : fromString ( "v4" ) ,
415416 secp256k1 : getV4Crypto ( ) . publicKey ( privateKey ) ,
416417 } ,
417418 BigInt ( 1 ) ,
0 commit comments