@otoplo/wallet-common 0.8.0 → 0.8.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@otoplo/wallet-common",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "Shared common library for internal use in Otoplo wallet",
5
5
  "license": "MIT",
6
6
  "author": "vgrunner",
@@ -1,5 +1,5 @@
1
1
  import type { Input, PrivateKey} from "libnexa-ts";
2
- import { BufferUtils, Hash, Opcode, Output, OutputSighashType, Script, ScriptFactory, SighashType, Transaction, TxSigner } from "libnexa-ts";
2
+ import { BufferUtils, Hash, InputSighashType, Opcode, Output, OutputSighashType, Script, ScriptFactory, SighashType, Transaction, TxSigner } from "libnexa-ts";
3
3
  import type { KeyManager } from "./key";
4
4
  import type { RostrumService } from "./rostrum";
5
5
  import type { AddressKey, AssetMovement, MovementDetails, TransactionDetails } from "../types";
@@ -14,8 +14,6 @@ interface TransactionContext {
14
14
  myOutputs: Map<number, AssetMovement>;
15
15
  involvedAssets: Set<string>;
16
16
  inputsToSign: Set<number>;
17
- coveredOutputs: Set<number>;
18
- allMyOutputsCovered: boolean;
19
17
  }
20
18
 
21
19
  export class TransactionTransformer {
@@ -55,8 +53,6 @@ export class TransactionTransformer {
55
53
  myOutputs: new Map<number, AssetMovement>(),
56
54
  involvedAssets: new Set<string>(),
57
55
  inputsToSign: new Set<number>(),
58
- coveredOutputs: new Set<number>(),
59
- allMyOutputsCovered: false,
60
56
  };
61
57
  } catch {
62
58
  throw new Error("Invalid transaction format.");
@@ -96,10 +92,27 @@ export class TransactionTransformer {
96
92
  }
97
93
 
98
94
  if (input.scriptSig.findPlaceholder() > 0) {
95
+ if (input.output.address != context.address && !this.isMyContractInput(context, input)) {
96
+ throw new Error("Transaction requests a signature for an input this wallet does not control.");
97
+ }
99
98
  context.inputsToSign.add(index);
100
99
  }
101
100
  }
102
101
 
102
+ /**
103
+ * A script template input that this wallet may sign: its visible args contain the wallet's public key
104
+ * (e.g. withdrawing or closing a contract offer posted by this wallet).
105
+ */
106
+ private isMyContractInput(context: TransactionContext, input: Input): boolean {
107
+ const scriptPubKey = input.output!.scriptPubKey;
108
+ if (!scriptPubKey.isScriptTemplateOut()) {
109
+ return false;
110
+ }
111
+
112
+ const pubKey = context.privateKey.publicKey.toBuffer();
113
+ return scriptPubKey.getVisibleArgs().chunks.some(chunk => chunk.buf != null && BufferUtils.equals(chunk.buf, pubKey));
114
+ }
115
+
103
116
  private processOutputs(context: TransactionContext): void {
104
117
  for (let i = 0; i < context.txDetails.tx.outputs.length; i++) {
105
118
  const output = context.txDetails.tx.outputs[i].toObject();
@@ -130,7 +143,6 @@ export class TransactionTransformer {
130
143
  const subscript = this.validateAndGetSubscript(input);
131
144
 
132
145
  if (input.scriptSig.isEmpty()) { // only p2pkt can be empty
133
- context.allMyOutputsCovered = true;
134
146
  const txSig = TxSigner.sign(context.txDetails.tx, index, SighashType.ALL, subscript, context.privateKey).toTxFormat();
135
147
  const constraint = Script.empty().add(context.privateKey.publicKey.toBuffer());
136
148
  input.scriptSig = ScriptFactory.buildScriptTemplateIn(undefined, constraint, Script.empty().add(txSig))
@@ -139,16 +151,13 @@ export class TransactionTransformer {
139
151
  const placeholder = input.scriptSig.chunks[placeholderIndex];
140
152
  const sigHashBuf = placeholder.buf!.subarray(64);
141
153
  const sigHashType = SighashType.fromBuffer(sigHashBuf);
142
- this.processSignatureCoverage(context, sigHashType);
154
+ this.validateSigHashType(sigHashType, index);
155
+ this.validateOutputsCoverage(context, sigHashType);
143
156
 
144
157
  const txSig = TxSigner.sign(context.txDetails.tx, index, sigHashType, subscript, context.privateKey).toTxFormat(sigHashBuf);
145
158
  input.scriptSig.replaceChunk(placeholderIndex, Script.empty().add(txSig));
146
159
  }
147
160
  }
148
-
149
- if (!context.allMyOutputsCovered) {
150
- throw new Error("Some of interested outputs are not covered by signatures.");
151
- }
152
161
  }
153
162
 
154
163
  private validateAndGetSubscript(input: Input): Script {
@@ -176,37 +185,44 @@ export class TransactionTransformer {
176
185
  throw new Error("Unsupported prevout script type.");
177
186
  }
178
187
 
179
- private processSignatureCoverage(context: TransactionContext, sigHashType: SighashType): void {
180
- if (context.allMyOutputsCovered) {
181
- return;
188
+ /**
189
+ * The signature must commit to the input it signs, otherwise it is not bound to any outpoint
190
+ * and can be replayed on any other coin locked by the same key and subscript.
191
+ * Only output sighash types that we know how to check coverage for are accepted.
192
+ */
193
+ private validateSigHashType(sigHashType: SighashType, index: number): void {
194
+ const inputCovered = sigHashType.inType == InputSighashType.ALL
195
+ || sigHashType.inType == InputSighashType.THISIN
196
+ || (sigHashType.inType == InputSighashType.FIRSTN && sigHashType.inData[0] > index);
197
+
198
+ if (!inputCovered) {
199
+ throw new Error("Signature type does not cover the input being signed.");
182
200
  }
183
201
 
184
- if (sigHashType.outType == OutputSighashType.TWO) {
185
- context.coveredOutputs.add(sigHashType.outData[0]);
186
- context.coveredOutputs.add(sigHashType.outData[1]);
187
- this.checkSignatureCoverage(context);
188
- } else if (sigHashType.outType == OutputSighashType.FIRSTN) {
189
- const n = sigHashType.outData[0];
190
- for (let i = 0; i < n; i++) {
191
- context.coveredOutputs.add(i);
192
- }
193
- this.checkSignatureCoverage(context);
194
- } else {
195
- context.allMyOutputsCovered = true;
202
+ switch (sigHashType.outType) {
203
+ case OutputSighashType.ALL:
204
+ case OutputSighashType.FIRSTN:
205
+ case OutputSighashType.TWO:
206
+ break;
207
+ default:
208
+ throw new Error("Unsupported signature type.");
196
209
  }
197
210
  }
198
211
 
199
- private checkSignatureCoverage(context: TransactionContext): void {
200
- if (context.allMyOutputsCovered) {
201
- return;
202
- }
203
-
212
+ /**
213
+ * Each signature must commit to every output paid to this wallet.
214
+ * Coverage is checked per signature, never pooled across signatures.
215
+ */
216
+ private validateOutputsCoverage(context: TransactionContext, sigHashType: SighashType): void {
204
217
  for (const i of context.myOutputs.keys()) {
205
- if (!context.coveredOutputs.has(i)) {
206
- return;
218
+ const covered = sigHashType.outType == OutputSighashType.ALL
219
+ || (sigHashType.outType == OutputSighashType.FIRSTN && i < sigHashType.outData[0])
220
+ || (sigHashType.outType == OutputSighashType.TWO && sigHashType.outData.includes(i));
221
+
222
+ if (!covered) {
223
+ throw new Error("Signature does not cover your outputs.");
207
224
  }
208
225
  }
209
- context.allMyOutputsCovered = true;
210
226
  }
211
227
 
212
228
  private async processAssetMovements(context: TransactionContext): Promise<void> {