hopper-cli 0.3.2

Command-line tooling for Hopper account inspection, schema export, and migration planning
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
//! `hopper publish-idl` - native-Rust IDL publishing to the SPL Program
//! Metadata program, with **zero Node dependencies**.
//!
//! Anchor's `anchor idl build && anchor idl init` path shells out to
//! `npx @solana-program/program-metadata` to write a program's IDL to the
//! canonical on-chain metadata account. Hopper already projects a
//! manifest into Anchor-IDL JSON (see
//! [`hopper_schema::anchor_idl::AnchorIdlFromManifest`]); this module adds
//! the last mile - deriving the metadata PDA, building the 96-byte account
//! header, and zlib-packing the payload - entirely in Rust so `hopper
//! publish-idl` needs no `npx`, no `node_modules`, and no second toolchain.
//!
//! ## Protocol (verified against `solana-program/program-metadata`)
//!
//! Program id: `ProgM6JCCvbYkfKqJYHePx4xxSUSqJp7rh8Lyv7nk7S`.
//!
//! On-chain `Header` is `#[repr(C)]`, 1-byte aligned, exactly 96 bytes,
//! with the associated data section starting at offset 96:
//!
//! ```text
//! off  size  field
//!   0    1   discriminator: u8        (Metadata = 2)
//!   1   32   program: [u8; 32]
//!  33   32   authority: ZeroableOption<[u8;32]>  (all-zero = None)
//!  65    1   mutable: u8
//!  66    1   canonical: u8
//!  67   16   seed: [u8; 16]           (b"idl" right-padded with zeros)
//!  83    1   encoding: u8             (Utf8 = 1)
//!  84    1   compression: u8          (Zlib = 2)
//!  85    1   format: u8               (Json = 1)
//!  86    1   data_source: u8          (Direct = 0)
//!  87    4   data_length: [u8; 4]     (LE u32; length of stored payload)
//!  91    5   _padding: [u8; 5]
//! ```
//!
//! `ZeroableOption<Address>` is a zero-overhead newtype (`struct
//! ZeroableOption<T>(T)`), so `None` is 32 zero bytes with no extra flag -
//! that is why the header sums to exactly 96 and is 8-aligned. The
//! enum values (`Encoding`, `Compression`, `Format`, `DataSource`) and the
//! `Metadata = 2` discriminator were read from the program's
//! `program/src/state/mod.rs`.
//!
//! ## PDA derivation
//!
//! * canonical:     `[program_id, seed16]`
//! * non-canonical: `[program_id, authority, seed16]`
//!
//! where `seed16` is the **fixed 16-byte** seed field, i.e. the reference
//! client's `fixEncoderSize(getUtf8Encoder(), 16)` - the seed string is
//! UTF-8 encoded then right-padded (or truncated) to 16 bytes, and that
//! fixed array is the PDA seed. For explorer interop, IDLs live under the
//! canonical `[program, b"idl"]` PDA as Utf8 + Zlib + Json.
//!
//! ## Instruction wire format (verified against the on-chain processor)
//!
//! The metadata program dispatches on the first instruction-data byte
//! (`entrypoint.rs`): `0 = Write`, `1 = Initialize`, `3 = SetData`,
//! `7 = Allocate`. The remaining bytes are the processor payload.
//!
//! * **Initialize** (`initialize.rs`): `[seed:16][encoding:1][compression:1]
//!   [format:1][data_source:1][payload…]`. When the target PDA is empty the
//!   program CPI-creates it (signed by the PDA seeds) with
//!   `space = 96 + payload.len()`, copies `payload` to offset 96, and writes
//!   the 96-byte header itself. The account must be **pre-funded** to rent
//!   exemption first (`CreateAccountAllowPrefund { funding: None }`). When the
//!   PDA is instead a pre-allocated `Buffer` (discriminator 1), no inline
//!   payload is allowed and the program finalizes the already-written bytes
//!   in place.
//! * **Allocate** (`allocate.rs`): `[seed:16]`. Creates a 96-byte `Buffer`
//!   header at the canonical PDA (must be pre-funded).
//! * **Write** (`write.rs`): `[offset:u32-le][chunk…]`. Copies `chunk` to
//!   `offset + 96` in the buffer, resizing as needed (so the account must be
//!   funded for its final size before the first write).
//! * **SetData** (`set_data.rs`): `[encoding:1][compression:1][format:1]
//!   [data_source:1][payload…]`. Rewrites the data section of an existing
//!   mutable metadata account (used by `--overwrite`).
//!
//! Account orderings are taken from the same source files and encoded in the
//! `*_accounts` helpers below. Canonical publishing requires the target
//! `program` account (executable) plus its BPF-Loader-v3 `ProgramData` PDA so
//! the program can verify the signer is the program's upgrade authority.
//!
//! ## Scope
//!
//! Shipped: the correctness-critical instruction encoders + PDA/header core
//! (unit-tested byte-for-byte), a `--dry-run` preview, and a **real signed
//! on-chain send**: fresh canonical publish via a single inline `Initialize`
//! for small IDLs, or `Allocate` + chunked `Write` + in-place `Initialize`
//! for IDLs that exceed one transaction's data budget, plus an inline
//! `--overwrite` (`SetData`) rewrite path. Rent pre-funding, "already
//! initialized", insufficient-balance, and RPC failures are surfaced
//! explicitly; nothing is half-written. The one bounded gap is *overwriting*
//! an IDL large enough to need chunking (a fresh large publish works): that
//! requires a separate source buffer + `SetData`-from-buffer and is reported
//! as a clear error pointing at close-then-republish.

use std::io::{self, Write};
use std::process;

use bs58;
use sha2::{Digest, Sha256};

// ---------------------------------------------------------------------------
// Protocol constants
// ---------------------------------------------------------------------------

/// Program Metadata program id (base58).
pub const METADATA_PROGRAM_ID_B58: &str = "ProgM6JCCvbYkfKqJYHePx4xxSUSqJp7rh8Lyv7nk7S";

/// On-chain `Header` length. Payload begins immediately after.
pub const HEADER_LEN: usize = 96;

/// Fixed seed-field width (`SEED_LEN` in the on-chain program).
pub const SEED_LEN: usize = 16;

/// Default metadata seed for a program IDL.
pub const DEFAULT_SEED: &[u8] = b"idl";

/// System program id (base58), shown in the instruction plan.
pub const SYSTEM_PROGRAM_ID_B58: &str = "11111111111111111111111111111111";

/// `AccountDiscriminator::Metadata`.
pub const DISC_METADATA: u8 = 2;

/// `Encoding::Utf8`.
pub const ENCODING_UTF8: u8 = 1;
/// `Compression::Zlib`.
pub const COMPRESSION_ZLIB: u8 = 2;
/// `Format::Json`.
pub const FORMAT_JSON: u8 = 1;
/// `DataSource::Direct` (payload stored inline in the account).
pub const DATA_SOURCE_DIRECT: u8 = 0;

// Instruction discriminators (first byte of the instruction data). Values
// pinned to the on-chain `ProgramMetadataInstruction` enum ordering.
/// `Write` instruction discriminator.
pub const INSTR_WRITE: u8 = 0;
/// `Initialize` instruction discriminator.
pub const INSTR_INITIALIZE: u8 = 1;
/// `SetData` instruction discriminator.
pub const INSTR_SET_DATA: u8 = 3;
/// `Allocate` instruction discriminator.
pub const INSTR_ALLOCATE: u8 = 7;

/// BPF Upgradeable Loader program id (base58); owns v3 programs and their
/// `ProgramData` accounts, from which the upgrade authority is read.
pub const BPF_LOADER_UPGRADEABLE_ID_B58: &str = "BPFLoaderUpgradeab1e11111111111111111111111";

/// Maximum compressed payload carried inline in a single `Initialize` /
/// `SetData` transaction. A Solana transaction serializes to at most ~1232
/// bytes; the inline `Initialize` data is `1 + 16 + 4 + payload`, and the tx
/// also carries a system transfer, five account metas, a signature and a
/// blockhash. 800 bytes leaves comfortable headroom; larger payloads use the
/// `Allocate` + chunked `Write` path.
pub const MAX_INLINE_PAYLOAD: usize = 800;

/// Maximum bytes per `Write` chunk when buffering a large payload. Each write
/// tx carries `1 + 4 + chunk` instruction bytes plus three account metas and a
/// signature, well under the transaction size limit at 900 bytes.
pub const WRITE_CHUNK_LEN: usize = 900;

/// Marker appended by `create_program_address` before hashing.
const PDA_MARKER: &[u8] = b"ProgramDerivedAddress";

// ---------------------------------------------------------------------------
// Seed handling
// ---------------------------------------------------------------------------

/// Encode a seed string into the fixed 16-byte seed field: UTF-8 bytes,
/// right-padded with zeros (or truncated to 16). Mirrors the reference
/// client's `fixEncoderSize(getUtf8Encoder(), 16)`.
pub fn pad_seed(seed: &[u8]) -> [u8; SEED_LEN] {
    let mut out = [0u8; SEED_LEN];
    let n = seed.len().min(SEED_LEN);
    out[..n].copy_from_slice(&seed[..n]);
    out
}

/// Decode the metadata program id into its 32 raw bytes.
pub fn metadata_program_id() -> [u8; 32] {
    let v = bs58::decode(METADATA_PROGRAM_ID_B58)
        .into_vec()
        .expect("metadata program id is valid base58");
    let mut out = [0u8; 32];
    out.copy_from_slice(&v);
    out
}

// ---------------------------------------------------------------------------
// PDA derivation (standard create/find_program_address over sha256)
// ---------------------------------------------------------------------------

/// Compute a candidate program-derived address for `seeds + [bump]` under
/// `program_id`. Returns `None` when a seed exceeds 32 bytes or the result
/// lands on the ed25519 curve (i.e. is not a valid PDA).
fn create_program_address(seeds: &[&[u8]], bump: u8, program_id: &[u8; 32]) -> Option<[u8; 32]> {
    let mut hasher = Sha256::new();
    for s in seeds {
        if s.len() > 32 {
            return None;
        }
        hasher.update(s);
    }
    hasher.update([bump]);
    hasher.update(program_id);
    hasher.update(PDA_MARKER);
    let digest = hasher.finalize();
    let mut hash = [0u8; 32];
    hash.copy_from_slice(&digest);
    if is_on_curve(&hash) {
        return None;
    }
    Some(hash)
}

/// Find the canonical `(address, bump)` for `seeds` under `program_id`,
/// walking bumps from 255 down to the first off-curve result.
fn find_program_address(seeds: &[&[u8]], program_id: &[u8; 32]) -> ([u8; 32], u8) {
    for bump in (0u8..=255).rev() {
        if let Some(addr) = create_program_address(seeds, bump, program_id) {
            return (addr, bump);
        }
    }
    // Astronomically unlikely: every bump 255..=0 was on-curve.
    panic!("no off-curve PDA exists for these seeds");
}

fn is_on_curve(bytes: &[u8; 32]) -> bool {
    curve25519_dalek::edwards::CompressedEdwardsY(*bytes)
        .decompress()
        .is_some()
}

/// Derive the canonical metadata PDA: `[program_id, seed16]` under the
/// metadata program.
pub fn derive_canonical_pda(program_id: &[u8; 32], seed16: &[u8; SEED_LEN]) -> ([u8; 32], u8) {
    let meta = metadata_program_id();
    find_program_address(&[program_id.as_ref(), seed16.as_ref()], &meta)
}

/// Decode the BPF Upgradeable Loader program id into its 32 raw bytes.
pub fn bpf_loader_upgradeable_id() -> [u8; 32] {
    let v = bs58::decode(BPF_LOADER_UPGRADEABLE_ID_B58)
        .into_vec()
        .expect("bpf loader upgradeable id is valid base58");
    let mut out = [0u8; 32];
    out.copy_from_slice(&v);
    out
}

/// Derive a program's `ProgramData` PDA: `find_program_address([program_id])`
/// under the BPF Upgradeable Loader. This account holds the upgrade authority
/// that the metadata program checks for canonical publishing.
pub fn derive_program_data_pda(program_id: &[u8; 32]) -> ([u8; 32], u8) {
    let loader = bpf_loader_upgradeable_id();
    find_program_address(&[program_id.as_ref()], &loader)
}

/// Derive a non-canonical metadata PDA: `[program_id, authority, seed16]`
/// under the metadata program. Part of the correctness-critical core and
/// pinned by tests; the `--non-canonical` CLI path is a followup, so this
/// is not yet wired into the (canonical-only) dry-run command.
#[allow(dead_code)]
pub fn derive_non_canonical_pda(
    program_id: &[u8; 32],
    authority: &[u8; 32],
    seed16: &[u8; SEED_LEN],
) -> ([u8; 32], u8) {
    let meta = metadata_program_id();
    find_program_address(
        &[program_id.as_ref(), authority.as_ref(), seed16.as_ref()],
        &meta,
    )
}

// ---------------------------------------------------------------------------
// 96-byte header encoder
// ---------------------------------------------------------------------------

/// In-memory view of the on-chain metadata `Header`. `encode` produces the
/// exact 96 bytes that will occupy `[0, 96)` of the account.
#[derive(Debug, Clone)]
pub struct MetadataHeader {
    /// Account discriminator (`DISC_METADATA` for a metadata account).
    pub discriminator: u8,
    /// The program this metadata describes.
    pub program: [u8; 32],
    /// Optional authority; `None` writes 32 zero bytes (the Zeroable None).
    pub authority: Option<[u8; 32]>,
    /// Whether the metadata can be updated later.
    pub mutable: bool,
    /// Whether this is the canonical (upgrade-authority) metadata account.
    pub canonical: bool,
    /// Fixed 16-byte seed field.
    pub seed: [u8; SEED_LEN],
    /// Payload encoding (`ENCODING_UTF8`).
    pub encoding: u8,
    /// Payload compression (`COMPRESSION_ZLIB`).
    pub compression: u8,
    /// Payload format (`FORMAT_JSON`).
    pub format: u8,
    /// Where the payload lives (`DATA_SOURCE_DIRECT`).
    pub data_source: u8,
    /// Length of the stored (post-compression) payload in bytes.
    pub data_len: u32,
}

impl MetadataHeader {
    /// Build the canonical IDL header describing `program`, recording the
    /// prepared `payload`'s encoding/compression/format and stored length.
    /// The payload tags are the single source of truth for these fields.
    pub fn canonical_idl(
        program: [u8; 32],
        seed16: [u8; SEED_LEN],
        payload: &PreparedPayload,
    ) -> Self {
        Self::canonical(program, seed16, payload)
    }

    /// Decode the 96-byte header at the front of a metadata account. The
    /// inverse of [`Self::encode`]; used to read a published record back.
    pub fn decode(bytes: &[u8]) -> Result<Self, String> {
        if bytes.len() < HEADER_LEN {
            return Err(format!(
                "metadata account holds {} bytes; the header alone is {HEADER_LEN}",
                bytes.len()
            ));
        }
        let mut program = [0u8; 32];
        program.copy_from_slice(&bytes[1..33]);
        let mut authority = [0u8; 32];
        authority.copy_from_slice(&bytes[33..65]);
        let mut seed = [0u8; SEED_LEN];
        seed.copy_from_slice(&bytes[67..83]);
        Ok(MetadataHeader {
            discriminator: bytes[0],
            program,
            authority: (authority != [0u8; 32]).then_some(authority),
            mutable: bytes[65] != 0,
            canonical: bytes[66] != 0,
            seed,
            encoding: bytes[83],
            compression: bytes[84],
            format: bytes[85],
            data_source: bytes[86],
            data_len: u32::from_le_bytes([bytes[87], bytes[88], bytes[89], bytes[90]]),
        })
    }

    /// The printable seed: the field up to its first zero byte.
    pub fn seed_text(&self) -> String {
        let end = self.seed.iter().position(|&b| b == 0).unwrap_or(SEED_LEN);
        String::from_utf8_lossy(&self.seed[..end]).into_owned()
    }

    /// Build the canonical header for any record under `seed16`, recording
    /// the prepared `payload`'s encoding/compression/format and stored
    /// length. The payload tags are the single source of truth for these
    /// fields.
    pub fn canonical(program: [u8; 32], seed16: [u8; SEED_LEN], payload: &PreparedPayload) -> Self {
        MetadataHeader {
            discriminator: DISC_METADATA,
            program,
            authority: None,
            mutable: true,
            canonical: true,
            seed: seed16,
            encoding: payload.encoding,
            compression: payload.compression,
            format: payload.format,
            data_source: DATA_SOURCE_DIRECT,
            data_len: payload.data_len(),
        }
    }

    /// Serialize to the exact 96 on-chain header bytes.
    pub fn encode(&self) -> [u8; HEADER_LEN] {
        let mut b = [0u8; HEADER_LEN];
        b[0] = self.discriminator;
        b[1..33].copy_from_slice(&self.program);
        // authority: Some -> the 32 bytes; None -> left as zeros (Zeroable None).
        if let Some(auth) = &self.authority {
            b[33..65].copy_from_slice(auth);
        }
        b[65] = self.mutable as u8;
        b[66] = self.canonical as u8;
        b[67..83].copy_from_slice(&self.seed);
        b[83] = self.encoding;
        b[84] = self.compression;
        b[85] = self.format;
        b[86] = self.data_source;
        b[87..91].copy_from_slice(&self.data_len.to_le_bytes());
        // b[91..96] padding stays zero.
        b
    }
}

// ---------------------------------------------------------------------------
// Payload prep (zlib)
// ---------------------------------------------------------------------------

/// Zlib-compress `data` using the same backend as the manifest encoder.
pub fn zlib_compress(data: &[u8]) -> Vec<u8> {
    use flate2::write::ZlibEncoder;
    use flate2::Compression;
    use std::io::Write;
    let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
    encoder.write_all(data).expect("zlib write");
    encoder.finish().expect("zlib finish")
}

/// Zlib-decompress `data` (round-trip inverse of [`zlib_compress`]).
/// Used by the compression round-trip tests and by `--read`, which fetches
/// a published record back and inflates it.
pub fn zlib_decompress(data: &[u8]) -> Result<Vec<u8>, String> {
    use flate2::read::ZlibDecoder;
    use std::io::Read;
    let mut out = Vec::new();
    ZlibDecoder::new(data)
        .read_to_end(&mut out)
        .map_err(|e| format!("zlib decompression failed: {e}"))?;
    Ok(out)
}

/// Prepared payload: the raw IDL bytes, the compressed bytes, and the
/// header field triple to record.
pub struct PreparedPayload {
    /// Raw JSON length before compression.
    pub raw_len: usize,
    /// Zlib-compressed payload bytes (what actually gets stored).
    pub compressed: Vec<u8>,
    /// Encoding tag to record in the header.
    pub encoding: u8,
    /// Compression tag to record in the header.
    pub compression: u8,
    /// Format tag to record in the header.
    pub format: u8,
}

impl PreparedPayload {
    /// Prepare any JSON document for on-chain storage as Utf8 + Zlib + Json,
    /// the tag triple the reference client writes for a `.json` file. The
    /// IDL, `security.txt`, and manifest records all share it; only the seed
    /// and the source differ.
    pub fn from_json(json: &str) -> Self {
        let compressed = zlib_compress(json.as_bytes());
        PreparedPayload {
            raw_len: json.len(),
            compressed,
            encoding: ENCODING_UTF8,
            compression: COMPRESSION_ZLIB,
            format: FORMAT_JSON,
        }
    }

    /// Prepare an Anchor-IDL JSON string for on-chain storage as
    /// Utf8 + Zlib + Json.
    pub fn from_idl_json(idl_json: &str) -> Self {
        Self::from_json(idl_json)
    }

    /// Stored payload length (post-compression), the header's `data_len`.
    pub fn data_len(&self) -> u32 {
        self.compressed.len() as u32
    }
}

// ---------------------------------------------------------------------------
// Instruction-data encoders (verified against the on-chain processor)
// ---------------------------------------------------------------------------

/// `Initialize` with inline data: `[1][seed:16][enc][comp][fmt][ds][payload…]`.
/// The program builds the 96-byte header from these tags and copies `payload`
/// to offset 96 of the freshly-created account.
pub fn build_initialize_inline_data(
    seed16: &[u8; SEED_LEN],
    encoding: u8,
    compression: u8,
    format: u8,
    data_source: u8,
    payload: &[u8],
) -> Vec<u8> {
    let mut d = Vec::with_capacity(1 + SEED_LEN + 4 + payload.len());
    d.push(INSTR_INITIALIZE);
    d.extend_from_slice(seed16);
    d.push(encoding);
    d.push(compression);
    d.push(format);
    d.push(data_source);
    d.extend_from_slice(payload);
    d
}

/// `Initialize` finalizing a pre-written `Buffer` in place (no inline payload):
/// `[1][seed:16][enc][comp][fmt][ds]`.
pub fn build_initialize_from_buffer_data(
    seed16: &[u8; SEED_LEN],
    encoding: u8,
    compression: u8,
    format: u8,
    data_source: u8,
) -> Vec<u8> {
    let mut d = Vec::with_capacity(1 + SEED_LEN + 4);
    d.push(INSTR_INITIALIZE);
    d.extend_from_slice(seed16);
    d.push(encoding);
    d.push(compression);
    d.push(format);
    d.push(data_source);
    d
}

/// `Allocate` a canonical PDA buffer: `[7][seed:16]`.
pub fn build_allocate_data(seed16: &[u8; SEED_LEN]) -> Vec<u8> {
    let mut d = Vec::with_capacity(1 + SEED_LEN);
    d.push(INSTR_ALLOCATE);
    d.extend_from_slice(seed16);
    d
}

/// `Write` a chunk at `offset`: `[0][offset:u32-le][chunk…]`. The program
/// stores `chunk` at `offset + 96` (past the buffer header).
pub fn build_write_data(offset: u32, chunk: &[u8]) -> Vec<u8> {
    let mut d = Vec::with_capacity(1 + 4 + chunk.len());
    d.push(INSTR_WRITE);
    d.extend_from_slice(&offset.to_le_bytes());
    d.extend_from_slice(chunk);
    d
}

/// `SetData` with inline data (the `--overwrite` rewrite path):
/// `[3][enc][comp][fmt][ds][payload…]`.
pub fn build_set_data_inline_data(
    encoding: u8,
    compression: u8,
    format: u8,
    data_source: u8,
    payload: &[u8],
) -> Vec<u8> {
    let mut d = Vec::with_capacity(1 + 4 + payload.len());
    d.push(INSTR_SET_DATA);
    d.push(encoding);
    d.push(compression);
    d.push(format);
    d.push(data_source);
    d.extend_from_slice(payload);
    d
}

/// Split `payload` into `(offset, chunk)` writes of at most `chunk_len` bytes.
/// Offsets are relative to the data section (the program adds the 96-byte
/// buffer-header offset itself), so they run `0, chunk_len, 2*chunk_len, …`.
pub fn plan_write_chunks(payload: &[u8], chunk_len: usize) -> Vec<(u32, &[u8])> {
    assert!(chunk_len > 0, "chunk_len must be positive");
    let mut out = Vec::new();
    let mut offset = 0usize;
    while offset < payload.len() {
        let end = (offset + chunk_len).min(payload.len());
        out.push((offset as u32, &payload[offset..end]));
        offset = end;
    }
    out
}

/// Reassemble the data section that a sequence of `Write` chunks would produce
/// on chain. Used by the dry-run-vs-send parity test to prove the buffered
/// path stores exactly the same bytes as the inline path.
#[allow(dead_code)] // exercised by the parity tests
pub fn reassemble_write_chunks(payload_len: usize, chunks: &[(u32, &[u8])]) -> Vec<u8> {
    let mut buf = vec![0u8; payload_len];
    for (offset, chunk) in chunks {
        let start = *offset as usize;
        buf[start..start + chunk.len()].copy_from_slice(chunk);
    }
    buf
}

/// The exact bytes the metadata account will hold after a successful publish:
/// the 96-byte canonical header followed by the compressed payload. Both the
/// inline and buffered send paths converge on this image, and it is what the
/// `--dry-run` header preview describes.
#[allow(dead_code)] // exercised by the parity tests
pub fn expected_account_image(
    program_id: &[u8; 32],
    seed16: &[u8; SEED_LEN],
    payload: &PreparedPayload,
) -> Vec<u8> {
    let header = MetadataHeader::canonical_idl(*program_id, *seed16, payload);
    let mut image = Vec::with_capacity(HEADER_LEN + payload.compressed.len());
    image.extend_from_slice(&header.encode());
    image.extend_from_slice(&payload.compressed);
    image
}

// ---------------------------------------------------------------------------
// Hex helper
// ---------------------------------------------------------------------------

fn hex_encode(bytes: &[u8]) -> String {
    let mut s = String::with_capacity(bytes.len() * 2);
    for b in bytes {
        use std::fmt::Write as _;
        let _ = write!(s, "{:02x}", b);
    }
    s
}

// ---------------------------------------------------------------------------
// Dry-run rendering
// ---------------------------------------------------------------------------

/// What a publish command is writing: the banner name, the noun used in
/// status lines and prompts, and a one-line provenance for the payload.
/// `publish-idl`, `publish-security`, and `publish-manifest` share every
/// byte of the send path; this is the only thing that differs.
pub struct PublishRecord {
    /// CLI command name for banners (`publish-idl`).
    pub command: &'static str,
    /// What the payload is, for status lines (`IDL`, `security.txt`).
    pub noun: &'static str,
    /// One-line provenance printed under `Payload: Source:`.
    pub source: String,
}

impl PublishRecord {
    /// The IDL record projected from a manifest.
    pub fn idl(manifest_name: &str, manifest_version: &str) -> Self {
        PublishRecord {
            command: "publish-idl",
            noun: "IDL",
            source: format!(
                "Solana IDL v0.1.0 (projected from manifest {manifest_name} v{manifest_version})"
            ),
        }
    }
}

/// Render the full `--dry-run` report for the IDL record. See
/// [`render_dry_run_record`].
pub fn render_dry_run(
    program_id: &[u8; 32],
    seed16: &[u8; SEED_LEN],
    idl_json: &str,
    manifest_name: &str,
    manifest_version: &str,
) -> String {
    render_dry_run_record(
        &PublishRecord::idl(manifest_name, manifest_version),
        program_id,
        seed16,
        &PreparedPayload::from_idl_json(idl_json),
    )
}

/// Render the full `--dry-run` report as a string: derived PDA, prepared
/// payload sizes, the 96-byte header (hex + decoded fields), and the
/// instruction plan that a signed send *would* submit. Pure and
/// side-effect free so it is directly unit-testable.
pub fn render_dry_run_record(
    record: &PublishRecord,
    program_id: &[u8; 32],
    seed16: &[u8; SEED_LEN],
    payload: &PreparedPayload,
) -> String {
    let program_b58 = bs58::encode(program_id).into_string();
    let (pda, bump) = derive_canonical_pda(program_id, seed16);
    let pda_b58 = bs58::encode(pda).into_string();

    let header = MetadataHeader::canonical(*program_id, *seed16, payload);
    let header_bytes = header.encode();

    // Render the seed field: printable prefix + hex of the fixed 16 bytes.
    let seed_text = header.seed_text();

    let mut out = String::new();
    use std::fmt::Write as _;

    let _ = writeln!(out, "=== hopper {} (dry run) ===", record.command);
    let _ = writeln!(out);
    let _ = writeln!(out, "Program:            {program_b58}");
    let _ = writeln!(out, "Metadata program:   {METADATA_PROGRAM_ID_B58}");
    let _ = writeln!(
        out,
        "Seed:               \"{seed_text}\"  (hex {})",
        hex_encode(seed16)
    );
    let _ = writeln!(out, "Derivation:         canonical [program, seed]");
    let _ = writeln!(out, "Metadata PDA:       {pda_b58}  (bump {bump})");
    let _ = writeln!(out);

    let _ = writeln!(out, "Payload:");
    let _ = writeln!(out, "  Source:           {}", record.source);
    let _ = writeln!(out, "  Raw size:         {} bytes", payload.raw_len);
    let _ = writeln!(
        out,
        "  Stored size:      {} bytes  (encoding=Utf8, compression=Zlib, format=Json)",
        payload.data_len()
    );
    let _ = writeln!(out);

    let _ = writeln!(out, "Account header ({HEADER_LEN} bytes):");
    let _ = writeln!(out, "  {}", hex_encode(&header_bytes));
    let _ = writeln!(
        out,
        "  disc={} mutable={} canonical={} encoding={} compression={} format={} data_source={} data_len={}",
        header.discriminator,
        header.mutable as u8,
        header.canonical as u8,
        header.encoding,
        header.compression,
        header.format,
        header.data_source,
        header.data_len,
    );
    let _ = writeln!(out);

    let (program_data, _pd_bump) = derive_program_data_pda(program_id);
    let program_data_b58 = bs58::encode(program_data).into_string();

    let stored = payload.data_len() as usize;
    let inline = stored <= MAX_INLINE_PAYLOAD;

    let _ = writeln!(out, "Instruction plan (NOT sent):");
    let _ = writeln!(out, "  metadata program: {METADATA_PROGRAM_ID_B58}");
    let _ = writeln!(out, "  program data PDA: {program_data_b58}");
    if inline {
        let init = build_initialize_inline_data(
            seed16,
            payload.encoding,
            payload.compression,
            payload.format,
            DATA_SOURCE_DIRECT,
            &payload.compressed,
        );
        let _ = writeln!(out, "  path:     single inline Initialize (fits one tx)");
        let _ = writeln!(
            out,
            "  ix[0]:    SystemProgram::transfer  (rent pre-fund -> PDA)"
        );
        let _ = writeln!(
            out,
            "  ix[1]:    Initialize (disc {INSTR_INITIALIZE})  data {} bytes",
            init.len()
        );
        let _ = writeln!(
            out,
            "            accounts: [metadata(w) {pda_b58}], [authority(s,w)],"
        );
        let _ = writeln!(
            out,
            "                      [program(r) {program_b58}], [programData(r)], [system(r) {SYSTEM_PROGRAM_ID_B58}]"
        );
    } else {
        let chunks = plan_write_chunks(&payload.compressed, WRITE_CHUNK_LEN);
        let _ = writeln!(
            out,
            "  path:     Allocate + {} chunked Write(s) + in-place Initialize",
            chunks.len()
        );
        let _ = writeln!(
            out,
            "  tx[0]:    transfer(rent) + Allocate (disc {INSTR_ALLOCATE}, seed)"
        );
        for (idx, (offset, chunk)) in chunks.iter().enumerate() {
            let _ = writeln!(
                out,
                "  tx[{}]:    Write (disc {INSTR_WRITE}) offset {} len {}",
                idx + 1,
                offset,
                chunk.len()
            );
        }
        let _ = writeln!(
            out,
            "  tx[{}]:    Initialize-from-buffer (disc {INSTR_INITIALIZE}, no inline data)",
            chunks.len() + 1
        );
    }
    let _ = writeln!(
        out,
        "  result:   96-byte header (above) + {stored} zlib bytes at the PDA"
    );
    let _ = writeln!(out);
    let _ = writeln!(out, "Dry run only. No transaction was created or sent.");

    out
}

// ---------------------------------------------------------------------------
// CLI entry point
// ---------------------------------------------------------------------------

fn print_usage() {
    eprintln!("Usage: hopper publish-idl --manifest <path> --program-id <pubkey>");
    eprintln!("                          [--cluster <name> | --url <rpc>] [--keypair <path>]");
    eprintln!("                          [--seed <str>] [--overwrite] [--yes] [--dry-run]");
    eprintln!();
    eprintln!("Publish a program's Solana IDL v0.1.0 through Program Metadata");
    eprintln!("program (canonical [program, \"idl\"] PDA), with zero Node dependencies.");
    eprintln!();
    eprintln!("Without --dry-run this signs and submits the on-chain transaction(s):");
    eprintln!("a single inline Initialize for small IDLs, or Allocate + chunked Write");
    eprintln!("+ Initialize for IDLs larger than one transaction. The signer must be");
    eprintln!("the program's upgrade authority (canonical metadata).");
    eprintln!();
    eprintln!("Options:");
    eprintln!("  --manifest <path>     Hopper program manifest JSON (also accepts inline/@file)");
    eprintln!("  --program-id <pubkey> Base58 program id the IDL describes");
    eprintln!("  --cluster <name>      devnet (default), testnet, mainnet-beta, or localnet");
    eprintln!("  --url, --rpc <rpc>    Custom RPC endpoint; treated as potentially mainnet");
    eprintln!("                        SOLANA_RPC_URL is the guarded fallback when set");
    eprintln!("  --keypair <path>      Upgrade-authority + fee-payer keypair");
    eprintln!("                        (default: ~/.config/solana/id.json)");
    eprintln!("  --seed <str>          Metadata seed (default \"idl\", padded to 16 bytes)");
    eprintln!("  --overwrite           Rewrite an already-initialized metadata account (SetData)");
    eprintln!("  --yes, -y             Skip mainnet/custom-RPC confirmation");
    eprintln!("  --dry-run             Preview the header/PDA/plan; no network access or prompt");
}

/// `hopper publish-idl` entry point.
pub fn cmd_publish_idl(args: &[String]) {
    let mut manifest_arg: Option<String> = None;
    let mut program_id_arg: Option<String> = None;
    let mut seed_arg: Option<String> = None;
    let mut url_arg: Option<String> = None;
    let mut cluster_arg: Option<String> = None;
    let mut keypair_arg: Option<String> = None;
    let mut overwrite = false;
    let mut dry_run = false;
    let mut yes = false;

    let mut i = 0;
    while i < args.len() {
        match args[i].as_str() {
            "--url" | "--rpc" | "-u" => {
                url_arg = args.get(i + 1).cloned();
                if url_arg.is_none() {
                    eprintln!("{} requires an RPC endpoint argument", args[i]);
                    process::exit(1);
                }
                i += 2;
            }
            "--cluster" => {
                cluster_arg = args.get(i + 1).cloned();
                if cluster_arg.is_none() {
                    eprintln!("--cluster requires a cluster moniker argument");
                    process::exit(1);
                }
                i += 2;
            }
            "--keypair" | "--signer" => {
                keypair_arg = args.get(i + 1).cloned();
                if keypair_arg.is_none() {
                    eprintln!("--keypair requires a path argument");
                    process::exit(1);
                }
                i += 2;
            }
            "--overwrite" => {
                overwrite = true;
                i += 1;
            }
            "--manifest" => {
                manifest_arg = args.get(i + 1).cloned();
                if manifest_arg.is_none() {
                    eprintln!("--manifest requires a path argument");
                    process::exit(1);
                }
                i += 2;
            }
            "--program-id" => {
                program_id_arg = args.get(i + 1).cloned();
                if program_id_arg.is_none() {
                    eprintln!("--program-id requires a base58 pubkey argument");
                    process::exit(1);
                }
                i += 2;
            }
            "--seed" => {
                seed_arg = args.get(i + 1).cloned();
                if seed_arg.is_none() {
                    eprintln!("--seed requires a string argument");
                    process::exit(1);
                }
                i += 2;
            }
            "--dry-run" => {
                dry_run = true;
                i += 1;
            }
            "--yes" | "-y" => {
                yes = true;
                i += 1;
            }
            "--help" | "-h" => {
                print_usage();
                return;
            }
            other => {
                eprintln!("Unknown publish-idl argument: {other}");
                print_usage();
                process::exit(1);
            }
        }
    }

    let manifest_arg = manifest_arg.unwrap_or_else(|| {
        eprintln!("--manifest is required");
        print_usage();
        process::exit(1);
    });
    let program_id_arg = program_id_arg.unwrap_or_else(|| {
        eprintln!("--program-id is required");
        print_usage();
        process::exit(1);
    });

    // Decode the program id.
    let program_bytes = bs58::decode(&program_id_arg)
        .into_vec()
        .unwrap_or_else(|e| {
            eprintln!("invalid base58 program id: {e}");
            process::exit(1);
        });
    if program_bytes.len() != 32 {
        eprintln!("program id must be 32 bytes, got {}", program_bytes.len());
        process::exit(1);
    }
    let mut program_id = [0u8; 32];
    program_id.copy_from_slice(&program_bytes);

    // Seed (default "idl"), padded to the fixed 16-byte field.
    let seed_bytes = seed_arg
        .as_deref()
        .map(|s| s.as_bytes().to_vec())
        .unwrap_or_else(|| DEFAULT_SEED.to_vec());
    if seed_bytes.len() > SEED_LEN {
        eprintln!(
            "seed is {} bytes; the metadata seed field is at most {SEED_LEN} bytes",
            seed_bytes.len()
        );
        process::exit(1);
    }
    let seed16 = pad_seed(&seed_bytes);

    // Load the manifest and project it into Solana IDL v0.1.0 JSON. `--manifest`
    // is a path; `@`-prefix and inline JSON are also accepted via the
    // shared resolver.
    let load_arg = if manifest_arg.starts_with('@') || manifest_arg.trim_start().starts_with('{') {
        manifest_arg.clone()
    } else {
        format!("@{manifest_arg}")
    };
    let manifest = crate::load_program_manifest(&load_arg);
    let projection = hopper_schema::anchor_idl::AnchorIdlFromManifest {
        manifest: &manifest,
        address: &program_id_arg,
    };
    if let Err(error) = projection.validate() {
        eprintln!("cannot publish a complete Solana IDL v0.1.0: {error}");
        process::exit(1);
    }
    let idl_json = format!("{projection}");

    if dry_run {
        print!(
            "{}",
            render_dry_run(
                &program_id,
                &seed16,
                &idl_json,
                manifest.name,
                manifest.version,
            )
        );
        return;
    }

    // Non-dry-run: build, sign, and submit the real on-chain transaction(s).
    let payload = PreparedPayload::from_idl_json(&idl_json);
    if let Err(e) = run_publish_send(
        &PublishRecord::idl(manifest.name, manifest.version),
        &program_id,
        &seed16,
        &payload,
        url_arg.as_deref(),
        cluster_arg.as_deref(),
        keypair_arg.as_deref(),
        overwrite,
        yes,
    ) {
        eprintln!("publish-idl: {e}");
        process::exit(1);
    }
}

// ---------------------------------------------------------------------------
// Signed on-chain send
// ---------------------------------------------------------------------------

/// Devnet default RPC endpoint for `publish-idl`.
const PUBLISH_DEFAULT_RPC: &str = "https://api.devnet.solana.com";

/// A resolved publish destination. Only named non-mainnet clusters are
/// considered safe without confirmation. Raw URLs cannot prove which Solana
/// cluster they proxy, so every custom or environment-derived endpoint is
/// conservatively treated as potentially mainnet.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct PublishTarget {
    pub(crate) url: String,
    pub(crate) label: String,
    pub(crate) requires_confirmation: bool,
}

impl PublishTarget {
    pub(crate) fn display_url(&self) -> String {
        crate::cmd::cluster::redact_rpc_url(&self.url)
    }

    fn destination_label(&self) -> &'static str {
        if self.label == "mainnet-beta" {
            "MAINNET-BETA"
        } else {
            "A CUSTOM CLUSTER (POTENTIALLY MAINNET-BETA)"
        }
    }
}

fn custom_publish_target(url: &str) -> Result<PublishTarget, String> {
    let trimmed = url.trim();
    if trimmed.chars().any(char::is_control) {
        return Err("RPC endpoint must not contain control characters".to_string());
    }
    let lower = trimmed.to_ascii_lowercase();
    if !(lower.starts_with("http://") || lower.starts_with("https://")) {
        return Err("RPC endpoint must start with http:// or https://".to_string());
    }
    Ok(PublishTarget {
        url: trimmed.to_string(),
        label: "custom".to_string(),
        requires_confirmation: true,
    })
}

fn named_publish_target(moniker: &str) -> Result<PublishTarget, String> {
    if moniker.chars().any(char::is_control) {
        return Err("cluster target must not contain control characters".to_string());
    }
    let (url, label, requires_confirmation) = crate::cmd::cluster::cluster_url(moniker)
        .ok_or_else(|| {
            format!(
                "unknown cluster moniker or RPC URL: {}",
                crate::cmd::cluster::redact_rpc_url(moniker)
            )
        })?;
    Ok(PublishTarget {
        url,
        label,
        requires_confirmation,
    })
}

/// Resolve a publish target without consulting global state. This makes the
/// safety precedence directly testable: explicit endpoint or cluster, then an
/// environment endpoint, then a safe devnet default.
pub(crate) fn resolve_publish_target(
    cli_url: Option<&str>,
    cli_cluster: Option<&str>,
    env_url: Option<&str>,
) -> Result<PublishTarget, String> {
    if cli_url.is_some() && cli_cluster.is_some() {
        return Err("use either --cluster or --url/--rpc, not both".to_string());
    }
    if let Some(url) = cli_url {
        return custom_publish_target(url);
    }
    if let Some(cluster) = cli_cluster {
        return named_publish_target(cluster);
    }
    if let Some(url) = env_url.filter(|url| !url.trim().is_empty()) {
        return custom_publish_target(url);
    }
    Ok(PublishTarget {
        url: PUBLISH_DEFAULT_RPC.to_string(),
        label: "devnet".to_string(),
        requires_confirmation: false,
    })
}

fn confirm_publish_target(
    target: &PublishTarget,
    yes: bool,
    overwrite: bool,
    program_id: &str,
    noun: &str,
) -> Result<(), String> {
    if !target.requires_confirmation || yes {
        return Ok(());
    }
    let action = if overwrite {
        format!("overwrite the published {noun}")
    } else {
        format!("publish the {noun}")
    };
    eprint!(
        "About to {action} for {program_id} on {} ({}). This submits signed transactions. Type 'yes' to continue: ",
        target.destination_label(),
        target.display_url()
    );
    io::stderr()
        .flush()
        .map_err(|e| format!("flush confirmation prompt: {e}"))?;
    let mut line = String::new();
    io::stdin()
        .read_line(&mut line)
        .map_err(|e| format!("read confirmation: {e}"))?;
    if line.trim() != "yes" {
        return Err("aborted".to_string());
    }
    Ok(())
}

pub(crate) fn redact_rpc_error(target: &PublishTarget, error: impl ToString) -> String {
    error
        .to_string()
        .replace(&target.url, &target.display_url())
}

/// Resolve the signer keypair path: explicit `--keypair` wins, else the
/// solana-cli default `~/.config/solana/id.json`.
fn resolve_keypair_path(cli: Option<&str>) -> Result<std::path::PathBuf, String> {
    if let Some(p) = cli {
        return Ok(std::path::PathBuf::from(p));
    }
    crate::workspace::default_solana_keypair_path().ok_or_else(|| {
        "no --keypair supplied and no default keypair at ~/.config/solana/id.json".to_string()
    })
}

/// Build, sign and submit the transaction(s) that write a record to the
/// canonical metadata PDA. Chooses the inline path for small payloads and
/// the `Allocate` + chunked `Write` + `Initialize` path for large ones, and
/// the `SetData` rewrite path under `--overwrite`. Every publish command
/// (`idl`, `security`, `hopper-manifest`) rides this one function.
#[allow(clippy::too_many_arguments)]
pub(crate) fn run_publish_send(
    record: &PublishRecord,
    program_id: &[u8; 32],
    seed16: &[u8; SEED_LEN],
    payload: &PreparedPayload,
    url_cli: Option<&str>,
    cluster_cli: Option<&str>,
    keypair_cli: Option<&str>,
    overwrite: bool,
    yes: bool,
) -> Result<(), String> {
    use solana_client::rpc_client::RpcClient;
    use solana_commitment_config::CommitmentConfig;
    use solana_instruction::{AccountMeta, Instruction};
    use solana_keypair::read_keypair_file;
    use solana_pubkey::Pubkey;
    use solana_signer::Signer;
    use solana_system_interface::instruction as system_instruction;
    use solana_transaction::Transaction;

    let env_url = std::env::var("SOLANA_RPC_URL").ok();
    let target = resolve_publish_target(url_cli, cluster_cli, env_url.as_deref())?;
    let program_b58 = bs58::encode(program_id).into_string();
    confirm_publish_target(&target, yes, overwrite, &program_b58, record.noun)?;
    let display_url = target.display_url();
    let url = target.url.clone();
    let keypair_path = resolve_keypair_path(keypair_cli)?;
    let authority = read_keypair_file(&keypair_path)
        .map_err(|e| format!("read keypair {}: {e}", keypair_path.display()))?;

    // Derive the canonical metadata PDA and the program's ProgramData PDA.
    let (pda, _bump) = derive_canonical_pda(program_id, seed16);
    let (program_data, _pd_bump) = derive_program_data_pda(program_id);
    let pda_b58 = bs58::encode(pda).into_string();

    let metadata_program = Pubkey::new_from_array(metadata_program_id());
    let system_program = Pubkey::new_from_array([0u8; 32]);
    let program_pk = Pubkey::new_from_array(*program_id);
    let program_data_pk = Pubkey::new_from_array(program_data);
    let pda_pk = Pubkey::new_from_array(pda);
    let authority_pk = authority.pubkey();

    let stored = payload.compressed.len();
    let final_space = HEADER_LEN + stored;

    println!("=== hopper {} ===", record.command);
    println!("rpc              : {display_url}");
    println!("signer/authority : {authority_pk}");
    println!("program          : {program_b58}");
    println!("metadata PDA     : {pda_b58}");
    println!(
        "payload          : {} raw -> {} zlib bytes",
        payload.raw_len, stored
    );
    println!("source           : {}", record.source);

    // Inspect the current state of the PDA (existence / discriminator / owner).
    let existing = crate::rpc::get_account_info(&url, &pda_b58).map_err(|e| {
        format!(
            "getAccountInfo({pda_b58}): {}",
            redact_rpc_error(&target, e)
        )
    })?;
    let existing_disc = existing.as_ref().and_then(|a| a.data.first().copied());
    let owned_by_metadata = existing
        .as_ref()
        .map(|a| a.owner == METADATA_PROGRAM_ID_B58)
        .unwrap_or(false);
    let existing_lamports = existing.as_ref().map(|a| a.lamports).unwrap_or(0);

    // Guard against clobbering an unrelated account that happens to sit at the
    // PDA (should be impossible for a real PDA, but fail loud rather than send).
    if let Some(acc) = existing.as_ref() {
        if !acc.data.is_empty() && !owned_by_metadata {
            return Err(format!(
                "PDA {pda_b58} exists but is owned by {} (not the metadata program); refusing to write",
                acc.owner
            ));
        }
    }

    let is_initialized = owned_by_metadata && existing_disc == Some(DISC_METADATA);
    let is_leftover_buffer = owned_by_metadata && existing_disc == Some(1 /* Buffer */);

    if is_initialized && !overwrite {
        return Err(format!(
            "metadata account {pda_b58} is already initialized; re-run with --overwrite to rewrite it"
        ));
    }

    let rpc = RpcClient::new_with_commitment(url.clone(), CommitmentConfig::confirmed());

    // Rent the account must hold at its final size, and how much to top up.
    let rent = rpc
        .get_minimum_balance_for_rent_exemption(final_space)
        .map_err(|e| {
            format!(
                "get_minimum_balance_for_rent_exemption({final_space}): {}",
                redact_rpc_error(&target, e)
            )
        })?;
    let topup = rent.saturating_sub(existing_lamports);

    // Fail early on an under-funded payer rather than mid-sequence.
    //
    // Reserve a base fee for every transaction the chosen path submits,
    // not just one. The buffered path sends Allocate + N Write +
    // Initialize (N+2 txs); reserving a single fee would let a payer pass
    // this guard and then run dry partway through the Write loop, leaving
    // a partially-written Buffer stranded on chain. Over-reserving by a
    // fee on the resume/inline paths is the safe direction.
    const BASE_FEE: u64 = 5_000;
    let planned_txs: u64 =
        if is_initialized || (stored <= MAX_INLINE_PAYLOAD && !is_leftover_buffer) {
            1 // overwrite SetData or fresh inline Initialize
        } else {
            // fresh buffered / resume: [Allocate or top-up] + N Write + Initialize
            plan_write_chunks(&payload.compressed, WRITE_CHUNK_LEN).len() as u64 + 2
        };
    let required = topup.saturating_add(planned_txs.saturating_mul(BASE_FEE));

    let payer_balance = rpc.get_balance(&authority_pk).map_err(|e| {
        format!(
            "get_balance({authority_pk}): {}",
            redact_rpc_error(&target, e)
        )
    })?;
    if payer_balance < required {
        let funding_hint = if target.label == "devnet" {
            format!("Try `solana airdrop 1 {authority_pk} --url {display_url}`.")
        } else {
            format!("Fund it on the selected target ({display_url}).")
        };
        return Err(format!(
            "insufficient balance: signer {authority_pk} has {payer_balance} lamports but needs \
             ~{required} (rent top-up {topup} + {planned_txs} tx fee(s)). {funding_hint}"
        ));
    }

    // Account-meta templates shared across instructions.
    let init_metas = || {
        vec![
            AccountMeta::new(pda_pk, false),                   // 0 metadata (w)
            AccountMeta::new(authority_pk, true),              // 1 authority (s, w)
            AccountMeta::new_readonly(program_pk, false),      // 2 program (r)
            AccountMeta::new_readonly(program_data_pk, false), // 3 program_data (r)
            AccountMeta::new_readonly(system_program, false),  // 4 system (r)
        ]
    };

    let send = |instructions: &[Instruction], label: &str| -> Result<String, String> {
        let recent = rpc.get_latest_blockhash().map_err(|e| {
            format!(
                "get_latest_blockhash ({label}): {}",
                redact_rpc_error(&target, e)
            )
        })?;
        let tx = Transaction::new_signed_with_payer(
            instructions,
            Some(&authority_pk),
            &[&authority],
            recent,
        );
        super::transaction_limits::ensure_legacy_transaction_size(
            &tx,
            &format!("hopper {} {label}", record.command),
        )?;
        rpc.send_and_confirm_transaction(&tx)
            .map(|s| s.to_string())
            .map_err(|e| format!("{label}: {}", redact_rpc_error(&target, e)))
    };

    // ---- Overwrite path: SetData on an already-initialized account. ----
    if is_initialized {
        if stored > MAX_INLINE_PAYLOAD {
            return Err(format!(
                "--overwrite of a {stored}-byte payload needs chunking, which the in-place \
                 SetData path does not support. Close the metadata account first, then \
                 re-publish fresh (the fresh large-publish path handles chunking)."
            ));
        }
        let mut ixs = Vec::new();
        if topup > 0 {
            ixs.push(system_instruction::transfer(&authority_pk, &pda_pk, topup));
        }
        let data = build_set_data_inline_data(
            payload.encoding,
            payload.compression,
            payload.format,
            DATA_SOURCE_DIRECT,
            &payload.compressed,
        );
        let metas = vec![
            AccountMeta::new(pda_pk, false),                    // 0 metadata (w)
            AccountMeta::new(authority_pk, true),               // 1 authority (s)
            AccountMeta::new_readonly(metadata_program, false), // 2 buffer placeholder = None
            AccountMeta::new_readonly(program_pk, false),       // 3 program (r)
            AccountMeta::new_readonly(program_data_pk, false),  // 4 program_data (r)
        ];
        ixs.push(Instruction {
            program_id: metadata_program,
            accounts: metas,
            data,
        });
        println!("path             : overwrite (inline SetData)");
        let sig = send(&ixs, "SetData")?;
        println!("signature        : {sig}");
        println!("status           : confirmed (metadata rewritten)");
        return Ok(());
    }

    // ---- Fresh inline path: single Initialize carries the whole payload. ----
    if stored <= MAX_INLINE_PAYLOAD && !is_leftover_buffer {
        let mut ixs = Vec::new();
        if topup > 0 {
            ixs.push(system_instruction::transfer(&authority_pk, &pda_pk, topup));
        }
        let data = build_initialize_inline_data(
            seed16,
            payload.encoding,
            payload.compression,
            payload.format,
            DATA_SOURCE_DIRECT,
            &payload.compressed,
        );
        ixs.push(Instruction {
            program_id: metadata_program,
            accounts: init_metas(),
            data,
        });
        println!("path             : fresh inline Initialize");
        let sig = send(&ixs, "Initialize")?;
        println!("signature        : {sig}");
        println!("status           : confirmed ({} published)", record.noun);
        return Ok(());
    }

    // ---- Fresh large path: Allocate + chunked Write + in-place Initialize. ----
    let chunks = plan_write_chunks(&payload.compressed, WRITE_CHUNK_LEN);
    println!(
        "path             : fresh buffered ({} write chunk(s))",
        chunks.len()
    );

    // Step 1: pre-fund to final size + Allocate the buffer header. If a
    // leftover buffer already exists at the PDA (a prior run died mid-write),
    // top up its lamports and skip Allocate. The Writes below overwrite by
    // offset, so resuming is safe and idempotent.
    if is_leftover_buffer {
        if topup > 0 {
            let sig = send(
                &[system_instruction::transfer(&authority_pk, &pda_pk, topup)],
                "top-up existing buffer",
            )?;
            println!("resume buffer    : topped up ({sig})");
        } else {
            println!("resume buffer    : reusing existing buffer at PDA");
        }
    } else {
        let mut ixs = Vec::new();
        if topup > 0 {
            ixs.push(system_instruction::transfer(&authority_pk, &pda_pk, topup));
        }
        ixs.push(Instruction {
            program_id: metadata_program,
            accounts: init_metas(),
            data: build_allocate_data(seed16),
        });
        let sig = send(&ixs, "Allocate")?;
        println!("allocate         : {sig}");
    }

    // Step 2: write each chunk. Write accounts = [buffer(w), authority(s),
    // source(r)] where source = the metadata program id signals "inline data".
    for (idx, (offset, chunk)) in chunks.iter().enumerate() {
        let metas = vec![
            AccountMeta::new(pda_pk, false),      // 0 target buffer (w)
            AccountMeta::new(authority_pk, true), // 1 authority (s)
            AccountMeta::new_readonly(metadata_program, false), // 2 source = None
        ];
        let ix = Instruction {
            program_id: metadata_program,
            accounts: metas,
            data: build_write_data(*offset, chunk),
        };
        let sig = send(&[ix], &format!("Write chunk {}/{}", idx + 1, chunks.len()))?;
        println!(
            "write [{:>2}/{:>2}]    : offset {:>6} len {:>4}  {sig}",
            idx + 1,
            chunks.len(),
            offset,
            chunk.len()
        );
    }

    // Step 3: finalize the buffer in place (no inline data allowed here).
    let data = build_initialize_from_buffer_data(
        seed16,
        payload.encoding,
        payload.compression,
        payload.format,
        DATA_SOURCE_DIRECT,
    );
    let ix = Instruction {
        program_id: metadata_program,
        accounts: init_metas(),
        data,
    };
    let sig = send(&[ix], "Initialize (finalize buffer)")?;
    println!("finalize         : {sig}");
    println!("status           : confirmed ({} published)", record.noun);
    Ok(())
}

/// A metadata record read back from the chain: the decoded 96-byte header
/// and the stored payload, inflated when the header says zlib.
pub struct PublishedRecord {
    /// The canonical PDA the record lives at.
    pub pda_b58: String,
    /// Decoded account header.
    pub header: MetadataHeader,
    /// Lamports held by the account (its rent).
    pub lamports: u64,
    /// Stored (post-compression) payload length.
    pub stored_len: usize,
    /// The payload after undoing the recorded compression. For a non-direct
    /// `data_source` this is the raw pointer bytes (a URL or an address).
    pub payload: Vec<u8>,
}

/// Fetch the canonical record for `program_id` under `seed16`, or `None`
/// when nothing is published at that PDA. Refuses accounts the metadata
/// program does not own and unfinalized buffers, so the caller never
/// mistakes a stray account for a record.
pub(crate) fn fetch_published_record(
    url: &str,
    program_id: &[u8; 32],
    seed16: &[u8; SEED_LEN],
) -> Result<Option<PublishedRecord>, String> {
    let (pda, _bump) = derive_canonical_pda(program_id, seed16);
    let pda_b58 = bs58::encode(pda).into_string();
    let Some(account) = crate::rpc::get_account_info(url, &pda_b58)? else {
        return Ok(None);
    };
    if account.owner != METADATA_PROGRAM_ID_B58 {
        return Err(format!(
            "{pda_b58} is owned by {}, not the Program Metadata program",
            account.owner
        ));
    }
    let header = MetadataHeader::decode(&account.data)?;
    if header.discriminator != DISC_METADATA {
        return Err(format!(
            "{pda_b58} holds a Program Metadata account with discriminator {} (an unfinalized \
             Buffer, not a published record)",
            header.discriminator
        ));
    }
    let stored_len = header.data_len as usize;
    let body = account
        .data
        .get(HEADER_LEN..HEADER_LEN + stored_len)
        .ok_or_else(|| {
            format!(
                "{pda_b58}: header declares {stored_len} payload bytes but the account holds {}",
                account.data.len().saturating_sub(HEADER_LEN)
            )
        })?;
    let payload = match header.compression {
        COMPRESSION_ZLIB => zlib_decompress(body)?,
        0 => body.to_vec(),
        other => {
            return Err(format!(
                "{pda_b58}: unsupported compression tag {other} (only none and zlib are read)"
            ))
        }
    };
    Ok(Some(PublishedRecord {
        pda_b58,
        header,
        lamports: account.lamports,
        stored_len,
        payload,
    }))
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    // A deterministic 32-byte program id used across PDA tests.
    fn sample_program() -> [u8; 32] {
        [1u8; 32]
    }

    #[test]
    fn seed_padding_is_right_padded_and_16_bytes() {
        let s = pad_seed(DEFAULT_SEED);
        assert_eq!(s.len(), SEED_LEN);
        // "idl" = 0x69 0x64 0x6c, then 13 zero bytes.
        assert_eq!(&s[..3], b"idl");
        assert!(s[3..].iter().all(|&b| b == 0), "tail must be zero-padded");
    }

    #[test]
    fn seed_padding_truncates_overlong_input() {
        let long = [0xABu8; 20];
        let s = pad_seed(&long);
        assert_eq!(s, [0xABu8; SEED_LEN]);
    }

    #[test]
    fn metadata_program_id_decodes_to_32_bytes() {
        let id = metadata_program_id();
        assert_eq!(id.len(), 32);
        // Round-trips back to the canonical base58 string.
        assert_eq!(bs58::encode(id).into_string(), METADATA_PROGRAM_ID_B58);
    }

    #[test]
    fn canonical_pda_is_deterministic() {
        let prog = sample_program();
        let seed = pad_seed(DEFAULT_SEED);
        let a = derive_canonical_pda(&prog, &seed);
        let b = derive_canonical_pda(&prog, &seed);
        assert_eq!(a, b, "same inputs must yield the same PDA + bump");
        // Result must itself be off-curve, and re-deriving with the found
        // bump must reproduce the same address.
        assert!(!is_on_curve(&a.0));
        let meta = metadata_program_id();
        let redo = create_program_address(&[prog.as_ref(), seed.as_ref()], a.1, &meta);
        assert_eq!(redo, Some(a.0));
    }

    #[test]
    fn canonical_and_non_canonical_differ() {
        let prog = sample_program();
        let authority = [7u8; 32];
        let seed = pad_seed(DEFAULT_SEED);
        let (canon, _) = derive_canonical_pda(&prog, &seed);
        let (non_canon, _) = derive_non_canonical_pda(&prog, &authority, &seed);
        assert_ne!(
            canon, non_canon,
            "adding the authority seed must change the derived address"
        );
    }

    #[test]
    fn different_seed_padding_changes_pda() {
        // The full 16-byte fixed field participates in derivation, so an
        // "idl" seed and an "idl\0...extra" seed with the SAME 3-byte prefix
        // but different padding must derive different addresses. This pins
        // that the padded field (not the trimmed slice) is the PDA seed.
        let prog = sample_program();
        let a = pad_seed(b"idl");
        let mut b = pad_seed(b"idl");
        b[8] = 0xFF; // perturb a padding byte
        let (pa, _) = derive_canonical_pda(&prog, &a);
        let (pb, _) = derive_canonical_pda(&prog, &b);
        assert_ne!(pa, pb);
    }

    #[test]
    fn header_encodes_byte_for_byte_fixture() {
        // Hand-computed fixture from the documented 96-byte layout.
        let header = MetadataHeader {
            discriminator: DISC_METADATA, // 2
            program: [0x01; 32],
            authority: None, // 32 zero bytes
            mutable: true,   // 1
            canonical: true, // 1
            seed: pad_seed(b"idl"),
            encoding: ENCODING_UTF8,         // 1
            compression: COMPRESSION_ZLIB,   // 2
            format: FORMAT_JSON,             // 1
            data_source: DATA_SOURCE_DIRECT, // 0
            data_len: 0x1234_5678,           // LE -> 78 56 34 12
        };

        let mut expected = [0u8; HEADER_LEN];
        expected[0] = 0x02; // disc = Metadata
        for b in expected[1..33].iter_mut() {
            *b = 0x01; // program
        }
        // authority 33..65 stays zero (None)
        expected[65] = 0x01; // mutable
        expected[66] = 0x01; // canonical
        expected[67] = 0x69; // 'i'
        expected[68] = 0x64; // 'd'
        expected[69] = 0x6c; // 'l'
                             // seed padding 70..83 stays zero
        expected[83] = 0x01; // encoding = Utf8
        expected[84] = 0x02; // compression = Zlib
        expected[85] = 0x01; // format = Json
        expected[86] = 0x00; // data_source = Direct
        expected[87] = 0x78; // data_len LE
        expected[88] = 0x56;
        expected[89] = 0x34;
        expected[90] = 0x12;
        // 91..96 padding stays zero

        assert_eq!(header.encode(), expected);
    }

    #[test]
    fn header_encodes_authority_some() {
        // The Some(authority) branch writes the 32 authority bytes at 33..65.
        let header = MetadataHeader {
            discriminator: DISC_METADATA,
            program: [0u8; 32],
            authority: Some([0xAA; 32]),
            mutable: false,
            canonical: false,
            seed: [0u8; SEED_LEN],
            encoding: ENCODING_UTF8,
            compression: COMPRESSION_ZLIB,
            format: FORMAT_JSON,
            data_source: DATA_SOURCE_DIRECT,
            data_len: 0,
        };
        let bytes = header.encode();
        assert!(bytes[33..65].iter().all(|&b| b == 0xAA));
        assert_eq!(bytes[65], 0); // mutable = false
        assert_eq!(bytes[66], 0); // canonical = false
    }

    #[test]
    fn header_len_is_96() {
        let header = MetadataHeader {
            discriminator: DISC_METADATA,
            program: [9u8; 32],
            authority: None,
            mutable: true,
            canonical: true,
            seed: pad_seed(b"idl"),
            encoding: ENCODING_UTF8,
            compression: COMPRESSION_ZLIB,
            format: FORMAT_JSON,
            data_source: DATA_SOURCE_DIRECT,
            data_len: 42,
        };
        assert_eq!(header.encode().len(), HEADER_LEN);
        // data_len lands little-endian at 87..91.
        assert_eq!(&header.encode()[87..91], &42u32.to_le_bytes());
    }

    #[test]
    fn canonical_idl_reads_payload_tags() {
        let payload = PreparedPayload::from_idl_json(r#"{"name":"demo"}"#);
        let header = MetadataHeader::canonical_idl([9u8; 32], pad_seed(b"idl"), &payload);
        assert_eq!(header.encoding, ENCODING_UTF8);
        assert_eq!(header.compression, COMPRESSION_ZLIB);
        assert_eq!(header.format, FORMAT_JSON);
        assert_eq!(header.data_len, payload.data_len());
    }

    #[test]
    fn zlib_round_trips() {
        let sample = br#"{"version":"0.1.0","name":"demo","instructions":[]}"#;
        let compressed = zlib_compress(sample);
        let restored = zlib_decompress(&compressed).expect("decompress");
        assert_eq!(restored, sample);
    }

    #[test]
    fn zlib_round_trips_large_repetitive_payload() {
        // Repetitive JSON-ish data should compress well and still restore.
        let big = "{\"field\":\"value\"},".repeat(4096);
        let compressed = zlib_compress(big.as_bytes());
        assert!(
            compressed.len() < big.len(),
            "repetitive payload should shrink"
        );
        let restored = zlib_decompress(&compressed).expect("decompress");
        assert_eq!(restored, big.as_bytes());
    }

    #[test]
    fn prepared_payload_reports_utf8_zlib_json() {
        let idl = r#"{"version":"0.1.0","name":"demo"}"#;
        let prepared = PreparedPayload::from_idl_json(idl);
        assert_eq!(prepared.raw_len, idl.len());
        assert_eq!(prepared.encoding, ENCODING_UTF8);
        assert_eq!(prepared.compression, COMPRESSION_ZLIB);
        assert_eq!(prepared.format, FORMAT_JSON);
        assert_eq!(prepared.data_len() as usize, prepared.compressed.len());
        // Compressed bytes decompress back to the original IDL.
        let restored = zlib_decompress(&prepared.compressed).expect("decompress");
        assert_eq!(restored, idl.as_bytes());
    }

    #[test]
    fn dry_run_output_contains_pda_and_header() {
        let prog = sample_program();
        let seed = pad_seed(DEFAULT_SEED);
        let idl = r#"{"version":"0.1.0","name":"demo","instructions":[]}"#;
        let report = render_dry_run(&prog, &seed, idl, "demo", "0.1.0");

        // Derived PDA must appear verbatim.
        let (pda, _) = derive_canonical_pda(&prog, &seed);
        let pda_b58 = bs58::encode(pda).into_string();
        assert!(
            report.contains(&pda_b58),
            "dry-run must print the derived metadata PDA"
        );

        // The 96-byte header (hex) must appear verbatim.
        let payload = PreparedPayload::from_idl_json(idl);
        let header = MetadataHeader::canonical_idl(prog, seed, &payload);
        let header_hex = hex_encode(&header.encode());
        assert!(
            report.contains(&header_hex),
            "dry-run must print the 96-byte header hex"
        );

        // Sanity: mentions the metadata program and does not claim to send.
        assert!(report.contains(METADATA_PROGRAM_ID_B58));
        assert!(report.contains("No transaction was created or sent"));
    }

    // -----------------------------------------------------------------
    // Instruction-encoder + chunking + parity tests
    // -----------------------------------------------------------------

    #[test]
    fn initialize_inline_data_is_disc_seed_tags_payload() {
        let seed = pad_seed(b"idl");
        let payload = [0xAA, 0xBB, 0xCC];
        let d = build_initialize_inline_data(
            &seed,
            ENCODING_UTF8,
            COMPRESSION_ZLIB,
            FORMAT_JSON,
            DATA_SOURCE_DIRECT,
            &payload,
        );
        // [1][seed:16][enc][comp][fmt][ds][payload]
        assert_eq!(d[0], INSTR_INITIALIZE);
        assert_eq!(&d[1..17], &seed);
        assert_eq!(d[17], ENCODING_UTF8);
        assert_eq!(d[18], COMPRESSION_ZLIB);
        assert_eq!(d[19], FORMAT_JSON);
        assert_eq!(d[20], DATA_SOURCE_DIRECT);
        assert_eq!(&d[21..], &payload);
        assert_eq!(d.len(), 1 + SEED_LEN + 4 + payload.len());
    }

    #[test]
    fn initialize_from_buffer_data_omits_payload() {
        let seed = pad_seed(b"idl");
        let d = build_initialize_from_buffer_data(
            &seed,
            ENCODING_UTF8,
            COMPRESSION_ZLIB,
            FORMAT_JSON,
            DATA_SOURCE_DIRECT,
        );
        assert_eq!(d[0], INSTR_INITIALIZE);
        assert_eq!(&d[1..17], &seed);
        assert_eq!(
            &d[17..21],
            &[
                ENCODING_UTF8,
                COMPRESSION_ZLIB,
                FORMAT_JSON,
                DATA_SOURCE_DIRECT
            ]
        );
        assert_eq!(
            d.len(),
            1 + SEED_LEN + 4,
            "no inline payload for the buffer path"
        );
    }

    #[test]
    fn allocate_data_is_disc_and_seed() {
        let seed = pad_seed(b"idl");
        let d = build_allocate_data(&seed);
        assert_eq!(d[0], INSTR_ALLOCATE);
        assert_eq!(&d[1..], &seed);
        assert_eq!(d.len(), 1 + SEED_LEN);
    }

    #[test]
    fn write_data_encodes_offset_le_then_chunk() {
        let chunk = [1u8, 2, 3, 4, 5];
        let d = build_write_data(0x0000_0900, &chunk);
        assert_eq!(d[0], INSTR_WRITE);
        // 0x900 = 2304 -> LE bytes 00 09 00 00
        assert_eq!(&d[1..5], &2304u32.to_le_bytes());
        assert_eq!(&d[5..], &chunk);
    }

    #[test]
    fn set_data_inline_is_disc_tags_payload() {
        let payload = [0xDE, 0xAD];
        let d = build_set_data_inline_data(
            ENCODING_UTF8,
            COMPRESSION_ZLIB,
            FORMAT_JSON,
            DATA_SOURCE_DIRECT,
            &payload,
        );
        assert_eq!(d[0], INSTR_SET_DATA);
        assert_eq!(
            &d[1..5],
            &[
                ENCODING_UTF8,
                COMPRESSION_ZLIB,
                FORMAT_JSON,
                DATA_SOURCE_DIRECT
            ]
        );
        assert_eq!(&d[5..], &payload);
    }

    #[test]
    fn chunking_boundaries_are_contiguous_and_cover_payload() {
        // 2050 bytes at 900/chunk -> offsets 0, 900, 1800 with lengths 900,900,250.
        let payload: Vec<u8> = (0..2050u32).map(|i| (i % 251) as u8).collect();
        let chunks = plan_write_chunks(&payload, WRITE_CHUNK_LEN);
        assert_eq!(chunks.len(), 3);
        assert_eq!(chunks[0].0, 0);
        assert_eq!(chunks[0].1.len(), 900);
        assert_eq!(chunks[1].0, 900);
        assert_eq!(chunks[1].1.len(), 900);
        assert_eq!(chunks[2].0, 1800);
        assert_eq!(chunks[2].1.len(), 250);
        // Every chunk is at most WRITE_CHUNK_LEN and offsets are contiguous.
        let mut expected_off = 0u32;
        for (off, ch) in &chunks {
            assert_eq!(*off, expected_off);
            assert!(ch.len() <= WRITE_CHUNK_LEN);
            expected_off += ch.len() as u32;
        }
        assert_eq!(expected_off as usize, payload.len());
    }

    #[test]
    fn chunks_reassemble_to_original_payload() {
        let payload: Vec<u8> = (0..5000u32).map(|i| (i * 7 % 256) as u8).collect();
        let chunks = plan_write_chunks(&payload, WRITE_CHUNK_LEN);
        let rebuilt = reassemble_write_chunks(payload.len(), &chunks);
        assert_eq!(
            rebuilt, payload,
            "buffered writes must reproduce the payload byte-for-byte"
        );
    }

    #[test]
    fn program_data_pda_is_deterministic_and_off_curve() {
        let prog = sample_program();
        let a = derive_program_data_pda(&prog);
        let b = derive_program_data_pda(&prog);
        assert_eq!(a, b);
        assert!(!is_on_curve(&a.0));
        // Must differ from the metadata PDA (different program + seeds).
        let (meta_pda, _) = derive_canonical_pda(&prog, &pad_seed(DEFAULT_SEED));
        assert_ne!(a.0, meta_pda);
    }

    #[test]
    fn expected_account_image_is_header_then_payload() {
        let prog = sample_program();
        let seed = pad_seed(DEFAULT_SEED);
        let idl = r#"{"version":"0.1.0","name":"demo","instructions":[]}"#;
        let payload = PreparedPayload::from_idl_json(idl);
        let image = expected_account_image(&prog, &seed, &payload);
        // First 96 bytes are exactly the canonical header.
        let header = MetadataHeader::canonical_idl(prog, seed, &payload);
        assert_eq!(&image[..HEADER_LEN], &header.encode());
        // Remainder is the compressed payload.
        assert_eq!(&image[HEADER_LEN..], &payload.compressed[..]);
        // And the recorded data_len matches the stored region length.
        assert_eq!(image.len() - HEADER_LEN, header.data_len as usize);
    }

    #[test]
    fn dry_run_vs_send_parity_inline() {
        // For a small IDL the inline Initialize carries the payload verbatim,
        // and the resulting on-chain account image equals the dry-run header
        // preview followed by the same compressed bytes.
        let prog = sample_program();
        let seed = pad_seed(DEFAULT_SEED);
        let idl = r#"{"version":"0.1.0","name":"demo","instructions":[]}"#;
        let payload = PreparedPayload::from_idl_json(idl);
        assert!(payload.compressed.len() <= MAX_INLINE_PAYLOAD);

        // The bytes the Initialize instruction carries past its 21-byte prefix
        // are exactly the payload the dry-run reports storing.
        let init = build_initialize_inline_data(
            &seed,
            payload.encoding,
            payload.compression,
            payload.format,
            DATA_SOURCE_DIRECT,
            &payload.compressed,
        );
        let carried = &init[1 + SEED_LEN + 4..];
        assert_eq!(carried, &payload.compressed[..]);

        // The final account image the send produces == dry-run header ++ payload.
        let image = expected_account_image(&prog, &seed, &payload);
        let (pda, _) = derive_canonical_pda(&prog, &seed);
        let report = render_dry_run(&prog, &seed, idl, "demo", "0.1.0");
        assert!(report.contains(&hex_encode(&image[..HEADER_LEN])));
        assert!(report.contains(&bs58::encode(pda).into_string()));
    }

    #[test]
    fn dry_run_vs_send_parity_buffered() {
        // A large IDL: the Allocate + Write + Initialize path must store the
        // exact same account image as the inline path would, byte-for-byte.
        let prog = sample_program();
        let seed = pad_seed(DEFAULT_SEED);
        // Build an IDL whose *compressed* form exceeds the inline threshold by
        // stuffing high-entropy content that zlib cannot shrink much.
        let mut names = String::new();
        for i in 0..400 {
            names.push_str(&format!("\"ix_{i:04x}_{}\",", (i * 2654435761u64) & 0xffff));
        }
        let idl = format!(
            r#"{{"version":"0.1.0","name":"big","docs":[{}]}}"#,
            names.trim_end_matches(',')
        );
        let payload = PreparedPayload::from_idl_json(&idl);
        assert!(
            payload.compressed.len() > MAX_INLINE_PAYLOAD,
            "test fixture must exceed the inline threshold (got {})",
            payload.compressed.len()
        );

        // Reassemble the chunked writes and prepend the header the finalize
        // Initialize builds; it must equal the canonical account image.
        let chunks = plan_write_chunks(&payload.compressed, WRITE_CHUNK_LEN);
        assert!(chunks.len() >= 2, "fixture should need multiple chunks");
        let data_section = reassemble_write_chunks(payload.compressed.len(), &chunks);
        let mut buffered_image = MetadataHeader::canonical_idl(prog, seed, &payload)
            .encode()
            .to_vec();
        buffered_image.extend_from_slice(&data_section);

        let inline_image = expected_account_image(&prog, &seed, &payload);
        assert_eq!(
            buffered_image, inline_image,
            "buffered and inline paths must converge on identical account bytes"
        );

        // The dry-run for this payload should describe the buffered plan.
        let report = render_dry_run(&prog, &seed, &idl, "big", "0.1.0");
        assert!(report.contains("Allocate"));
        assert!(report.contains("Write"));
    }

    #[test]
    fn bpf_loader_id_round_trips() {
        let id = bpf_loader_upgradeable_id();
        assert_eq!(
            bs58::encode(id).into_string(),
            BPF_LOADER_UPGRADEABLE_ID_B58
        );
    }

    #[test]
    fn publish_target_defaults_to_named_devnet() {
        let target = resolve_publish_target(None, None, None).unwrap();
        assert_eq!(target.url, PUBLISH_DEFAULT_RPC);
        assert_eq!(target.label, "devnet");
        assert!(!target.requires_confirmation);
    }

    #[test]
    fn named_clusters_have_conservative_confirmation_classes() {
        for name in ["devnet", "testnet", "localnet"] {
            let target = resolve_publish_target(None, Some(name), None).unwrap();
            assert_eq!(target.label, name);
            assert!(!target.requires_confirmation, "{name} should not prompt");
        }

        let mainnet = resolve_publish_target(None, Some("mainnet-beta"), None).unwrap();
        assert_eq!(mainnet.label, "mainnet-beta");
        assert!(mainnet.requires_confirmation);
    }

    #[test]
    fn every_raw_rpc_url_is_treated_as_potentially_mainnet() {
        let devnet_url =
            resolve_publish_target(Some("https://api.devnet.solana.com"), None, None).unwrap();
        assert_eq!(devnet_url.label, "custom");
        assert!(devnet_url.requires_confirmation);

        let custom_via_cluster =
            resolve_publish_target(None, Some("https://rpc.example.invalid/project"), None)
                .unwrap();
        assert_eq!(custom_via_cluster.label, "custom");
        assert!(custom_via_cluster.requires_confirmation);
    }

    #[test]
    fn environment_rpc_cannot_silently_bypass_guard() {
        let target =
            resolve_publish_target(None, None, Some("https://api.mainnet-beta.solana.com"))
                .unwrap();
        assert_eq!(target.label, "custom");
        assert!(target.requires_confirmation);

        let named_cli =
            resolve_publish_target(None, Some("devnet"), Some("https://mainnet.invalid")).unwrap();
        assert_eq!(named_cli.label, "devnet");
        assert!(!named_cli.requires_confirmation);
    }

    #[test]
    fn publish_target_rejects_ambiguous_or_invalid_overrides() {
        assert!(
            resolve_publish_target(Some("https://rpc.example.invalid"), Some("devnet"), None)
                .is_err()
        );
        assert!(resolve_publish_target(Some("devnet"), None, None).is_err());
        assert!(resolve_publish_target(
            Some("https://rpc.example.invalid/\nterminal-injection"),
            None,
            None
        )
        .is_err());
        assert!(resolve_publish_target(
            None,
            Some("https://rpc.example.invalid/\nterminal-injection"),
            None
        )
        .is_err());
        assert!(resolve_publish_target(None, Some("unknown"), None).is_err());
    }

    #[test]
    fn publish_target_display_redacts_credentials_and_query() {
        let target = resolve_publish_target(
            Some("https://user:secret@rpc.example.invalid/path?api-key=secret"),
            None,
            None,
        )
        .unwrap();
        assert_eq!(
            target.display_url(),
            "https://<redacted>@rpc.example.invalid/path?<redacted>"
        );

        let rendered_error = redact_rpc_error(&target, format!("request to {} failed", target.url));
        assert!(!rendered_error.contains("secret"));
        assert!(rendered_error.contains(&target.display_url()));

        let invalid =
            named_publish_target("ftp://user:secret@rpc.example.invalid/path?api-key=secret")
                .unwrap_err();
        assert!(!invalid.contains("secret"));
    }
}