From bd9ee6b5b1d34330754c48fa26d8b654b60f3811 Mon Sep 17 00:00:00 2001 From: Peter Benc Date: Mon, 9 Nov 2020 10:57:44 +0100 Subject: [PATCH] Replace official bech32 lib by cardano-crypto --- .../wallet/shelley/helpers/addresses.ts | 6 ++--- app/frontend/wallet/shelley/helpers/bech32.ts | 23 ------------------- .../shelley/shelley-js-crypto-provider.ts | 2 -- .../shelley/shelley-ledger-crypto-provider.ts | 2 -- .../shelley/shelley-transaction-planner.ts | 3 +-- .../wallet/shelley/shelley-transaction.ts | 3 +-- app/package.json | 1 - app/yarn.lock | 5 ---- 8 files changed, 5 insertions(+), 40 deletions(-) delete mode 100644 app/frontend/wallet/shelley/helpers/bech32.ts diff --git a/app/frontend/wallet/shelley/helpers/addresses.ts b/app/frontend/wallet/shelley/helpers/addresses.ts index 603554b3dc..103a27ee47 100644 --- a/app/frontend/wallet/shelley/helpers/addresses.ts +++ b/app/frontend/wallet/shelley/helpers/addresses.ts @@ -1,10 +1,10 @@ -import bech32 from './bech32' import { packBaseAddress, packRewardAddress, getAddressType, AddressTypes, base58, + bech32, getPubKeyBlake2b224Hash, } from 'cardano-crypto.js' import {HARDENED_THRESHOLD} from '../../constants' @@ -37,7 +37,7 @@ export const base58AddressToHex = (address: string): HexString => { export const accountAddressFromXpub = (stakeXpub: Xpub, networkId): string => { const addrBuffer = packRewardAddress(xpub2blake2b224Hash(stakeXpub), networkId) - return bech32.encode({prefix: 'addr', data: addrBuffer}) + return bech32.encode('addr', addrBuffer) } export const accountHexAddressFromXpub = (stakeXpub: Xpub, networkId): HexString => { @@ -51,7 +51,7 @@ export const baseAddressFromXpub = (spendXpub: Xpub, stakeXpub: Xpub, networkId) xpub2blake2b224Hash(stakeXpub), networkId ) - return bech32.encode({prefix: 'addr', data: addrBuffer}) + return bech32.encode('addr', addrBuffer) } export const isShelleyFormat = (address: string): boolean => { diff --git a/app/frontend/wallet/shelley/helpers/bech32.ts b/app/frontend/wallet/shelley/helpers/bech32.ts deleted file mode 100644 index eda9010fd5..0000000000 --- a/app/frontend/wallet/shelley/helpers/bech32.ts +++ /dev/null @@ -1,23 +0,0 @@ -import * as bech32 from 'bech32' - -interface Bech32Info { - prefix: string - data: Buffer -} - -export const encode = ({prefix, data}: Bech32Info): string => { - // Note(ppershing): yeah, really, bech32 lib has only api - const words = bech32.toWords(data) - // we need longer than default length for privkeys and 1000 should suffice - return bech32.encode(prefix, words, 1000) -} - -export const decode = (str: string): Bech32Info => { - const tmp = bech32.decode(str, 1000) - return { - prefix: tmp.prefix, - data: new Buffer(bech32.fromWords(tmp.words)), - } -} - -export default {encode, decode} diff --git a/app/frontend/wallet/shelley/shelley-js-crypto-provider.ts b/app/frontend/wallet/shelley/shelley-js-crypto-provider.ts index c62c2e453f..d08582b92d 100644 --- a/app/frontend/wallet/shelley/shelley-js-crypto-provider.ts +++ b/app/frontend/wallet/shelley/shelley-js-crypto-provider.ts @@ -16,7 +16,6 @@ import { ShelleyTxWitnessByron, } from './shelley-transaction' -// import {PROTOCOL_MAGIC_KEY} from '../constants' import {isShelleyPath} from './helpers/addresses' import CachedDeriveXpubFactory from '../helpers/CachedDeriveXpubFactory' @@ -93,7 +92,6 @@ const ShelleyJsCryptoProvider = ({ const _byronWitnesses = [] inputs.forEach((input) => { const inputPath = addressToAbsPathMapper(input.address) - // console.log(inputPath)await getHdPassphrase() isShelleyPath(inputPath) ? _shelleyWitnesses.push(build_shelley_witness(tx_body_hash, inputPath, sign)) : _byronWitnesses.push(build_byron_witness(tx_body_hash, sign, inputPath, input.address)) diff --git a/app/frontend/wallet/shelley/shelley-ledger-crypto-provider.ts b/app/frontend/wallet/shelley/shelley-ledger-crypto-provider.ts index 75ddbb4404..57987110b3 100644 --- a/app/frontend/wallet/shelley/shelley-ledger-crypto-provider.ts +++ b/app/frontend/wallet/shelley/shelley-ledger-crypto-provider.ts @@ -12,8 +12,6 @@ import { import * as platform from 'platform' import {hasMinimalVersion} from './helpers/version-check' -// import {PROTOCOL_MAGIC_KEY} from '../constants' - import { bechAddressToHex, isShelleyPath, diff --git a/app/frontend/wallet/shelley/shelley-transaction-planner.ts b/app/frontend/wallet/shelley/shelley-transaction-planner.ts index f0e92874bf..888daa8aa2 100644 --- a/app/frontend/wallet/shelley/shelley-transaction-planner.ts +++ b/app/frontend/wallet/shelley/shelley-transaction-planner.ts @@ -1,5 +1,4 @@ import {encode} from 'borc' -import bech32 from './helpers/bech32' import {ShelleyTxInputFromUtxo, ShelleyWitdrawal, ShelleyTxCert} from './shelley-transaction' @@ -8,7 +7,7 @@ import CborIndefiniteLengthArray from '../byron/helpers/CborIndefiniteLengthArra import NamedError from '../../helpers/NamedError' import {Lovelace} from '../../state' import getDonationAddress from '../../helpers/getDonationAddress' -import {base58} from 'cardano-crypto.js' +import {base58, bech32} from 'cardano-crypto.js' import {isShelleyFormat, isV1Address} from './helpers/addresses' export function txFeeFunction(txSizeInBytes: number): Lovelace { diff --git a/app/frontend/wallet/shelley/shelley-transaction.ts b/app/frontend/wallet/shelley/shelley-transaction.ts index 05daf27d03..164287aa49 100644 --- a/app/frontend/wallet/shelley/shelley-transaction.ts +++ b/app/frontend/wallet/shelley/shelley-transaction.ts @@ -1,8 +1,7 @@ /* eslint-disable no-use-before-define */ /* eslint-disable camelcase */ import {encode} from 'borc' -import {blake2b, base58} from 'cardano-crypto.js' -import bech32 from './helpers/bech32' +import {blake2b, base58, bech32} from 'cardano-crypto.js' import {isShelleyFormat} from './helpers/addresses' function ShelleyTxAux(inputs, outputs, fee, ttl, certs?, withdrawals?) { diff --git a/app/package.json b/app/package.json index 2b624870dd..f6fb1eb8ac 100644 --- a/app/package.json +++ b/app/package.json @@ -30,7 +30,6 @@ "@ledgerhq/hw-transport-u2f": "^5.15.0", "@ledgerhq/hw-transport-webusb": "^5.16.0", "babel-regenerator-runtime": "^6.5.0", - "bech32": "^1.1.3", "bip39-light": "^1.0.7", "borc": "^2.1.0", "cardano-crypto.js": "^6.0.0", diff --git a/app/yarn.lock b/app/yarn.lock index a986034160..744fd13ec8 100644 --- a/app/yarn.lock +++ b/app/yarn.lock @@ -466,11 +466,6 @@ base@^0.11.1: mixin-deep "^1.2.0" pascalcase "^0.1.1" -bech32@^1.1.3: - version "1.1.3" - resolved "https://registry.yarnpkg.com/bech32/-/bech32-1.1.3.tgz#bd47a8986bbb3eec34a56a097a84b8d3e9a2dfcd" - integrity sha512-yuVFUvrNcoJi0sv5phmqc6P+Fl1HjRDRNOOkHY2X/3LBy2bIGNSFx4fZ95HMaXHupuS7cZR15AsvtmCIF4UEyg== - bech32@^1.1.4: version "1.1.4" resolved "https://registry.yarnpkg.com/bech32/-/bech32-1.1.4.tgz#e38c9f37bf179b8eb16ae3a772b40c356d4832e9"