Skip to content
This repository was archived by the owner on Nov 24, 2021. It is now read-only.

Commit 9ffd00d

Browse files
committed
Add some types
Ignore linting on a couple of files. We should add types for these too, and re-enable linting.
1 parent 767e064 commit 9ffd00d

File tree

7 files changed

+48
-26
lines changed

7 files changed

+48
-26
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ node_js:
66
- 13
77
script:
88
- yarn compile
9-
- yarn test
109
- yarn lint
10+
- yarn test

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"devDependencies": {
2121
"@types/mocha": "^5.2.7",
22-
"@types/node": "^13.1.5",
22+
"@types/node": "^13.5.2",
2323
"@typescript-eslint/eslint-plugin": "^2.16.0",
2424
"@typescript-eslint/parser": "^2.16.0",
2525
"eslint": "^6.8.0",

src/index.ts

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3636
const 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

6773
const 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

147165
const { decodeSeed } = addressCodec
148166

149-
module.exports = {
167+
export {
150168
generateSeed,
151169
deriveKeypair,
152170
sign,

src/secp256k1.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable */
2+
13
import * as elliptic from 'elliptic'
24
import Sha512 from './sha512'
35

src/sha512.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable */
2+
13
import * as hashjs from 'hash.js'
24
import * as BigNum from 'bn.js'
35

src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as assert from 'assert'
22
import * as hashjs from 'hash.js'
33
import * as BN from 'bn.js'
44

5-
function bytesToHex(a) {
5+
function bytesToHex(a): string {
66
return a
77
.map((byteValue) => {
88
const hex = byteValue.toString(16).toUpperCase()
@@ -11,7 +11,7 @@ function bytesToHex(a) {
1111
.join('')
1212
}
1313

14-
function hexToBytes(a) {
14+
function hexToBytes(a): number[] {
1515
assert(a.length % 2 === 0)
1616
return new BN(a, 16).toArray(null, a.length / 2)
1717
}

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@
154154
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-5.2.7.tgz#315d570ccb56c53452ff8638738df60726d5b6ea"
155155
integrity sha512-NYrtPht0wGzhwe9+/idPaBB+TqkY9AhTvOLMkThm0IoEfLaiVQZwBwyJ5puCkO3AUCWrmcoePjp2mbFocKy4SQ==
156156

157-
"@types/node@^13.1.5":
158-
version "13.5.1"
159-
resolved "https://registry.yarnpkg.com/@types/node/-/node-13.5.1.tgz#6fae50892d1841f4b38b298e2f78fb68c5960cb9"
160-
integrity sha512-Jj2W7VWQ2uM83f8Ls5ON9adxN98MvyJsMSASYFuSvrov8RMRY64Ayay7KV35ph1TSGIJ2gG9ZVDdEq3c3zaydA==
157+
"@types/node@^13.5.2":
158+
version "13.5.2"
159+
resolved "https://registry.yarnpkg.com/@types/node/-/node-13.5.2.tgz#3de53b55fd39efc428a901a0f6db31f761cfa131"
160+
integrity sha512-Fr6a47c84PRLfd7M7u3/hEknyUdQrrBA6VoPmkze0tcflhU5UnpWEX2kn12ktA/lb+MNHSqFlSiPHIHsaErTPA==
161161

162162
"@typescript-eslint/eslint-plugin@^2.16.0":
163163
version "2.18.0"

0 commit comments

Comments
 (0)