Skip to content

Commit

Permalink
simplify dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
jleni committed Sep 1, 2024
1 parent 8cc190d commit b76d2bf
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 420 deletions.
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,15 @@
"dependencies": {
"@ledgerhq/hw-transport": "6.31.2",
"@zondax/ledger-js": "^1.0.1",
"bech32": "^2.0.0",
"@scure/base": "^1.1.7",
"buffer": "^6.0.3",
"crypto-browserify": "^3.12.0",
"ripemd160": "^2.0.2",
"stream-browserify": "^3.0.0"
"@noble/hashes": "^1.4",
"ripemd160": "^2.0.2"
},
"devDependencies": {
"@ledgerhq/hw-transport-mocker": "^6.29.2",
"@ledgerhq/hw-transport-node-hid": "^6.29.3",
"@noble/curves": "^1",
"@swc/core": "^1.7.22",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/jest": "^29.5.12",
"@types/ledgerhq__hw-transport": "4.21.8",
Expand All @@ -57,7 +55,6 @@
"@typescript-eslint/parser": "^8.3.0",
"bip32": "4.0.0",
"bip39": "3.1.0",
"core-js": "^3.38.1",
"eslint": "^9.9.1",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
Expand Down
20 changes: 10 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import { ByteStream } from '@zondax/ledger-js/dist/byteStream'
import { CLA, P1_VALUES, P2_VALUES, PKLEN } from './consts'
import { type ResponseAddress, type ResponsePubkey, ResponseSign } from './types'

const crypto = require('crypto')
const bech32 = require('bech32')
const Ripemd160 = require('ripemd160')
import { ripemd160 } from '@noble/hashes/ripemd160'
import { sha256 } from '@noble/hashes/sha256'
import { bech32 } from '@scure/base'

export default class CosmosApp extends BaseApp {
static _INS = {
Expand Down Expand Up @@ -91,7 +91,7 @@ export default class CosmosApp extends BaseApp {
}

const buf = new ByteStream()
pathArray.forEach((child: string, i: number) => {
pathArray.forEach((child: string) => {
let value = 0

if (child.endsWith("'")) {
Expand All @@ -112,7 +112,7 @@ export default class CosmosApp extends BaseApp {
}

async publicKey(path: string): Promise<ResponsePubkey> {
const serializedPath = await this.serializeCosmosPath(path)
const serializedPath = this.serializeCosmosPath(path)
const data = Buffer.concat([this.serializeHRP('cosmos'), serializedPath])

try {
Expand All @@ -129,7 +129,7 @@ export default class CosmosApp extends BaseApp {
}

async getAddressAndPubKey(path: string, hrp: string): Promise<ResponseAddress> {
const serializedPath = await this.serializeCosmosPath(path)
const serializedPath = this.serializeCosmosPath(path)
const data = Buffer.concat([this.serializeHRP(hrp), serializedPath])

try {
Expand All @@ -149,7 +149,7 @@ export default class CosmosApp extends BaseApp {
}

async showAddressAndPubKey(path: string, hrp: string): Promise<ResponseAddress> {
const serializedPath = await this.serializeCosmosPath(path)
const serializedPath = this.serializeCosmosPath(path)
const data = Buffer.concat([this.serializeHRP(hrp), serializedPath])

try {
Expand All @@ -172,13 +172,13 @@ export default class CosmosApp extends BaseApp {
if (pk.length !== 33) {
throw new Error('expected compressed public key [31 bytes]')
}
const hashSha256 = crypto.createHash('sha256').update(pk).digest()
const hashRip = new Ripemd160().update(hashSha256).digest()
const hashSha256 = sha256(pk)
const hashRip = ripemd160(hashSha256)
return bech32.encode(hrp, bech32.toWords(hashRip))
}

async prepareChunks_hrp(path: string, buffer: Buffer, hrp: string | undefined) {
const serializedPath = await this.serializeCosmosPath(path)
const serializedPath = this.serializeCosmosPath(path)
const firstChunk = hrp === undefined ? serializedPath : Buffer.concat([serializedPath, this.serializeHRP(hrp)])

const chunks = []
Expand Down
7 changes: 4 additions & 3 deletions tests/test_integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
} from './helper'

describe('CosmosApp Address Generation', () => {
it('Retreive Address and Pubkey', async () => {
it('Retrieve Address and Pubkey', async () => {
const responseBuffer = Buffer.from(GET_ADDRESS_PUBKEY_RESPONSE, 'hex')

const transport = new MockTransport(responseBuffer)
Expand Down Expand Up @@ -65,9 +65,10 @@ describe('CosmosApp Address Generation', () => {
const transport = new MockTransport(responseBuffer)
const app = new CosmosApp(transport)
try {
const resp = await app.getAddressAndPubKey(ETH_PATH, COSMOS_HRP)
await app.getAddressAndPubKey(ETH_PATH, COSMOS_HRP)
} catch (e: any) {
expect(e.message).toEqual('Transaction rejected')
expect(e.returnCode).toEqual(0x6986)
}
})

Expand All @@ -87,7 +88,7 @@ describe('CosmosApp Address Generation', () => {
const transport = new MockTransport(responseBuffer)
const app = new CosmosApp(transport)
try {
const resp = await app.getAddressAndPubKey("m/44'/2147483647'/0'/0/4294967295", 'cosmos')
await app.getAddressAndPubKey("m/44'/2147483647'/0'/0/4294967295", 'cosmos')
} catch (e: any) {
expect(e.returnCode).toEqual(0xffff)
}
Expand Down
Loading

0 comments on commit b76d2bf

Please sign in to comment.