Skip to content

Commit

Permalink
refactor(utils)!: migrate keystore.js to ts
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `validateKeyObj` removed
Rely TypeScript checks instead.

BREAKING CHANGE: `deriveKeyUsingArgon2id` removed
Use `argon2-browser` package instead.
  • Loading branch information
martinkaintas authored and davidyuk committed Apr 27, 2022
1 parent 44c6e89 commit c013c01
Show file tree
Hide file tree
Showing 11 changed files with 236 additions and 263 deletions.
2 changes: 0 additions & 2 deletions docs/guides/error-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,9 @@ BaseError
└───CryptographyError
│ │ InvalidChecksumError
│ │ InvalidDerivationPathError
│ │ InvalidKeyError
│ │ InvalidPasswordError
│ │ MerkleTreeHashMismatchError
│ │ MissingNodeInTreeError
│ │ UnsupportedAlgorithmError
│ │ NotHardenedSegmentError
│ │ UnknownNodeLengthError
│ │ UnknownPathNibbleError
Expand Down
26 changes: 26 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@
"@commitlint/cli": "^16.1.0",
"@commitlint/config-conventional": "^16.0.0",
"@types/aes-js": "^3.1.1",
"@types/argon2-browser": "^1.18.1",
"@types/bn.js": "^5.1.0",
"@types/bs58": "^4.0.1",
"@types/chai": "^4.3.0",
"@types/json-bigint": "^1.0.1",
"@types/mocha": "^9.1.0",
"@types/node": "^17.0.17",
"@types/sha.js": "^2.4.0",
"@types/uuid": "^8.3.4",
"@typescript-eslint/eslint-plugin": "^5.11.0",
"@typescript-eslint/parser": "^5.11.0",
"babel-loader": "^8.2.3",
Expand Down
3 changes: 3 additions & 0 deletions src/typings/@aeternity__argon2-browser/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module '@aeternity/argon2-browser/dist/argon2-bundled.min.js' {
export * from 'argon2-browser'
}
3 changes: 3 additions & 0 deletions src/typings/@aeternity__uuid/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module '@aeternity/uuid' {
export * from 'uuid'
}
4 changes: 4 additions & 0 deletions src/utils/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,7 @@ export function str2buf (str: string, enc?: BufferEncoding): Buffer {
enc ?? (isHex(str) ? 'hex' : undefined) ?? (isBase64(str) ? 'base64' : undefined)
)
}

export const bytesToHex = (b: Uint8Array): string => Buffer.from(b).toString('hex')

export const hexToBytes = (s: string): Uint8Array => Buffer.from(s, 'hex')
9 changes: 4 additions & 5 deletions src/utils/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@ const Ecb = aesjs.ModeOfOperation.ecb
/**
* Generate address from secret key
* @rtype (secret: String) => tx: Promise[String]
* @param {String | Buffer} secret - Private key
* @param {String | Uint8Array} secret - Private key
* @return {String} Public key
*/
export function getAddressFromPriv (secret: string | Buffer): string {
const secretBuffer = Buffer.isBuffer(secret) ? secret : str2buf(secret)
export function getAddressFromPriv (secret: string | Uint8Array): string {
const secretBuffer = typeof secret === 'string' ? str2buf(secret) : secret
const keys = nacl.sign.keyPair.fromSecretKey(secretBuffer)
const publicBuffer = Buffer.from(keys.publicKey)
return encode(publicBuffer, 'ak')
return encode(keys.publicKey, 'ak')
}

/**
Expand Down
18 changes: 2 additions & 16 deletions src/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,16 +448,9 @@ export class InvalidDerivationPathError extends CryptographyError {
}
}

export class InvalidKeyError extends CryptographyError {
constructor (message: string) {
super(message)
this.name = 'InvalidKeyError'
}
}

export class InvalidPasswordError extends CryptographyError {
constructor (message: string) {
super(message)
constructor () {
super('Invalid password or nonce')
this.name = 'InvalidPasswordError'
}
}
Expand All @@ -476,13 +469,6 @@ export class MissingNodeInTreeError extends CryptographyError {
}
}

export class UnsupportedAlgorithmError extends CryptographyError {
constructor (algo: string) {
super(algo + ' is not available')
this.name = 'UnsupportedAlgorithmError'
}
}

export class NotHardenedSegmentError extends CryptographyError {
constructor (message: string) {
super(message)
Expand Down
213 changes: 0 additions & 213 deletions src/utils/keystore.js

This file was deleted.

Loading

0 comments on commit c013c01

Please sign in to comment.