1- import { toAscii , toHex } from "@cosmjs/encoding" ;
1+ import { fromHex , toAscii , toHex } from "@cosmjs/encoding" ;
22import { Uint32 , Uint53 } from "@cosmjs/math" ;
33import { secp256k1 } from "@noble/curves/secp256k1" ;
4- // eslint-disable-next-line @typescript-eslint/naming-convention
5- import BN from "bn.js" ;
64
75import { Hmac } from "./hmac" ;
86import { Sha512 } from "./sha" ;
@@ -26,6 +24,12 @@ function bytesToUnsignedBigInt(a: Uint8Array): bigint {
2624 return BigInt ( "0x" + toHex ( a ) ) ;
2725}
2826
27+ function intTo32be ( n : bigint ) : Uint8Array {
28+ // 32 bytes is 64 hexadecimal characters
29+ const hex = n . toString ( 16 ) . padStart ( 64 , "0" ) ;
30+ return fromHex ( hex ) ;
31+ }
32+
2933/**
3034 * Reverse mapping of Slip10Curve
3135 */
@@ -174,9 +178,9 @@ export class Slip10 {
174178 }
175179
176180 // step 5
177- const n = this . n ( curve ) ;
178- const returnChildKeyAsNumber = new BN ( il ) . add ( new BN ( parentPrivkey ) ) . mod ( n ) ;
179- const returnChildKey = Uint8Array . from ( returnChildKeyAsNumber . toArray ( "be" , 32 ) ) ;
181+ const n : bigint = this . n ( curve ) ;
182+ const returnChildKeyAsNumber = ( bytesToUnsignedBigInt ( il ) + bytesToUnsignedBigInt ( parentPrivkey ) ) % n ;
183+ const returnChildKey = intTo32be ( returnChildKeyAsNumber ) ;
180184
181185 // step 6
182186 if ( this . isGteN ( curve , il ) || this . isZero ( returnChildKey ) ) {
@@ -198,14 +202,14 @@ export class Slip10 {
198202 }
199203
200204 private static isGteN ( curve : Slip10Curve , privkey : Uint8Array ) : boolean {
201- const keyAsNumber = new BN ( privkey ) ;
202- return keyAsNumber . gte ( this . n ( curve ) ) ;
205+ const keyAsNumber : bigint = bytesToUnsignedBigInt ( privkey ) ;
206+ return keyAsNumber >= this . n ( curve ) ;
203207 }
204208
205- private static n ( curve : Slip10Curve ) : BN {
209+ private static n ( curve : Slip10Curve ) : bigint {
206210 switch ( curve ) {
207211 case Slip10Curve . Secp256k1 :
208- return new BN ( "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141" , 16 ) ;
212+ return 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141n ;
209213 default :
210214 throw new Error ( "curve not supported" ) ;
211215 }
0 commit comments