-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
1269 lines (1160 loc) · 40.2 KB
/
app.js
File metadata and controls
1269 lines (1160 loc) · 40.2 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
connect,
disconnect,
isConnected,
request,
} from "https://esm.sh/@stacks/connect@8.2.5?bundle&target=es2020";
import { createNetwork } from "https://esm.sh/@stacks/network@7.2.0?bundle&target=es2020";
import {
Cl,
Pc,
cvToJSON,
fetchCallReadOnlyFunction,
getAddressFromPublicKey,
principalCV,
publicKeyFromSignatureRsv,
serializeCV,
} from "https://esm.sh/@stacks/transactions@7.2.0?bundle&target=es2020";
const CHAIN_IDS = {
mainnet: 1n,
testnet: 2147483648n,
devnet: 2147483648n,
};
const DEFAULT_API_BY_NETWORK = {
mainnet: "https://api.hiro.so",
testnet: "https://api.testnet.hiro.so",
devnet: "http://127.0.0.1:3999",
};
const STACKFLOW_MESSAGE_VERSION = "0.6.0";
const BNSV2_API_BASE = "https://api.bnsv2.com";
const CONTRACT_PRESETS_BY_NETWORK = {
devnet: [
{
key: "stx-devnet",
label: "STX (devnet default)",
contractId: "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.stackflow",
tokenContract: "",
},
{
key: "sbtc-devnet",
label: "sBTC (devnet default)",
contractId: "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.stackflow-sbtc",
tokenContract: "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.test-token",
},
],
testnet: [
{
key: "stx-testnet",
label: "STX (testnet default)",
contractId: "ST126XFZQ3ZHYM6Q6KAQZMMJSDY91A8BTT59ZTE2J.stackflow-0-6-0",
tokenContract: "",
},
{
key: "sbtc-testnet",
label: "sBTC (testnet default)",
contractId: "ST126XFZQ3ZHYM6Q6KAQZMMJSDY91A8BTT59ZTE2J.stackflow-sbtc-0-6-0",
tokenContract: "",
},
],
mainnet: [
{
key: "stx-mainnet",
label: "STX (mainnet default)",
contractId: "SP126XFZQ3ZHYM6Q6KAQZMMJSDY91A8BTT6AD08RV.stackflow-0-6-0",
tokenContract: "",
},
{
key: "sbtc-mainnet",
label: "sBTC (mainnet default)",
contractId: "SP126XFZQ3ZHYM6Q6KAQZMMJSDY91A8BTT6AD08RV.stackflow-sbtc-0-6-0",
tokenContract: "SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4.sbtc-token",
},
],
};
const elements = {
network: document.getElementById("network"),
stacksApiUrl: document.getElementById("stacks-api-url"),
contractId: document.getElementById("contract-id"),
contractPreset: document.getElementById("contract-preset"),
counterparty: document.getElementById("counterparty"),
tokenContract: document.getElementById("token-contract"),
forPrincipal: document.getElementById("for-principal"),
openAmount: document.getElementById("open-amount"),
openNonce: document.getElementById("open-nonce"),
// Pipe State
pipeNonce: document.getElementById("pipe-nonce"),
pipeMyBalance: document.getElementById("pipe-my-balance"),
pipeTheirBalance: document.getElementById("pipe-their-balance"),
// Proposed Action
actionType: document.getElementById("action-type"),
actionAmount: document.getElementById("action-amount"),
actionAmountRow: document.getElementById("action-amount-row"),
resultNonce: document.getElementById("result-nonce"),
resultActionCode: document.getElementById("result-action-code"),
resultMyBalance: document.getElementById("result-my-balance"),
resultTheirBalance: document.getElementById("result-their-balance"),
// Shared transfer fields
transferActor: document.getElementById("transfer-actor"),
actorCustomRow: document.getElementById("actor-custom-row"),
transferActorCustom: document.getElementById("transfer-actor-custom"),
transferSecret: document.getElementById("transfer-secret"),
transferValidAfter: document.getElementById("transfer-valid-after"),
// Sign & Validate
mySignature: document.getElementById("my-signature"),
validateSignature: document.getElementById("validate-signature"),
validateSigBtn: document.getElementById("validate-sig-btn"),
useMySignatureBtn: document.getElementById("use-my-sig-btn"),
validationResult: document.getElementById("validation-result"),
validationIcon: document.getElementById("validation-icon"),
validationText: document.getElementById("validation-text"),
// Common
walletStatus: document.getElementById("wallet-status"),
connectWallet: document.getElementById("connect-wallet"),
disconnectWallet: document.getElementById("disconnect-wallet"),
getPipe: document.getElementById("get-pipe"),
openPipe: document.getElementById("open-pipe"),
forceCancel: document.getElementById("force-cancel"),
signTransfer: document.getElementById("sign-transfer"),
buildPayload: document.getElementById("build-payload"),
copyOutput: document.getElementById("copy-output"),
output: document.getElementById("output"),
log: document.getElementById("log"),
};
const state = {
connectedAddress: null,
lastSignature: null,
lastPayload: null,
nameCache: new Map(),
};
function normalizedText(value) {
return String(value ?? "").trim();
}
function isStacksAddress(value) {
return /^S[PMTN][A-Z0-9]{38,42}$/i.test(normalizedText(value));
}
function isAddressOnNetwork(address, network = readNetwork()) {
const text = normalizedText(address).toUpperCase();
if (!text) {
return false;
}
if (network === "mainnet") {
return /^S[PM]/.test(text);
}
return /^S[TN]/.test(text);
}
function isPrincipalText(value) {
const text = normalizedText(value);
if (!text || !/^S/i.test(text)) {
return false;
}
try {
principalCV(text);
return true;
} catch {
return false;
}
}
function getStacksApiBase() {
const apiBase = normalizedText(elements.stacksApiUrl.value).replace(/\/+$/, "");
if (!apiBase) {
throw new Error("Stacks API URL is required");
}
return apiBase;
}
function looksLikeBtcName(value) {
const text = normalizedText(value).toLowerCase();
return /^[a-z0-9][a-z0-9-]{0,36}\.btc$/.test(text);
}
function nowStamp() {
return new Date().toISOString().slice(11, 19);
}
function appendLog(message, { error = false } = {}) {
const next = `[${nowStamp()}] ${message}`;
elements.log.textContent = `${elements.log.textContent}\n${next}`.trim();
elements.log.scrollTop = elements.log.scrollHeight;
if (error) {
console.error(`[pipe-console] ${message}`);
} else {
console.log(`[pipe-console] ${message}`);
}
}
function setOutput(value) {
elements.output.textContent =
typeof value === "string" ? value : JSON.stringify(value, null, 2);
}
function setWalletStatus(message, { error = false } = {}) {
elements.walletStatus.textContent = message;
elements.walletStatus.classList.toggle("error", error);
}
function toHex(bytes) {
return Array.from(bytes)
.map((value) => value.toString(16).padStart(2, "0"))
.join("");
}
function cvHex(cv) {
return `0x${toHex(serializeCV(cv))}`;
}
function compareBytes(left, right) {
const len = Math.min(left.length, right.length);
for (let i = 0; i < len; i += 1) {
if (left[i] < right[i]) return -1;
if (left[i] > right[i]) return 1;
}
if (left.length < right.length) return -1;
if (left.length > right.length) return 1;
return 0;
}
function canonicalPrincipals(a, b) {
const aBytes = serializeCV(principalCV(a));
const bBytes = serializeCV(principalCV(b));
return compareBytes(aBytes, bBytes) <= 0
? { principal1: a, principal2: b }
: { principal1: b, principal2: a };
}
function unwrapClarityJson(value) {
if (Array.isArray(value)) {
return value.map((entry) => unwrapClarityJson(entry));
}
if (!value || typeof value !== "object") {
return value;
}
const record = value;
const keys = Object.keys(record);
if (keys.length === 2 && keys.includes("type") && keys.includes("value")) {
const type = String(record.type || "");
if (type === "uint" || type === "int") {
return String(record.value ?? "");
}
if (type === "optional_none") {
return null;
}
return unwrapClarityJson(record.value);
}
const output = {};
for (const [key, nested] of Object.entries(record)) {
output[key] = unwrapClarityJson(nested);
}
return output;
}
function parseContractId() {
const raw = normalizedText(elements.contractId.value);
if (!raw.includes(".")) {
throw new Error("Contract must be ADDRESS.NAME");
}
const [address, name] = raw.split(".");
if (!address || !name) {
throw new Error("Contract must be ADDRESS.NAME");
}
principalCV(address);
return { contractId: raw, contractAddress: address, contractName: name };
}
function readNetwork() {
const network = normalizedText(elements.network.value).toLowerCase();
if (!CHAIN_IDS[network]) {
throw new Error(`Unsupported network: ${network}`);
}
return network;
}
function extractPrincipalFromNamePayload(payload) {
const visited = new Set();
const crawl = (value) => {
if (value == null) {
return null;
}
if (typeof value === "string") {
return isPrincipalText(value) ? value : null;
}
if (typeof value !== "object") {
return null;
}
if (visited.has(value)) {
return null;
}
visited.add(value);
if (Array.isArray(value)) {
for (const entry of value) {
const found = crawl(entry);
if (found) {
return found;
}
}
return null;
}
const priorityKeys = [
"address",
"owner",
"owner_address",
"ownerAddress",
"current_owner",
"principal",
];
for (const key of priorityKeys) {
if (key in value) {
const found = crawl(value[key]);
if (found) {
return found;
}
}
}
for (const nested of Object.values(value)) {
const found = crawl(nested);
if (found) {
return found;
}
}
return null;
};
return crawl(payload);
}
async function resolveBtcNameToPrincipal(name) {
const normalizedName = normalizedText(name).toLowerCase();
const network = readNetwork();
const cacheKey = `${network}:${normalizedName}`;
const cached = state.nameCache.get(cacheKey);
if (cached) {
return cached;
}
const encoded = encodeURIComponent(normalizedName);
const endpoints =
network === "mainnet"
? [`${BNSV2_API_BASE}/names/${encoded}`]
: network === "testnet"
? [`${BNSV2_API_BASE}/testnet/names/${encoded}`]
: [`${BNSV2_API_BASE}/testnet/names/${encoded}`, `${BNSV2_API_BASE}/names/${encoded}`];
const failures = [];
for (const endpoint of endpoints) {
try {
const response = await fetch(endpoint, {
headers: { accept: "application/json" },
});
if (response.status === 404) {
failures.push(`${response.status} ${endpoint}`);
continue;
}
if (!response.ok) {
failures.push(`${response.status} ${endpoint}`);
continue;
}
const body = await response.json().catch(() => null);
const principal = extractPrincipalFromNamePayload(body);
if (principal) {
state.nameCache.set(cacheKey, principal);
return principal;
}
failures.push(`no-principal ${endpoint}`);
} catch (error) {
failures.push(
`error ${endpoint}: ${error instanceof Error ? error.message : String(error)}`,
);
}
}
throw new Error(
`Could not resolve ${normalizedName}. Tried: ${failures.slice(0, 3).join(" | ")}`,
);
}
async function resolvePrincipalInput(fieldName, value, { required = true } = {}) {
const input = normalizedText(value);
if (!input) {
if (required) {
throw new Error(`${fieldName} is required`);
}
return null;
}
if (isPrincipalText(input)) {
return input;
}
if (looksLikeBtcName(input)) {
const principal = await resolveBtcNameToPrincipal(input);
appendLog(`${fieldName}: resolved ${input} -> ${principal}`);
return principal;
}
throw new Error(`${fieldName} must be a Stacks principal or .btc name`);
}
function parseOptionalTokenCV() {
const token = normalizedText(elements.tokenContract.value);
if (!token) {
return { cv: Cl.none(), tokenText: null };
}
principalCV(token);
return { cv: Cl.some(Cl.principal(token)), tokenText: token };
}
function parseUintInput(fieldName, value, { min = 0n } = {}) {
const raw = normalizedText(value);
if (!/^\d+$/.test(raw)) {
throw new Error(`${fieldName} must be an unsigned integer`);
}
const parsed = BigInt(raw);
if (parsed < min) {
throw new Error(`${fieldName} must be >= ${min.toString(10)}`);
}
return parsed;
}
function hexToBytes(value) {
const text = normalizedText(value).toLowerCase();
const normalized = text.startsWith("0x") ? text.slice(2) : text;
if (!/^[0-9a-f]*$/.test(normalized) || normalized.length % 2 !== 0) {
throw new Error("hashed secret must be valid hex");
}
const bytes = new Uint8Array(normalized.length / 2);
for (let i = 0; i < normalized.length; i += 2) {
bytes[i / 2] = Number.parseInt(normalized.slice(i, i + 2), 16);
}
return bytes;
}
function parseHashedSecretCV() {
const secret = normalizedText(elements.transferSecret.value);
if (!secret) {
return { cv: Cl.none(), text: null };
}
const bytes = hexToBytes(secret);
if (bytes.length !== 32) {
throw new Error("hashed secret must be exactly 32 bytes");
}
return { cv: Cl.some(Cl.buffer(bytes)), text: `0x${toHex(bytes)}` };
}
function parseValidAfterCV() {
const raw = normalizedText(elements.transferValidAfter.value);
if (!raw) {
return { cv: Cl.none(), text: null };
}
const value = parseUintInput("Valid After", raw);
return { cv: Cl.some(Cl.uint(value)), text: value.toString(10) };
}
function extractAddress(response, network = readNetwork()) {
const seen = new Set();
const found = [];
const crawl = (value) => {
if (value == null) return;
if (typeof value === "string" && isStacksAddress(value)) {
found.push(value);
return;
}
if (typeof value !== "object") return;
if (seen.has(value)) return;
seen.add(value);
if (Array.isArray(value)) {
for (const entry of value) {
crawl(entry);
}
return;
}
if (typeof value.address === "string" && isStacksAddress(value.address)) {
found.push(value.address);
}
for (const nested of Object.values(value)) {
crawl(nested);
}
};
crawl(response);
return found.find((address) => isAddressOnNetwork(address, network)) || found[0] || null;
}
function extractSignature(response) {
if (!response || typeof response !== "object") return null;
if (typeof response.signature === "string") return response.signature;
if (response.result && typeof response.result === "object") {
if (typeof response.result.signature === "string") return response.result.signature;
}
return null;
}
function extractTxid(response) {
if (!response || typeof response !== "object") return null;
if (typeof response.txid === "string") return response.txid;
if (response.result && typeof response.result === "object") {
if (typeof response.result.txid === "string") return response.result.txid;
}
return null;
}
// ── Wallet error handling ───────────────────────────────────────────────────
function isWalletCancellationError(error) {
const msg = (error instanceof Error ? error.message : String(error ?? "")).toLowerCase();
return msg.includes("cancelled") || msg.includes("canceled")
|| msg.includes("user rejected") || msg.includes("user denied")
|| msg.includes("denied by user") || msg.includes("request aborted")
|| msg.includes("aborterror") || msg.includes("4001");
}
function normalizeWalletPromptError(error, action) {
if (isWalletCancellationError(error)) {
if (action === "connect") return new Error("Wallet connection cancelled");
if (action === "transaction") return new Error("Transaction cancelled");
return new Error("Signature cancelled");
}
return error instanceof Error ? error : new Error(String(error ?? "Unknown wallet error"));
}
async function ensureWallet({ interactive }) {
if (state.connectedAddress) {
return state.connectedAddress;
}
let connected = false;
try {
connected = await Promise.resolve(isConnected());
} catch {
connected = false;
}
if (!connected && interactive) {
await connect();
}
if (connected || interactive) {
const addresses = await request("getAddresses");
const address = extractAddress(addresses, readNetwork());
if (!address) {
throw new Error("No Stacks address was returned by the wallet");
}
state.connectedAddress = address;
elements.forPrincipal.value = elements.forPrincipal.value || address;
setWalletStatus(`Connected: ${address}`);
updateActorOptions();
return address;
}
return null;
}
function updateNetworkDefaults() {
const network = readNetwork();
if (!normalizedText(elements.stacksApiUrl.value)) {
elements.stacksApiUrl.value = DEFAULT_API_BY_NETWORK[network];
}
}
function getPresetsForNetwork(network = readNetwork()) {
return CONTRACT_PRESETS_BY_NETWORK[network] || [];
}
function findPresetByKey(presetKey, network = readNetwork()) {
return getPresetsForNetwork(network).find((preset) => preset.key === presetKey) || null;
}
function renderPresetOptions(network = readNetwork()) {
const presets = getPresetsForNetwork(network);
const selected = normalizedText(elements.contractPreset.value) || "custom";
const options = [
...presets.map((preset) => `<option value="${preset.key}">${preset.label}</option>`),
'<option value="custom">Custom (manual)</option>',
];
elements.contractPreset.innerHTML = options.join("");
if (presets.some((preset) => preset.key === selected) || selected === "custom") {
elements.contractPreset.value = selected;
} else {
elements.contractPreset.value = "custom";
}
}
function getPresetKeyByValues(contractId, tokenContract, network = readNetwork()) {
const contractText = normalizedText(contractId);
const tokenText = normalizedText(tokenContract);
for (const preset of getPresetsForNetwork(network)) {
if (
normalizedText(preset.contractId) === contractText &&
normalizedText(preset.tokenContract) === tokenText
) {
return preset.key;
}
}
return "custom";
}
function applyContractPreset(presetKey, { log = true } = {}) {
const preset = findPresetByKey(presetKey);
if (!preset) {
return;
}
elements.contractId.value = preset.contractId;
elements.tokenContract.value = preset.tokenContract;
if (log) {
appendLog(
`Applied preset ${presetKey}: contract=${preset.contractId}, token=${
preset.tokenContract || "(none)"
}`,
);
}
}
async function handleConnectWallet() {
try {
const address = await ensureWallet({ interactive: true });
appendLog(`Wallet connected: ${address}`);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
setWalletStatus(message, { error: true });
appendLog(`Connect wallet failed: ${message}`, { error: true });
}
}
async function handleDisconnectWallet() {
const previousAddress = state.connectedAddress;
try {
await disconnect();
} catch {
// Some providers may not implement explicit disconnect cleanly; still clear local state.
}
state.connectedAddress = null;
state.lastSignature = null;
if (previousAddress && normalizedText(elements.forPrincipal.value) === previousAddress) {
elements.forPrincipal.value = "";
}
if (previousAddress && normalizedText(elements.transferActor.value) === previousAddress) {
elements.transferActor.value = "";
}
setWalletStatus("Wallet not connected.");
appendLog(previousAddress ? `Wallet disconnected: ${previousAddress}` : "Wallet disconnected.");
}
function getReadOnlySenderCandidates(sender, contractAddress, network = readNetwork()) {
const candidates = [];
const senderText = normalizedText(sender);
const contractAddressText = normalizedText(contractAddress);
if (senderText && isAddressOnNetwork(senderText, network)) {
candidates.push(senderText);
}
if (
contractAddressText &&
isAddressOnNetwork(contractAddressText, network) &&
!candidates.includes(contractAddressText)
) {
candidates.push(contractAddressText);
}
if (senderText && !candidates.includes(senderText)) {
candidates.push(senderText);
}
if (contractAddressText && !candidates.includes(contractAddressText)) {
candidates.push(contractAddressText);
}
return candidates;
}
async function fetchReadOnly(functionName, functionArgs, sender) {
const { contractAddress, contractName } = parseContractId();
const network = createNetwork({
network: readNetwork(),
client: { baseUrl: getStacksApiBase() },
});
const senders = getReadOnlySenderCandidates(sender, contractAddress);
let lastError = null;
for (const senderCandidate of senders) {
try {
const result = await fetchCallReadOnlyFunction({
network,
senderAddress: senderCandidate,
contractAddress,
contractName,
functionName,
functionArgs,
});
if (senderCandidate !== sender) {
appendLog(`Read-only ${functionName} used fallback sender=${senderCandidate}.`);
}
return result;
} catch (error) {
lastError = error;
}
}
const message =
lastError instanceof Error ? lastError.message : "Read-only call failed for all sender candidates";
throw new Error(message);
}
async function handleGetPipe() {
try {
const withPrincipal = await resolvePrincipalInput(
"Counterparty",
elements.counterparty.value,
);
const forPrincipal = await resolvePrincipalInput(
"For Principal",
elements.forPrincipal.value || state.connectedAddress,
);
const { cv: tokenCV } = parseOptionalTokenCV();
const resultCv = await fetchReadOnly(
"get-pipe",
[tokenCV, Cl.principal(withPrincipal)],
forPrincipal,
);
const resultHex = cvHex(resultCv);
const decoded = unwrapClarityJson(cvToJSON(resultCv));
setOutput({
call: "get-pipe",
forPrincipal,
withPrincipal,
resultHex,
decoded,
});
// Populate Pipe State fields when we get valid data
if (decoded && decoded.nonce !== undefined) {
elements.pipeNonce.value = decoded.nonce;
const pair = canonicalPrincipals(forPrincipal, withPrincipal);
const iAmP1 = pair.principal1 === forPrincipal;
elements.pipeMyBalance.value = iAmP1 ? (decoded["balance-1"] ?? "0") : (decoded["balance-2"] ?? "0");
elements.pipeTheirBalance.value = iAmP1 ? (decoded["balance-2"] ?? "0") : (decoded["balance-1"] ?? "0");
updatePreview();
appendLog("Fetched pipe state and populated fields.");
} else {
appendLog("Fetched pipe state via read-only get-pipe.");
}
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
setOutput(`Error: ${message}`);
appendLog(`Get pipe failed: ${message}`, { error: true });
}
}
function stxPostConditionForAmount(principal, amount) {
return Pc.principal(principal).willSendEq(amount).ustx();
}
async function callContract(functionName, functionArgs, options = {}) {
const { contractId } = parseContractId();
const network = readNetwork();
return request("stx_callContract", {
contract: contractId,
functionName,
functionArgs,
network,
postConditionMode: options.postConditionMode ?? "deny",
postConditions: options.postConditions ?? [],
});
}
async function handleOpenPipe() {
try {
const sender = await ensureWallet({ interactive: true });
if (!sender) {
throw new Error("Connect wallet first");
}
const withPrincipal = await resolvePrincipalInput(
"Counterparty",
elements.counterparty.value,
);
const amount = parseUintInput("Amount", elements.openAmount.value, { min: 1n });
const nonceText = normalizedText(elements.openNonce.value);
const nonce = nonceText ? parseUintInput("Nonce", nonceText) : 0n;
const { cv: tokenCV, tokenText } = parseOptionalTokenCV();
const args = [
tokenCV,
Cl.uint(amount),
Cl.principal(withPrincipal),
Cl.uint(nonce),
];
const options =
tokenText == null
? {
postConditionMode: "deny",
postConditions: [stxPostConditionForAmount(sender, amount)],
}
: {
postConditionMode: "allow",
postConditions: [],
};
const response = await callContract("fund-pipe", args, options);
const txid = extractTxid(response);
setOutput({
action: "fund-pipe",
txid,
response,
});
appendLog(txid ? `fund-pipe submitted: ${txid}` : "fund-pipe submitted.");
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
setOutput(`Error: ${message}`);
appendLog(`Open pipe failed: ${message}`, { error: true });
}
}
async function handleForceCancel() {
try {
await ensureWallet({ interactive: true });
const withPrincipal = await resolvePrincipalInput(
"Counterparty",
elements.counterparty.value,
);
const { cv: tokenCV } = parseOptionalTokenCV();
const response = await callContract("force-cancel", [
tokenCV,
Cl.principal(withPrincipal),
]);
const txid = extractTxid(response);
setOutput({
action: "force-cancel",
txid,
response,
});
appendLog(txid ? `force-cancel submitted: ${txid}` : "force-cancel submitted.");
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
setOutput(`Error: ${message}`);
appendLog(`Force cancel failed: ${message}`, { error: true });
}
}
function computeAutoResult() {
const actionType = normalizedText(elements.actionType.value);
const pipeNonce = parseUintInput("Nonce", elements.pipeNonce.value);
const pipeMyBalance = parseUintInput("My Balance", elements.pipeMyBalance.value);
const pipeTheirBalance = parseUintInput("Their Balance", elements.pipeTheirBalance.value);
const amount = parseUintInput("Amount", elements.actionAmount.value);
if (actionType === "transfer-to") {
if (amount > pipeMyBalance) throw new Error("Amount exceeds my balance");
return { nonce: pipeNonce + 1n, myBalance: pipeMyBalance - amount, theirBalance: pipeTheirBalance + amount, actionCode: 1n };
}
if (actionType === "transfer-from") {
if (amount > pipeTheirBalance) throw new Error("Amount exceeds their balance");
return { nonce: pipeNonce + 1n, myBalance: pipeMyBalance + amount, theirBalance: pipeTheirBalance - amount, actionCode: 1n };
}
if (actionType === "close") {
return { nonce: pipeNonce + 1n, myBalance: pipeMyBalance, theirBalance: pipeTheirBalance, actionCode: 0n };
}
throw new Error(`Unknown action type: ${actionType}`);
}
function updatePreview() {
try {
const result = computeAutoResult();
elements.resultNonce.value = result.nonce.toString();
elements.resultMyBalance.value = result.myBalance.toString();
elements.resultTheirBalance.value = result.theirBalance.toString();
elements.resultActionCode.value = result.actionCode.toString();
} catch {
// leave fields as-is on error
}
}
function truncateAddr(addr) {
const t = normalizedText(addr);
if (!t) return "";
return t.length > 14 ? `${t.slice(0, 8)}…${t.slice(-4)}` : t;
}
function updateActorOptions() {
const myRaw = normalizedText(elements.forPrincipal.value) || state.connectedAddress || "";
const themRaw = normalizedText(elements.counterparty.value) || "";
const opts = elements.transferActor.options;
opts[0].text = myRaw ? `Me — ${truncateAddr(myRaw)}` : "Me";
opts[1].text = themRaw ? `Them — ${truncateAddr(themRaw)}` : "Them";
}
function handleActorChange() {
const isCustom = normalizedText(elements.transferActor.value) === "custom";
elements.actorCustomRow.classList.toggle("hidden", !isCustom);
}
function handleActionTypeChange() {
const actionType = normalizedText(elements.actionType.value);
const hasAmount = actionType === "transfer-to" || actionType === "transfer-from";
elements.actionAmountRow.classList.toggle("hidden", !hasAmount);
// Auto-select actor based on action
if (actionType === "transfer-to") {
elements.transferActor.value = "me";
} else if (actionType === "transfer-from") {
elements.transferActor.value = "them";
} else if (actionType === "close") {
elements.transferActor.value = "me";
}
handleActorChange();
updatePreview();
}
function cvToBytes(cv) {
const result = serializeCV(cv);
// serializeCV returns a hex string in stacks.js v7
if (typeof result === "string") {
const hex = result.startsWith("0x") ? result.slice(2) : result;
const bytes = new Uint8Array(hex.length / 2);
for (let i = 0; i < hex.length; i += 2) {
bytes[i / 2] = Number.parseInt(hex.slice(i, i + 2), 16);
}
return bytes;
}
return result;
}
async function computeStructuredDataHash(domain, message) {
// SIP-018: sha256("SIP018" || sha256(domain_bytes) || sha256(message_bytes))
const prefix = new Uint8Array([0x53, 0x49, 0x50, 0x30, 0x31, 0x38]);
const [domainHashBuf, messageHashBuf] = await Promise.all([
crypto.subtle.digest("SHA-256", cvToBytes(domain)),
crypto.subtle.digest("SHA-256", cvToBytes(message)),
]);
const payload = new Uint8Array(prefix.length + 32 + 32);
payload.set(prefix, 0);
payload.set(new Uint8Array(domainHashBuf), prefix.length);
payload.set(new Uint8Array(messageHashBuf), prefix.length + 32);
return new Uint8Array(await crypto.subtle.digest("SHA-256", payload));
}
async function buildTransferContext() {
const network = readNetwork();
const { contractId } = parseContractId();
const forPrincipal = await resolvePrincipalInput(
"For Principal",
elements.forPrincipal.value || state.connectedAddress,
);
const withPrincipal = await resolvePrincipalInput(
"Counterparty",
elements.counterparty.value,
);
const actorMode = normalizedText(elements.transferActor.value);
const actor =
actorMode === "me" ? forPrincipal
: actorMode === "them" ? withPrincipal
: await resolvePrincipalInput("Custom Actor", elements.transferActorCustom.value);
const myBalance = parseUintInput("My Resulting Balance", elements.resultMyBalance.value);
const theirBalance = parseUintInput("Their Resulting Balance", elements.resultTheirBalance.value);
const nonce = parseUintInput("Resulting Nonce", elements.resultNonce.value);
const action = parseUintInput("Action Code", elements.resultActionCode.value);
const { cv: tokenCV, tokenText } = parseOptionalTokenCV();
const { cv: hashedSecretCV, text: hashedSecretText } = parseHashedSecretCV();
const { cv: validAfterCV, text: validAfterText } = parseValidAfterCV();
const pair = canonicalPrincipals(forPrincipal, withPrincipal);
const balance1 = pair.principal1 === forPrincipal ? myBalance : theirBalance;
const balance2 = pair.principal1 === forPrincipal ? theirBalance : myBalance;
const domain = Cl.tuple({
name: Cl.stringAscii(contractId),
version: Cl.stringAscii(STACKFLOW_MESSAGE_VERSION),
"chain-id": Cl.uint(CHAIN_IDS[network]),
});
const message = Cl.tuple({
token: tokenCV,
"principal-1": Cl.principal(pair.principal1),
"principal-2": Cl.principal(pair.principal2),
"balance-1": Cl.uint(balance1),
"balance-2": Cl.uint(balance2),
nonce: Cl.uint(nonce),
action: Cl.uint(action),
actor: Cl.principal(actor),
"hashed-secret": hashedSecretCV,
"valid-after": validAfterCV,
});
return {
network,
contractId,
forPrincipal,
withPrincipal,
token: tokenText,
myBalance: myBalance.toString(10),
theirBalance: theirBalance.toString(10),