@@ -18,14 +18,14 @@ function generateSeed(
1818 entropy ?: Uint8Array
1919 algorithm ?: 'ed25519' | 'secp256k1'
2020 } = { } ,
21- ) {
21+ ) : string {
2222 assert ( ! options . entropy || options . entropy . length >= 16 , 'entropy too short' )
2323 const entropy = options . entropy ? options . entropy . slice ( 0 , 16 ) : brorand ( 16 )
2424 const type = options . algorithm === 'ed25519' ? 'ed25519' : 'secp256k1'
2525 return addressCodec . encodeSeed ( entropy , type )
2626}
2727
28- function hash ( message ) {
28+ function hash ( message ) : number [ ] {
2929 return hashjs
3030 . sha512 ( )
3131 . update ( message )
@@ -34,7 +34,13 @@ function hash(message) {
3434}
3535
3636const secp256k1 = {
37- deriveKeypair ( entropy , options ) {
37+ deriveKeypair (
38+ entropy ,
39+ options ,
40+ ) : {
41+ privateKey : string
42+ publicKey : string
43+ } {
3844 const prefix = '00'
3945
4046 const privateKey =
@@ -51,21 +57,26 @@ const secp256k1 = {
5157 return { privateKey, publicKey }
5258 } ,
5359
54- sign ( message , privateKey ) {
60+ sign ( message , privateKey ) : string {
5561 return bytesToHex (
5662 Secp256k1 . sign ( hash ( message ) , hexToBytes ( privateKey ) , {
5763 canonical : true ,
5864 } ) . toDER ( ) ,
5965 )
6066 } ,
6167
62- verify ( message , signature , publicKey ) {
68+ verify ( message , signature , publicKey ) : boolean {
6369 return Secp256k1 . verify ( hash ( message ) , signature , hexToBytes ( publicKey ) )
6470 } ,
6571}
6672
6773const ed25519 = {
68- deriveKeypair ( entropy ) {
74+ deriveKeypair (
75+ entropy ,
76+ ) : {
77+ privateKey : string
78+ publicKey : string
79+ } {
6980 const prefix = 'ED'
7081 const rawPrivateKey = hash ( entropy )
7182 const privateKey = prefix + bytesToHex ( rawPrivateKey )
@@ -74,7 +85,7 @@ const ed25519 = {
7485 return { privateKey, publicKey }
7586 } ,
7687
77- sign ( message , privateKey ) {
88+ sign ( message , privateKey ) : string {
7889 // caution: Ed25519.sign interprets all strings as hex, stripping
7990 // any non-hex characters without warning
8091 assert ( Array . isArray ( message ) , 'message must be array of octets' )
@@ -83,7 +94,7 @@ const ed25519 = {
8394 )
8495 } ,
8596
86- verify ( message , signature , publicKey ) {
97+ verify ( message , signature , publicKey ) : boolean {
8798 return Ed25519 . verify (
8899 message ,
89100 hexToBytes ( signature ) ,
@@ -92,12 +103,19 @@ const ed25519 = {
92103 } ,
93104}
94105
95- function select ( algorithm ) {
106+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
107+ function select ( algorithm ) : any {
96108 const methods = { 'ecdsa-secp256k1' : secp256k1 , ed25519 }
97109 return methods [ algorithm ]
98110}
99111
100- function deriveKeypair ( seed , options ) {
112+ function deriveKeypair (
113+ seed ,
114+ options ,
115+ ) : {
116+ publicKey : string
117+ privateKey : string
118+ } {
101119 const decoded = addressCodec . decodeSeed ( seed )
102120 const algorithm = decoded . type === 'ed25519' ? 'ed25519' : 'ecdsa-secp256k1'
103121 const method = select ( algorithm )
@@ -111,42 +129,42 @@ function deriveKeypair(seed, options) {
111129 return keypair
112130}
113131
114- function getAlgorithmFromKey ( key ) {
132+ function getAlgorithmFromKey ( key ) : 'ed25519' | 'ecdsa-secp256k1' {
115133 const bytes = hexToBytes ( key )
116134 return bytes . length === 33 && bytes [ 0 ] === 0xed
117135 ? 'ed25519'
118136 : 'ecdsa-secp256k1'
119137}
120138
121- function sign ( messageHex , privateKey ) {
139+ function sign ( messageHex , privateKey ) : string {
122140 const algorithm = getAlgorithmFromKey ( privateKey )
123141 return select ( algorithm ) . sign ( hexToBytes ( messageHex ) , privateKey )
124142}
125143
126- function verify ( messageHex , signature , publicKey ) {
144+ function verify ( messageHex , signature , publicKey ) : boolean {
127145 const algorithm = getAlgorithmFromKey ( publicKey )
128146 return select ( algorithm ) . verify ( hexToBytes ( messageHex ) , signature , publicKey )
129147}
130148
131- function deriveAddressFromBytes ( publicKeyBytes : Buffer ) {
149+ function deriveAddressFromBytes ( publicKeyBytes : Buffer ) : string {
132150 return addressCodec . encodeAccountID (
133151 utils . computePublicKeyHash ( publicKeyBytes ) ,
134152 )
135153}
136154
137- function deriveAddress ( publicKey ) {
138- return deriveAddressFromBytes ( hexToBytes ( publicKey ) )
155+ function deriveAddress ( publicKey ) : string {
156+ return deriveAddressFromBytes ( Buffer . from ( hexToBytes ( publicKey ) ) )
139157}
140158
141- function deriveNodeAddress ( publicKey ) {
159+ function deriveNodeAddress ( publicKey ) : string {
142160 const generatorBytes = addressCodec . decodeNodePublic ( publicKey )
143161 const accountPublicBytes = accountPublicFromPublicGenerator ( generatorBytes )
144162 return deriveAddressFromBytes ( accountPublicBytes )
145163}
146164
147165const { decodeSeed } = addressCodec
148166
149- module . exports = {
167+ export {
150168 generateSeed ,
151169 deriveKeypair ,
152170 sign ,
0 commit comments