/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

  1. import { bip32asBuffer } from "@ledgerhq/hw-app-btc/lib/bip32";
  2. import type { DerivationMode } from "../../derivation";
  3. import { getAddressFormatDerivationMode } from "../../derivation";
  4. import invariant from "invariant";
  5. const addressFormatMap = {
  6. legacy: 0,
  7. p2sh: 1,
  8. bech32: 2,
  9. bitcoin_cash: 3,
  10. bech32m: 4,
  11. };
  12. const getSerializedAddressParameters = (
  13. path: string,
  14. derivationMode: DerivationMode,
  15. addressFormat?: string
  16. ): {
  17. addressParameters: Buffer;
  18. } => {
  19. const format =
  20. addressFormat && addressFormat in addressFormatMap
  21. ? addressFormat
  22. : getAddressFormatDerivationMode(derivationMode);
  23. invariant(
  24. Object.keys(addressFormatMap).includes(format),
  25. "unsupported format %s",
  26. format
  27. );
  28. const buffer = bip32asBuffer(path);
  29. const addressParameters = Buffer.concat([
  30. Buffer.from([addressFormatMap[format]]),
  31. buffer,
  32. ]);
  33. return {
  34. addressParameters,
  35. };
  36. };
  37. export default {
  38. getSerializedAddressParameters,
  39. };