@exodus/hw-ledger 2.2.6 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,12 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [2.3.0](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/hw-ledger@2.2.6...@exodus/hw-ledger@2.3.0) (2024-02-08)
7
+
8
+ ### Features
9
+
10
+ - add bip322 message support to Ledger ([#5641](https://github.com/ExodusMovement/exodus-hydra/issues/5641)) ([b89dd27](https://github.com/ExodusMovement/exodus-hydra/commit/b89dd27435e8894f9a4ad3a10c88c0f7d165f9a0))
11
+
6
12
  ## [2.2.6](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/hw-ledger@2.2.5...@exodus/hw-ledger@2.2.6) (2024-01-31)
7
13
 
8
14
  ### Bug Fixes
@@ -1,8 +1,10 @@
1
1
  import bs58 from 'bs58';
2
2
  import { AppClient, PsbtV2 } from '@exodus/ledger-bitcoin';
3
+ import { BIP322 } from '@exodus/bip322-js';
3
4
  import { splitBip44DerivationPath } from './utils';
4
5
  import assert from 'minimalistic-assert';
5
6
  import { assertPsbtOnlyHasAllowedDerivationPaths, tagPsbtWithDerivationPaths, getWalletPolicy, derivationPathsToWalletPolicy, } from './utils/psbt';
7
+ import { buildBip322PSBT } from './utils/bip322';
6
8
  async function createHandler(transport) {
7
9
  const app = new AppClient(transport);
8
10
  const getAddress = async (params) => {
@@ -48,9 +50,48 @@ async function createHandler(transport) {
48
50
  return result;
49
51
  };
50
52
  const signMessage = async (params) => {
51
- assert(Buffer.isBuffer(params.message.rawMessage), 'rawMessage must be buffer');
52
- const signedMessageHex = await app.signMessage(params.message.rawMessage, params.derivationPath);
53
- return Buffer.from(signedMessageHex, 'base64');
53
+ if (params.message.rawMessage) {
54
+ assert(Buffer.isBuffer(params.message.rawMessage), 'rawMessage must be buffer');
55
+ const signedMessageHex = await app.signMessage(params.message.rawMessage, params.derivationPath);
56
+ return Buffer.from(signedMessageHex, 'base64');
57
+ }
58
+ if (params.message.bip322Message) {
59
+ const { message, address } = params.message.bip322Message;
60
+ const { derivationPath } = params;
61
+ assert(Buffer.isBuffer(message), 'bip322Message.message must be buffer');
62
+ assert(typeof address === 'string', 'bip322Message.address must be string');
63
+ const [purpose, coin, account] = splitBip44DerivationPath(derivationPath);
64
+ const fpr = await app.getMasterFingerprint();
65
+ const xpub = await app.getExtendedPubkey(`m/${purpose}/${coin}/${account}`);
66
+ const walletPolicy = getWalletPolicy(fpr, xpub, purpose, coin, account);
67
+ const psbtBip322 = buildBip322PSBT(message.toString(), address);
68
+ const psbt = new PsbtV2();
69
+ psbt.deserialize(psbtBip322.toBuffer());
70
+ const derivationPathToPublicKeyMap = {
71
+ [derivationPath]: await getPublicKey({
72
+ assetName: 'bitcoin',
73
+ derivationPath,
74
+ }),
75
+ };
76
+ tagPsbtWithDerivationPaths(psbt, fpr, derivationPathToPublicKeyMap);
77
+ const signatures = await app.signPsbt(psbt.serialize(), walletPolicy, null);
78
+ const signature = signatures[0][1];
79
+ const isTaprootSig = signature.pubkey.length === 32;
80
+ if (isTaprootSig) {
81
+ psbtBip322.updateInput(0, {
82
+ tapKeySig: signature.signature,
83
+ });
84
+ }
85
+ else {
86
+ psbtBip322.updateInput(0, {
87
+ partialSig: [signature],
88
+ });
89
+ }
90
+ psbtBip322.finalizeAllInputs();
91
+ const encodedWitness = BIP322.encodeWitness(psbtBip322);
92
+ return Buffer.from(encodedWitness, 'base64');
93
+ }
94
+ throw new Error('rawMessage or bip322Message must be defined');
54
95
  };
55
96
  return {
56
97
  getAddress,
@@ -0,0 +1,2 @@
1
+ import * as bitcoin from '@exodus/bitcoinjs-lib';
2
+ export declare function buildBip322PSBT(message: string, address: string): bitcoin.Psbt;
@@ -0,0 +1,29 @@
1
+ import * as bitcoin from '@exodus/bitcoinjs-lib';
2
+ import { BIP322, Address } from '@exodus/bip322-js';
3
+ export function buildBip322PSBT(message, address) {
4
+ const scriptPubKey = Address.convertAdressToScriptPubkey(address);
5
+ const toSpendTx = BIP322.buildToSpendTx(message, scriptPubKey);
6
+ const toSpendTxId = toSpendTx.getId();
7
+ if (Address.isP2SH(address)) {
8
+ throw new Error('Unable to sign BIP-322 message for unimplemented address type.');
9
+ }
10
+ else if (Address.isP2WPKH(address) || Address.isP2TR(address)) {
11
+ return buildToSignNativeSegwitOrTaproot(toSpendTxId, scriptPubKey);
12
+ }
13
+ throw new Error('Unable to sign BIP-322 message for unsupported address type.');
14
+ }
15
+ function buildToSignNativeSegwitOrTaproot(toSpendTxId, scriptPubKey) {
16
+ return new bitcoin.Psbt()
17
+ .setVersion(0)
18
+ .setLocktime(0)
19
+ .addInput({
20
+ hash: toSpendTxId,
21
+ index: 0,
22
+ sequence: 0,
23
+ witnessUtxo: { value: 0, script: scriptPubKey },
24
+ })
25
+ .addOutput({
26
+ value: 0,
27
+ script: Buffer.from([bitcoin.opcodes.OP_RETURN]),
28
+ });
29
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/hw-ledger",
3
- "version": "2.2.6",
3
+ "version": "2.3.0",
4
4
  "description": "The ledger module",
5
5
  "author": "Exodus Movement Inc.",
6
6
  "repository": {
@@ -30,6 +30,7 @@
30
30
  "prepublishOnly": "yarn run -T build --scope @exodus/hw-ledger"
31
31
  },
32
32
  "dependencies": {
33
+ "@exodus/bip322-js": "^1.1.0-exodus.4",
33
34
  "@exodus/bitcoinjs-lib": "^6.1.5-exodus.0",
34
35
  "@exodus/ethersproject-transactions": "^5.4.2-exodus.0",
35
36
  "@exodus/hw-common": "^2.0.0",
@@ -67,5 +68,5 @@
67
68
  "ts-jest": "^29.0.3",
68
69
  "typescript": "^4.7.3"
69
70
  },
70
- "gitHead": "e4a97ba8123768d13a5f73b03d2ff87d1845ba19"
71
+ "gitHead": "c066fd85fa5cf0090a3d1b573403c79c21a84102"
71
72
  }