@exodus/hw-ledger 2.2.6 → 2.3.1

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,18 @@
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.1](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/hw-ledger@2.3.0...@exodus/hw-ledger@2.3.1) (2024-02-13)
7
+
8
+ ### Bug Fixes
9
+
10
+ - add dummy inputs when signing psbt on ledger ([#5680](https://github.com/ExodusMovement/exodus-hydra/issues/5680)) ([b11c6c6](https://github.com/ExodusMovement/exodus-hydra/commit/b11c6c67db196d7b970ded3228e7482ff1881c57))
11
+
12
+ ## [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)
13
+
14
+ ### Features
15
+
16
+ - add bip322 message support to Ledger ([#5641](https://github.com/ExodusMovement/exodus-hydra/issues/5641)) ([b89dd27](https://github.com/ExodusMovement/exodus-hydra/commit/b89dd27435e8894f9a4ad3a10c88c0f7d165f9a0))
17
+
6
18
  ## [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
19
 
8
20
  ### 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
- import { assertPsbtOnlyHasAllowedDerivationPaths, tagPsbtWithDerivationPaths, getWalletPolicy, derivationPathsToWalletPolicy, } from './utils/psbt';
6
+ import { assertPsbtOnlyHasAllowedDerivationPaths, tagPsbtWithDerivationPaths, getWalletPolicy, derivationPathsToWalletPolicy, addDummyInputs, } 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) => {
@@ -32,6 +34,7 @@ async function createHandler(transport) {
32
34
  });
33
35
  }
34
36
  tagPsbtWithDerivationPaths(psbt, fpr, derivationPathToPublicKeyMap);
37
+ addDummyInputs(psbt);
35
38
  assertPsbtOnlyHasAllowedDerivationPaths(psbt, fpr, params.derivationPaths);
36
39
  const signatures = [];
37
40
  const walletPolicies = await derivationPathsToWalletPolicy(fpr, app, params.derivationPaths);
@@ -48,9 +51,48 @@ async function createHandler(transport) {
48
51
  return result;
49
52
  };
50
53
  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');
54
+ if (params.message.rawMessage) {
55
+ assert(Buffer.isBuffer(params.message.rawMessage), 'rawMessage must be buffer');
56
+ const signedMessageHex = await app.signMessage(params.message.rawMessage, params.derivationPath);
57
+ return Buffer.from(signedMessageHex, 'base64');
58
+ }
59
+ if (params.message.bip322Message) {
60
+ const { message, address } = params.message.bip322Message;
61
+ const { derivationPath } = params;
62
+ assert(Buffer.isBuffer(message), 'bip322Message.message must be buffer');
63
+ assert(typeof address === 'string', 'bip322Message.address must be string');
64
+ const [purpose, coin, account] = splitBip44DerivationPath(derivationPath);
65
+ const fpr = await app.getMasterFingerprint();
66
+ const xpub = await app.getExtendedPubkey(`m/${purpose}/${coin}/${account}`);
67
+ const walletPolicy = getWalletPolicy(fpr, xpub, purpose, coin, account);
68
+ const psbtBip322 = buildBip322PSBT(message.toString(), address);
69
+ const psbt = new PsbtV2();
70
+ psbt.deserialize(psbtBip322.toBuffer());
71
+ const derivationPathToPublicKeyMap = {
72
+ [derivationPath]: await getPublicKey({
73
+ assetName: 'bitcoin',
74
+ derivationPath,
75
+ }),
76
+ };
77
+ tagPsbtWithDerivationPaths(psbt, fpr, derivationPathToPublicKeyMap);
78
+ const signatures = await app.signPsbt(psbt.serialize(), walletPolicy, null);
79
+ const signature = signatures[0][1];
80
+ const isTaprootSig = signature.pubkey.length === 32;
81
+ if (isTaprootSig) {
82
+ psbtBip322.updateInput(0, {
83
+ tapKeySig: signature.signature,
84
+ });
85
+ }
86
+ else {
87
+ psbtBip322.updateInput(0, {
88
+ partialSig: [signature],
89
+ });
90
+ }
91
+ psbtBip322.finalizeAllInputs();
92
+ const encodedWitness = BIP322.encodeWitness(psbtBip322);
93
+ return Buffer.from(encodedWitness, 'base64');
94
+ }
95
+ throw new Error('rawMessage or bip322Message must be defined');
54
96
  };
55
97
  return {
56
98
  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
+ }
@@ -4,3 +4,4 @@ export declare function assertPsbtOnlyHasAllowedDerivationPaths(psbt: PsbtV2, ma
4
4
  export declare function tagPsbtWithDerivationPaths(psbt: PsbtV2, masterFingerprint: string, derivationPathToPublicKeyMap: Record<string, Buffer>): void;
5
5
  export declare const getWalletPolicy: (fpr: string, xpub: string, purpose: string, coin: string, account: string) => DefaultWalletPolicy;
6
6
  export declare function derivationPathsToWalletPolicy(fpr: string, app: any, derivationPaths: string[]): Promise<WalletPolicy[]>;
7
+ export declare function addDummyInputs(psbt: PsbtV2): void;
@@ -119,3 +119,40 @@ export async function derivationPathsToWalletPolicy(fpr, app, derivationPaths) {
119
119
  }
120
120
  return walletPolicies;
121
121
  }
122
+ function canModifyInputs(psbt) {
123
+ const SIGHASH_ALL = 0x01;
124
+ const SIGHASH_NONE = 0x02;
125
+ const SIGHASH_SINGLE = 0x03;
126
+ for (let i = 0; i < psbt.getGlobalInputCount(); i++) {
127
+ if ([SIGHASH_ALL, SIGHASH_NONE, SIGHASH_SINGLE, undefined].includes(psbt.getInputSighashType(i))) {
128
+ return false;
129
+ }
130
+ }
131
+ return true;
132
+ }
133
+ export function addDummyInputs(psbt) {
134
+ const canAddDummy = canModifyInputs(psbt);
135
+ if (!canAddDummy) {
136
+ return;
137
+ }
138
+ let outputAmount = 0;
139
+ for (let i = 0; i < psbt.getGlobalOutputCount(); i++) {
140
+ outputAmount += psbt.getOutputAmount(i);
141
+ }
142
+ let inputAmount = 0;
143
+ for (let i = 0; i < psbt.getGlobalInputCount(); i++) {
144
+ const witnessUtxo = psbt.getInputWitnessUtxo(i);
145
+ if (witnessUtxo) {
146
+ inputAmount += witnessUtxo.amount;
147
+ }
148
+ }
149
+ if (outputAmount > inputAmount) {
150
+ const newInputIndex = psbt.getGlobalInputCount();
151
+ const amount = outputAmount - inputAmount;
152
+ psbt.setGlobalInputCount(newInputIndex + 1);
153
+ const FIRST_BITCOIN_TXID = '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b';
154
+ psbt.setInputPreviousTxId(newInputIndex, Buffer.from(FIRST_BITCOIN_TXID, 'hex'));
155
+ psbt.setInputOutputIndex(newInputIndex, 0);
156
+ psbt.setInputWitnessUtxo(newInputIndex, amount, Buffer.from('01', 'hex'));
157
+ }
158
+ }
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.1",
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": "fac836c7ae4dbf64c3df293db1fb34b632400a02"
71
72
  }