Skip to content

Commit

Permalink
refactor: improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
bangjelkoski committed Nov 7, 2023
1 parent 090cab5 commit 8f87e3f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 151 deletions.
1 change: 0 additions & 1 deletion packages/sdk-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
},
"dependencies": {
"@apollo/client": "^3.5.8",
"eth-crypto": "^2.6.0",
"@cosmjs/amino": "^0.30.1",
"@cosmjs/proto-signing": "^0.30.1",
"@cosmjs/stargate": "^0.30.1",
Expand Down
16 changes: 9 additions & 7 deletions packages/sdk-ts/src/core/accounts/PublicKey.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { BECH32_PUBKEY_ACC_PREFIX } from '../../utils'
import { BECH32_PUBKEY_ACC_PREFIX, decompressPubKey } from '../../utils'
import { bech32 } from 'bech32'
import { toBuffer } from 'ethereumjs-util'
import secp256k1 from 'secp256k1'
import { Address } from './Address'
import {
publicKey as EthCryptoPublicKey,
util as EthCryptoUtil,
} from 'eth-crypto'
import { keccak256 } from 'js-sha3'
import {
GoogleProtobufAny,
Expand Down Expand Up @@ -63,9 +59,15 @@ export class PublicKey {

public toAddress(): Address {
const publicKeyHex = this.toHex()
const decompressedPublicKey = EthCryptoPublicKey.decompress(publicKeyHex)
const decompressedPublicKey = decompressPubKey(publicKeyHex)
const addressBuffer = Buffer.from(
keccak256(toBuffer(EthCryptoUtil.addLeading0x(decompressedPublicKey))),
keccak256(
toBuffer(
decompressedPublicKey.startsWith('0x')
? decompressedPublicKey
: '0x' + decompressedPublicKey,
),
),
'hex',
).subarray(-20)

Expand Down
24 changes: 24 additions & 0 deletions packages/sdk-ts/src/utils/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,27 @@ export const messageHash = (message: any) =>
message.types,
SignTypedDataVersion.V4,
)

export function uint8ArrayToHex(arr: Uint8Array) {
return Buffer.from(arr).toString('hex')
}

export function hexToUnit8Array(str: string) {
return new Uint8Array(Buffer.from(str, 'hex'))
}

export function decompressPubKey(startsWith02Or03: string) {
// if already decompressed an not has trailing 04
const testBuffer = Buffer.from(startsWith02Or03, 'hex')

if (testBuffer.length === 64) startsWith02Or03 = '04' + startsWith02Or03

let decompressed = uint8ArrayToHex(
secp256k1.publicKeyConvert(hexToUnit8Array(startsWith02Or03), false),
)

// remove trailing 04
decompressed = decompressed.substring(2)

return decompressed
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { domainHash, messageHash } from './utils'
import { WalletAction, WalletDeviceType } from '../../../../types/enums'
import { getKeyFromRpcUrl } from '../../../../utils/alchemy'
import { Alchemy, Network as AlchemyNetwork } from 'alchemy-sdk'
import { ledgerService } from '@ledgerhq/hw-app-eth'

const getNetworkFromChainId = (chainId: EthereumChainId): Chain => {
if (chainId === EthereumChainId.Goerli) {
Expand Down Expand Up @@ -299,10 +300,7 @@ export default class LedgerBase
try {
const ledger = await this.ledger.getInstance()
const { derivationPath } = await this.getWalletForAddress(options.address)
const ledgerService = await import(
'@ledgerhq/hw-app-eth/lib/services/ledger'
)
const resolution = await ledgerService.default.resolveTransaction(
const resolution = await ledgerService.resolveTransaction(
encodedMessageHex,
{},
{},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import TransportWebHID from '@ledgerhq/hw-transport-webhid'
import TransportWebUSB from '@ledgerhq/hw-transport-webusb'
import type EthereumApp from '@ledgerhq/hw-app-eth'
import EthereumApp from '@ledgerhq/hw-app-eth'
import type Transport from '@ledgerhq/hw-transport'
import {
ErrorType,
Expand Down Expand Up @@ -49,16 +49,14 @@ export default class LedgerTransport {
}

async getInstance(): Promise<EthereumApp> {
const EthereumApp = await import('@ledgerhq/hw-app-eth')

if (this.ledger) {
return this.ledger
}

try {
const transport = await LedgerTransport.getTransport()

this.ledger = new EthereumApp.default(transport)
this.ledger = new EthereumApp(transport)

transport.on('disconnect', () => {
this.ledger = null
Expand Down
Loading

0 comments on commit 8f87e3f

Please sign in to comment.