/src/families/bitcoin/exchange.ts
https://github.com/LedgerHQ/ledger-live-common · TypeScript · 41 lines · 39 code · 2 blank · 0 comment · 3 complexity · 2cb1a312072189f9bee8f78b0bccaba8 MD5 · raw file
- import { bip32asBuffer } from "@ledgerhq/hw-app-btc/lib/bip32";
- import type { DerivationMode } from "../../derivation";
- import { getAddressFormatDerivationMode } from "../../derivation";
- import invariant from "invariant";
- const addressFormatMap = {
- legacy: 0,
- p2sh: 1,
- bech32: 2,
- bitcoin_cash: 3,
- bech32m: 4,
- };
- const getSerializedAddressParameters = (
- path: string,
- derivationMode: DerivationMode,
- addressFormat?: string
- ): {
- addressParameters: Buffer;
- } => {
- const format =
- addressFormat && addressFormat in addressFormatMap
- ? addressFormat
- : getAddressFormatDerivationMode(derivationMode);
- invariant(
- Object.keys(addressFormatMap).includes(format),
- "unsupported format %s",
- format
- );
- const buffer = bip32asBuffer(path);
- const addressParameters = Buffer.concat([
- Buffer.from([addressFormatMap[format]]),
- buffer,
- ]);
- return {
- addressParameters,
- };
- };
- export default {
- getSerializedAddressParameters,
- };