-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdispute-executor.ts
More file actions
143 lines (117 loc) · 4.03 KB
/
dispute-executor.ts
File metadata and controls
143 lines (117 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { createNetwork } from '@stacks/network';
import {
PostConditionMode,
broadcastTransaction,
bufferCV,
getAddressFromPrivateKey,
makeContractCall,
noneCV,
principalCV,
someCV,
uintCV,
} from '@stacks/transactions';
import { hexToBytes, splitContractId } from './principal-utils.js';
import type {
ClosureRecord,
DisputeExecutor,
SignatureStateRecord,
StackflowPrintEvent,
SubmitDisputeResult,
StackflowNodeConfig,
} from './types.js';
function normalizePrivateKey(input: string): string {
const trimmed = input.trim();
return trimmed.startsWith('0x') ? trimmed.slice(2) : trimmed;
}
function parseContractPrincipal(contractId: string): { address: string; name: string } {
return splitContractId(contractId);
}
export class StacksDisputeExecutor implements DisputeExecutor {
readonly enabled: boolean;
readonly signerAddress: string | null;
private readonly network: ReturnType<typeof createNetwork>;
private readonly disputeSignerKey: string | null;
constructor(config: Pick<StackflowNodeConfig, 'stacksNetwork' | 'stacksApiUrl' | 'disputeSignerKey'>) {
this.network = createNetwork({
network: config.stacksNetwork,
client: config.stacksApiUrl ? { baseUrl: config.stacksApiUrl } : undefined,
});
this.disputeSignerKey = config.disputeSignerKey ? normalizePrivateKey(config.disputeSignerKey) : null;
this.enabled = Boolean(this.disputeSignerKey);
this.signerAddress = this.disputeSignerKey
? getAddressFromPrivateKey(this.disputeSignerKey, this.network)
: null;
}
async submitDispute({
signatureState,
resolvedSecret,
}: {
signatureState: SignatureStateRecord;
resolvedSecret: string | null;
closure: ClosureRecord;
triggerEvent: StackflowPrintEvent;
}): Promise<SubmitDisputeResult> {
if (!this.disputeSignerKey) {
throw new Error('stackflow-node dispute signer key not configured');
}
const contract = parseContractPrincipal(signatureState.contractId);
const tokenArg = signatureState.token
? someCV(principalCV(signatureState.token))
: noneCV();
const secretArg = resolvedSecret
? someCV(bufferCV(hexToBytes(resolvedSecret)))
: noneCV();
const validAfterArg = signatureState.validAfter
? someCV(uintCV(BigInt(signatureState.validAfter)))
: noneCV();
const tx = await makeContractCall({
network: this.network,
senderKey: this.disputeSignerKey,
contractAddress: contract.address,
contractName: contract.name,
functionName: 'dispute-closure-for',
functionArgs: [
principalCV(signatureState.forPrincipal),
tokenArg,
principalCV(signatureState.withPrincipal),
uintCV(BigInt(signatureState.myBalance)),
uintCV(BigInt(signatureState.theirBalance)),
bufferCV(hexToBytes(signatureState.mySignature)),
bufferCV(hexToBytes(signatureState.theirSignature)),
uintCV(BigInt(signatureState.nonce)),
uintCV(BigInt(signatureState.action)),
principalCV(signatureState.actor),
secretArg,
validAfterArg,
],
postConditionMode: PostConditionMode.Allow,
validateWithAbi: false,
});
const result = await broadcastTransaction({
transaction: tx,
network: this.network,
});
if ('reason' in result) {
throw new Error(
`dispute broadcast failed: ${result.reason}${result.error ? ` (${result.error})` : ''}`,
);
}
return { txid: result.txid };
}
}
export class NoopDisputeExecutor implements DisputeExecutor {
readonly enabled = false;
readonly signerAddress = null;
async submitDispute(): Promise<SubmitDisputeResult> {
throw new Error('dispute executor disabled');
}
}
export class MockDisputeExecutor implements DisputeExecutor {
readonly enabled = true;
readonly signerAddress = 'ST3AM1A8YQ4X5MMR7Z5T3VYV9N0ZVEX7QPHQ4RM9P';
private nonce = 0;
async submitDispute(): Promise<SubmitDisputeResult> {
this.nonce += 1;
return { txid: `0xmock${this.nonce.toString(16).padStart(8, '0')}` };
}
}