@otoplo/wallet-common 0.8.0 → 0.8.2
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/dist/index.d.ts +19 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +163 -141
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/services/tx-transformer.ts +50 -34
- package/src/services/wallet.ts +25 -3
- package/src/types/wallet.types.ts +1 -0
package/package.json
CHANGED
|
@@ -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.
|
|
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
|
-
|
|
180
|
-
|
|
181
|
-
|
|
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
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
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
|
-
|
|
200
|
-
|
|
201
|
-
|
|
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
|
-
|
|
206
|
-
|
|
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> {
|
package/src/services/wallet.ts
CHANGED
|
@@ -26,7 +26,7 @@ export type WalletEvent =
|
|
|
26
26
|
| { type: 'new_vault'; vault: VaultInfo; }
|
|
27
27
|
| { type: 'sync_wallet'; loading: boolean; }
|
|
28
28
|
| { type: 'clear_wallet'; }
|
|
29
|
-
| { type: 'account_balance_updated'; accountId: number; balance: Balance, tokensBalance: Record<string, Balance>; }
|
|
29
|
+
| { type: 'account_balance_updated'; accountId: number; balance: Balance, tokensBalance: Record<string, Balance>; hasActivity: boolean; }
|
|
30
30
|
| { type: 'vault_balance_updated'; address: string, balance: Balance; }
|
|
31
31
|
|
|
32
32
|
export type WalletUpdateCallback = (event: WalletEvent) => void;
|
|
@@ -144,6 +144,13 @@ export class WalletManager {
|
|
|
144
144
|
}));
|
|
145
145
|
}
|
|
146
146
|
|
|
147
|
+
private hasActivity(accountId = HD_WALLET_ID): boolean {
|
|
148
|
+
if (accountId === HD_WALLET_ID) {
|
|
149
|
+
return this.receiveAddresses.some(a => a.used);
|
|
150
|
+
}
|
|
151
|
+
return Boolean(this.accounts.get(accountId)?.statusHash);
|
|
152
|
+
}
|
|
153
|
+
|
|
147
154
|
/**
|
|
148
155
|
* Switches to another server (or reconnects to the current one without `params`): saves it, disconnects, then
|
|
149
156
|
* connects with `connect()`, retrying until it does. Check the server first with `RostrumService.checkInstance()`.
|
|
@@ -309,6 +316,7 @@ export class WalletManager {
|
|
|
309
316
|
address: this.getReceiveAddress(),
|
|
310
317
|
balance: sumBalance(walletAddresses.map(a => a.balance)),
|
|
311
318
|
tokensBalance: sumTokensBalance(walletAddresses.map(a => a.tokensBalance)),
|
|
319
|
+
hasActivity: this.hasActivity(HD_WALLET_ID),
|
|
312
320
|
sessions: {}
|
|
313
321
|
}
|
|
314
322
|
});
|
|
@@ -322,6 +330,7 @@ export class WalletManager {
|
|
|
322
330
|
address: account.address,
|
|
323
331
|
balance: account.balance,
|
|
324
332
|
tokensBalance: account.tokensBalance,
|
|
333
|
+
hasActivity: this.hasActivity(account.id),
|
|
325
334
|
sessions: {}
|
|
326
335
|
}
|
|
327
336
|
});
|
|
@@ -519,7 +528,13 @@ export class WalletManager {
|
|
|
519
528
|
const walletBalance = sumBalance(walletAddresses.map(a => a.balance));
|
|
520
529
|
const walletTokenBalances = sumTokensBalance(walletAddresses.map(a => a.tokensBalance));
|
|
521
530
|
|
|
522
|
-
this.notify({
|
|
531
|
+
this.notify({
|
|
532
|
+
type: 'account_balance_updated',
|
|
533
|
+
accountId: HD_WALLET_ID,
|
|
534
|
+
balance: walletBalance,
|
|
535
|
+
tokensBalance: walletTokenBalances,
|
|
536
|
+
hasActivity: this.hasActivity(HD_WALLET_ID),
|
|
537
|
+
});
|
|
523
538
|
|
|
524
539
|
await this.postProcessActivity(HD_WALLET_ID, txHistory, walletAddresses.map(a => a.address), walletTokenBalances);
|
|
525
540
|
}
|
|
@@ -533,7 +548,13 @@ export class WalletManager {
|
|
|
533
548
|
for (const { address, txs } of history) {
|
|
534
549
|
const account = address as AccountDTO;
|
|
535
550
|
|
|
536
|
-
this.notify({
|
|
551
|
+
this.notify({
|
|
552
|
+
type: 'account_balance_updated',
|
|
553
|
+
accountId: account.id,
|
|
554
|
+
balance: account.balance,
|
|
555
|
+
tokensBalance: account.tokensBalance,
|
|
556
|
+
hasActivity: this.hasActivity(account.id),
|
|
557
|
+
});
|
|
537
558
|
|
|
538
559
|
const p = this.postProcessActivity(account.id, txs, [account.address], account.tokensBalance);
|
|
539
560
|
activityPromises.push(p);
|
|
@@ -648,6 +669,7 @@ export class WalletManager {
|
|
|
648
669
|
address: account.address,
|
|
649
670
|
balance: account.balance,
|
|
650
671
|
tokensBalance: account.tokensBalance,
|
|
672
|
+
hasActivity: this.hasActivity(account.id),
|
|
651
673
|
sessions: {}
|
|
652
674
|
}
|
|
653
675
|
});
|