From cc3f148699baed8be98b569aa7ce36c25699ed17 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Sat, 24 Oct 2026 18:21:52 +0000 Subject: [PATCH 01/27] build(deps)!: update Platform to v5.1-dev 063589bf Pin dash-sdk, rs-sdk-trusted-context-provider, platform-wallet and platform-wallet-storage to dashpay/platform v5.1-dev 063589bf (5.0.0-rc.8). The lockfile moves only the Platform packages. The upstream V019 wallet-storage migration adds a nullable asset-lock operation id; V001-V018 are unchanged. The compatibility bridge gets the 033ecae destination snapshot it compares against, and the pin guard records the new revision with an unchanged migration fingerprint. BREAKING CHANGE: the wallet storage upgrade to V019 is forward only. An older build cannot open upgraded storage; going back needs a copy of the data directory taken before the upgrade. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- Cargo.lock | 1052 ++++++++++------- Cargo.toml | 8 +- .../expected-migration-fingerprint.txt | 6 +- .../platform_compatibility/engine.rs | 3 +- .../platform_compatibility/engine/tests.rs | 47 + .../fixtures/033ecae.sql | 413 +++++++ .../platform_compatibility/fixtures/README.md | 17 +- 7 files changed, 1084 insertions(+), 462 deletions(-) create mode 100644 src/wallet_backend/platform_compatibility/fixtures/033ecae.sql diff --git a/Cargo.lock b/Cargo.lock index 00d31d4ed..3095615ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -146,7 +146,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0" dependencies = [ "crypto-common 0.1.7", - "generic-array 0.14.7", + "generic-array", ] [[package]] @@ -179,6 +179,7 @@ dependencies = [ "cipher 0.5.2", "cpubits", "cpufeatures 0.3.1", + "zeroize", ] [[package]] @@ -233,6 +234,18 @@ version = "0.2.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" +[[package]] +name = "ambassador" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd3d4da7fddbc6567cbd7b5c30364f91313701b1857ab5b925eb3acf06162eed" +dependencies = [ + "itertools 0.10.5", + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "android-activity" version = "0.6.1" @@ -309,7 +322,7 @@ version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" dependencies = [ - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -320,7 +333,7 @@ checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" dependencies = [ "anstyle", "once_cell_polyfill", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -329,6 +342,17 @@ version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" +[[package]] +name = "app-connect-contract" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" +dependencies = [ + "platform-value", + "platform-version", + "serde_json", + "thiserror 2.0.20", +] + [[package]] name = "apple-native-keyring-store" version = "1.0.0" @@ -770,11 +794,21 @@ checksum = "4c7f02d4ea65f2c1853089ffd8d2787bdbc63de2f0d29dedbcf8ccdfa0ccd4cf" [[package]] name = "base58ck" -version = "0.1.101" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "365c0acd5b2e8dd0111a46c4faea83fb3cfb6e39a49a7c73a06e090db7b2eff0" +checksum = "df540278d807651653f9b6f21e18903d790d7769eadfbc0dc4df0a141260318f" dependencies = [ - "bitcoin_hashes", + "bitcoin-internals 0.6.0", + "bitcoin_hashes 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "base58ck" +version = "0.5.0" +source = "git+https://github.com/rust-bitcoin/rust-bitcoin?rev=7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516#7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516" +dependencies = [ + "bitcoin-internals 0.7.0 (git+https://github.com/rust-bitcoin/rust-bitcoin?rev=7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516)", + "bitcoin_hashes 1.2.0 (git+https://github.com/rust-bitcoin/rust-bitcoin?rev=7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516)", ] [[package]] @@ -795,15 +829,6 @@ version = "0.23.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac07cdecf99051d9a5238b80f35af32cdeba5b336e55d957b318b50137e18da5" -[[package]] -name = "base64-compat" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a8d4d2746f89841e49230dd26917df1876050f95abafafbe34f47cb534b88d7" -dependencies = [ - "byteorder", -] - [[package]] name = "base64ct" version = "1.8.3" @@ -896,7 +921,7 @@ version = "2.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90dbd31c98227229239363921e60fcf5e558e43ec69094d46fc4996f08d1d5bc" dependencies = [ - "bitcoin_hashes", + "bitcoin_hashes 0.14.101", "rand 0.8.8", "rand_core 0.6.4", "serde", @@ -937,27 +962,130 @@ checksum = "b71798fca2c1fe1086445a7258a4bc81e6e49dcd24c8d0dd9a1e57395b603f51" [[package]] name = "bitcoin-consensus-encoding" version = "1.2.0" +source = "git+https://github.com/rust-bitcoin/rust-bitcoin?rev=7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516#7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516" +dependencies = [ + "bitcoin-internals 0.7.0 (git+https://github.com/rust-bitcoin/rust-bitcoin?rev=7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516)", + "hex-conservative 1.3.0", + "serde", +] + +[[package]] +name = "bitcoin-consensus-encoding" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6712f9c6fd6785b3b270884e57c441c403dc5d7e19ca45368c97c7a1de3000ec" +checksum = "9daa31138eb443d5751b207f3f64154e2bb09cd59960562ccc7a7112be38147f" dependencies = [ - "bitcoin-internals", + "bitcoin-internals 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "hex-conservative 1.3.0", "serde", ] +[[package]] +name = "bitcoin-crypto" +version = "0.3.0" +source = "git+https://github.com/rust-bitcoin/rust-bitcoin?rev=7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516#7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516" +dependencies = [ + "base58ck 0.5.0 (git+https://github.com/rust-bitcoin/rust-bitcoin?rev=7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516)", + "bitcoin-internals 0.7.0 (git+https://github.com/rust-bitcoin/rust-bitcoin?rev=7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516)", + "bitcoin-network-kind", + "bitcoin-primitives 0.103.1", + "bitcoin_hashes 1.2.0 (git+https://github.com/rust-bitcoin/rust-bitcoin?rev=7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516)", + "hex-conservative 1.3.0", + "secp256k1", + "serde", +] + +[[package]] +name = "bitcoin-internals" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a30a22d1f112dde8e16be7b45c63645dc165cef254f835b3e1e9553e485cfa64" +dependencies = [ + "hex-conservative 0.3.2", +] + [[package]] name = "bitcoin-internals" version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d573f4cf32996a8dce612e4348cece65a241f1882ed594047c9ba348e8869fa5" +[[package]] +name = "bitcoin-internals" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8bea3a9f0cfece4564e37cb49a38cc245ca5184719e50d7d0dda3268722c4e2" + +[[package]] +name = "bitcoin-internals" +version = "0.7.0" +source = "git+https://github.com/rust-bitcoin/rust-bitcoin?rev=7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516#7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516" +dependencies = [ + "serde", +] + [[package]] name = "bitcoin-io" version = "0.1.101" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb5de036369d1ac59d3c1819ebc4d850f89466f5401c571a285b6ed564a4cb78" dependencies = [ - "bitcoin-consensus-encoding", + "bitcoin-consensus-encoding 1.3.0", +] + +[[package]] +name = "bitcoin-network-kind" +version = "1.0.0" +source = "git+https://github.com/rust-bitcoin/rust-bitcoin?rev=7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516#7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516" +dependencies = [ + "bitcoin-internals 0.7.0 (git+https://github.com/rust-bitcoin/rust-bitcoin?rev=7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516)", + "serde", +] + +[[package]] +name = "bitcoin-primitives" +version = "0.103.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82e12cb1814d037edbd667554c2c0e6b067db72c83ee935b645a6b5829aca908" +dependencies = [ + "bitcoin-consensus-encoding 1.3.0", + "bitcoin-internals 0.6.0", + "bitcoin-units 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "bitcoin_hashes 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "hex-conservative 1.3.0", +] + +[[package]] +name = "bitcoin-primitives" +version = "0.103.1" +source = "git+https://github.com/rust-bitcoin/rust-bitcoin?rev=7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516#7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516" +dependencies = [ + "bitcoin-consensus-encoding 1.2.0", + "bitcoin-internals 0.7.0 (git+https://github.com/rust-bitcoin/rust-bitcoin?rev=7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516)", + "bitcoin-units 0.5.0 (git+https://github.com/rust-bitcoin/rust-bitcoin?rev=7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516)", + "bitcoin_hashes 1.2.0 (git+https://github.com/rust-bitcoin/rust-bitcoin?rev=7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516)", + "hex-conservative 1.3.0", +] + +[[package]] +name = "bitcoin-units" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ae6dcb008158c2bdbe698470280439844be5f640310e4f5d6ef0a0209e066c2" +dependencies = [ + "bitcoin-consensus-encoding 1.3.0", + "bitcoin-internals 0.5.0", + "serde", +] + +[[package]] +name = "bitcoin-units" +version = "0.5.0" +source = "git+https://github.com/rust-bitcoin/rust-bitcoin?rev=7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516#7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516" +dependencies = [ + "bitcoin-consensus-encoding 1.2.0", + "bitcoin-internals 0.7.0 (git+https://github.com/rust-bitcoin/rust-bitcoin?rev=7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516)", + "serde", ] [[package]] @@ -968,6 +1096,29 @@ checksum = "bca4c7abb40c8817d77403c880988cfd484f23ab2365726afb2f798363e2c4a2" dependencies = [ "bitcoin-io", "hex-conservative 0.2.3", + "serde", +] + +[[package]] +name = "bitcoin_hashes" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5304e53726dbe5f93141535e102ed97b5bf4714fbecefdda8f9fb98d7fdaff0e" +dependencies = [ + "bitcoin-consensus-encoding 1.3.0", + "bitcoin-internals 0.6.0", + "hex-conservative 1.3.0", +] + +[[package]] +name = "bitcoin_hashes" +version = "1.2.0" +source = "git+https://github.com/rust-bitcoin/rust-bitcoin?rev=7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516#7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516" +dependencies = [ + "bitcoin-consensus-encoding 1.2.0", + "bitcoin-internals 0.7.0 (git+https://github.com/rust-bitcoin/rust-bitcoin?rev=7ba35c7c1dd63892bfb9c9395bbd80e2ab5df516)", + "hex-conservative 1.3.0", + "serde", ] [[package]] @@ -1044,7 +1195,7 @@ version = "0.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" dependencies = [ - "generic-array 0.14.7", + "generic-array", ] [[package]] @@ -1062,7 +1213,7 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8894febbff9f758034a5b8e12d87918f56dfc64a8e1fe757d65e29041538d93" dependencies = [ - "generic-array 0.14.7", + "generic-array", ] [[package]] @@ -1111,41 +1262,16 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d7bc6d6292be3a19e6379786dac800f551e5865a5bb51ebbe3064ab80433f403" dependencies = [ - "ff", + "ff 0.13.1", "rand_core 0.6.4", "subtle", ] -[[package]] -name = "blsful" -version = "3.0.0" -source = "git+https://github.com/dashpay/agora-blsful?rev=0c34a7a488a0bd1c9a9a2196e793b303ad35c900#0c34a7a488a0bd1c9a9a2196e793b303ad35c900" -dependencies = [ - "anyhow", - "blstrs_plus", - "hex", - "hkdf", - "merlin", - "pairing", - "rand 0.8.8", - "rand_chacha", - "rand_core 0.6.4", - "serde", - "serde_bare", - "sha2 0.10.9", - "sha3", - "subtle", - "thiserror 2.0.20", - "uint-zigzag", - "vsss-rs", - "zeroize", -] - [[package]] name = "blst" -version = "0.3.12" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62dc83a094a71d43eeadd254b1ec2d24cb6a0bb6cadce00df51f0db594711a32" +checksum = "c20659f9bbee16cbbd2f7393e40ab6309f5a98f76a2eb57a995ec508b72387fe" dependencies = [ "cc", "glob", @@ -1153,24 +1279,6 @@ dependencies = [ "zeroize", ] -[[package]] -name = "blstrs_plus" -version = "0.8.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a16dd4b0d6b4538e1fa0388843acb186363082713a8fc8416d802a04d013818" -dependencies = [ - "arrayref", - "blst", - "elliptic-curve", - "ff", - "group", - "pairing", - "rand_core 0.6.4", - "serde", - "subtle", - "zeroize", -] - [[package]] name = "bs58" version = "0.5.1" @@ -1845,26 +1953,13 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" -[[package]] -name = "crypto-bigint" -version = "0.5.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0dc92fb57ca44df6db8059111ab3af99a63d5d0f8375d9972e319a379c6bab76" -dependencies = [ - "generic-array 0.14.7", - "rand_core 0.6.4", - "serdect", - "subtle", - "zeroize", -] - [[package]] name = "crypto-common" version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" dependencies = [ - "generic-array 0.14.7", + "generic-array", "rand_core 0.6.4", "typenum", ] @@ -1931,6 +2026,7 @@ dependencies = [ "curve25519-dalek-derive", "digest 0.11.3", "fiat-crypto 0.3.0", + "rand_core 0.10.1", "rustc_version", "subtle", "zeroize", @@ -1949,14 +2045,15 @@ dependencies = [ [[package]] name = "dapi-grpc" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "dash-platform-macros", "futures-core", "getrandom 0.2.17", "platform-version", "prost", + "prost-build", "serde", "serde_bytes", "serde_json", @@ -2054,8 +2151,8 @@ dependencies = [ [[package]] name = "dash-async" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "futures", "thiserror 2.0.20", @@ -2066,8 +2163,8 @@ dependencies = [ [[package]] name = "dash-context-provider" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "dash-async", "dpp", @@ -2160,8 +2257,8 @@ dependencies = [ [[package]] name = "dash-network" -version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +version = "0.46.0" +source = "git+https://github.com/dashpay/rust-dashcore?rev=aca020263990a86a15cc1d97ae2cbf965d488db5#aca020263990a86a15cc1d97ae2cbf965d488db5" dependencies = [ "cbindgen", "grovedb-bincode", @@ -2171,16 +2268,86 @@ dependencies = [ [[package]] name = "dash-network-seeds" -version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +version = "0.46.0" +source = "git+https://github.com/dashpay/rust-dashcore?rev=aca020263990a86a15cc1d97ae2cbf965d488db5#aca020263990a86a15cc1d97ae2cbf965d488db5" dependencies = [ "dash-network", ] +[[package]] +name = "dash-num" +version = "0.1.0-beta" +source = "git+https://github.com/dashpay/base-sdk?rev=33fcc59e1868c803f9608e0e78ee8bddf3abcee7#33fcc59e1868c803f9608e0e78ee8bddf3abcee7" +dependencies = [ + "ambassador", + "bitcoin-internals 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "dash-types 0.1.0-beta (git+https://github.com/dashpay/base-sdk?rev=33fcc59e1868c803f9608e0e78ee8bddf3abcee7)", + "delegate", + "hex-conservative 1.3.0", +] + +[[package]] +name = "dash-num" +version = "0.1.0-beta" +source = "git+https://github.com/dashpay/base-sdk?rev=d9e0d241e2e06edff455f88c9cb3b9ce0d8a8ac5#d9e0d241e2e06edff455f88c9cb3b9ce0d8a8ac5" +dependencies = [ + "ambassador", + "bitcoin-internals 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "dash-types 0.1.0-beta (git+https://github.com/dashpay/base-sdk?rev=d9e0d241e2e06edff455f88c9cb3b9ce0d8a8ac5)", + "delegate", + "hex-conservative 1.3.0", + "serde", +] + +[[package]] +name = "dash-pkc" +version = "0.1.0-beta" +source = "git+https://github.com/dashpay/base-sdk?rev=33fcc59e1868c803f9608e0e78ee8bddf3abcee7#33fcc59e1868c803f9608e0e78ee8bddf3abcee7" +dependencies = [ + "aes 0.9.3", + "bitcoin_hashes 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "blst", + "cfg-if", + "dash-num 0.1.0-beta (git+https://github.com/dashpay/base-sdk?rev=33fcc59e1868c803f9608e0e78ee8bddf3abcee7)", + "dash-types 0.1.0-beta (git+https://github.com/dashpay/base-sdk?rev=33fcc59e1868c803f9608e0e78ee8bddf3abcee7)", + "ff 0.14.0", + "group 0.14.0", + "hex-conservative 1.3.0", + "rand_core 0.10.1", + "sha2 0.11.0", + "subtle", + "zeroize", +] + +[[package]] +name = "dash-pkc" +version = "0.1.0-beta" +source = "git+https://github.com/dashpay/base-sdk?rev=d9e0d241e2e06edff455f88c9cb3b9ce0d8a8ac5#d9e0d241e2e06edff455f88c9cb3b9ce0d8a8ac5" +dependencies = [ + "aes 0.9.3", + "base58ck 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "bitcoin_hashes 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "blst", + "cfg-if", + "dash-num 0.1.0-beta (git+https://github.com/dashpay/base-sdk?rev=d9e0d241e2e06edff455f88c9cb3b9ce0d8a8ac5)", + "dash-types 0.1.0-beta (git+https://github.com/dashpay/base-sdk?rev=d9e0d241e2e06edff455f88c9cb3b9ce0d8a8ac5)", + "ed25519-dalek 3.0.0", + "ff 0.14.0", + "group 0.14.0", + "hex-conservative 1.3.0", + "rand_core 0.10.1", + "secp256k1", + "serde", + "sha2 0.11.0", + "subtle", + "thiserror 2.0.20", + "zeroize", +] + [[package]] name = "dash-platform-macros" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "heck", "quote", @@ -2189,8 +2356,8 @@ dependencies = [ [[package]] name = "dash-platform-queries" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "dapi-grpc", "dash-context-provider", @@ -2207,8 +2374,8 @@ dependencies = [ [[package]] name = "dash-sdk" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "arc-swap", "async-trait", @@ -2246,8 +2413,8 @@ dependencies = [ [[package]] name = "dash-spv" -version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +version = "0.46.0" +source = "git+https://github.com/dashpay/rust-dashcore?rev=aca020263990a86a15cc1d97ae2cbf965d488db5#aca020263990a86a15cc1d97ae2cbf965d488db5" dependencies = [ "async-trait", "chrono", @@ -2257,10 +2424,10 @@ dependencies = [ "dashcore_hashes", "futures", "git-state", - "hex", + "hex-conservative 1.3.0", "key-wallet", "key-wallet-manager", - "rand 0.8.8", + "rand 0.9.5", "rayon", "serde", "serde_json", @@ -2273,44 +2440,90 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "dash-types" +version = "0.1.0-beta" +source = "git+https://github.com/dashpay/base-sdk?rev=33fcc59e1868c803f9608e0e78ee8bddf3abcee7#33fcc59e1868c803f9608e0e78ee8bddf3abcee7" +dependencies = [ + "cfg-if", + "hex-conservative 1.3.0", + "subtle", + "zeroize", +] + +[[package]] +name = "dash-types" +version = "0.1.0-beta" +source = "git+https://github.com/dashpay/base-sdk?rev=d9e0d241e2e06edff455f88c9cb3b9ce0d8a8ac5#d9e0d241e2e06edff455f88c9cb3b9ce0d8a8ac5" +dependencies = [ + "bitcoin-consensus-encoding 1.3.0", + "bitcoin-primitives 0.103.0", + "cfg-if", + "hex-conservative 1.3.0", + "serde", + "subtle", + "thiserror 2.0.20", + "zeroize", +] + [[package]] name = "dashcore" -version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +version = "0.46.0" +source = "git+https://github.com/dashpay/rust-dashcore?rev=aca020263990a86a15cc1d97ae2cbf965d488db5#aca020263990a86a15cc1d97ae2cbf965d488db5" dependencies = [ "anyhow", - "base64-compat", + "base64 0.22.1", "bech32 0.9.1", "bitvec", "blake3", - "blsful", "dash-network", + "dash-num 0.1.0-beta (git+https://github.com/dashpay/base-sdk?rev=d9e0d241e2e06edff455f88c9cb3b9ce0d8a8ac5)", + "dash-types 0.1.0-beta (git+https://github.com/dashpay/base-sdk?rev=d9e0d241e2e06edff455f88c9cb3b9ce0d8a8ac5)", + "dashcore-crypto", "dashcore-private", "dashcore_hashes", - "ed25519-dalek 2.2.0", "grovedb-bincode", "grovedb-bincode-derive", - "hex", - "hex_lit", - "rustversion", - "secp256k1", + "hex-conservative 1.3.0", "serde", "thiserror 2.0.20", "tracing", ] +[[package]] +name = "dashcore-crypto" +version = "0.46.0" +source = "git+https://github.com/dashpay/rust-dashcore?rev=aca020263990a86a15cc1d97ae2cbf965d488db5#aca020263990a86a15cc1d97ae2cbf965d488db5" +dependencies = [ + "base58ck 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "bitcoin-crypto", + "dash-network", + "dash-pkc 0.1.0-beta (git+https://github.com/dashpay/base-sdk?rev=d9e0d241e2e06edff455f88c9cb3b9ce0d8a8ac5)", + "dash-types 0.1.0-beta (git+https://github.com/dashpay/base-sdk?rev=d9e0d241e2e06edff455f88c9cb3b9ce0d8a8ac5)", + "dashcore-private", + "dashcore_hashes", + "delegate", + "grovedb-bincode", + "grovedb-bincode-derive", + "hex-conservative 1.3.0", + "serde", + "thiserror 2.0.20", + "tracing", + "zeroize", +] + [[package]] name = "dashcore-private" -version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +version = "0.46.0" +source = "git+https://github.com/dashpay/rust-dashcore?rev=aca020263990a86a15cc1d97ae2cbf965d488db5#aca020263990a86a15cc1d97ae2cbf965d488db5" [[package]] name = "dashcore-rpc" -version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +version = "0.46.0" +source = "git+https://github.com/dashpay/rust-dashcore?rev=aca020263990a86a15cc1d97ae2cbf965d488db5#aca020263990a86a15cc1d97ae2cbf965d488db5" dependencies = [ "dashcore-rpc-json", - "hex", + "hex-conservative 1.3.0", "jsonrpc", "serde", "serde_json", @@ -2319,12 +2532,12 @@ dependencies = [ [[package]] name = "dashcore-rpc-json" -version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +version = "0.46.0" +source = "git+https://github.com/dashpay/rust-dashcore?rev=aca020263990a86a15cc1d97ae2cbf965d488db5#aca020263990a86a15cc1d97ae2cbf965d488db5" dependencies = [ "dashcore", "grovedb-bincode", - "hex", + "hex-conservative 1.3.0", "key-wallet", "serde", "serde_json", @@ -2334,19 +2547,20 @@ dependencies = [ [[package]] name = "dashcore_hashes" -version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +version = "0.46.0" +source = "git+https://github.com/dashpay/rust-dashcore?rev=aca020263990a86a15cc1d97ae2cbf965d488db5#aca020263990a86a15cc1d97ae2cbf965d488db5" dependencies = [ - "dashcore-private", + "bitcoin_hashes 0.14.101", "grovedb-bincode", + "hex-conservative 1.3.0", "rs-x11-hash", "serde", ] [[package]] name = "dashpay-contract" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "platform-value", "platform-version", @@ -2356,14 +2570,16 @@ dependencies = [ [[package]] name = "data-contracts" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ + "app-connect-contract", "dashpay-contract", "document-history-contract", "dpns-contract", "keyword-search-contract", "masternode-reward-shares-contract", + "moderation-charters-contract", "platform-value", "platform-version", "serde_json", @@ -2450,6 +2666,17 @@ dependencies = [ "thiserror 2.0.20", ] +[[package]] +name = "delegate" +version = "0.13.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "780eb241654bf097afb00fc5f054a09b687dad862e485fdcf8399bb056565370" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "der" version = "0.7.10" @@ -2606,7 +2833,7 @@ dependencies = [ "libc", "option-ext", "redox_users", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -2658,8 +2885,8 @@ dependencies = [ [[package]] name = "document-history-contract" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "platform-value", "platform-version", @@ -2687,8 +2914,8 @@ checksum = "d8b14ccef22fc6f5a8f4d7d768562a182c04ce9a3b3157b91390b52ddfdf1a76" [[package]] name = "dpns-contract" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "platform-value", "platform-version", @@ -2698,26 +2925,31 @@ dependencies = [ [[package]] name = "dpp" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "anyhow", "async-trait", "base64 0.22.1", "bech32 0.11.1", + "blst", "bs58", "byteorder", "chrono", "chrono-tz", "ciborium", + "dash-pkc 0.1.0-beta (git+https://github.com/dashpay/base-sdk?rev=33fcc59e1868c803f9608e0e78ee8bddf3abcee7)", "dash-spv", "dashcore", "dashcore-rpc", "data-contracts", "derive_more 1.0.0", "dpp-json-convertible-derive", + "ed25519-dalek 2.2.0", "env_logger", "getrandom 0.2.17", + "getrandom 0.3.4", + "getrandom 0.4.3", "grovedb-bincode", "grovedb-commitment-tree", "hex", @@ -2738,6 +2970,7 @@ dependencies = [ "platform-versioning", "rand 0.8.8", "regex", + "secp256k1", "serde", "serde_json", "serde_repr", @@ -2745,12 +2978,13 @@ dependencies = [ "strum", "thiserror 2.0.20", "tracing", + "zeroize", ] [[package]] name = "dpp-json-convertible-derive" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "proc-macro2", "quote", @@ -2759,19 +2993,20 @@ dependencies = [ [[package]] name = "drive" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "byteorder", "derive_more 1.0.0", "dpp", - "grovedb 6.0.1", + "grovedb 6.1.1", "grovedb-bincode", - "grovedb-costs 6.0.1", + "grovedb-costs 6.1.1", "grovedb-epoch-based-storage-flags", - "grovedb-path 6.0.1", + "grovedb-merk 6.1.1", + "grovedb-path 6.1.1", "grovedb-query", - "grovedb-version 6.0.1", + "grovedb-version 6.1.1", "hex", "indexmap 2.14.2", "integer-encoding", @@ -2785,8 +3020,8 @@ dependencies = [ [[package]] name = "drive-proof-verifier" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "dapi-grpc", "dash-context-provider", @@ -2892,7 +3127,9 @@ checksum = "6ebaa1a2bf1290ab3bfe5a7b771d050ebffab2711c19a81691c683a5144a25de" dependencies = [ "curve25519-dalek 5.0.0", "ed25519 3.0.0", + "rand_core 0.10.1", "sha2 0.11.0", + "signature 3.0.0", "subtle", "zeroize", ] @@ -3072,41 +3309,6 @@ version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "252afb9ae5eaa683babdc6a068b3f5726eb19e05070c731f9b2a23a7c3e8ed34" -[[package]] -name = "elliptic-curve" -version = "0.13.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5e6043086bf7973472e0c7dff2142ea0b680d30e18d9cc40f267efbf222bd47" -dependencies = [ - "base16ct", - "crypto-bigint", - "digest 0.10.7", - "ff", - "generic-array 0.14.7", - "group", - "hkdf", - "pkcs8", - "rand_core 0.6.4", - "sec1", - "subtle", - "tap", - "zeroize", -] - -[[package]] -name = "elliptic-curve-tools" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1de2b6fae800f08032a6ea32995b52925b1d451bff9d445c8ab2932323277faf" -dependencies = [ - "elliptic-curve", - "heapless 0.8.0", - "hex", - "multiexp", - "serde", - "zeroize", -] - [[package]] name = "emath" version = "0.36.2" @@ -3305,7 +3507,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -3415,6 +3617,16 @@ dependencies = [ "subtle", ] +[[package]] +name = "ff" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1f686ab92a9fb0eaf188f6c6c87b89490baa6fdb0db4544ba4dc47f7942489f" +dependencies = [ + "rand_core 0.10.1", + "subtle", +] + [[package]] name = "fiat-crypto" version = "0.2.9" @@ -3733,18 +3945,6 @@ checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" dependencies = [ "typenum", "version_check", - "zeroize", -] - -[[package]] -name = "generic-array" -version = "1.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "337d46834ee672ab3e48caca2cb0c78cc174fb12b3a68d0d88f99a0519a5e36e" -dependencies = [ - "rustversion", - "serde_core", - "typenum", ] [[package]] @@ -3777,9 +3977,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" dependencies = [ "cfg-if", + "js-sys", "libc", "r-efi 5.3.0", "wasip2", + "wasm-bindgen", ] [[package]] @@ -3828,8 +4030,8 @@ dependencies = [ [[package]] name = "git-state" -version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +version = "0.46.0" +source = "git+https://github.com/dashpay/rust-dashcore?rev=aca020263990a86a15cc1d97ae2cbf965d488db5#aca020263990a86a15cc1d97ae2cbf965d488db5" [[package]] name = "gl_generator" @@ -3974,11 +4176,20 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0f9ef7462f7c099f518d754361858f86d8a07af53ba9af0fe635bbccb151a63" dependencies = [ - "ff", + "ff 0.13.1", "memuse", - "rand 0.8.8", "rand_core 0.6.4", - "rand_xorshift", + "subtle", +] + +[[package]] +name = "group" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fd1a1c7a5206c5b7a3f5a0d7ccd3ff85d0c8f5133d62a02680255b0004af5f4" +dependencies = [ + "ff 0.14.0", + "rand_core 0.10.1", "subtle", ] @@ -4006,21 +4217,21 @@ dependencies = [ [[package]] name = "grovedb" -version = "6.0.1" -source = "git+https://github.com/dashpay/grovedb?rev=74818ceb95805f56f141d24ca41f58b75c031a7f#74818ceb95805f56f141d24ca41f58b75c031a7f" +version = "6.1.1" +source = "git+https://github.com/dashpay/grovedb?rev=2128ea18f9b1c2f9502f99967b1b1c6485c91f8b#2128ea18f9b1c2f9502f99967b1b1c6485c91f8b" dependencies = [ "blake3", "grovedb-bincode", "grovedb-bincode-derive", "grovedb-bulk-append-tree", - "grovedb-costs 6.0.1", + "grovedb-costs 6.1.1", "grovedb-dense-fixed-sized-merkle-tree", - "grovedb-element 6.0.1", - "grovedb-merk 6.0.1", + "grovedb-element 6.1.1", + "grovedb-merk 6.1.1", "grovedb-merkle-mountain-range", - "grovedb-path 6.0.1", + "grovedb-path 6.1.1", "grovedb-query", - "grovedb-version 6.0.1", + "grovedb-version 6.1.1", "hex", "indexmap 2.14.2", "integer-encoding", @@ -4049,16 +4260,16 @@ dependencies = [ [[package]] name = "grovedb-bulk-append-tree" -version = "6.0.1" -source = "git+https://github.com/dashpay/grovedb?rev=74818ceb95805f56f141d24ca41f58b75c031a7f#74818ceb95805f56f141d24ca41f58b75c031a7f" +version = "6.1.1" +source = "git+https://github.com/dashpay/grovedb?rev=2128ea18f9b1c2f9502f99967b1b1c6485c91f8b#2128ea18f9b1c2f9502f99967b1b1c6485c91f8b" dependencies = [ "blake3", "grovedb-bincode", - "grovedb-costs 6.0.1", + "grovedb-costs 6.1.1", "grovedb-dense-fixed-sized-merkle-tree", "grovedb-merkle-mountain-range", "grovedb-query", - "grovedb-version 6.0.1", + "grovedb-version 6.1.1", "hex", "integer-encoding", "thiserror 2.0.20", @@ -4066,13 +4277,13 @@ dependencies = [ [[package]] name = "grovedb-commitment-tree" -version = "6.0.1" -source = "git+https://github.com/dashpay/grovedb?rev=74818ceb95805f56f141d24ca41f58b75c031a7f#74818ceb95805f56f141d24ca41f58b75c031a7f" +version = "6.1.1" +source = "git+https://github.com/dashpay/grovedb?rev=2128ea18f9b1c2f9502f99967b1b1c6485c91f8b#2128ea18f9b1c2f9502f99967b1b1c6485c91f8b" dependencies = [ "blake3", "grovedb-bulk-append-tree", - "grovedb-costs 6.0.1", - "grovedb-version 6.0.1", + "grovedb-costs 6.1.1", + "grovedb-version 6.1.1", "incrementalmerkletree", "orchard", "rusqlite", @@ -4092,8 +4303,8 @@ dependencies = [ [[package]] name = "grovedb-costs" -version = "6.0.1" -source = "git+https://github.com/dashpay/grovedb?rev=74818ceb95805f56f141d24ca41f58b75c031a7f#74818ceb95805f56f141d24ca41f58b75c031a7f" +version = "6.1.1" +source = "git+https://github.com/dashpay/grovedb?rev=2128ea18f9b1c2f9502f99967b1b1c6485c91f8b#2128ea18f9b1c2f9502f99967b1b1c6485c91f8b" dependencies = [ "integer-encoding", "intmap", @@ -4102,14 +4313,14 @@ dependencies = [ [[package]] name = "grovedb-dense-fixed-sized-merkle-tree" -version = "6.0.1" -source = "git+https://github.com/dashpay/grovedb?rev=74818ceb95805f56f141d24ca41f58b75c031a7f#74818ceb95805f56f141d24ca41f58b75c031a7f" +version = "6.1.1" +source = "git+https://github.com/dashpay/grovedb?rev=2128ea18f9b1c2f9502f99967b1b1c6485c91f8b#2128ea18f9b1c2f9502f99967b1b1c6485c91f8b" dependencies = [ "blake3", "grovedb-bincode", - "grovedb-costs 6.0.1", + "grovedb-costs 6.1.1", "grovedb-query", - "grovedb-version 6.0.1", + "grovedb-version 6.1.1", "thiserror 2.0.20", ] @@ -4129,14 +4340,14 @@ dependencies = [ [[package]] name = "grovedb-element" -version = "6.0.1" -source = "git+https://github.com/dashpay/grovedb?rev=74818ceb95805f56f141d24ca41f58b75c031a7f#74818ceb95805f56f141d24ca41f58b75c031a7f" +version = "6.1.1" +source = "git+https://github.com/dashpay/grovedb?rev=2128ea18f9b1c2f9502f99967b1b1c6485c91f8b#2128ea18f9b1c2f9502f99967b1b1c6485c91f8b" dependencies = [ "grovedb-bincode", "grovedb-bincode-derive", - "grovedb-path 6.0.1", + "grovedb-path 6.1.1", "grovedb-query", - "grovedb-version 6.0.1", + "grovedb-version 6.1.1", "hex", "integer-encoding", "thiserror 2.0.20", @@ -4144,10 +4355,10 @@ dependencies = [ [[package]] name = "grovedb-epoch-based-storage-flags" -version = "6.0.1" -source = "git+https://github.com/dashpay/grovedb?rev=74818ceb95805f56f141d24ca41f58b75c031a7f#74818ceb95805f56f141d24ca41f58b75c031a7f" +version = "6.1.1" +source = "git+https://github.com/dashpay/grovedb?rev=2128ea18f9b1c2f9502f99967b1b1c6485c91f8b#2128ea18f9b1c2f9502f99967b1b1c6485c91f8b" dependencies = [ - "grovedb-costs 6.0.1", + "grovedb-costs 6.1.1", "hex", "integer-encoding", "intmap", @@ -4177,20 +4388,20 @@ dependencies = [ [[package]] name = "grovedb-merk" -version = "6.0.1" -source = "git+https://github.com/dashpay/grovedb?rev=74818ceb95805f56f141d24ca41f58b75c031a7f#74818ceb95805f56f141d24ca41f58b75c031a7f" +version = "6.1.1" +source = "git+https://github.com/dashpay/grovedb?rev=2128ea18f9b1c2f9502f99967b1b1c6485c91f8b#2128ea18f9b1c2f9502f99967b1b1c6485c91f8b" dependencies = [ "blake3", "byteorder", "ed", "grovedb-bincode", "grovedb-bincode-derive", - "grovedb-costs 6.0.1", - "grovedb-element 6.0.1", - "grovedb-path 6.0.1", + "grovedb-costs 6.1.1", + "grovedb-element 6.1.1", + "grovedb-path 6.1.1", "grovedb-query", - "grovedb-version 6.0.1", - "grovedb-visualize 6.0.1", + "grovedb-version 6.1.1", + "grovedb-visualize 6.1.1", "hex", "indexmap 2.14.2", "integer-encoding", @@ -4199,13 +4410,13 @@ dependencies = [ [[package]] name = "grovedb-merkle-mountain-range" -version = "6.0.1" -source = "git+https://github.com/dashpay/grovedb?rev=74818ceb95805f56f141d24ca41f58b75c031a7f#74818ceb95805f56f141d24ca41f58b75c031a7f" +version = "6.1.1" +source = "git+https://github.com/dashpay/grovedb?rev=2128ea18f9b1c2f9502f99967b1b1c6485c91f8b#2128ea18f9b1c2f9502f99967b1b1c6485c91f8b" dependencies = [ "blake3", "grovedb-bincode", - "grovedb-costs 6.0.1", - "grovedb-version 6.0.1", + "grovedb-costs 6.1.1", + "grovedb-version 6.1.1", ] [[package]] @@ -4218,16 +4429,16 @@ dependencies = [ [[package]] name = "grovedb-path" -version = "6.0.1" -source = "git+https://github.com/dashpay/grovedb?rev=74818ceb95805f56f141d24ca41f58b75c031a7f#74818ceb95805f56f141d24ca41f58b75c031a7f" +version = "6.1.1" +source = "git+https://github.com/dashpay/grovedb?rev=2128ea18f9b1c2f9502f99967b1b1c6485c91f8b#2128ea18f9b1c2f9502f99967b1b1c6485c91f8b" dependencies = [ "hex", ] [[package]] name = "grovedb-query" -version = "6.0.1" -source = "git+https://github.com/dashpay/grovedb?rev=74818ceb95805f56f141d24ca41f58b75c031a7f#74818ceb95805f56f141d24ca41f58b75c031a7f" +version = "6.1.1" +source = "git+https://github.com/dashpay/grovedb?rev=2128ea18f9b1c2f9502f99967b1b1c6485c91f8b#2128ea18f9b1c2f9502f99967b1b1c6485c91f8b" dependencies = [ "byteorder", "ed", @@ -4249,8 +4460,8 @@ dependencies = [ [[package]] name = "grovedb-version" -version = "6.0.1" -source = "git+https://github.com/dashpay/grovedb?rev=74818ceb95805f56f141d24ca41f58b75c031a7f#74818ceb95805f56f141d24ca41f58b75c031a7f" +version = "6.1.1" +source = "git+https://github.com/dashpay/grovedb?rev=2128ea18f9b1c2f9502f99967b1b1c6485c91f8b#2128ea18f9b1c2f9502f99967b1b1c6485c91f8b" dependencies = [ "thiserror 2.0.20", "versioned-feature-core", @@ -4267,8 +4478,8 @@ dependencies = [ [[package]] name = "grovedb-visualize" -version = "6.0.1" -source = "git+https://github.com/dashpay/grovedb?rev=74818ceb95805f56f141d24ca41f58b75c031a7f#74818ceb95805f56f141d24ca41f58b75c031a7f" +version = "6.1.1" +source = "git+https://github.com/dashpay/grovedb?rev=2128ea18f9b1c2f9502f99967b1b1c6485c91f8b#2128ea18f9b1c2f9502f99967b1b1c6485c91f8b" dependencies = [ "hex", "itertools 0.14.0", @@ -4359,8 +4570,8 @@ checksum = "fb2a697cad929f706b7987fe804ad57d43622cd37463ba7e4d662a926fdcfea3" dependencies = [ "arrayvec", "bitvec", - "ff", - "group", + "ff 0.13.1", + "group 0.13.0", "halo2_poseidon", "halo2_proofs", "lazy_static", @@ -4384,8 +4595,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fa3da60b81f02f9b33ebc6252d766f843291fb4d2247a07ae73d20b791fc56f" dependencies = [ "bitvec", - "ff", - "group", + "ff 0.13.1", + "group 0.13.0", "pasta_curves", ] @@ -4396,8 +4607,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f5aca1c66059a919227dec97444a11a4350d2f9c820ca48690988f0aa0e81cbf" dependencies = [ "blake2b_simd", - "ff", - "group", + "ff 0.13.1", + "group 0.13.0", "halo2_legacy_pdqsort", "indexmap 1.9.3", "maybe-rayon", @@ -4427,15 +4638,6 @@ dependencies = [ "byteorder", ] -[[package]] -name = "hash32" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "47d60b12902ba28e2730cd37e95b8c9223af2808df9e902d4df49588d1470606" -dependencies = [ - "byteorder", -] - [[package]] name = "hashbrown" version = "0.12.3" @@ -4487,20 +4689,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdc6457c0eb62c71aac4bc17216026d8410337c4126773b9c5daba343f17964f" dependencies = [ "atomic-polyfill", - "hash32 0.2.1", + "hash32", "rustc_version", "serde", - "spin 0.9.9", - "stable_deref_trait", -] - -[[package]] -name = "heapless" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad" -dependencies = [ - "hash32 0.3.1", + "spin", "stable_deref_trait", ] @@ -4521,9 +4713,6 @@ name = "hex" version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" -dependencies = [ - "serde", -] [[package]] name = "hex-conservative" @@ -4534,6 +4723,15 @@ dependencies = [ "arrayvec", ] +[[package]] +name = "hex-conservative" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830e599c2904b08f0834ee6337d8fe8f0ed4a63b5d9e7a7f49c0ffa06d08d360" +dependencies = [ + "arrayvec", +] + [[package]] name = "hex-conservative" version = "1.3.0" @@ -4549,12 +4747,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e712f64ec3850b98572bffac52e2c6f282b29fe6c5fa6d42334b30be438d95c1" -[[package]] -name = "hex_lit" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3011d1213f159867b13cfd6ac92d2cd5f1345762c63be3554e84092d85a50bbd" - [[package]] name = "hkdf" version = "0.12.4" @@ -4951,7 +5143,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01" dependencies = [ "block-padding 0.3.3", - "generic-array 0.14.7", + "generic-array", ] [[package]] @@ -5168,8 +5360,8 @@ checksum = "8499f7a74008aafbecb2a2e608a3e13e4dd3e84df198b604451efe93f2de6e61" dependencies = [ "bitvec", "bls12_381", - "ff", - "group", + "ff 0.13.1", + "group 0.13.0", "rand_core 0.6.4", "subtle", ] @@ -5185,22 +5377,19 @@ dependencies = [ [[package]] name = "key-wallet" -version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +version = "0.46.0" +source = "git+https://github.com/dashpay/rust-dashcore?rev=aca020263990a86a15cc1d97ae2cbf965d488db5#aca020263990a86a15cc1d97ae2cbf965d488db5" dependencies = [ "async-trait", - "base58ck", "bip39", "bitflags 2.13.2", "dashcore", "dashcore-private", "dashcore_hashes", - "getrandom 0.2.17", - "hex", - "secp256k1", + "getrandom 0.4.3", + "hex-conservative 1.3.0", "serde", "serde_json", - "sha2 0.10.9", "tracing", "unicode-normalization", "zeroize", @@ -5208,11 +5397,12 @@ dependencies = [ [[package]] name = "key-wallet-manager" -version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +version = "0.46.0" +source = "git+https://github.com/dashpay/rust-dashcore?rev=aca020263990a86a15cc1d97ae2cbf965d488db5#aca020263990a86a15cc1d97ae2cbf965d488db5" dependencies = [ "async-trait", "dashcore", + "hex-conservative 1.3.0", "key-wallet", "rayon", "serde", @@ -5232,8 +5422,8 @@ dependencies = [ [[package]] name = "keyword-search-contract" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "platform-value", "platform-version", @@ -5286,7 +5476,7 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" dependencies = [ - "spin 0.9.9", + "spin", ] [[package]] @@ -5427,8 +5617,8 @@ dependencies = [ [[package]] name = "masternode-reward-shares-contract" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "platform-value", "platform-version", @@ -5579,6 +5769,17 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "moderation-charters-contract" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" +dependencies = [ + "platform-value", + "platform-version", + "serde_json", + "thiserror 2.0.20", +] + [[package]] name = "moxcms" version = "0.8.1" @@ -5589,20 +5790,6 @@ dependencies = [ "pxfm", ] -[[package]] -name = "multiexp" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ec2ce93a6f06ac6cae04c1da3f2a6a24fcfc1f0eb0b4e0f3d302f0df45326cb" -dependencies = [ - "ff", - "group", - "rand_core 0.6.4", - "rustversion", - "std-shims", - "zeroize", -] - [[package]] name = "multimap" version = "0.10.1" @@ -5799,7 +5986,7 @@ version = "0.50.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" dependencies = [ - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -5824,8 +6011,6 @@ checksum = "c89e69e7e0f03bea5ef08013795c25018e101932225a656383bd384495ecc367" dependencies = [ "num-integer", "num-traits", - "rand 0.8.8", - "serde", ] [[package]] @@ -5835,8 +6020,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" dependencies = [ "num-traits", - "rand 0.8.8", - "serde", ] [[package]] @@ -5884,7 +6067,6 @@ dependencies = [ "num-bigint", "num-integer", "num-traits", - "serde", ] [[package]] @@ -6363,10 +6545,10 @@ dependencies = [ "bitvec", "blake2b_simd", "corez", - "ff", + "ff 0.13.1", "fpe", "getset", - "group", + "group 0.13.0", "halo2_gadgets", "halo2_poseidon", "halo2_proofs", @@ -6427,15 +6609,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "pairing" -version = "0.23.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81fec4625e73cf41ef4bb6846cafa6d44736525f442ba45e407c4a000a13996f" -dependencies = [ - "group", -] - [[package]] name = "parking" version = "2.2.1" @@ -6502,8 +6675,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3437083215c505e867eea5478371feba43d7689d6d15ec0a209eb46fb0d4cda6" dependencies = [ "blake2b_simd", - "ff", - "group", + "ff 0.13.1", + "group 0.13.0", "lazy_static", "rand 0.8.8", "static_assertions", @@ -6733,8 +6906,8 @@ checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" [[package]] name = "platform-encryption" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "aes 0.8.4", "cbc 0.1.2", @@ -6746,8 +6919,8 @@ dependencies = [ [[package]] name = "platform-serialization" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "grovedb-bincode", "platform-version", @@ -6755,8 +6928,8 @@ dependencies = [ [[package]] name = "platform-serialization-derive" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "proc-macro2", "quote", @@ -6766,8 +6939,8 @@ dependencies = [ [[package]] name = "platform-value" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "base64 0.22.1", "bs58", @@ -6786,19 +6959,19 @@ dependencies = [ [[package]] name = "platform-version" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "grovedb-bincode", - "grovedb-version 6.0.1", + "grovedb-version 6.1.1", "thiserror 2.0.20", "versioned-feature-core", ] [[package]] name = "platform-versioning" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "proc-macro2", "quote", @@ -6807,8 +6980,8 @@ dependencies = [ [[package]] name = "platform-wallet" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "arc-swap", "async-trait", @@ -6841,8 +7014,8 @@ dependencies = [ [[package]] name = "platform-wallet-storage" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "apple-native-keyring-store", "argon2 0.5.3", @@ -7002,7 +7175,7 @@ dependencies = [ "cobs", "embedded-io 0.4.0", "embedded-io 0.6.1", - "heapless 0.7.17", + "heapless", "serde", ] @@ -7097,7 +7270,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "03da047801ff44bb6a4d407d4860c05fd70bb81714e6b2f3812603d5b145b042" dependencies = [ "heck", - "itertools 0.14.0", + "itertools 0.10.5", "log", "multimap", "petgraph", @@ -7118,7 +7291,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b570b25f7617e43d59005d0990ccb79e950a423952cea19671b7a876da390adf" dependencies = [ "anyhow", - "itertools 0.14.0", + "itertools 0.10.5", "proc-macro2", "quote", "syn 2.0.119", @@ -7237,7 +7410,7 @@ dependencies = [ "once_cell", "socket2", "tracing", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -7274,10 +7447,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e058c7de0b26af77780c769414d6257830bb240f3c38477dbc2c16e5f54d6d4c" dependencies = [ "libc", - "rand_chacha", + "rand_chacha 0.3.1", "rand_core 0.6.4", ] +[[package]] +name = "rand" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9ef1d0d795eb7d84685bca4f72f3649f064e6641543d3a8c415898726a57b41" +dependencies = [ + "rand_chacha 0.9.0", + "rand_core 0.9.5", +] + [[package]] name = "rand" version = "0.10.2" @@ -7299,6 +7482,16 @@ dependencies = [ "rand_core 0.6.4", ] +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core 0.9.5", +] + [[package]] name = "rand_core" version = "0.6.4" @@ -7308,6 +7501,15 @@ dependencies = [ "getrandom 0.2.17", ] +[[package]] +name = "rand_core" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" +dependencies = [ + "getrandom 0.3.4", +] + [[package]] name = "rand_core" version = "0.10.1" @@ -7323,15 +7525,6 @@ dependencies = [ "rand_core 0.10.1", ] -[[package]] -name = "rand_xorshift" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d25bf25ec5ae4a3f1b92f929810509a2f53d7dca2f50b794ff57e3face536c8f" -dependencies = [ - "rand_core 0.6.4", -] - [[package]] name = "range-alloc" version = "0.1.5" @@ -7405,7 +7598,7 @@ dependencies = [ "blake2b_simd", "byteorder", "frost-rerandomized", - "group", + "group 0.13.0", "hex", "jubjub", "pasta_curves", @@ -7795,8 +7988,8 @@ dependencies = [ [[package]] name = "rs-dapi-client" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "backon", "chrono", @@ -7807,6 +8000,7 @@ dependencies = [ "hex", "http", "http-serde", + "hyper-util", "lru", "rand 0.8.8", "serde", @@ -7814,29 +8008,35 @@ dependencies = [ "sha2 0.10.9", "thiserror 2.0.20", "tokio", + "tokio-socks", "tonic-web-wasm-client", + "tower-service", "tracing", "wasm-bindgen-futures", ] [[package]] name = "rs-sdk-trusted-context-provider" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "arc-swap", "dash-async", "dash-context-provider", "dpp", "futures", + "gloo-timers", "hex", "lru", "reqwest 0.12.28", "serde", "serde_json", "thiserror 2.0.20", + "tokio", "tracing", "url", + "wasm-bindgen-futures", + "web-time", ] [[package]] @@ -7954,7 +8154,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.12.1", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -8013,7 +8213,7 @@ dependencies = [ "security-framework", "security-framework-sys", "webpki-root-certs", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -8115,37 +8315,22 @@ dependencies = [ "tiny-skia 0.11.4", ] -[[package]] -name = "sec1" -version = "0.7.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3e97a565f76233a6003f9f5c54be1d9c5bdfa3eccfb189469f11ec4901c47dc" -dependencies = [ - "base16ct", - "der", - "generic-array 0.14.7", - "pkcs8", - "subtle", - "zeroize", -] - [[package]] name = "secp256k1" -version = "0.30.0" +version = "0.33.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b50c5943d326858130af85e049f2661ba3c78b26589b8ab98e65e80ae44a1252" +checksum = "d7f404a8dab7a7a5a631e741d8699aa9c8e1d689fc26ccf897c7187565490b69" dependencies = [ - "bitcoin_hashes", - "rand 0.8.8", + "rand 0.9.5", "secp256k1-sys", "serde", ] [[package]] name = "secp256k1-sys" -version = "0.10.1" +version = "0.14.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4387882333d3aa8cb20530a17c69a3752e97837832f34f6dccc760e715001d9" +checksum = "f6b2992d4a3cd244539a7d5d0966aadbe5ca7fb868a5d7e38c29499b9e709bbd" dependencies = [ "cc", ] @@ -8195,15 +8380,6 @@ dependencies = [ "serde_derive", ] -[[package]] -name = "serde_bare" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51c55386eed0f1ae957b091dc2ca8122f287b60c79c774cbe3d5f2b69fded660" -dependencies = [ - "serde", -] - [[package]] name = "serde_bytes" version = "0.11.19" @@ -8442,6 +8618,9 @@ name = "signature" version = "3.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28d567dcbaf0049cb8ac2608a76cd95ff9e4412e1899d389ee400918ca7537f5" +dependencies = [ + "rand_core 0.10.1", +] [[package]] name = "simd-adler32" @@ -8480,7 +8659,7 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d268ae0ea06faafe1662e9967cd4f9022014f5eeb798e0c302c876df8b7af9c" dependencies = [ - "group", + "group 0.13.0", "pasta_curves", "subtle", ] @@ -8604,7 +8783,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4" dependencies = [ "libc", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -8616,12 +8795,6 @@ dependencies = [ "lock_api", ] -[[package]] -name = "spin" -version = "0.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "023a211cb3138dbc438680b32560ad89f699977624c9f8dbb95a47d5b4c07dd3" - [[package]] name = "spirv" version = "0.4.0+sdk-1.4.341.0" @@ -8687,17 +8860,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" -[[package]] -name = "std-shims" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "227c4f8561598188d0df96dbe749824576174bba278b5b6bb2eacff1066067d0" -dependencies = [ - "hashbrown 0.16.1", - "rustversion", - "spin 0.10.1", -] - [[package]] name = "strict-num" version = "0.1.1" @@ -8853,10 +9015,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" dependencies = [ "fastrand", - "getrandom 0.3.4", + "getrandom 0.4.3", "once_cell", "rustix 1.1.4", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -9096,8 +9258,8 @@ checksum = "fd3ca314f692efd6c868f8408f53fe444634a845f96c028b97d35f6a1f79f0ee" [[package]] name = "token-history-contract" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "platform-value", "platform-version", @@ -9175,6 +9337,18 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "tokio-socks" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7e2948f60dbe26b35f2c7fb74ac2854c1fddded0fe9d7548fcc674a246f7615" +dependencies = [ + "either", + "futures-util", + "thiserror 1.0.69", + "tokio", +] + [[package]] name = "tokio-stream" version = "0.1.19" @@ -9577,7 +9751,7 @@ checksum = "f2f6fb2847f6742cd76af783a2a2c49e9375d0a111c7bef6f71cd9e738c72d6e" dependencies = [ "memoffset", "tempfile", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -9592,12 +9766,6 @@ dependencies = [ "static_assertions", ] -[[package]] -name = "uint-zigzag" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61faa33dc26b2851a37da5390a1a4cac015887b1e97ecd77ce7b4f987431de9f" - [[package]] name = "unicase" version = "2.9.0" @@ -9888,24 +10056,6 @@ dependencies = [ "syn 2.0.119", ] -[[package]] -name = "vsss-rs" -version = "5.1.0" -source = "git+https://github.com/dashpay/vsss-rs?branch=main#668f1406bf25a4b9a95cd97c9069f7a1632897c3" -dependencies = [ - "crypto-bigint", - "elliptic-curve", - "elliptic-curve-tools", - "generic-array 1.4.5", - "hex", - "num", - "rand_core 0.6.4", - "serde", - "sha3", - "subtle", - "zeroize", -] - [[package]] name = "walkdir" version = "2.5.0" @@ -9918,8 +10068,8 @@ dependencies = [ [[package]] name = "wallet-utils-contract" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "platform-value", "platform-version", @@ -10475,7 +10625,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -10932,7 +11082,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d6f32a0ff4a9f6f01231eb2059cc85479330739333e0e58cadf03b6af2cca10" dependencies = [ "cfg-if", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -11061,8 +11211,8 @@ checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" [[package]] name = "withdrawals-contract" -version = "4.2.0-beta.3" -source = "git+https://github.com/dashpay/platform?rev=9f7ed16935bd540e4c2a542688800d2953f9f67a#9f7ed16935bd540e4c2a542688800d2953f9f67a" +version = "5.0.0-rc.8" +source = "git+https://github.com/dashpay/platform?rev=063589bf0adbe1a8823a943cee95385b423b0e3f#063589bf0adbe1a8823a943cee95385b423b0e3f" dependencies = [ "num_enum 0.5.11", "platform-value", @@ -11350,9 +11500,9 @@ dependencies = [ [[package]] name = "zeroize" -version = "1.8.2" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b97154e67e32c85465826e8bcc1c59429aaaf107c1e4a9e53c8d8ccd5eff88d0" +checksum = "e13c156562582aa81c60cb29407084cdb54c4164760106ab78e6c5b0858cf64e" dependencies = [ "serde", "zeroize_derive", diff --git a/Cargo.toml b/Cargo.toml index bd1648d5e..fe597b3b3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ base64 = "0.23.1" # and `DisplayFromStr` adapters — the vulnerable `KeyValueMap` serializer is never invoked in the # dependency chain, so the advisory is not exploitable here (Dependabot alert #41; dismissal as # `not_used` was attempted but requires manual GitHub action — see coordinator note). -dash-sdk = { git = "https://github.com/dashpay/platform", rev = "9f7ed16935bd540e4c2a542688800d2953f9f67a", features = [ +dash-sdk = { git = "https://github.com/dashpay/platform", rev = "063589bf0adbe1a8823a943cee95385b423b0e3f", features = [ "core_key_wallet", "core_key_wallet_manager", "core_bincode", @@ -34,15 +34,15 @@ dash-sdk = { git = "https://github.com/dashpay/platform", rev = "9f7ed16935bd540 "core_spv", "shielded", ] } -rs-sdk-trusted-context-provider = { git = "https://github.com/dashpay/platform", rev = "9f7ed16935bd540e4c2a542688800d2953f9f67a" } -platform-wallet = { git = "https://github.com/dashpay/platform", rev = "9f7ed16935bd540e4c2a542688800d2953f9f67a", features = [ +rs-sdk-trusted-context-provider = { git = "https://github.com/dashpay/platform", rev = "063589bf0adbe1a8823a943cee95385b423b0e3f" } +platform-wallet = { git = "https://github.com/dashpay/platform", rev = "063589bf0adbe1a8823a943cee95385b423b0e3f", features = [ "serde", "shielded", ] } # `serde` backs `model::secret::Secret`'s `Deserialize`: the upstream # visitor copies a borrowed `&str` straight into guarded memory instead of # routing through a transient `String`. Secret schemas are included by `secrets`. -platform-wallet-storage = { git = "https://github.com/dashpay/platform", rev = "9f7ed16935bd540e4c2a542688800d2953f9f67a", features = [ +platform-wallet-storage = { git = "https://github.com/dashpay/platform", rev = "063589bf0adbe1a8823a943cee95385b423b0e3f", features = [ "shielded", "serde", ] } diff --git a/scripts/migration-fixtures/expected-migration-fingerprint.txt b/scripts/migration-fixtures/expected-migration-fingerprint.txt index cdd017629..7f45cb36e 100644 --- a/scripts/migration-fixtures/expected-migration-fingerprint.txt +++ b/scripts/migration-fixtures/expected-migration-fingerprint.txt @@ -5,7 +5,7 @@ # # fingerprint = sha256 of the LC_ALL=C-sorted " " listing of every # file under packages/rs-platform-wallet-storage/migrations at that rev. -pin=9f7ed16935bd540e4c2a542688800d2953f9f67a +pin=063589bf0adbe1a8823a943cee95385b423b0e3f target_branch=v5.1-dev -migration_count=18 -fingerprint=1c6a683d1b781045f4528048b49b1e945f4da6caeb71e72d8de7429904399b99 +migration_count=19 +fingerprint=79465f56868e4dfd6e0ba05e4eefe7e7d4ac8ef478c2579ba019d12f868278e6 diff --git a/src/wallet_backend/platform_compatibility/engine.rs b/src/wallet_backend/platform_compatibility/engine.rs index af1fd4f7e..4397c0fb5 100644 --- a/src/wallet_backend/platform_compatibility/engine.rs +++ b/src/wallet_backend/platform_compatibility/engine.rs @@ -6,7 +6,7 @@ use rusqlite::{Connection, OpenFlags, OptionalExtension, TransactionBehavior}; use super::storage_failure::StorageFailure; const OLD_SCHEMA: &str = include_str!("fixtures/67d4ef3.sql"); -const TARGET_SCHEMA: &str = include_str!("fixtures/e3cd7cf.sql"); +const TARGET_SCHEMA: &str = include_str!("fixtures/033ecae.sql"); const HISTORY: &str = "refinery_schema_history"; const IDENTITY_INDEX_KEY: &str = "det:identity_index:v1"; @@ -740,6 +740,7 @@ fn allowed_columns( "winner_mined_height", ], "core_sync_state" => &["chainlock_height"], + "asset_locks" => &["operation_id"], _ => &[], }; let expected_removed: &[&str] = if table == "identities" { diff --git a/src/wallet_backend/platform_compatibility/engine/tests.rs b/src/wallet_backend/platform_compatibility/engine/tests.rs index a6132f4b1..adbfc6489 100644 --- a/src/wallet_backend/platform_compatibility/engine/tests.rs +++ b/src/wallet_backend/platform_compatibility/engine/tests.rs @@ -1055,3 +1055,50 @@ fn platform_compatibility_lock_survives_concurrent_process_spawns() { outcome.expect("a released lock must be re-acquirable while processes spawn"); }); } + +#[test] +fn platform_compatibility_asset_lock_blobs_survive_with_no_operation_id() { + let (_dir, path, target) = fixture(); + let conn = Connection::open(&path).unwrap(); + conn.execute_batch(include_str!("../fixtures/public-rows.sql")) + .unwrap(); + conn.execute("INSERT INTO asset_locks (wallet_id, outpoint, status, account_index, identity_index, amount_duffs, lifecycle_blob) VALUES (?1, ?2, 'built', 0, 0, 123, ?3)", rusqlite::params![&[0xA1u8;32][..], &[0xB1u8;36][..], &[0xC1u8,0xC2][..]]).unwrap(); + drop(conn); + let backup = upgrade(&path, &target, |_| Ok(())).unwrap().unwrap(); + let conn = Connection::open(&path).unwrap(); + let (blob, operation): (Vec, Option>) = conn + .query_row( + "SELECT lifecycle_blob, operation_id FROM asset_locks", + [], + |row| Ok((row.get(0)?, row.get(1)?)), + ) + .unwrap(); + assert_eq!(blob, [0xC1, 0xC2]); + assert_eq!(operation, None); + assert_eq!( + Connection::open(backup) + .unwrap() + .query_row("SELECT lifecycle_blob FROM asset_locks", [], |row| row + .get::<_, Vec>(0)) + .unwrap(), + blob + ); +} + +#[test] +fn platform_compatibility_refuses_a_different_version_19_without_changes() { + let (dir, path, target) = fixture(); + let conn = Connection::open(&path).unwrap(); + conn.execute_batch("INSERT INTO refinery_schema_history VALUES (19, 'core_transaction_accounting', '2026-10-21T00:00:00Z', '1'); CREATE TABLE core_transaction_inputs (opaque BLOB);").unwrap(); + drop(conn); + let before = snapshot(&path); + assert!( + upgrade(&path, &target, |_| panic!( + "unrecognized history must not reach validation" + )) + .unwrap() + .is_none() + ); + assert_eq!(snapshot(&path), before); + assert!(backup_files(dir.path()).is_empty()); +} diff --git a/src/wallet_backend/platform_compatibility/fixtures/033ecae.sql b/src/wallet_backend/platform_compatibility/fixtures/033ecae.sql new file mode 100644 index 000000000..185a9e2af --- /dev/null +++ b/src/wallet_backend/platform_compatibility/fixtures/033ecae.sql @@ -0,0 +1,413 @@ +-- Empty schema captured from Platform 033ecaee2f3badc8e04ff8be664062f33516e3e4. +-- Generated by SqlitePersister::open; no wallet or identity data. +BEGIN TRANSACTION; +CREATE TABLE "account_registrations" ( + wallet_id BLOB NOT NULL, + account_type TEXT NOT NULL CHECK (account_type IN ('standard', 'standard_bip44', 'standard_bip32', 'coinjoin', 'identity_registration', 'identity_topup', 'identity_topup_unbound', 'identity_invitation', 'asset_lock_address_topup', 'asset_lock_shielded_topup', 'provider_voting', 'provider_owner', 'provider_operator', 'provider_platform', 'dashpay_receiving', 'dashpay_external', 'platform_payment')), + account_index INTEGER NOT NULL, + key_class INTEGER NOT NULL DEFAULT 0, + user_identity_id BLOB NOT NULL DEFAULT (zeroblob(32)), + friend_identity_id BLOB NOT NULL DEFAULT (zeroblob(32)), + account_xpub_bytes BLOB NOT NULL, + PRIMARY KEY (wallet_id, account_type, account_index, key_class, user_identity_id, friend_identity_id), + FOREIGN KEY (wallet_id) REFERENCES wallets(wallet_id) ON DELETE CASCADE +); +CREATE TABLE "asset_locks" ( + wallet_id BLOB NOT NULL, + outpoint BLOB NOT NULL, + status TEXT NOT NULL CHECK (status IN ('built', 'broadcast', 'is_locked', 'chain_locked', 'consumed', 'recovered_from_chain')), + account_index INTEGER NOT NULL, + identity_index INTEGER NOT NULL, + amount_duffs INTEGER NOT NULL, + lifecycle_blob BLOB NOT NULL, operation_id BLOB CHECK (operation_id IS NULL OR (typeof(operation_id) = 'blob' AND length(operation_id) = 16)), + PRIMARY KEY (wallet_id, outpoint), + FOREIGN KEY (wallet_id) REFERENCES "wallets"(wallet_id) ON DELETE CASCADE +); +CREATE TABLE contacts ( + wallet_id BLOB NOT NULL, + owner_id BLOB NOT NULL, + contact_id BLOB NOT NULL, + state TEXT NOT NULL CHECK (state IN ('sent', 'received', 'established')), + outgoing_request BLOB, + incoming_request BLOB, + alias TEXT, + note TEXT, + is_hidden INTEGER, + accepted_accounts BLOB, + -- G1c: set when external-account registration permanently fails for a + -- contact (so the sync sweep stops retrying a poisoned channel); + -- cleared on a superseding rotation. Nullable — readers treat NULL as + -- `false`. + payment_channel_broken INTEGER, + updated_at INTEGER NOT NULL DEFAULT (unixepoch()), + PRIMARY KEY (wallet_id, owner_id, contact_id), + FOREIGN KEY (wallet_id) REFERENCES "wallets"(wallet_id) ON DELETE CASCADE +); +CREATE TABLE core_address_pool ( + wallet_id BLOB NOT NULL, + account_type TEXT NOT NULL, + account_index INTEGER NOT NULL, + key_class INTEGER NOT NULL DEFAULT 0, + user_identity_id BLOB NOT NULL DEFAULT (zeroblob(32)), + friend_identity_id BLOB NOT NULL DEFAULT (zeroblob(32)), + pool_type INTEGER NOT NULL CHECK (pool_type IN (0, 1, 2, 3)), + address_index INTEGER NOT NULL, + script BLOB NOT NULL, + used INTEGER NOT NULL DEFAULT 0 CHECK (used IN (0, 1)), public_key BLOB NULL, key_type INTEGER NULL CHECK (key_type IS NULL OR key_type IN (0, 1, 2)), reserved_at INTEGER NULL, + PRIMARY KEY (wallet_id, account_type, account_index, key_class, user_identity_id, friend_identity_id, pool_type, address_index), + FOREIGN KEY (wallet_id) REFERENCES wallets(wallet_id) ON DELETE CASCADE +); +CREATE TABLE core_instant_locks ( + wallet_id BLOB NOT NULL, + txid BLOB NOT NULL, + islock_blob BLOB NOT NULL, + PRIMARY KEY (wallet_id, txid), + FOREIGN KEY (wallet_id) REFERENCES "wallets"(wallet_id) ON DELETE CASCADE +); +CREATE TABLE core_sync_state ( + wallet_id BLOB NOT NULL PRIMARY KEY, + last_processed_height INTEGER, + synced_height INTEGER, chainlock_height INTEGER, last_applied_chain_lock BLOB, + FOREIGN KEY (wallet_id) REFERENCES "wallets"(wallet_id) ON DELETE CASCADE +); +CREATE TABLE "core_transactions" ( + wallet_id BLOB NOT NULL, + txid BLOB NOT NULL, + height INTEGER, + block_hash BLOB, + block_time INTEGER, + finalized INTEGER NOT NULL, + record_blob BLOB, + PRIMARY KEY (wallet_id, txid), + FOREIGN KEY (wallet_id) REFERENCES wallets(wallet_id) ON DELETE CASCADE +); +CREATE TABLE core_utxos ( + wallet_id BLOB NOT NULL, + outpoint BLOB NOT NULL, + value INTEGER NOT NULL, + script BLOB NOT NULL, + spent INTEGER NOT NULL, + spent_in_txid BLOB, winner_mined_height INTEGER, is_sweep_placeholder INTEGER NOT NULL DEFAULT 0, + PRIMARY KEY (wallet_id, outpoint), + FOREIGN KEY (wallet_id) REFERENCES "wallets"(wallet_id) ON DELETE CASCADE +); +CREATE TABLE dashpay_payments_overlay ( + identity_id BLOB NOT NULL, + payment_id TEXT NOT NULL, + overlay_blob BLOB NOT NULL, + PRIMARY KEY (identity_id, payment_id), + FOREIGN KEY (identity_id) REFERENCES identities(identity_id) ON DELETE CASCADE +); +CREATE TABLE dashpay_profiles ( + identity_id BLOB NOT NULL PRIMARY KEY, + profile_blob BLOB NOT NULL, + FOREIGN KEY (identity_id) REFERENCES identities(identity_id) ON DELETE CASCADE +); +CREATE TABLE dpns_name_states ( + wallet_id BLOB NOT NULL, + document_id BLOB NOT NULL, + identity_id BLOB NOT NULL, + label TEXT NOT NULL, + normalized_label TEXT NOT NULL, + normalized_parent_domain TEXT NOT NULL, + price INTEGER CHECK (price IS NULL OR price >= 0), + status TEXT NOT NULL CHECK (status IN ('owned', 'sold', 'transferred')), + counterparty_id BLOB, + created_at_ms INTEGER, + updated_at_ms INTEGER, + transferred_at_ms INTEGER, + last_synced_at_ms INTEGER NOT NULL, + PRIMARY KEY (wallet_id, document_id), + CHECK ((status = 'owned') = (counterparty_id IS NULL)), + FOREIGN KEY (wallet_id) REFERENCES "wallets"(wallet_id) ON DELETE CASCADE + ); +CREATE TABLE identities ( + identity_id BLOB NOT NULL PRIMARY KEY, + wallet_id BLOB, + identity_index INTEGER, + entry_blob BLOB NOT NULL, + FOREIGN KEY (wallet_id) REFERENCES "wallets"(wallet_id) ON DELETE CASCADE +); +CREATE TABLE "identity_keys" ( + wallet_id BLOB, + identity_id BLOB NOT NULL, + key_id INTEGER NOT NULL, + public_key_blob BLOB NOT NULL, + public_key_hash BLOB NOT NULL, + -- Reserved for a future typed projection; always NULL today. + -- derivation_indices lives inside public_key_blob (the IdentityKeyWire + -- blob is the single source of truth). + derivation_blob BLOB, + PRIMARY KEY (identity_id, key_id), + FOREIGN KEY (wallet_id) REFERENCES wallets(wallet_id) ON DELETE CASCADE, + FOREIGN KEY (wallet_id, identity_id) + REFERENCES identities(wallet_id, identity_id) ON DELETE CASCADE +); +CREATE TABLE identity_scan_failed_indices ( + wallet_id BLOB NOT NULL, + failed_index INTEGER NOT NULL CHECK (failed_index >= 0), + PRIMARY KEY (wallet_id, failed_index), + FOREIGN KEY (wallet_id) REFERENCES identity_scan_states(wallet_id) ON DELETE CASCADE +); +CREATE TABLE identity_scan_states ( + wallet_id BLOB NOT NULL PRIMARY KEY, + complete INTEGER NOT NULL CHECK (complete IN (0, 1)), + probed_from INTEGER NOT NULL CHECK (probed_from >= 0), + probed_through INTEGER NOT NULL CHECK (probed_through >= probed_from), + unlocated_gap INTEGER NOT NULL CHECK (unlocated_gap IN (0, 1)), + -- A scan cannot both have answered everything and be sitting on a gap + -- nobody could name. + CHECK (complete = 0 OR unlocated_gap = 0), + FOREIGN KEY (wallet_id) REFERENCES wallets(wallet_id) ON DELETE CASCADE +); +CREATE TABLE ignored_senders ( + wallet_id BLOB NOT NULL, + owner_id BLOB NOT NULL, + sender_id BLOB NOT NULL, + ignored_at INTEGER NOT NULL DEFAULT (unixepoch()), + PRIMARY KEY (wallet_id, owner_id, sender_id), + FOREIGN KEY (wallet_id) REFERENCES "wallets"(wallet_id) ON DELETE CASCADE +); +CREATE TABLE invitations ( + wallet_id BLOB NOT NULL, + outpoint BLOB NOT NULL, + status TEXT NOT NULL CHECK (status IN ('created', 'claimed', 'reclaimed')), + funding_index INTEGER NOT NULL, + amount_duffs INTEGER NOT NULL, + expiry_unix INTEGER NOT NULL, + created_at_secs INTEGER NOT NULL, + has_inviter INTEGER NOT NULL, + PRIMARY KEY (wallet_id, outpoint), + FOREIGN KEY (wallet_id) REFERENCES "wallets"(wallet_id) ON DELETE CASCADE + ); +CREATE TABLE meta_contact ( + wallet_id BLOB NOT NULL, + owner_id BLOB NOT NULL, + contact_id BLOB NOT NULL, + key TEXT NOT NULL CHECK (length(key) BETWEEN 1 AND 128), + value BLOB NOT NULL, + updated_at INTEGER NOT NULL DEFAULT (unixepoch()), + PRIMARY KEY (wallet_id, owner_id, contact_id, key) +); +CREATE TABLE meta_data_versions ( + wallet_id BLOB NOT NULL, + domain TEXT NOT NULL, + seq INTEGER NOT NULL DEFAULT 0, + PRIMARY KEY (wallet_id, domain) +); +CREATE TABLE meta_global ( + key TEXT NOT NULL PRIMARY KEY CHECK (length(key) BETWEEN 1 AND 128), + value BLOB NOT NULL, + updated_at INTEGER NOT NULL DEFAULT (unixepoch()) +); +CREATE TABLE meta_identity ( + identity_id BLOB NOT NULL, + key TEXT NOT NULL CHECK (length(key) BETWEEN 1 AND 128), + value BLOB NOT NULL, + updated_at INTEGER NOT NULL DEFAULT (unixepoch()), + PRIMARY KEY (identity_id, key) +); +CREATE TABLE meta_platform_address ( + wallet_id BLOB NOT NULL, + address BLOB NOT NULL, + key TEXT NOT NULL CHECK (length(key) BETWEEN 1 AND 128), + value BLOB NOT NULL, + updated_at INTEGER NOT NULL DEFAULT (unixepoch()), + PRIMARY KEY (wallet_id, address, key) +); +CREATE TABLE meta_store_generation ( + id INTEGER NOT NULL PRIMARY KEY CHECK (id = 0), + generation BLOB NOT NULL +); +INSERT INTO "meta_store_generation" VALUES(0,X'90C76E7005D87413639127DDBBC6C290'); +CREATE TABLE meta_token ( + identity_id BLOB NOT NULL, + token_id BLOB NOT NULL, + key TEXT NOT NULL CHECK (length(key) BETWEEN 1 AND 128), + value BLOB NOT NULL, + updated_at INTEGER NOT NULL DEFAULT (unixepoch()), + PRIMARY KEY (identity_id, token_id, key) +); +CREATE TABLE meta_wallet ( + wallet_id BLOB NOT NULL, + key TEXT NOT NULL CHECK (length(key) BETWEEN 1 AND 128), + value BLOB NOT NULL, + updated_at INTEGER NOT NULL DEFAULT (unixepoch()), + PRIMARY KEY (wallet_id, key) +); +CREATE TABLE pending_contact_crypto ( + wallet_id BLOB NOT NULL, + owner_identity_id BLOB NOT NULL, + contact_id BLOB NOT NULL, + kind TEXT NOT NULL CHECK (kind IN ('register_receiving', 'register_external', 'contact_info_decrypt', 'auto_accept')), + payload BLOB NOT NULL, + enqueued_at_ms INTEGER NOT NULL, + PRIMARY KEY (wallet_id, owner_identity_id, contact_id, kind), + FOREIGN KEY (wallet_id) REFERENCES "wallets"(wallet_id) ON DELETE CASCADE +); +CREATE TABLE platform_address_sync ( + wallet_id BLOB NOT NULL PRIMARY KEY, + sync_height INTEGER NOT NULL, + sync_timestamp INTEGER NOT NULL, + last_known_recent_block INTEGER NOT NULL, + FOREIGN KEY (wallet_id) REFERENCES "wallets"(wallet_id) ON DELETE CASCADE +); +CREATE TABLE platform_addresses ( + wallet_id BLOB NOT NULL, + account_index INTEGER NOT NULL, + address_index INTEGER NOT NULL, + address BLOB NOT NULL, + balance INTEGER NOT NULL, + nonce INTEGER NOT NULL, as_of_height INTEGER NOT NULL DEFAULT 0, + PRIMARY KEY (wallet_id, address), + FOREIGN KEY (wallet_id) REFERENCES "wallets"(wallet_id) ON DELETE CASCADE +); +CREATE TABLE refinery_schema_history( + version int4 PRIMARY KEY, + name VARCHAR(255), + applied_on VARCHAR(255), + checksum VARCHAR(255)); +INSERT INTO "refinery_schema_history" VALUES(1,'initial','2026-10-21T00:00:00Z','16458714021387590417'); +INSERT INTO "refinery_schema_history" VALUES(2,'address_height_pin','2026-10-21T00:00:00Z','9304453063389616695'); +INSERT INTO "refinery_schema_history" VALUES(3,'invitations','2026-10-21T00:00:00Z','6768710306020099565'); +INSERT INTO "refinery_schema_history" VALUES(4,'asset_lock_recovered_status','2026-10-21T00:00:00Z','14446754812616312642'); +INSERT INTO "refinery_schema_history" VALUES(5,'dpns_name_states','2026-10-21T00:00:00Z','16834970739141546284'); +INSERT INTO "refinery_schema_history" VALUES(6,'tracked_masternodes','2026-10-21T00:00:00Z','13718514320935649081'); +INSERT INTO "refinery_schema_history" VALUES(7,'utxo_sweep_winner_height','2026-10-21T00:00:00Z','4410976868559573281'); +INSERT INTO "refinery_schema_history" VALUES(8,'rehydration_base_schema','2026-10-21T00:00:00Z','8883371305674850298'); +INSERT INTO "refinery_schema_history" VALUES(9,'unified','2026-10-21T00:00:00Z','8389295906542582065'); +INSERT INTO "refinery_schema_history" VALUES(10,'pool_public_key','2026-10-21T00:00:00Z','15088205568906581463'); +INSERT INTO "refinery_schema_history" VALUES(11,'pool_reserved_at','2026-10-21T00:00:00Z','1184102594786438566'); +INSERT INTO "refinery_schema_history" VALUES(12,'drop_core_utxo_metadata','2026-10-21T00:00:00Z','14281733277552006158'); +INSERT INTO "refinery_schema_history" VALUES(13,'shielded_viewing_keys','2026-10-21T00:00:00Z','2886147678304763509'); +INSERT INTO "refinery_schema_history" VALUES(14,'single_source_core_confirmation_height','2026-10-21T00:00:00Z','13462228277166821110'); +INSERT INTO "refinery_schema_history" VALUES(15,'purge_legacy_empty_script_spent_utxos','2026-10-21T00:00:00Z','723175730717787103'); +INSERT INTO "refinery_schema_history" VALUES(16,'identity_keys_null_scope_requires_existing_identity','2026-10-21T00:00:00Z','5573841766625796742'); +INSERT INTO "refinery_schema_history" VALUES(17,'identity_scan_state','2026-10-21T00:00:00Z','14086773559107496341'); +INSERT INTO "refinery_schema_history" VALUES(18,'identity_hard_delete','2026-10-21T00:00:00Z','14764815300602197850'); +INSERT INTO "refinery_schema_history" VALUES(19,'asset_lock_operation_id','2026-10-21T00:00:00Z','1537039181339941563'); +CREATE TABLE shielded_viewing_keys ( + wallet_id BLOB NOT NULL, + account_index INTEGER NOT NULL CHECK (account_index BETWEEN 0 AND 4294967295), + viewing_key BLOB NOT NULL, + PRIMARY KEY (wallet_id, account_index), + FOREIGN KEY (wallet_id) REFERENCES wallets(wallet_id) ON DELETE CASCADE +); +CREATE TABLE token_balances ( + identity_id BLOB NOT NULL, + token_id BLOB NOT NULL, + balance INTEGER NOT NULL, + updated_at INTEGER NOT NULL, + PRIMARY KEY (identity_id, token_id), + FOREIGN KEY (identity_id) REFERENCES identities(identity_id) ON DELETE CASCADE +); +CREATE TABLE tracked_masternodes ( + network TEXT NOT NULL CHECK (network IN ('mainnet', 'testnet', 'devnet', 'regtest')), + pro_tx_hash BLOB NOT NULL CHECK (length(pro_tx_hash) = 32), + label TEXT, + added_at INTEGER NOT NULL, + snapshot_json TEXT NOT NULL, + PRIMARY KEY (network, pro_tx_hash) + ); +CREATE TABLE "wallets" ( + wallet_id BLOB NOT NULL PRIMARY KEY, + network TEXT NOT NULL CHECK (network IN ('mainnet', 'testnet', 'devnet', 'regtest')), + birth_height INTEGER NOT NULL +); +CREATE INDEX idx_core_utxos_spent ON core_utxos(wallet_id, spent); +CREATE INDEX idx_identities_wallet ON identities(wallet_id); +CREATE TRIGGER cascade_meta_on_wallet_delete +AFTER DELETE ON "wallets" +FOR EACH ROW +BEGIN + DELETE FROM meta_wallet WHERE wallet_id = OLD.wallet_id; + DELETE FROM meta_contact WHERE wallet_id = OLD.wallet_id; + DELETE FROM meta_platform_address WHERE wallet_id = OLD.wallet_id; +END; +CREATE TRIGGER cascade_meta_on_identity_delete +AFTER DELETE ON identities +FOR EACH ROW +BEGIN + DELETE FROM meta_identity WHERE identity_id = OLD.identity_id; + DELETE FROM meta_token WHERE identity_id = OLD.identity_id; +END; +CREATE TRIGGER cascade_meta_token_on_token_balance_delete +AFTER DELETE ON token_balances +FOR EACH ROW +BEGIN + DELETE FROM meta_token + WHERE identity_id = OLD.identity_id AND token_id = OLD.token_id; +END; +CREATE TRIGGER cascade_meta_contact_on_contact_delete +AFTER DELETE ON contacts +FOR EACH ROW +BEGIN + DELETE FROM meta_contact + WHERE wallet_id = OLD.wallet_id + AND owner_id = OLD.owner_id + AND contact_id = OLD.contact_id; +END; +CREATE TRIGGER cascade_meta_platform_address_on_address_delete +AFTER DELETE ON platform_addresses +FOR EACH ROW +BEGIN + DELETE FROM meta_platform_address + WHERE wallet_id = OLD.wallet_id AND address = OLD.address; +END; +CREATE UNIQUE INDEX idx_identities_wallet_identity ON identities(wallet_id, identity_id); +CREATE INDEX idx_identity_keys_wallet_identity ON identity_keys(wallet_id, identity_id); +CREATE INDEX idx_core_address_pool_used + ON core_address_pool(wallet_id, used); +CREATE INDEX idx_core_address_pool_script + ON core_address_pool(wallet_id, script); +CREATE TRIGGER cascade_meta_data_versions_on_wallet_delete +AFTER DELETE ON wallets +FOR EACH ROW +BEGIN + DELETE FROM meta_data_versions WHERE wallet_id = OLD.wallet_id; +END; +CREATE INDEX idx_core_transactions_height ON core_transactions(wallet_id, height); +CREATE INDEX idx_core_utxos_unmaterialized ON core_utxos(wallet_id, winner_mined_height) + WHERE is_sweep_placeholder = 1; +CREATE TRIGGER setnull_core_utxos_on_tx_delete AFTER DELETE ON core_transactions +BEGIN + UPDATE core_utxos SET spent_in_txid = NULL + WHERE wallet_id = OLD.wallet_id AND spent_in_txid = OLD.txid; +END; +CREATE TRIGGER identity_keys_null_scope_requires_unowned_identity +BEFORE INSERT ON identity_keys +FOR EACH ROW WHEN NEW.wallet_id IS NULL +BEGIN + SELECT RAISE(ABORT, 'identity_keys.wallet_id is NULL but the named identity is missing or wallet-owned') + WHERE NOT EXISTS ( + SELECT 1 FROM identities i + WHERE i.identity_id = NEW.identity_id AND i.wallet_id IS NULL + ); +END; +CREATE TRIGGER identity_keys_null_scope_requires_unowned_identity_on_update +BEFORE UPDATE ON identity_keys +FOR EACH ROW WHEN NEW.wallet_id IS NULL +BEGIN + SELECT RAISE(ABORT, 'identity_keys.wallet_id is NULL but the named identity is missing or wallet-owned') + WHERE NOT EXISTS ( + SELECT 1 FROM identities i + WHERE i.identity_id = NEW.identity_id AND i.wallet_id IS NULL + ); +END; +CREATE TRIGGER cascade_children_on_identity_delete +AFTER DELETE ON identities +FOR EACH ROW +BEGIN + DELETE FROM identity_keys WHERE identity_id = OLD.identity_id; + DELETE FROM contacts WHERE owner_id = OLD.identity_id; + DELETE FROM ignored_senders WHERE owner_id = OLD.identity_id; + DELETE FROM pending_contact_crypto WHERE owner_identity_id = OLD.identity_id; +END; +CREATE INDEX idx_contacts_owner ON contacts(owner_id); +CREATE INDEX idx_ignored_senders_owner ON ignored_senders(owner_id); +CREATE INDEX idx_pending_contact_crypto_owner ON pending_contact_crypto(owner_identity_id); +CREATE UNIQUE INDEX asset_locks_wallet_operation_id ON asset_locks(wallet_id, operation_id) WHERE operation_id IS NOT NULL; +CREATE TRIGGER asset_locks_operation_id_immutable BEFORE UPDATE OF operation_id ON asset_locks WHEN OLD.operation_id IS NOT NULL AND NEW.operation_id IS NOT OLD.operation_id BEGIN SELECT RAISE(ABORT, 'asset lock operation ID cannot change'); END; +COMMIT; +PRAGMA application_id = 1347180372; +PRAGMA user_version = 0; diff --git a/src/wallet_backend/platform_compatibility/fixtures/README.md b/src/wallet_backend/platform_compatibility/fixtures/README.md index 4582259f9..a0f8ae0d4 100644 --- a/src/wallet_backend/platform_compatibility/fixtures/README.md +++ b/src/wallet_backend/platform_compatibility/fixtures/README.md @@ -18,9 +18,20 @@ The production bridge compares these exact histories and materialized schemas before translating data. The destination itself is created by the current public `SqlitePersister::open`, not by the fixture SQL. Updating either schema revision requires reviewing and regenerating these guards. -The selected dependency pin `63cf57f40d0000bf3b2b26026c8fa1c71162852d` -has identical migration code and schema to `e3cd7cf`, so it uses the same -destination guard. +The current destination guard is `033ecae.sql`, captured from an empty database +opened by `SqlitePersister::open` at Platform revision +`033ecaee2f3badc8e04ff8be664062f33516e3e4`. The V001–V018 migration names and +checksums match `e3cd7cf.sql`. V019 adds only the nullable `asset_locks.operation_id` +column, its unique index and immutability trigger (checksum `1537039181339941563`). +The snapshot contains no domain rows; history timestamps are normalized. The +bridge copies existing asset-lock blobs unchanged and leaves the new column NULL. +A different migration occupying version 19 is not an accepted source history. +Platform revision `a0d7b6ffec6f91e3818530b83d75adcd62480ebe` changes only doc +comments in `V007__utxo_sweep_winner_height.rs`; every migration's SQL and +checksum, and so the snapshot, are unchanged. +Revisions `132ae31a1356d035cd8326f27c1109685090a71d` (5.0.0-rc.8) and +`063589bf0adbe1a8823a943cee95385b423b0e3f` leave the migrations untouched. + `public-rows.sql` contains only public synthetic rows from that old revision's `packages/rs-platform-wallet-storage/tests/fixtures/populated_v001.db`, after From 82eecdd1d93f8acb80221b97dd6f0aa44e9d3591 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Sat, 24 Oct 2026 18:21:52 +0000 Subject: [PATCH 02/27] refactor: adapt to the rust-dashcore cryptography API The new rust-dashcore wraps secp256k1 keys in EcdsaSecretKey and EcdsaPublicKey, returns secret bytes as Zeroizing, and drops the Secp256k1 context argument. Signers, single-key wallets, DashPay derivation, address and QR handling, migrations and their tests follow the new types. Signing stays on the non-grinding dpp::secp256k1 path, and a parity test pins the BLS signature bytes the previous SDK made. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- src/backend_task/dashpay/auto_accept_proof.rs | 39 +++---- src/backend_task/dashpay/encryption.rs | 27 +++-- src/backend_task/dashpay/errors.rs | 2 +- src/backend_task/dashpay/payments.rs | 6 +- .../identity/add_key_to_identity.rs | 20 ++-- src/backend_task/identity/load_identity.rs | 18 ++- src/backend_task/identity/mod.rs | 17 ++- src/backend_task/migration/finish_unwire.rs | 6 +- src/backend_task/migration/legacy_restore.rs | 4 +- .../migration/single_key_restore.rs | 13 +-- src/backend_task/migration/v093_upgrade.rs | 16 ++- .../wallet/derive_identity_key_for_display.rs | 4 +- src/backend_task/wallet/mod.rs | 66 ++++++++--- src/backend_task/wallet/rename_wallet.rs | 20 ++-- .../wallet/sign_message_with_key.rs | 4 +- src/context/wallet_lifecycle/tests.rs | 15 ++- src/database/test_helpers.rs | 8 +- src/database/wallet.rs | 10 +- src/mcp/tools/masternode.rs | 32 +++--- src/model/address.rs | 10 +- src/model/dashpay_derivation/dip14.rs | 33 +++--- src/model/dashpay_derivation/hd.rs | 30 ++--- src/model/dashpay_derivation/mod.rs | 6 +- src/model/key_input.rs | 2 +- src/model/qualified_identity/mod.rs | 62 ++++++---- .../qualified_identity_public_key.rs | 8 +- src/model/single_key.rs | 6 +- src/model/wallet/auth_pubkey_cache.rs | 13 +-- src/model/wallet/mod.rs | 106 ++++++++---------- src/model/wallet/single_key.rs | 12 +- src/ui/components/address_input.rs | 18 +-- src/ui/identity/funding_common.rs | 9 +- src/ui/identity/keys/add_key_screen.rs | 12 +- src/ui/identity/keys/key_info_screen.rs | 42 ++++--- src/ui/state/account_summary.rs | 10 +- src/ui/wallets/send_screen.rs | 11 +- .../wallets/wallets_screen/address_table.rs | 8 +- src/wallet_backend/auth_pubkey_cache.rs | 9 +- src/wallet_backend/contact_profile_cache.rs | 9 +- src/wallet_backend/dashpay.rs | 40 ++----- src/wallet_backend/det_platform_signer.rs | 27 +++-- src/wallet_backend/det_signer.rs | 80 +++++++------ src/wallet_backend/secret_access.rs | 25 +++-- src/wallet_backend/sidecar.rs | 5 +- src/wallet_backend/single_key.rs | 66 +++++------ src/wallet_backend/snapshot.rs | 51 ++++++++- src/wallet_backend/wallet_context.rs | 4 +- tests/backend-e2e/dashpay_tasks.rs | 17 +-- tests/backend-e2e/identity_in_vault_sign.rs | 11 +- tests/backend-e2e/identity_tasks.rs | 9 +- tests/backend-e2e/wallet_reregistration.rs | 6 +- tests/backend-e2e/z_broadcast_st_tasks.rs | 18 ++- tests/kittest/address_input.rs | 7 +- tests/kittest/send_screen.rs | 5 +- tests/kittest/top_up_identity_screen.rs | 5 +- tests/migration-matrix/assertions.rs | 6 +- tests/migration-matrix/public_identities.rs | 14 ++- 57 files changed, 602 insertions(+), 537 deletions(-) diff --git a/src/backend_task/dashpay/auto_accept_proof.rs b/src/backend_task/dashpay/auto_accept_proof.rs index 71bbd9a76..4a3fd5b22 100644 --- a/src/backend_task/dashpay/auto_accept_proof.rs +++ b/src/backend_task/dashpay/auto_accept_proof.rs @@ -1,9 +1,9 @@ use crate::backend_task::error::TaskError; use crate::model::dashpay_derivation::derive_auto_accept_key; use crate::model::qualified_identity::QualifiedIdentity; -use dash_sdk::dpp::dashcore::secp256k1::{Message, Secp256k1, SecretKey}; use dash_sdk::dpp::identity::accessors::IdentityGettersV0; use dash_sdk::dpp::identity::{KeyType, Purpose, SecurityLevel}; +use dash_sdk::dpp::secp256k1::Message; use dash_sdk::platform::Identifier; use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; @@ -28,8 +28,10 @@ impl AutoAcceptProofData { key_data.extend_from_slice(&self.proof_key); // The actual key // Encode key data in base58 using dashcore's base58 implementation - use dash_sdk::dpp::dashcore::base58; - let key_data_base58 = base58::encode_slice(&key_data); + let key_data_base58 = dash_sdk::dpp::platform_value::string_encoding::encode( + &key_data, + dash_sdk::dpp::platform_value::string_encoding::Encoding::Base58, + ); // For QR codes without username (identity-based) format!( @@ -85,8 +87,7 @@ impl AutoAcceptProofData { key_data_base58.ok_or("Missing proof key data in QR code".to_string())?; // Decode the key data from base58 - use dash_sdk::dpp::dashcore::base58; - let key_data = base58::decode(&key_data_base58) + let key_data = dash_sdk::dpp::dashcore::base58::decode(&key_data_base58) .map_err(|e| format!("Invalid base58 key data: {}", e))?; // Parse key data format: key_type (1) + timestamp (4) + key_size (1) + key (32-64) @@ -170,7 +171,7 @@ pub async fn generate_auto_accept_proof( .map_err(|e| TaskError::DashPay(DashPayError::from(e)))?; // Extract the private key bytes (32 bytes) - let proof_key = auto_accept_xprv.private_key.secret_bytes(); + let proof_key = *auto_accept_xprv.private_key.to_bytes(); Ok(AutoAcceptProofData { identity_id: identity.identity.id(), @@ -209,14 +210,13 @@ pub fn create_auto_accept_proof_bytes_with_key( let message_hash = hasher.finalize(); // Create secp256k1 message and sign - let secp = Secp256k1::new(); - let message = Message::from_digest_slice(&message_hash) - .map_err(|e| format!("Failed to create message: {}", e))?; - let secret_key = SecretKey::from_slice(signing_key_bytes) + let message = Message::from_digest(message_hash.into()); + + let secret_key = dash_sdk::dpp::secp256k1::SecretKey::from_secret_bytes(*(signing_key_bytes)) .map_err(|e| format!("Failed to create secret key: {}", e))?; - let signature = secp.sign_ecdsa(&message, &secret_key); + let signature = dash_sdk::dpp::secp256k1::ecdsa::sign(message, &secret_key); let sig_bytes = signature.serialize_compact(); // Build the proof bytes @@ -274,9 +274,8 @@ pub async fn verify_auto_accept_proof( let mut hasher = Sha256::new(); hasher.update(&message_data); let message_hash = hasher.finalize(); - let secp = Secp256k1::new(); - let message = Message::from_digest_slice(&message_hash) - .map_err(|e| format!("Failed to create message: {}", e))?; + + let message = Message::from_digest(message_hash.into()); // Derive expected pubkey from our seed and key index (timestamp) // Use ENCRYPTION key (ECDSA_SECP256K1) for HD derivation as per DIP-15 @@ -299,17 +298,11 @@ pub async fn verify_auto_accept_proof( .ok_or("Private key not found")?; let xprv = derive_auto_accept_key(&wallet_seed[..], our_identity.network, key_index) .map_err(|e| format!("Failed to derive auto-accept key: {}", e))?; - let pubkey = dash_sdk::dpp::dashcore::secp256k1::PublicKey::from_secret_key( - &secp, - &dash_sdk::dpp::dashcore::secp256k1::SecretKey::from_slice( - &xprv.private_key.secret_bytes(), - ) - .map_err(|e| format!("Failed to create secret key: {}", e))?, - ); - let sig = dash_sdk::dpp::dashcore::secp256k1::ecdsa::Signature::from_compact(signature_bytes) + let pubkey = dash_sdk::dpp::secp256k1::PublicKey::from(xprv.private_key.public_key()); + let sig = dash_sdk::dpp::secp256k1::ecdsa::Signature::from_compact(signature_bytes) .map_err(|e| format!("Invalid signature bytes: {}", e))?; - match secp.verify_ecdsa(&message, &sig, &pubkey) { + match dash_sdk::dpp::secp256k1::ecdsa::verify(&sig, message, &pubkey) { Ok(_) => Ok(true), Err(_) => Ok(false), } diff --git a/src/backend_task/dashpay/encryption.rs b/src/backend_task/dashpay/encryption.rs index ca0f060fd..e6f11be17 100644 --- a/src/backend_task/dashpay/encryption.rs +++ b/src/backend_task/dashpay/encryption.rs @@ -1,10 +1,10 @@ use aes_gcm::aes::Aes256; use bip39::rand::{self, RngCore}; use cbc; -use dash_sdk::dpp::dashcore::secp256k1::{PublicKey, Secp256k1, SecretKey}; use dash_sdk::dpp::identity::IdentityPublicKey; use dash_sdk::dpp::identity::KeyType; use dash_sdk::dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0; +use dash_sdk::dpp::secp256k1::{PublicKey, SecretKey}; use sha2::{Digest, Sha256}; use zeroize::Zeroizing; @@ -18,11 +18,13 @@ pub fn generate_ecdh_shared_key( private_key: &[u8], public_key: &IdentityPublicKey, ) -> Result, String> { - let _secp = Secp256k1::new(); - // Parse the private key - let secret_key = - SecretKey::from_slice(private_key).map_err(|e| format!("Invalid private key: {}", e))?; + let secret_key = SecretKey::from_secret_bytes( + private_key + .try_into() + .map_err(|_| "Invalid private key length".to_string())?, + ) + .map_err(|e| format!("Invalid private key: {}", e))?; // Get the public key data - only works for full secp256k1 keys match public_key.key_type() { @@ -34,7 +36,7 @@ pub fn generate_ecdh_shared_key( // Perform ECDH to get shared secret. The 64-byte point is secret; // hold it in `Zeroizing` so it is wiped once the key is hashed out. let shared_secret = Zeroizing::new( - dash_sdk::dpp::dashcore::secp256k1::ecdh::shared_secret_point( + dash_sdk::dpp::secp256k1::ecdh::shared_secret_point( &public_key, &secret_key, ), @@ -295,7 +297,7 @@ pub fn decrypt_account_label( mod tests { use super::*; use bip39::rand::{self, RngCore}; - use dash_sdk::dpp::dashcore::secp256k1::{PublicKey, Secp256k1, SecretKey}; + use dash_sdk::dpp::secp256k1::{PublicKey, SecretKey}; fn generate_test_shared_key() -> [u8; 32] { let mut shared_key = [0u8; 32]; @@ -304,14 +306,13 @@ mod tests { } fn generate_test_key_pair() -> (SecretKey, PublicKey) { - let secp = Secp256k1::new(); - let secret_key = SecretKey::from_slice(&[ + let secret_key = SecretKey::from_secret_bytes([ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, ]) .unwrap(); - let public_key = PublicKey::from_secret_key(&secp, &secret_key); + let public_key = PublicKey::from_secret_key(&secret_key); (secret_key, public_key) } @@ -380,8 +381,6 @@ mod tests { /// wipes the sha2 digest) cannot silently alter the derived material. #[test] fn ecdh_shared_key_matches_pinned_vector() { - let secp = Secp256k1::new(); - // Our private key: 0x01..=0x20 (same fixed vector the suite uses). let private_key = [ 0x01u8, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, @@ -391,13 +390,13 @@ mod tests { // Counterpart key derived from a different fixed secret so the ECDH // point is non-degenerate. - let counterpart_secret = SecretKey::from_slice(&[ + let counterpart_secret = SecretKey::from_secret_bytes([ 0xA1u8, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF, 0xC0, ]) .unwrap(); - let counterpart_pub = PublicKey::from_secret_key(&secp, &counterpart_secret); + let counterpart_pub = PublicKey::from_secret_key(&counterpart_secret); let counterpart_ipk = ecdsa_identity_public_key(&counterpart_pub); let shared_key = generate_ecdh_shared_key(&private_key, &counterpart_ipk) diff --git a/src/backend_task/dashpay/errors.rs b/src/backend_task/dashpay/errors.rs index caa14ffaa..a8561ed0c 100644 --- a/src/backend_task/dashpay/errors.rs +++ b/src/backend_task/dashpay/errors.rs @@ -1,4 +1,4 @@ -use dash_sdk::dpp::dashcore::secp256k1; +use dash_sdk::dpp::secp256k1; use dash_sdk::platform::Identifier; use thiserror::Error; diff --git a/src/backend_task/dashpay/payments.rs b/src/backend_task/dashpay/payments.rs index 31db1e4de..37d27de5e 100644 --- a/src/backend_task/dashpay/payments.rs +++ b/src/backend_task/dashpay/payments.rs @@ -92,7 +92,7 @@ pub async fn derive_contact_payment_address( contact_id: Identifier, ) -> Result<(Address, u32), String> { // Fetch the contact request from the contact to us (they sent us their encrypted xpub) - let dashpay_contract = app_context.dashpay_contract.clone(); + let dashpay_contract = app_context.dashpay_contract(); let mut query = DocumentQuery::new(dashpay_contract.clone(), "contactRequest") .map_err(|e| format!("Failed to create query: {}", e))?; @@ -185,8 +185,8 @@ pub async fn derive_contact_payment_address( // Create extended public key from components // This is simplified - in production you'd properly reconstruct with all fields - use dash_sdk::dpp::dashcore::secp256k1::{PublicKey, Secp256k1}; use dash_sdk::dpp::key_wallet::bip32::{ChainCode, ChildNumber, ExtendedPubKey, Fingerprint}; + use dash_sdk::dpp::secp256k1::{PublicKey, Secp256k1}; let _secp = Secp256k1::new(); let pubkey = @@ -199,7 +199,7 @@ pub async fn derive_contact_payment_address( parent_fingerprint: Fingerprint::default(), child_number: ChildNumber::from_normal_idx(0) .expect("invariant: BIP32 child index 0 is below 2^31"), - public_key: pubkey, + public_key: pubkey.into(), chain_code: ChainCode::from(chain_code), }; diff --git a/src/backend_task/identity/add_key_to_identity.rs b/src/backend_task/identity/add_key_to_identity.rs index fb3954960..91fe6c1fe 100644 --- a/src/backend_task/identity/add_key_to_identity.rs +++ b/src/backend_task/identity/add_key_to_identity.rs @@ -641,7 +641,7 @@ mod tests { async fn derived_key_persists_as_path_and_signs_after_reload() { use crate::context::test_staging::stage_identity_with_vaulted_keys; use crate::model::derived_identity_key::test_support::fixture; - use dash_sdk::dpp::dashcore::secp256k1::{Message, Secp256k1, SecretKey}; + use dash_sdk::dpp::secp256k1::{Message, SecretKey}; let staged = stage_identity_with_vaulted_keys([0xAA; 32], [0xBB; 32]).await; let (mut identity, cache, seed_hash, seed) = fixture(); @@ -703,14 +703,14 @@ mod tests { .get_resolve_with_seed(&placement, &wallets, &seed, Network::Testnet) .unwrap() .unwrap(); - let secp = Secp256k1::new(); - let secret = SecretKey::from_slice(bytes.as_ref()).unwrap(); + + let secret = SecretKey::from_secret_bytes(*bytes).unwrap(); let message = Message::from_digest([1; 32]); - let signature = secp.sign_ecdsa(&message, &secret); - secp.verify_ecdsa( - &message, + let signature = dash_sdk::dpp::secp256k1::ecdsa::sign(message, &secret); + dash_sdk::dpp::secp256k1::ecdsa::verify( &signature, - &cache.get(Network::Testnet, 0, 3).unwrap().inner, + message, + &cache.get(Network::Testnet, 0, 3).unwrap().inner.into(), ) .unwrap(); } @@ -833,14 +833,14 @@ mod tests { /// The stage's identity as published after another device added a key /// (not from this wallet) at key id 1. fn network_record_with_foreign_key(stage: &DerivedStage) -> Identity { - use dash_sdk::dpp::dashcore::secp256k1::{PublicKey, Secp256k1, SecretKey}; + use dash_sdk::dpp::secp256k1::{PublicKey, SecretKey}; let mut record = stage.identity.identity.clone(); let mut foreign = record.public_keys()[&0].clone(); foreign.set_id(1); foreign.set_security_level(dash_sdk::dpp::identity::SecurityLevel::HIGH); - let secret = SecretKey::from_slice(&[9; 32]).unwrap(); + let secret = SecretKey::from_secret_bytes([9; 32]).unwrap(); foreign.set_data( - PublicKey::from_secret_key(&Secp256k1::new(), &secret) + PublicKey::from_secret_key(&secret) .serialize() .to_vec() .into(), diff --git a/src/backend_task/identity/load_identity.rs b/src/backend_task/identity/load_identity.rs index e6e687210..b7dc03e53 100644 --- a/src/backend_task/identity/load_identity.rs +++ b/src/backend_task/identity/load_identity.rs @@ -19,7 +19,7 @@ use crate::model::wallet::{Wallet, WalletSeedHash}; use crate::ui::identity::add_new_identity_screen::MAX_IDENTITY_INDEX; use dash_sdk::Sdk; use dash_sdk::dashcore_rpc::dashcore::PrivateKey; -use dash_sdk::dashcore_rpc::dashcore::key::Secp256k1; + use dash_sdk::dpp::dashcore::ProTxHash; use dash_sdk::dpp::dashcore::hashes::Hash; use dash_sdk::dpp::identity::KeyType; @@ -268,10 +268,7 @@ impl AppContext { if let Ok(private_key) = PrivateKey::from_byte_array(&voting_private_key_bytes, self.network) { - let voting_address = private_key - .public_key(&Secp256k1::new()) - .pubkey_hash() - .to_byte_array(); + let voting_address = private_key.public_key().pubkey_hash().to_byte_array(); let voter_identifier = voter_identity_id( ProTxHash::from_byte_array(identity_id.to_buffer()), &voting_address, @@ -334,7 +331,6 @@ impl AppContext { }) .collect::, TaskError>>()?; - let secp = Secp256k1::new(); #[allow(clippy::type_complexity)] let (public_key_lookup, public_key_hash_lookup): ( HashMap, [u8; 32]>, @@ -342,12 +338,12 @@ impl AppContext { ) = input_private_keys .into_iter() .map(|private_key| { - let public_key = private_key.public_key(&secp); + let public_key = private_key.public_key(); let public_key_bytes = public_key.to_bytes(); let pub_key_hash = public_key.pubkey_hash().to_byte_array(); ( - (public_key_bytes, private_key.inner.secret_bytes()), - (pub_key_hash, private_key.inner.secret_bytes()), + (public_key_bytes, *private_key.inner.to_bytes()), + (pub_key_hash, *private_key.inner.to_bytes()), ) }) .unzip(); @@ -1042,7 +1038,7 @@ mod tests { .unwrap() .unwrap(); let private_key = PrivateKey::from_byte_array(&rand::random(), Network::Testnet).unwrap(); - let address = private_key.public_key(&Secp256k1::new()).pubkey_hash(); + let address = private_key.public_key().pubkey_hash(); let other_node = Identifier::from([0x75; 32]); let other_voter = Identifier::create_voter_identifier(other_node.as_bytes(), address.as_ref()); @@ -1125,7 +1121,7 @@ mod tests { .unwrap() .unwrap(); let private_key = PrivateKey::from_byte_array(&rand::random(), Network::Testnet).unwrap(); - let address = private_key.public_key(&Secp256k1::new()).pubkey_hash(); + let address = private_key.public_key().pubkey_hash(); let voter = Identifier::create_voter_identifier(staged.id.as_bytes(), address.as_ref()); let mut voter_identity = Identity::create_basic_identity(voter, PlatformVersion::latest()).unwrap(); diff --git a/src/backend_task/identity/mod.rs b/src/backend_task/identity/mod.rs index fb0a12944..54f6f963a 100644 --- a/src/backend_task/identity/mod.rs +++ b/src/backend_task/identity/mod.rs @@ -647,7 +647,7 @@ pub fn build_identity_registration_with_seed( identity_index: u32, funding_amount: Duffs, ) -> Result { - let dashpay_contract_id = app_context.dashpay_contract.id(); + let dashpay_contract_id = app_context.dashpay_contract().id(); let key_specs = default_identity_key_specs(dashpay_contract_id); let network = app_context.network; @@ -1372,10 +1372,7 @@ mod tests { /// rejected or the wallet could not sign for its own identity. #[test] fn identity_key_entry_public_matches_private_derivation() { - use dash_sdk::dpp::dashcore::secp256k1::Secp256k1; - let seed = [0x42u8; 64]; - let secp = Secp256k1::new(); for network in [Network::Testnet, Network::Mainnet] { let wallet = Wallet::new_from_seed(seed, network, None, None).expect("build wallet"); @@ -1413,8 +1410,8 @@ mod tests { .to_priv(); assert_eq!( - entry.public_key.inner.serialize(), - private_key.public_key(&secp).inner.serialize(), + entry.public_key.inner.to_compressed(), + private_key.public_key().inner.to_compressed(), "public/private key drift at ({network:?}, id={identity_index}, key={key_index})" ); @@ -1501,12 +1498,12 @@ mod tests { .expect("resolve master") .expect("master present"); // The resolved private key's public key must match the spec's public key. - let secp = dash_sdk::dpp::dashcore::secp256k1::Secp256k1::new(); - let resolved_pub = dash_sdk::dpp::dashcore::secp256k1::SecretKey::from_slice(&stored.1[..]) + + let resolved_pub = dash_sdk::dpp::secp256k1::SecretKey::from_secret_bytes(*stored.1) .expect("secret") - .public_key(&secp); + .public_key(); assert_eq!( - master.public_key.inner.serialize(), + master.public_key.inner.to_compressed(), resolved_pub.serialize(), "stored path resolves to the spec's public key" ); diff --git a/src/backend_task/migration/finish_unwire.rs b/src/backend_task/migration/finish_unwire.rs index 8f48fbc81..721eda3de 100644 --- a/src/backend_task/migration/finish_unwire.rs +++ b/src/backend_task/migration/finish_unwire.rs @@ -518,7 +518,7 @@ async fn refresh_dapi_nodes_once_with_legacy_check( return; } - if let Err(error) = Arc::clone(app_context).reinit_core_client_and_sdk() { + if let Err(error) = app_context.rebuild_connection().await { tracing::warn!( target = "migration::finish_unwire", ?network, @@ -3955,7 +3955,7 @@ mod tests { use crate::wallet_backend::single_key::{ SingleKeyView, label_for_address, single_key_namespace_id, }; - use dash_sdk::dpp::dashcore::secp256k1::Secp256k1; + use dash_sdk::dpp::dashcore::{Address, Network, PrivateKey, PublicKey}; let dir = tempfile::tempdir().expect("tempdir"); @@ -3970,7 +3970,7 @@ mod tests { let priv_key = PrivateKey::from_byte_array(&raw, Network::Testnet).expect("priv"); let pub_key = PublicKey { compressed: priv_key.compressed, - inner: priv_key.inner.public_key(&Secp256k1::new()), + inner: priv_key.inner.public_key(), }; let address = Address::p2pkh(&pub_key, Network::Testnet).to_string(); let key_hash = crate::model::wallet::single_key::ClosedSingleKey::compute_key_hash(&raw); diff --git a/src/backend_task/migration/legacy_restore.rs b/src/backend_task/migration/legacy_restore.rs index 3dfe0e3b2..888c17b15 100644 --- a/src/backend_task/migration/legacy_restore.rs +++ b/src/backend_task/migration/legacy_restore.rs @@ -365,12 +365,12 @@ mod tests { #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn review_regression_restore_counts_unreadable_protected_keys() { use crate::model::wallet::single_key::ClosedSingleKey; - use dash_sdk::dpp::dashcore::{Address, PrivateKey, PublicKey, secp256k1::Secp256k1}; + use dash_sdk::dpp::dashcore::{Address, PrivateKey, PublicKey}; let dir = tempfile::tempdir().expect("tempdir"); let ctx = app_context(dir.path()); let raw = [0x39; 32]; let private = PrivateKey::from_byte_array(&raw, ctx.network).unwrap(); - let public = PublicKey::new(private.inner.public_key(&Secp256k1::new())); + let public = PublicKey::new(private.inner.public_key()); let address = Address::p2pkh(&public, ctx.network).to_string(); let password = format!("test-password-{}", dir.path().display()); let envelope = ClosedSingleKey::encrypt_private_key(&raw, &password).unwrap(); diff --git a/src/backend_task/migration/single_key_restore.rs b/src/backend_task/migration/single_key_restore.rs index fd0db97cf..dacfc4065 100644 --- a/src/backend_task/migration/single_key_restore.rs +++ b/src/backend_task/migration/single_key_restore.rs @@ -29,7 +29,6 @@ use std::sync::Arc; -use dash_sdk::dpp::dashcore::secp256k1::Secp256k1; use dash_sdk::dpp::dashcore::{Address, Network, PrivateKey, PublicKey}; use rusqlite::{Connection, OpenFlags}; use zeroize::Zeroizing; @@ -333,10 +332,10 @@ pub(super) fn derive_p2pkh_address(wif: &str, network: Network) -> Result &'static TestSecrets { fn random_private_key() -> [u8; 32] { loop { let bytes = rand::random::<[u8; 32]>(); - if dash_sdk::dpp::dashcore::secp256k1::SecretKey::from_slice(&bytes).is_ok() { + if dash_sdk::dpp::secp256k1::SecretKey::from_secret_bytes(bytes).is_ok() { return bytes; } } @@ -132,13 +132,12 @@ const V093_MASTERNODE_BLOB_HEX: &str = "00bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb /// Populate only key payloads, preserving the captured v0.9.3 encoding layout. fn v093_masternode_blob() -> Vec { - use dash_sdk::dpp::dashcore::{ - hashes::{Hash, hash160}, - secp256k1::{PublicKey, Secp256k1, SecretKey}, - }; + use dash_sdk::dpp::dashcore::hashes::{Hash, hash160}; + use dash_sdk::dpp::secp256k1::PublicKey; let public_hash = |key: &[u8; 32]| { - let key = SecretKey::from_slice(key).expect("generated valid key"); - let public = PublicKey::from_secret_key(&Secp256k1::new(), &key); + let key = dash_sdk::dpp::secp256k1::SecretKey::from_secret_bytes(*(key)) + .expect("generated valid key"); + let public = PublicKey::from_secret_key(&key); hex::encode(hash160::Hash::hash(&public.serialize()).to_byte_array()) }; let encoded = V093_MASTERNODE_BLOB_HEX @@ -1642,12 +1641,11 @@ const SKIPPED_PROTECTED_ADDRESS: &str = "XprotectedPaperKeyAddress"; /// The P2PKH address of [`SKIPPED_SINGLE_KEY`] on mainnet. fn skipped_single_key_address() -> String { - use dash_sdk::dpp::dashcore::secp256k1::Secp256k1; use dash_sdk::dpp::dashcore::{Address, PrivateKey, PublicKey}; let key = PrivateKey::from_byte_array(&SKIPPED_SINGLE_KEY, Network::Mainnet).expect("key"); let public = PublicKey { compressed: true, - inner: key.inner.public_key(&Secp256k1::new()), + inner: key.inner.public_key(), }; Address::p2pkh(&public, Network::Mainnet).to_string() } diff --git a/src/backend_task/wallet/derive_identity_key_for_display.rs b/src/backend_task/wallet/derive_identity_key_for_display.rs index 1d7efe1f2..52dd10322 100644 --- a/src/backend_task/wallet/derive_identity_key_for_display.rs +++ b/src/backend_task/wallet/derive_identity_key_for_display.rs @@ -28,7 +28,9 @@ impl AppContext { let network = self.network; let wif = self .with_identity_secret_key(identity_id, target.clone(), key_id, |secret_key| { - let private_key = PrivateKey::new(secret_key, network); + let private_key = + PrivateKey::from_byte_array(secret_key.as_secret_bytes(), network) + .map_err(|_| TaskError::IdentityKeyMalformed)?; Ok(Secret::new(private_key.to_wif())) }) .await?; diff --git a/src/backend_task/wallet/mod.rs b/src/backend_task/wallet/mod.rs index 1d7011244..64afbe9ec 100644 --- a/src/backend_task/wallet/mod.rs +++ b/src/backend_task/wallet/mod.rs @@ -18,14 +18,15 @@ use crate::model::qualified_identity::PrivateKeyTarget; use crate::model::wallet::WalletSeedHash; use dash_sdk::dpp::address_funds::PlatformAddress; use dash_sdk::dpp::balances::credits::Credits; +use dash_sdk::dpp::dashcore::ecdsa::EcdsaRecSignature; use dash_sdk::dpp::dashcore::hashes::Hash; -use dash_sdk::dpp::dashcore::secp256k1::{Message, Secp256k1, SecretKey}; -use dash_sdk::dpp::dashcore::sign_message::{MessageSignature, signed_msg_hash}; +use dash_sdk::dpp::dashcore::sign_message::{self, signed_msg_hash}; use dash_sdk::dpp::dashcore::{OutPoint, PrivateKey}; use dash_sdk::dpp::identity::core_script::CoreScript; use dash_sdk::dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0; use dash_sdk::dpp::identity::{KeyID, KeyType}; use dash_sdk::dpp::key_wallet::bip32::DerivationPath; +use dash_sdk::dpp::secp256k1::{Message, SecretKey}; use dash_sdk::platform::Identifier; use std::collections::BTreeMap; use std::sync::Arc; @@ -40,11 +41,19 @@ pub(crate) fn dash_signed_message( secret_key: &SecretKey, compressed: bool, ) -> String { - let secp = Secp256k1::new(); let message_hash = signed_msg_hash(message); let digest = Message::from_digest(*message_hash.as_byte_array()); - let recoverable = secp.sign_ecdsa_recoverable(&digest, secret_key); - MessageSignature::new(recoverable, compressed).to_base64() + let recoverable = dash_sdk::dpp::secp256k1::ecdsa::RecoverableSignature::sign_ecdsa_recoverable( + digest, secret_key, + ); + let (recovery_id, _) = recoverable.serialize_compact(); + let envelope = EcdsaRecSignature::from_parts( + recoverable.to_standard().into(), + recovery_id.to_u8(), + compressed, + ) + .expect("a recovery id from signing is in range"); + sign_message::to_base64(envelope) } impl AppContext { @@ -183,7 +192,7 @@ impl AppContext { .ok_or(TaskError::IdentityKeyMissing)?; // Present-but-malformed key bytes are distinct from a // genuinely absent key and from a signing/derivation failure. - let secret_key = SecretKey::from_byte_array(key).map_err(|detail| { + let secret_key = SecretKey::from_secret_bytes(*key).map_err(|detail| { tracing::warn!(error = %detail, "Identity-key secret construction failed"); TaskError::IdentityKeyMalformed })?; @@ -375,13 +384,13 @@ mod tests { use crate::utils::tasks::TaskManager; use crate::wallet_backend::IdentityKeyView; use dash_sdk::dpp::dashcore::Network; - use dash_sdk::dpp::dashcore::secp256k1::PublicKey; - use dash_sdk::dpp::dashcore::sign_message::{MessageSignature, signed_msg_hash}; + use dash_sdk::dpp::dashcore::sign_message::{self, signed_msg_hash}; use dash_sdk::dpp::identity::accessors::IdentityGettersV0; use dash_sdk::dpp::identity::identity_public_key::v0::IdentityPublicKeyV0; use dash_sdk::dpp::identity::{Identity, Purpose, SecurityLevel}; use dash_sdk::dpp::key_wallet::bip32::ChildNumber; use dash_sdk::dpp::platform_value::BinaryData; + use dash_sdk::dpp::secp256k1::PublicKey; use dash_sdk::dpp::version::PlatformVersion; use dash_sdk::platform::IdentityPublicKey; use std::sync::atomic::{AtomicBool, Ordering}; @@ -506,22 +515,43 @@ mod tests { /// hardcoded recovery header would fail ~50% of the time here. Both the /// wallet-key and identity-key signers call this one helper. fn assert_recovers(compressed: bool) { - let secp = Secp256k1::new(); - let secret_key = SecretKey::from_byte_array(&[0x42u8; 32]).expect("valid secret"); - let expected_pubkey = PublicKey::from_secret_key(&secp, &secret_key); + let secret_key = SecretKey::from_secret_bytes([0x42u8; 32]).expect("valid secret"); + let expected_pubkey = PublicKey::from_secret_key(&secret_key); let message = "Bilby was here"; let base64 = dash_signed_message(message, &secret_key, compressed); - let parsed = MessageSignature::from_base64(&base64).expect("valid envelope"); - assert_eq!(parsed.compressed, compressed); + let parsed = sign_message::from_base64(&base64).expect("valid envelope"); + assert_eq!(parsed.is_compressed().expect("valid header"), compressed); - let recovered = parsed - .recover_pubkey(&secp, signed_msg_hash(message)) + let recovered = sign_message::recover_pubkey(&parsed, signed_msg_hash(message)) .expect("recovers a public key"); - assert_eq!(recovered.inner, expected_pubkey); + assert_eq!(PublicKey::from(recovered.inner), expected_pubkey); assert_eq!(recovered.compressed, compressed); } + /// The envelope keeps the layout the previous encoder produced: a header + /// byte `27 + recovery id` (`+ 4` when compressed), then the compact + /// `r || s` of the same deterministic signature. + #[test] + fn the_envelope_keeps_its_byte_layout() { + use dash_sdk::dpp::secp256k1::Message; + use dash_sdk::dpp::secp256k1::ecdsa::RecoverableSignature; + let message = "Bilby was here"; + let secret_key = SecretKey::from_secret_bytes([0x42u8; 32]).expect("valid secret"); + let digest = Message::from_digest(*signed_msg_hash(message).as_byte_array()); + let (recovery_id, compact) = + RecoverableSignature::sign_ecdsa_recoverable(digest, &secret_key).serialize_compact(); + for compressed in [false, true] { + let mut expected = [0u8; 65]; + expected[0] = 27 + recovery_id.to_u8() + if compressed { 4 } else { 0 }; + expected[1..].copy_from_slice(&compact); + let encoded = + sign_message::from_base64(&dash_signed_message(message, &secret_key, compressed)) + .expect("valid envelope"); + assert_eq!(encoded.as_bytes(), &expected); + } + } + #[test] fn recovers_signer_pubkey_compressed() { assert_recovers(true); @@ -603,7 +633,7 @@ mod tests { let bytes = fixture .ctx - .with_identity_secret_key(identity_id, MAIN, 7, |key| Ok(key.secret_bytes())) + .with_identity_secret_key(identity_id, MAIN, 7, |key| Ok(key.to_secret_bytes())) .await .expect("a key that matches its record must resolve"); @@ -746,7 +776,7 @@ mod tests { let bytes = fixture .ctx - .with_identity_secret_key(identity_id, MAIN, 7, |key| Ok(key.secret_bytes())) + .with_identity_secret_key(identity_id, MAIN, 7, |key| Ok(key.to_secret_bytes())) .await .expect("the sibling placement's live bytes must be served"); diff --git a/src/backend_task/wallet/rename_wallet.rs b/src/backend_task/wallet/rename_wallet.rs index 963687315..b2f86c08c 100644 --- a/src/backend_task/wallet/rename_wallet.rs +++ b/src/backend_task/wallet/rename_wallet.rs @@ -94,8 +94,8 @@ mod tests { use crate::utils::tasks::TaskManager; use crate::wallet_backend::kv_test_support::InMemoryKv; use crate::wallet_backend::{DetKv, DetScope}; - use dash_sdk::dpp::dashcore::secp256k1::SecretKey; use dash_sdk::dpp::dashcore::{Network, PrivateKey}; + use dash_sdk::dpp::secp256k1::SecretKey; use platform_wallet_storage::{KvError, KvStore, ObjectId}; use std::sync::{Condvar, Mutex}; use std::time::Duration; @@ -804,8 +804,10 @@ mod tests { let f = fixture().await; let backend = f.ctx.wallet_backend().expect("backend"); // Mint a deterministic throwaway key rather than committing a WIF. - let sk = SecretKey::from_byte_array(&[0x11u8; 32]).expect("valid scalar"); - let wif = PrivateKey::new(sk, Network::Testnet).to_wif(); + let sk = SecretKey::from_secret_bytes([0x11u8; 32]).expect("valid scalar"); + let wif = PrivateKey::from_byte_array(sk.as_secret_bytes(), Network::Testnet) + .expect("valid secret key") + .to_wif(); let (imported, display_wallet) = f .ctx .import_single_key_wif( @@ -850,8 +852,10 @@ mod tests { use crate::model::wallet::alias::AliasSource; let f = fixture().await; let backend = f.ctx.wallet_backend().expect("backend"); - let key = SecretKey::from_byte_array(&[0x33; 32]).expect("valid scalar"); - let wif = PrivateKey::new(key, Network::Testnet).to_wif(); + let key = SecretKey::from_secret_bytes([0x33; 32]).expect("valid scalar"); + let wif = PrivateKey::from_byte_array(key.as_secret_bytes(), Network::Testnet) + .expect("valid secret key") + .to_wif(); let (first, retained) = f .ctx .import_single_key_wif( @@ -902,8 +906,10 @@ mod tests { async fn rename_single_key_blank_alias_resets_to_default_name() { let f = fixture().await; let backend = f.ctx.wallet_backend().expect("backend"); - let sk = SecretKey::from_byte_array(&[0x22u8; 32]).expect("valid scalar"); - let wif = PrivateKey::new(sk, Network::Testnet).to_wif(); + let sk = SecretKey::from_secret_bytes([0x22u8; 32]).expect("valid scalar"); + let wif = PrivateKey::from_byte_array(sk.as_secret_bytes(), Network::Testnet) + .expect("valid secret key") + .to_wif(); let address = backend .single_key() .import_wif( diff --git a/src/backend_task/wallet/sign_message_with_key.rs b/src/backend_task/wallet/sign_message_with_key.rs index c0f4cf28c..c52dd6a58 100644 --- a/src/backend_task/wallet/sign_message_with_key.rs +++ b/src/backend_task/wallet/sign_message_with_key.rs @@ -6,9 +6,9 @@ use crate::backend_task::error::TaskError; use crate::backend_task::wallet::dash_signed_message; use crate::context::AppContext; use crate::model::wallet::WalletSeedHash; -use dash_sdk::dpp::dashcore::secp256k1::SecretKey; use dash_sdk::dpp::identity::KeyType; use dash_sdk::dpp::key_wallet::bip32::DerivationPath; +use dash_sdk::dpp::secp256k1::SecretKey; use std::sync::Arc; impl AppContext { @@ -40,7 +40,7 @@ impl AppContext { &derivation_path, TaskError::WalletMessageSigningFailed, |private_key| { - let secret_key = SecretKey::from_byte_array(&private_key.inner.secret_bytes()) + let secret_key = SecretKey::from_secret_bytes(*private_key.inner.to_bytes()) .map_err(|detail| { tracing::warn!(error = %detail, "Sign-message secret key construction failed"); TaskError::WalletMessageSigningFailed diff --git a/src/context/wallet_lifecycle/tests.rs b/src/context/wallet_lifecycle/tests.rs index 7880d5609..dece54874 100644 --- a/src/context/wallet_lifecycle/tests.rs +++ b/src/context/wallet_lifecycle/tests.rs @@ -2335,7 +2335,7 @@ async fn clear_network_database_wipes_wallet_meta_and_seed_envelope() { async fn clear_network_database_removes_an_auth_pubkey_cache_rewritten_mid_clear() { use crate::backend_task::system_task::SystemTask; use dash_sdk::dpp::dashcore::PublicKey; - use dash_sdk::dpp::dashcore::secp256k1::{Secp256k1, SecretKey}; + use dash_sdk::dpp::secp256k1::SecretKey; let (ctx, sender, _tmp) = offline_testnet_context(); ctx.ensure_wallet_backend(sender.clone()) @@ -2351,11 +2351,10 @@ async fn clear_network_database_removes_an_auth_pubkey_cache_rewritten_mid_clear let backend = ctx.wallet_backend().expect("backend wired"); let late_writer = Arc::clone(&backend); backend.set_after_forget_all_test_hook(move || { - let secp = Secp256k1::new(); let key = PublicKey::new( - SecretKey::from_slice(&[0x42; 32]) + SecretKey::from_secret_bytes([0x42; 32]) .expect("valid secret key") - .public_key(&secp), + .public_key(), ); late_writer .auth_pubkey_cache() @@ -3596,7 +3595,7 @@ fn seed_legacy_protected_single_key( alias: Option<&str>, ) -> String { use crate::model::wallet::single_key::ClosedSingleKey; - use dash_sdk::dpp::dashcore::secp256k1::Secp256k1; + use dash_sdk::dpp::dashcore::{Address, PrivateKey, PublicKey}; let path = ctx.db.db_file_path().expect("data.db path"); @@ -3608,10 +3607,10 @@ fn seed_legacy_protected_single_key( nonce, } = ClosedSingleKey::encrypt_private_key(raw_key, password).expect("encrypt"); let priv_key = PrivateKey::from_byte_array(raw_key, Network::Testnet).expect("priv"); - let secp = Secp256k1::new(); + let pub_key = PublicKey { compressed: priv_key.compressed, - inner: priv_key.inner.public_key(&secp), + inner: priv_key.inner.public_key(), }; let address = Address::p2pkh(&pub_key, Network::Testnet).to_string(); let key_hash = ClosedSingleKey::compute_key_hash(raw_key); @@ -3625,7 +3624,7 @@ fn seed_legacy_protected_single_key( ciphertext, salt, nonce, - pub_key.inner.serialize().to_vec(), + pub_key.inner.to_compressed().to_vec(), address, alias, Network::Testnet.to_string(), diff --git a/src/database/test_helpers.rs b/src/database/test_helpers.rs index 17dc6123c..01248ae89 100644 --- a/src/database/test_helpers.rs +++ b/src/database/test_helpers.rs @@ -161,21 +161,21 @@ pub fn legacy_master_epk_bytes( network: dash_sdk::dpp::dashcore::Network, ) -> Vec { use dash_sdk::dpp::dashcore::Network; - use dash_sdk::dpp::dashcore::secp256k1::Secp256k1; + use dash_sdk::dpp::key_wallet::bip32::{ ChildNumber, DerivationPath, ExtendedPrivKey, ExtendedPubKey, }; let coin_type = if network == Network::Mainnet { 5 } else { 1 }; - let secp = Secp256k1::new(); + let master = ExtendedPrivKey::new_master(network, seed).expect("master key"); let path = DerivationPath::from(vec![ ChildNumber::Hardened { index: 44 }, ChildNumber::Hardened { index: coin_type }, ChildNumber::Hardened { index: 0 }, ]); - let account = master.derive_priv(&secp, &path).expect("derive account"); - ExtendedPubKey::from_priv(&secp, &account).encode().to_vec() + let account = master.derive_priv(&path).expect("derive account"); + ExtendedPubKey::from_priv(&account).encode().to_vec() } /// Create the legacy `scheduled_votes` table in `data.db`. Fresh installs no diff --git a/src/database/wallet.rs b/src/database/wallet.rs index 5e4a793a4..bbae151d3 100644 --- a/src/database/wallet.rs +++ b/src/database/wallet.rs @@ -554,7 +554,7 @@ impl From for rusqlite::Error { mod tests { use super::*; use crate::database::test_helpers::create_test_database; - use dash_sdk::dpp::dashcore::secp256k1::Secp256k1; + use dash_sdk::dpp::key_wallet::bip32::DerivationPath; use dash_sdk::dpp::key_wallet::bip32::{ChildNumber, ExtendedPrivKey}; @@ -568,17 +568,15 @@ mod tests { fn create_test_master_epk_bytes(network: Network) -> Vec { let seed = [7u8; 64]; - let secp = Secp256k1::new(); + let master = ExtendedPrivKey::new_master(network, &seed).expect("master key"); let path = DerivationPath::from(vec![ ChildNumber::Hardened { index: 44 }, ChildNumber::Hardened { index: 1 }, ChildNumber::Hardened { index: 0 }, ]); - let account = master - .derive_priv(&secp, &path) - .expect("derive bip44 account"); - ExtendedPubKey::from_priv(&secp, &account).encode() + let account = master.derive_priv(&path).expect("derive bip44 account"); + ExtendedPubKey::from_priv(&account).encode() } #[test] diff --git a/src/mcp/tools/masternode.rs b/src/mcp/tools/masternode.rs index eb210dc69..24910a17b 100644 --- a/src/mcp/tools/masternode.rs +++ b/src/mcp/tools/masternode.rs @@ -699,13 +699,13 @@ mod tests { /// Derive a real, checksum-valid testnet P2PKH address from a fixed key. fn sample_core_address() -> String { - use dash_sdk::dashcore_rpc::dashcore::secp256k1::{Secp256k1, SecretKey}; use dash_sdk::dashcore_rpc::dashcore::{Address, Network, PrivateKey, PublicKey}; + use dash_sdk::dpp::secp256k1::SecretKey; - let secp = Secp256k1::new(); - let sk = SecretKey::from_slice(&[7u8; 32]).expect("valid secret key"); - let privkey = PrivateKey::new(sk, Network::Testnet); - let pubkey = PublicKey::from_private_key(&secp, &privkey); + let sk = SecretKey::from_secret_bytes([7u8; 32]).expect("valid secret key"); + let privkey = PrivateKey::from_byte_array(sk.as_secret_bytes(), Network::Testnet) + .expect("valid secret key"); + let pubkey = PublicKey::from_private_key(&privkey); Address::p2pkh(&pubkey, Network::Testnet).to_string() } @@ -855,11 +855,14 @@ mod tests { } fn testnet_core_address() -> String { - use dash_sdk::dashcore_rpc::dashcore::secp256k1::{Secp256k1, SecretKey}; use dash_sdk::dashcore_rpc::dashcore::{Address, Network, PrivateKey, PublicKey}; - let secp = Secp256k1::new(); - let sk = SecretKey::from_slice(&[9u8; 32]).expect("valid secret key"); - let pk = PublicKey::from_private_key(&secp, &PrivateKey::new(sk, Network::Testnet)); + use dash_sdk::dpp::secp256k1::SecretKey; + + let sk = SecretKey::from_secret_bytes([9u8; 32]).expect("valid secret key"); + let pk = PublicKey::from_private_key( + &PrivateKey::from_byte_array(sk.as_secret_bytes(), Network::Testnet) + .expect("valid secret key"), + ); Address::p2pkh(&pk, Network::Testnet).to_string() } @@ -933,11 +936,14 @@ mod tests { #[test] fn transfer_mode_cross_network_address_rejected() { - use dash_sdk::dashcore_rpc::dashcore::secp256k1::{Secp256k1, SecretKey}; use dash_sdk::dashcore_rpc::dashcore::{Address, Network, PrivateKey, PublicKey}; - let secp = Secp256k1::new(); - let sk = SecretKey::from_slice(&[3u8; 32]).expect("valid secret key"); - let pk = PublicKey::from_private_key(&secp, &PrivateKey::new(sk, Network::Mainnet)); + use dash_sdk::dpp::secp256k1::SecretKey; + + let sk = SecretKey::from_secret_bytes([3u8; 32]).expect("valid secret key"); + let pk = PublicKey::from_private_key( + &PrivateKey::from_byte_array(sk.as_secret_bytes(), Network::Mainnet) + .expect("valid secret key"), + ); let mainnet_addr = Address::p2pkh(&pk, Network::Mainnet).to_string(); let qi = masternode_fixture(false, true); diff --git a/src/model/address.rs b/src/model/address.rs index 7f612d8ba..438ca9c23 100644 --- a/src/model/address.rs +++ b/src/model/address.rs @@ -587,13 +587,13 @@ mod tests { #[test] fn detect_core_address() { - use dash_sdk::dashcore_rpc::dashcore::secp256k1::{Secp256k1, SecretKey}; use dash_sdk::dashcore_rpc::dashcore::{PrivateKey, PublicKey}; + use dash_sdk::dpp::secp256k1::SecretKey; - let secp = Secp256k1::new(); - let sk = SecretKey::from_slice(&[1u8; 32]).unwrap(); - let privkey = PrivateKey::new(sk, Network::Testnet); - let pubkey = PublicKey::from_private_key(&secp, &privkey); + let sk = SecretKey::from_secret_bytes([1u8; 32]).unwrap(); + let privkey = PrivateKey::from_byte_array(sk.as_secret_bytes(), Network::Testnet) + .expect("valid secret key"); + let pubkey = PublicKey::from_private_key(&privkey); let addr = Address::p2pkh(&pubkey, Network::Testnet); assert_eq!( AddressKind::detect(&addr.to_string()), diff --git a/src/model/dashpay_derivation/dip14.rs b/src/model/dashpay_derivation/dip14.rs index 923a1fc36..1009eaf40 100644 --- a/src/model/dashpay_derivation/dip14.rs +++ b/src/model/dashpay_derivation/dip14.rs @@ -1,4 +1,5 @@ use dash_sdk::dpp::dashcore::Network; +use dash_sdk::dpp::dashcore::ecdsa::EcdsaPublicKey; use dash_sdk::dpp::dashcore::hashes::hmac::{Hmac, HmacEngine}; use dash_sdk::dpp::dashcore::hashes::sha512; /// DIP-14 compliant 256-bit HD key derivation implementation @@ -6,8 +7,8 @@ use dash_sdk::dpp::dashcore::hashes::sha512; /// This module implements Extended Key Derivation using 256-bit Unsigned Integers /// as specified in DIP-0014 for DashPay contact relationships. use dash_sdk::dpp::dashcore::hashes::{Hash, HashEngine}; -use dash_sdk::dpp::dashcore::secp256k1::{PublicKey, Secp256k1, SecretKey}; use dash_sdk::dpp::key_wallet::bip32::{ChainCode, ExtendedPrivKey, ExtendedPubKey, Fingerprint}; +use dash_sdk::dpp::secp256k1::SecretKey; use dash_sdk::platform::Identifier; use super::DerivationError; @@ -22,8 +23,6 @@ pub fn ckd_priv_256( index: &[u8; 32], // 256-bit index hardened: bool, ) -> Result { - let secp = Secp256k1::new(); - // Check if this is a compatibility mode derivation (index < 2^32) let is_compatibility_mode = is_index_less_than_2_32(index); @@ -33,7 +32,7 @@ pub fn ckd_priv_256( if hardened { // Hardened derivation: 0x00 || ser_256(k_par) || ser(i) hmac_engine.input(&[0x00]); - hmac_engine.input(&parent_key.private_key.secret_bytes()); + hmac_engine.input(&*parent_key.private_key.to_bytes()); if is_compatibility_mode { // Use ser_32(i) for compatibility @@ -44,8 +43,8 @@ pub fn ckd_priv_256( } } else { // Non-hardened derivation: ser_P(point(k_par)) || ser(i) - let parent_pubkey = parent_key.private_key.public_key(&secp); - hmac_engine.input(&parent_pubkey.serialize()); + let parent_pubkey = parent_key.private_key.public_key(); + hmac_engine.input(&parent_pubkey.to_compressed()); if is_compatibility_mode { // Use ser_32(i) for compatibility @@ -62,11 +61,15 @@ pub fn ckd_priv_256( // Split into I_L (first 32 bytes) and I_R (last 32 bytes) let (i_l, i_r) = hmac_bytes.split_at(32); - // Parse I_L as a private key and add to parent key - let i_l_key = SecretKey::from_slice(i_l)?; + // Parse I_L as a private key (refusing zero and values >= n) and add it + // to the parent key. + let i_l: &[u8; 32] = i_l + .try_into() + .map_err(|_| dash_sdk::dpp::secp256k1::Error::InvalidSecretKey)?; + SecretKey::from_secret_bytes(*i_l)?; // k_i = parse_256(I_L) + k_par (mod n) - let child_key = parent_key.private_key.add_tweak(&i_l_key.into())?; + let child_key = parent_key.private_key.add_tweak(i_l)?; // Chain code is I_R (32 bytes) let mut chain_code_bytes = [0u8; 32]; @@ -74,7 +77,7 @@ pub fn ckd_priv_256( let child_chain_code = ChainCode::from(chain_code_bytes); // Calculate child fingerprint - let parent_pubkey = parent_key.private_key.public_key(&secp); + let parent_pubkey = parent_key.private_key.public_key(); let parent_fingerprint = calculate_fingerprint(&parent_pubkey); // Create the child extended private key @@ -113,8 +116,8 @@ pub fn derive_dashpay_incoming_xpub_dip14( let base_path = DerivationPath::from_str(&format!("m/9'/{coin_type}'/15'/{account}'"))?; // Derive to the account level using standard BIP32 - let secp = Secp256k1::new(); - let account_xprv = master_xprv.derive_priv(&secp, &base_path)?; + + let account_xprv = master_xprv.derive_priv(&base_path)?; // Now use DIP-14 256-bit derivation for the identity levels // Derive: account_key/(sender_id) @@ -126,7 +129,7 @@ pub fn derive_dashpay_incoming_xpub_dip14( let contact_xprv = ckd_priv_256(&sender_level, &recipient_index, false)?; // Convert to extended public key - Ok(ExtendedPubKey::from_priv(&secp, &contact_xprv)) + Ok(ExtendedPubKey::from_priv(&contact_xprv)) } /// Convert an Identifier to a 256-bit index for DIP-14 derivation @@ -175,10 +178,10 @@ fn index_to_child_number( } /// Calculate fingerprint for a public key (first 4 bytes of HASH160) -fn calculate_fingerprint(pubkey: &PublicKey) -> Fingerprint { +fn calculate_fingerprint(pubkey: &EcdsaPublicKey) -> Fingerprint { use dash_sdk::dpp::dashcore::hashes::hash160; - let hash = hash160::Hash::hash(&pubkey.serialize()); + let hash = hash160::Hash::hash(&pubkey.to_compressed()); let mut fingerprint_bytes = [0u8; 4]; fingerprint_bytes.copy_from_slice(&hash.to_byte_array()[0..4]); Fingerprint::from(fingerprint_bytes) diff --git a/src/model/dashpay_derivation/hd.rs b/src/model/dashpay_derivation/hd.rs index 9b38fbd8b..cf4a4862b 100644 --- a/src/model/dashpay_derivation/hd.rs +++ b/src/model/dashpay_derivation/hd.rs @@ -38,10 +38,8 @@ pub fn derive_payment_address( contact_xpub: &ExtendedPubKey, index: u32, ) -> Result { - let secp = dash_sdk::dpp::dashcore::secp256k1::Secp256k1::new(); - // Derive the specific address key - let address_key = contact_xpub.derive_pub(&secp, &[ChildNumber::from_normal_idx(index)?])?; + let address_key = contact_xpub.derive_pub(&[ChildNumber::from_normal_idx(index)?])?; // Convert to Dash address // The ExtendedPubKey's public_key is a secp256k1::PublicKey @@ -72,7 +70,7 @@ pub fn generate_contact_xpub_data( let chain_code = xpub.chain_code.to_bytes(); // Get the public key bytes (33 bytes compressed) - let public_key_bytes = xpub.public_key.serialize(); + let public_key_bytes = xpub.public_key.to_compressed(); Ok((parent_fingerprint, chain_code, public_key_bytes)) } @@ -96,8 +94,7 @@ pub fn derive_auto_accept_key( ))?; // Derive the key - let auto_accept_key = - master_xprv.derive_priv(&dash_sdk::dpp::dashcore::secp256k1::Secp256k1::new(), &path)?; + let auto_accept_key = master_xprv.derive_priv(&path)?; Ok(auto_accept_key) } @@ -151,10 +148,7 @@ mod tests { let master_seed = [2u8; 64]; let master_xprv = ExtendedPrivKey::new_master(network, &master_seed).unwrap(); - let xpub = ExtendedPubKey::from_priv( - &dash_sdk::dpp::dashcore::secp256k1::Secp256k1::new(), - &master_xprv, - ); + let xpub = ExtendedPubKey::from_priv(&master_xprv); let account_ref = calculate_account_reference(&secret_key, &xpub, 0, 0); @@ -255,8 +249,8 @@ mod tests { // Different timestamps should produce different keys assert_ne!( - key1.private_key.secret_bytes(), - key2.private_key.secret_bytes(), + key1.private_key.to_bytes(), + key2.private_key.to_bytes(), "Different timestamps should produce different keys" ); } @@ -300,8 +294,8 @@ mod tests { let master_seed = [2u8; 64]; let master_xprv = ExtendedPrivKey::new_master(network, &master_seed).unwrap(); - let secp = dash_sdk::dpp::dashcore::secp256k1::Secp256k1::new(); - let xpub = ExtendedPubKey::from_priv(&secp, &master_xprv); + + let xpub = ExtendedPubKey::from_priv(&master_xprv); // Test version 0 let ref_v0 = calculate_account_reference(&secret_key, &xpub, 0, 0); @@ -335,8 +329,8 @@ mod tests { .expect("Should derive xpub for account 1"); assert_ne!( - xpub_account0.public_key.serialize(), - xpub_account1.public_key.serialize(), + xpub_account0.public_key.to_compressed(), + xpub_account1.public_key.to_compressed(), "Different accounts should produce different xpubs" ); } @@ -373,8 +367,8 @@ mod tests { assert_eq!(xpub_testnet.network, Network::Testnet); assert_eq!(xpub_mainnet.network, Network::Mainnet); assert_ne!( - xpub_testnet.public_key.serialize(), - xpub_mainnet.public_key.serialize(), + xpub_testnet.public_key.to_compressed(), + xpub_mainnet.public_key.to_compressed(), "different coin types must produce different keys" ); } diff --git a/src/model/dashpay_derivation/mod.rs b/src/model/dashpay_derivation/mod.rs index 4363ffe6a..24a2aaad5 100644 --- a/src/model/dashpay_derivation/mod.rs +++ b/src/model/dashpay_derivation/mod.rs @@ -24,5 +24,9 @@ pub enum DerivationError { /// A secp256k1 operation on derived key material failed. #[error("The wallet key material could not be processed. The wallet data may be invalid.")] - Secp256k1(#[from] dash_sdk::dpp::dashcore::secp256k1::Error), + Secp256k1(#[from] dash_sdk::dpp::secp256k1::Error), + + /// An ECDSA operation on derived key material failed. + #[error("The wallet key material could not be processed. The wallet data may be invalid.")] + Ecdsa(#[from] dash_sdk::dpp::dashcore::ecdsa::EcdsaError), } diff --git a/src/model/key_input.rs b/src/model/key_input.rs index af7008b5b..cf7793f29 100644 --- a/src/model/key_input.rs +++ b/src/model/key_input.rs @@ -60,7 +60,7 @@ pub fn verify_key_input( key_name: key_name.to_owned(), }), 51 | 52 => PrivateKey::from_wif(private_key) - .map(|key| Some(key.inner.secret_bytes())) + .map(|key| Some(*key.inner.to_bytes())) .map_err(|_| KeyInputError::BadWif { key_name: key_name.to_owned(), }), diff --git a/src/model/qualified_identity/mod.rs b/src/model/qualified_identity/mod.rs index e8d48b64d..4166ab369 100644 --- a/src/model/qualified_identity/mod.rs +++ b/src/model/qualified_identity/mod.rs @@ -18,7 +18,6 @@ use crate::model::wallet::{Wallet, WalletSeedHash}; use bincode::{Decode, Encode}; use dash_sdk::dashcore_rpc::dashcore::{PubkeyHash, signer}; use dash_sdk::dpp::async_trait::async_trait; -use dash_sdk::dpp::bls_signatures::{Bls12381G2Impl, SignatureSchemes}; use dash_sdk::dpp::dashcore::address::Payload; use dash_sdk::dpp::dashcore::hashes::Hash; use dash_sdk::dpp::dashcore::{Address, Network, ScriptHash}; @@ -35,7 +34,7 @@ use dash_sdk::dpp::key_wallet::bip32::ChildNumber; use dash_sdk::dpp::platform_value::BinaryData; use dash_sdk::dpp::platform_value::string_encoding::Encoding; use dash_sdk::dpp::state_transition::errors::InvalidIdentityPublicKeyTypeError; -use dash_sdk::dpp::{ProtocolError, bls_signatures, ed25519_dalek}; +use dash_sdk::dpp::{ProtocolError, bls, ed25519_dalek}; use dash_sdk::platform::IdentityPublicKey; use std::collections::{BTreeMap, HashSet}; use std::fmt::{Display, Formatter}; @@ -445,13 +444,12 @@ impl Signer for QualifiedIdentity { if identity_public_key.key_type() == KeyType::ECDSA_HASH160 { use dash_sdk::dpp::dashcore::PublicKey; use dash_sdk::dpp::dashcore::hashes::{Hash, ripemd160, sha256}; - use dash_sdk::dpp::dashcore::secp256k1::{Secp256k1, SecretKey}; + use dash_sdk::dpp::secp256k1::SecretKey; let platform_key_data = identity_public_key.data().as_slice(); - if let Ok(secret_key) = SecretKey::from_slice(&private_key[..]) { - let secp = Secp256k1::new(); - let derived_pubkey = PublicKey::new(secret_key.public_key(&secp)); + if let Ok(secret_key) = SecretKey::from_secret_bytes(*private_key) { + let derived_pubkey = PublicKey::new(secret_key.public_key()); let pubkey_bytes = derived_pubkey.to_bytes(); let sha256_hash = sha256::Hash::hash(&pubkey_bytes); let hash160 = ripemd160::Hash::hash(sha256_hash.as_byte_array()); @@ -482,17 +480,10 @@ impl Signer for QualifiedIdentity { Ok(signature.to_vec().into()) } KeyType::BLS12_381 => { - let pk = bls_signatures::SecretKey::::from_be_bytes(&private_key) - .into_option() - .ok_or(ProtocolError::Generic( - "bls private key from bytes isn't correct".to_string(), - ))?; - Ok(pk - .sign(SignatureSchemes::Basic, data)? - .as_raw_value() - .to_compressed() - .to_vec() - .into()) + let pk = bls::SecretKey::from_be_bytes(&private_key).ok_or( + ProtocolError::Generic("bls private key from bytes isn't correct".to_string()), + )?; + Ok(pk.sign(data)?.to_bytes().to_vec().into()) } KeyType::EDDSA_25519_HASH160 => { let pk = ed25519_dalek::SigningKey::from(&*private_key); @@ -990,7 +981,7 @@ impl QualifiedIdentity { ) -> Result, ProtocolError> { use dash_sdk::dpp::dashcore::PublicKey; use dash_sdk::dpp::dashcore::hashes::{Hash, ripemd160, sha256}; - use dash_sdk::dpp::dashcore::secp256k1::Secp256k1; + use dash_sdk::dpp::key_wallet::bip32::{DerivationPath as DP, KeyDerivationType}; let (Some(secret_access), Some(seed_hash)) = @@ -1012,7 +1003,7 @@ impl QualifiedIdentity { let Some(seed) = plaintext.expose_hd_seed() else { return Ok(None); }; - let secp = Secp256k1::new(); + for identity_index in 0..10u32 { let correct_path = DP::identity_authentication_path( network, @@ -1025,8 +1016,7 @@ impl QualifiedIdentity { else { continue; }; - let correct_pubkey = - PublicKey::new(extended_key.private_key.public_key(&secp)); + let correct_pubkey = PublicKey::new(extended_key.private_key.public_key()); let correct_hash = ripemd160::Hash::hash( sha256::Hash::hash(&correct_pubkey.to_bytes()).as_byte_array(), ); @@ -1038,7 +1028,7 @@ impl QualifiedIdentity { "Using corrected derivation path for signing (found via scan)" ); let signature = - signer::sign(&data, &extended_key.private_key.secret_bytes()) + signer::sign(&data, extended_key.private_key.to_bytes().as_slice()) .map_err(|_| TaskError::EncryptionError { detail: "Failed to sign with the recovered derivation path." @@ -2022,6 +2012,34 @@ mod key_resolution_tests { } } + #[tokio::test] + async fn bls_signing_preserves_the_previous_sdk_basic_signature_bytes() { + // Generated with the previous pin's blsful Basic signer using this synthetic key. + let secret = [0x11; 32]; + let public = bls::SecretKey::from_be_bytes(&secret).unwrap().public_key(); + let key = IdentityPublicKey::V0(IdentityPublicKeyV0 { + id: 3, + purpose: Purpose::AUTHENTICATION, + security_level: SecurityLevel::HIGH, + contract_bounds: None, + key_type: KeyType::BLS12_381, + read_only: false, + data: BinaryData::new(public.to_bytes().to_vec()), + disabled_at: None, + }); + let identity = masternode_with(&key, &[(MAIN, PrivateKeyData::Clear(secret))]); + let signature = identity + .sign(&key, b"DET BLS adapter parity") + .await + .unwrap(); + assert_eq!( + hex::encode(signature.as_slice()), + "94765bd6758b7cd55024a38dc591f3090e9535d97a94ae7674239c559b4733e64dcae72c3d50d96788dfb0475e921bd608410c7a556ab0fab22f9ceb15139c54f8cf8a34e3900f7ab58c6497d3e9b7f6e04445237da30cf635550276eb981c45" + ); + let invalid = masternode_with(&key, &[(MAIN, PrivateKeyData::Clear([0; 32]))]); + assert!(invalid.sign(&key, b"DET BLS adapter parity").await.is_err()); + } + /// T0 — a `Purpose::VOTING` key on the main identity must be signable /// wherever its private half is filed. Held under `Main` is what the /// authoritative loader writes and what the structural target names; held diff --git a/src/model/qualified_identity/qualified_identity_public_key.rs b/src/model/qualified_identity/qualified_identity_public_key.rs index dbfd56424..b5e1e0966 100644 --- a/src/model/qualified_identity/qualified_identity_public_key.rs +++ b/src/model/qualified_identity/qualified_identity_public_key.rs @@ -132,11 +132,11 @@ fn find_wallet_path( mod tests { use super::*; use dash_sdk::dpp::dashcore::hashes::Hash; - use dash_sdk::dpp::dashcore::secp256k1::{Secp256k1, SecretKey}; use dash_sdk::dpp::identity::identity_public_key::v0::IdentityPublicKeyV0; use dash_sdk::dpp::identity::{Purpose, SecurityLevel}; use dash_sdk::dpp::key_wallet::bip32::{DerivationPath, KeyDerivationType}; use dash_sdk::dpp::platform_value::BinaryData; + use dash_sdk::dpp::secp256k1::SecretKey; const TEST_SEED: [u8; 64] = [7u8; 64]; @@ -145,9 +145,9 @@ mod tests { fn pubkey(n: u8) -> PublicKey { let mut sk_bytes = [3u8; 32]; sk_bytes[31] = n.max(1); - let secp = Secp256k1::new(); - let sk = SecretKey::from_slice(&sk_bytes).expect("valid secret key"); - PublicKey::new(sk.public_key(&secp)) + + let sk = SecretKey::from_secret_bytes(sk_bytes).expect("valid secret key"); + PublicKey::new(sk.public_key()) } /// An identity public key of `key_type` carrying `data`. `id`/`purpose`/ diff --git a/src/model/single_key.rs b/src/model/single_key.rs index 85b21b8b0..27951c958 100644 --- a/src/model/single_key.rs +++ b/src/model/single_key.rs @@ -10,7 +10,7 @@ //! (spending gated) until single-key moves onto the upstream wallet runtime. use dash_sdk::dpp::dashcore::key::Error as KeyError; -use dash_sdk::dpp::dashcore::secp256k1::Secp256k1; + use dash_sdk::dpp::dashcore::{Address, Network, PrivateKey, PublicKey}; use serde::{Deserialize, Serialize}; @@ -22,10 +22,10 @@ use serde::{Deserialize, Serialize}; /// task is the authoritative enforcement layer. pub fn validate_wif(wif: &str, network: Network) -> Result { let priv_key = PrivateKey::from_wif(wif)?; - let secp = Secp256k1::new(); + let pub_key = PublicKey { compressed: priv_key.compressed, - inner: priv_key.inner.public_key(&secp), + inner: priv_key.inner.public_key(), }; let address = Address::p2pkh(&pub_key, network); Ok(address.to_string()) diff --git a/src/model/wallet/auth_pubkey_cache.rs b/src/model/wallet/auth_pubkey_cache.rs index 374653d82..4dcb8284a 100644 --- a/src/model/wallet/auth_pubkey_cache.rs +++ b/src/model/wallet/auth_pubkey_cache.rs @@ -117,7 +117,7 @@ impl AuthPubkeyCache { public_key: &PublicKey, ) -> bool { let coord = AuthKeyCoord::new(network, identity_index, key_index); - let bytes = public_key.inner.serialize().to_vec(); + let bytes = public_key.inner.to_compressed().to_vec(); match self.entries.entry(coord) { Entry::Occupied(e) if *e.get() == bytes => false, Entry::Occupied(mut e) => { @@ -156,15 +156,12 @@ impl std::fmt::Debug for AuthPubkeyCache { #[cfg(test)] mod tests { use super::*; - use dash_sdk::dpp::dashcore::secp256k1::{ - PublicKey as Secp256k1PublicKey, Secp256k1, SecretKey, - }; + use dash_sdk::dpp::secp256k1::{PublicKey as Secp256k1PublicKey, SecretKey}; /// Deterministic test public key from a one-byte seed. fn pubkey(seed: u8) -> PublicKey { - let secp = Secp256k1::new(); - let sk = SecretKey::from_slice(&[seed.max(1); 32]).expect("valid secret key"); - let inner = Secp256k1PublicKey::from_secret_key(&secp, &sk); + let sk = SecretKey::from_secret_bytes([seed.max(1); 32]).expect("valid secret key"); + let inner = Secp256k1PublicKey::from_secret_key(&sk); PublicKey::new(inner) } @@ -187,7 +184,7 @@ mod tests { assert!(cache.insert(Network::Mainnet, 1, 2, &pk)); let got = cache.get(Network::Mainnet, 1, 2).expect("hit"); assert_eq!(got, pk); - assert_eq!(got.inner.serialize(), pk.inner.serialize()); + assert_eq!(got.inner.to_compressed(), pk.inner.to_compressed()); } /// AUTH-CACHE-003 — coordinates are network-keyed: the same indices diff --git a/src/model/wallet/mod.rs b/src/model/wallet/mod.rs index d84ab6ac2..600c585e9 100644 --- a/src/model/wallet/mod.rs +++ b/src/model/wallet/mod.rs @@ -22,7 +22,6 @@ use dash_sdk::dpp::prelude::AddressNonce; use dash_sdk::platform::address_sync::{AddressFunds, AddressIndex, AddressProvider}; use platform_wallet_storage::secrets::{MAX_PASSPHRASE_LEN, MIN_PASSPHRASE_LEN}; -use dash_sdk::dpp::dashcore::secp256k1::Secp256k1; use dash_sdk::dpp::dashcore::{ Address, BlockHash, Network, PrivateKey, PublicKey, Transaction, Txid, }; @@ -486,14 +485,13 @@ impl Wallet { } })?; let bip44_path = Self::bip44_account0_path(network); - let secp = Secp256k1::new(); - let account_priv = master_priv.derive_priv(&secp, &bip44_path).map_err(|e| { + + let account_priv = master_priv.derive_priv(&bip44_path).map_err(|e| { WalletCreationError::KeyDerivation { source: Box::new(e), } })?; - let master_bip44_ecdsa_extended_public_key = - ExtendedPubKey::from_priv(&secp, &account_priv); + let master_bip44_ecdsa_extended_public_key = ExtendedPubKey::from_priv(&account_priv); // Cache the DIP-17 platform-payment account xpub up front (like the // BIP44 one above) so platform addresses stay reverse-derivable — @@ -511,7 +509,7 @@ impl Wallet { // Derive the first receive address (m/44'/coin'/0'/0/0) let (known_addresses, watched_addresses) = - Self::derive_first_address(&master_bip44_ecdsa_extended_public_key, network, &secp) + Self::derive_first_address(&master_bip44_ecdsa_extended_public_key, network) .map_err(|e| WalletCreationError::KeyDerivation { source: e.into() })?; Ok(Wallet { @@ -551,7 +549,6 @@ impl Wallet { fn derive_first_address( master_pub: &ExtendedPubKey, network: Network, - secp: &Secp256k1, ) -> Result< ( BTreeMap, @@ -570,7 +567,7 @@ impl Wallet { .as_slice(), ); - let pk = master_pub.derive_pub(secp, &address_path)?; + let pk = master_pub.derive_pub(&address_path)?; let address = Address::p2pkh(&pk.to_pub(), network); let bip44 = match network { Network::Mainnet => &DASH_BIP44_ACCOUNT_0_PATH_MAINNET, @@ -1036,18 +1033,13 @@ impl Wallet { for wallet in slice { let wallet_ref = wallet.read_recover(); if wallet_ref.seed_hash() == wallet_seed_hash { - // SECURITY: `ExtendedPrivKey` is a `Copy` BIP-32 type from - // key_wallet with no `Drop`, so its inner SecretKey + ChainCode - // cannot be wiped by RAII here. Extract the key straight into a - // `Zeroizing` buffer and never bind the intermediate to a named - // local; the transient copy left on the stack is the - // unavoidable residue of a third-party `Copy` type. - let secret = Zeroizing::new( - derivation_path - .derive_priv_ecdsa_for_master_seed(seed, network)? - .private_key - .secret_bytes(), - ); + // SECURITY: extract the key straight into a `Zeroizing` buffer + // and never bind the intermediate `ExtendedPrivKey` to a named + // local; its secret key wipes itself when the temporary drops. + let secret = derivation_path + .derive_priv_ecdsa_for_master_seed(seed, network)? + .private_key + .to_bytes(); return Ok(Some(secret)); } } @@ -1243,7 +1235,7 @@ impl Wallet { key_index: u32, public_key: &PublicKey, ) -> Result<(), WalletError> { - public_key_result_map.insert(public_key.inner.serialize().to_vec(), key_index); + public_key_result_map.insert(public_key.inner.to_compressed().to_vec(), key_index); public_key_hash_result_map.insert(public_key.pubkey_hash().to_byte_array(), key_index); if register_addresses { let derivation_path = DerivationPath::identity_authentication_path( @@ -1271,8 +1263,7 @@ impl Wallet { path_reference: DerivationPathReference, app_context: &AppContext, ) -> Result<(), WalletError> { - let secp = Secp256k1::new(); - let address = Address::p2pkh(&private_key.public_key(&secp), app_context.network); + let address = Address::p2pkh(&private_key.public_key(), app_context.network); self.register_address( address, derivation_path, @@ -1350,7 +1341,7 @@ impl Wallet { app_context: &AppContext, ) -> Result<(), WalletError> { let coin_type = coin_type_for_network(network); - let secp = Secp256k1::new(); + for (change_flag, max) in [ (false, BOOTSTRAP_BIP44_EXTERNAL_COUNT), (true, BOOTSTRAP_BIP44_CHANGE_COUNT), @@ -1364,8 +1355,8 @@ impl Wallet { ]; let derived = self .master_bip44_ecdsa_extended_public_key - .derive_pub(&secp, &child_path)?; - let dash_public_key = PublicKey::from_slice(&derived.public_key.serialize()) + .derive_pub(&child_path)?; + let dash_public_key = PublicKey::from_slice(&derived.public_key.to_compressed()) .map_err(|e| WalletError::PublicKeyParse(Box::new(e)))?; let derivation_path = DerivationPath::from(vec![ ChildNumber::Hardened { index: 44 }, @@ -1640,8 +1631,8 @@ impl Wallet { let private_key = extended_private_key.to_priv(); // Create a P2PKH address for platform payment - let secp = Secp256k1::new(); - let public_key = private_key.public_key(&secp); + + let public_key = private_key.public_key(); let platform_address = Address::p2pkh(&public_key, network); let canonical = Wallet::canonical_address(&platform_address, network); @@ -1699,7 +1690,6 @@ impl Wallet { seed: &[u8; 64], network: Network, ) -> Result { - let secp = Secp256k1::new(); let account = 0u32; let key_class = 0u32; @@ -1716,7 +1706,7 @@ impl Wallet { let extended_private_key = derivation_path.derive_priv_ecdsa_for_master_seed(seed, network)?; let private_key = extended_private_key.to_priv(); - let public_key = private_key.public_key(&secp); + let public_key = private_key.public_key(); // Create a P2PKH address for platform payment let platform_address = Address::p2pkh(&public_key, network); @@ -1733,7 +1723,6 @@ impl Wallet { change: bool, address_index: u32, ) -> Result { - let secp = Secp256k1::new(); let path_extension = [ ChildNumber::Normal { index: change as u32, @@ -1744,7 +1733,7 @@ impl Wallet { ]; let public_key = self .master_bip44_ecdsa_extended_public_key - .derive_pub(&secp, &path_extension)? + .derive_pub(&path_extension)? .to_pub(); Ok(Address::p2pkh(&public_key, network)) } @@ -1920,9 +1909,8 @@ impl Wallet { .max(DEFAULT_GAP_LIMIT) .saturating_add(PLATFORM_RECONCILE_SCAN_MARGIN); - let secp = Secp256k1::new(); for index in 0..=ceiling { - let child = match account_xpub.derive_pub(&secp, &[ChildNumber::Normal { index }]) { + let child = match account_xpub.derive_pub(&[ChildNumber::Normal { index }]) { Ok(child) => child, Err(e) => { tracing::warn!( @@ -2024,10 +2012,10 @@ pub(crate) fn derive_platform_payment_account_xpub( ChildNumber::Hardened { index: account }, ChildNumber::Hardened { index: key_class }, ]); - let secp = Secp256k1::new(); + let master = ExtendedPrivKey::new_master(network, seed)?; - let account_priv = master.derive_priv(&secp, &account_path)?; - Ok(ExtendedPubKey::from_priv(&secp, &account_priv)) + let account_priv = master.derive_priv(&account_path)?; + Ok(ExtendedPubKey::from_priv(&account_priv)) } /// Provider for wallet Platform addresses that implements AddressProvider for SDK address sync. @@ -2217,10 +2205,9 @@ impl WalletAddressProvider { &self, index: AddressIndex, ) -> Result<(PlatformAddress, Address), WalletError> { - let secp = Secp256k1::new(); let child = self .account_xpub - .derive_pub(&secp, &[ChildNumber::Normal { index }])?; + .derive_pub(&[ChildNumber::Normal { index }])?; let public_key = child.to_pub(); // Create P2PKH address @@ -2346,7 +2333,6 @@ pub(crate) mod test_support { pub(crate) fn open_wallet() -> Wallet { let seed = TEST_SEED; let network = Network::Testnet; - let secp = Secp256k1::new(); // Derive master private key from seed let master_private_key = @@ -2359,10 +2345,10 @@ pub(crate) mod test_support { ChildNumber::Hardened { index: 0 }, ]); let bip44_account_private = master_private_key - .derive_priv(&secp, &bip44_account_path) + .derive_priv(&bip44_account_path) .expect("bip44 derivation"); let master_bip44_ecdsa_extended_public_key = - ExtendedPubKey::from_priv(&secp, &bip44_account_private); + ExtendedPubKey::from_priv(&bip44_account_private); let seed_hash = ClosedKeyItem::compute_seed_hash(&seed); @@ -2409,12 +2395,12 @@ mod tests { /// Helper: create a test address on Testnet fn test_address(index: u8) -> Address { - use dash_sdk::dpp::dashcore::secp256k1::SecretKey; - let secp = Secp256k1::new(); + use dash_sdk::dpp::secp256k1::SecretKey; + let mut sk_bytes = [0u8; 32]; sk_bytes[0] = if index == 0 { 1 } else { index }; - let sk = SecretKey::from_slice(&sk_bytes).expect("valid secret key"); - let inner = dash_sdk::dpp::dashcore::secp256k1::PublicKey::from_secret_key(&secp, &sk); + let sk = SecretKey::from_secret_bytes(sk_bytes).expect("valid secret key"); + let inner = dash_sdk::dpp::secp256k1::PublicKey::from_secret_key(&sk); let pubkey = PublicKey::from_slice(&inner.serialize()).expect("valid pubkey"); Address::p2pkh(&pubkey, Network::Testnet) } @@ -2964,7 +2950,10 @@ mod tests { .expect("cache hit"); assert_eq!(from_cache, from_seed); - assert_eq!(from_cache.inner.serialize(), from_seed.inner.serialize()); + assert_eq!( + from_cache.inner.to_compressed(), + from_seed.inner.to_compressed() + ); assert_eq!(from_cache.pubkey_hash(), from_seed.pubkey_hash()); } } @@ -3122,8 +3111,8 @@ mod tests { .derive_priv_ecdsa_for_master_seed(&seed, network) .expect("reference derive") .to_priv(); - let secp = Secp256k1::new(); - let address = Address::p2pkh(&reference.public_key(&secp), network); + + let address = Address::p2pkh(&reference.public_key(), network); let mut wallet = wallet; wallet.known_addresses.insert(address.clone(), path); @@ -3150,8 +3139,8 @@ mod tests { let reference_xprv = reference_path .derive_priv_ecdsa_for_master_seed(&seed, network) .expect("reference derive"); - let secp = Secp256k1::new(); - let reference = Address::p2pkh(&reference_xprv.to_priv().public_key(&secp), network); + + let reference = Address::p2pkh(&reference_xprv.to_priv().public_key(), network); let param = param_wallet .generate_platform_receive_address_with_seed(&seed, network) @@ -3175,11 +3164,11 @@ mod tests { let slice = vec![Arc::new(RwLock::new(wallet))]; let path = DerivationPath::identity_registration_path(network, 0); - let reference = path + let reference = *path .derive_priv_ecdsa_for_master_seed(&seed, network) .expect("reference derive") .private_key - .secret_bytes(); + .to_bytes(); let param = Wallet::derive_private_key_in_arc_rw_lock_slice_with_seed( &slice, seed_hash, &seed, &path, network, ) @@ -3550,7 +3539,6 @@ mod tests { let wallet = Wallet::new_from_seed(seed, network, None, None).expect("wallet"); let provider = WalletAddressProvider::new(&wallet, network, &seed).expect("provider"); - let secp = Secp256k1::new(); for index in 0u32..DEFAULT_GAP_LIMIT { // Legacy seed-based derivation (what the old provider and the // platform signer used). @@ -3559,7 +3547,7 @@ mod tests { .derive_priv_ecdsa_for_master_seed(&seed, network) .expect("legacy derive") .to_priv(); - let legacy_address = Address::p2pkh(&legacy_priv.public_key(&secp), network); + let legacy_address = Address::p2pkh(&legacy_priv.public_key(), network); // Provider's xpub-based derivation. let (_platform, provider_address) = provider @@ -3795,11 +3783,11 @@ mod tests { /// for. Independent of the reconciliation path under test. fn platform_address_at(index: u32, network: Network) -> Address { let path = DerivationPath::platform_payment_path(network, 0, 0, index); - let secp = Secp256k1::new(); + let xprv = path .derive_priv_ecdsa_for_master_seed(&TEST_SEED, network) .expect("derive platform key"); - Address::p2pkh(&xprv.to_priv().public_key(&secp), network) + Address::p2pkh(&xprv.to_priv().public_key(), network) } /// `new_from_seed` caches the platform-payment account xpub eagerly, and the @@ -4022,11 +4010,11 @@ mod tests { let foreign_seed = [0x11u8; 64]; let path = DerivationPath::platform_payment_path(network, 0, 0, 0); - let secp = Secp256k1::new(); + let xprv = path .derive_priv_ecdsa_for_master_seed(&foreign_seed, network) .expect("derive foreign key"); - let foreign = Address::p2pkh(&xprv.to_priv().public_key(&secp), network); + let foreign = Address::p2pkh(&xprv.to_priv().public_key(), network); assert!(!wallet.reconcile_platform_address(&foreign, network)); assert!(!wallet.known_addresses.contains_key(&foreign)); diff --git a/src/model/wallet/single_key.rs b/src/model/wallet/single_key.rs index eaf01b82a..9cb27bbca 100644 --- a/src/model/wallet/single_key.rs +++ b/src/model/wallet/single_key.rs @@ -10,7 +10,7 @@ use aes_gcm::aead::Aead; use aes_gcm::{Aes256Gcm, KeyInit}; -use dash_sdk::dpp::dashcore::secp256k1::Secp256k1; + use dash_sdk::dpp::dashcore::{Address, Network, OutPoint, PrivateKey, PublicKey, TxOut}; use sha2::{Digest, Sha256}; use std::collections::HashMap; @@ -233,12 +233,10 @@ impl SingleKeyWallet { password: Option<&str>, alias: Option, ) -> Result { - let secp = Secp256k1::new(); - // Create PrivateKey and derive public key and address let private_key = PrivateKey::from_byte_array(&private_key_bytes, network).map_err(|e| e.to_string())?; - let public_key = private_key.public_key(&secp); + let public_key = private_key.public_key(); let address = Address::p2pkh(&public_key, network); let key_hash = ClosedSingleKey::compute_key_hash(&private_key_bytes); @@ -305,7 +303,7 @@ impl SingleKeyWallet { let private_key = PrivateKey::from_wif(wif).map_err(|e| e.to_string())?; let network = private_key.network; let mut key_bytes = [0u8; 32]; - key_bytes.copy_from_slice(&private_key.inner[..]); + key_bytes.copy_from_slice(&*private_key.inner.to_bytes()); Self::new(key_bytes, network, password, alias) } @@ -518,8 +516,8 @@ mod tests { // Through a SingleKeyWallet that holds the closed key. let priv_key = PrivateKey::from_byte_array(&secret, Network::Testnet).expect("valid key bytes"); - let secp = Secp256k1::new(); - let public_key = priv_key.public_key(&secp); + + let public_key = priv_key.public_key(); let address = Address::p2pkh(&public_key, Network::Testnet); let wallet = SingleKeyWallet { private_key_data: data, diff --git a/src/ui/components/address_input.rs b/src/ui/components/address_input.rs index b732e6782..9dba6378d 100644 --- a/src/ui/components/address_input.rs +++ b/src/ui/components/address_input.rs @@ -1483,15 +1483,15 @@ fn row_accessible_label(entry: &AddressEntry, address_display: &str, balance_str #[cfg(test)] mod tests { use super::*; - use dash_sdk::dashcore_rpc::dashcore::secp256k1::{Secp256k1, SecretKey}; use dash_sdk::dashcore_rpc::dashcore::{PrivateKey, PublicKey}; + use dash_sdk::dpp::secp256k1::SecretKey; /// Generate a valid testnet P2PKH address for testing. fn testnet_core_address() -> (String, Address) { - let secp = Secp256k1::new(); - let sk = SecretKey::from_slice(&[1u8; 32]).unwrap(); - let privkey = PrivateKey::new(sk, Network::Testnet); - let pubkey = PublicKey::from_private_key(&secp, &privkey); + let sk = SecretKey::from_secret_bytes([1u8; 32]).unwrap(); + let privkey = PrivateKey::from_byte_array(sk.as_secret_bytes(), Network::Testnet) + .expect("valid secret key"); + let pubkey = PublicKey::from_private_key(&privkey); let addr = Address::p2pkh(&pubkey, Network::Testnet); (addr.to_string(), addr) } @@ -1513,10 +1513,10 @@ mod tests { /// Generate a valid mainnet P2PKH address for testing. fn mainnet_core_address() -> (String, Address) { - let secp = Secp256k1::new(); - let sk = SecretKey::from_slice(&[2u8; 32]).unwrap(); - let privkey = PrivateKey::new(sk, Network::Mainnet); - let pubkey = PublicKey::from_private_key(&secp, &privkey); + let sk = SecretKey::from_secret_bytes([2u8; 32]).unwrap(); + let privkey = PrivateKey::from_byte_array(sk.as_secret_bytes(), Network::Mainnet) + .expect("valid secret key"); + let pubkey = PublicKey::from_private_key(&privkey); let addr = Address::p2pkh(&pubkey, Network::Mainnet); (addr.to_string(), addr) } diff --git a/src/ui/identity/funding_common.rs b/src/ui/identity/funding_common.rs index 30250ff74..946af984a 100644 --- a/src/ui/identity/funding_common.rs +++ b/src/ui/identity/funding_common.rs @@ -373,6 +373,7 @@ pub fn asset_lock_status_label(status: &AssetLockStatus) -> &'static str { AssetLockStatus::InstantSendLocked => "Confirmed and ready to use.", AssetLockStatus::ChainLocked => "Confirmed and ready to use.", AssetLockStatus::Consumed => "Already used to fund an identity.", + AssetLockStatus::DrainCompleted => "Already used to transfer the wallet funds.", // Core-side finality is proven, but whether Platform already spent this // lock is unknown, so it must claim neither "ready" nor "already used". AssetLockStatus::RecoveredFromChain => { @@ -679,16 +680,16 @@ mod tests { } use dash_sdk::dpp::dashcore::PublicKey; - use dash_sdk::dpp::dashcore::secp256k1::{Secp256k1, SecretKey}; + use dash_sdk::dpp::secp256k1::SecretKey; /// A distinct testnet p2pkh address keyed off `n` (derived from a valid /// secret key so the pubkey is a real curve point). fn addr(n: u8) -> Address { let mut sk_bytes = [1u8; 32]; sk_bytes[31] = n.max(1); - let secp = Secp256k1::new(); - let sk = SecretKey::from_slice(&sk_bytes).expect("valid secret key"); - let pubkey = PublicKey::new(sk.public_key(&secp)); + + let sk = SecretKey::from_secret_bytes(sk_bytes).expect("valid secret key"); + let pubkey = PublicKey::new(sk.public_key()); Address::p2pkh(&pubkey, Network::Testnet) } diff --git a/src/ui/identity/keys/add_key_screen.rs b/src/ui/identity/keys/add_key_screen.rs index 18819f00b..ae36c722a 100644 --- a/src/ui/identity/keys/add_key_screen.rs +++ b/src/ui/identity/keys/add_key_screen.rs @@ -193,7 +193,7 @@ impl AddKeyScreen { let derivation = DerivedKeyChooser::new(app_context, &identity, KeyType::ECDSA_SECP256K1); let dashpay_contract_id = app_context - .dashpay_contract + .dashpay_contract() .id() .to_string(Encoding::Base58); @@ -244,7 +244,7 @@ impl AddKeyScreen { let derivation = DerivedKeyChooser::new(app_context, &identity, KeyType::ECDSA_SECP256K1); let dashpay_contract_id = app_context - .dashpay_contract + .dashpay_contract() .id() .to_string(Encoding::Base58); @@ -1382,7 +1382,7 @@ mod derived_key_tests { contract_bounds: None, read_only: false, disabled_at: None, - data: dash_sdk::dpp::dashcore::secp256k1::PublicKey::from_slice(&[ + data: dash_sdk::dpp::secp256k1::PublicKey::from_slice(&[ 0x02, 0x79, 0xBE, 0x66, 0x7E, 0xF9, 0xDC, 0xBB, 0xAC, 0x55, 0xA0, 0x62, 0x95, 0xCE, 0x87, 0x0B, 0x07, 0x02, 0x9B, 0xFC, 0xDB, 0x2D, 0xCE, 0x28, 0xD9, 0x59, 0xF2, 0x81, 0x5B, 0x16, 0xF8, 0x17, 0x98, @@ -1674,9 +1674,9 @@ mod derived_key_tests { /// follow the fresh record. #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn derived_key_id_change_refreshes_and_reselects_the_matching_slot() { - use dash_sdk::dpp::dashcore::secp256k1::{PublicKey, Secp256k1, SecretKey}; use dash_sdk::dpp::identity::accessors::IdentitySettersV0; use dash_sdk::dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeySettersV0; + use dash_sdk::dpp::secp256k1::{PublicKey, SecretKey}; let (staged, mut identity) = staged_screen_parts(true).await; identity.identity.set_id(staged.id); @@ -1707,9 +1707,9 @@ mod derived_key_tests { let mut refreshed = identity.clone(); let mut foreign = identity.identity.public_keys()[&0].clone(); foreign.set_id(1); - let secret = SecretKey::from_slice(&[9; 32]).unwrap(); + let secret = SecretKey::from_secret_bytes([9; 32]).unwrap(); foreign.set_data( - PublicKey::from_secret_key(&Secp256k1::new(), &secret) + PublicKey::from_secret_key(&secret) .serialize() .to_vec() .into(), diff --git a/src/ui/identity/keys/key_info_screen.rs b/src/ui/identity/keys/key_info_screen.rs index 87c66fb2a..9c0e3d69c 100644 --- a/src/ui/identity/keys/key_info_screen.rs +++ b/src/ui/identity/keys/key_info_screen.rs @@ -34,8 +34,6 @@ use crate::wallet_backend::secret_seam::SecretScheme; use dash_sdk::dashcore_rpc::dashcore::PrivateKey as RPCPrivateKey; use dash_sdk::dpp::dashcore::address::Payload; use dash_sdk::dpp::dashcore::hashes::Hash; -use dash_sdk::dpp::dashcore::secp256k1::{Message, Secp256k1, SecretKey}; -use dash_sdk::dpp::dashcore::sign_message::{MessageSignature, signed_msg_hash}; use dash_sdk::dpp::dashcore::{Address, PrivateKey, PubkeyHash, ScriptHash}; use dash_sdk::dpp::identity::KeyType; use dash_sdk::dpp::identity::KeyType::BIP13_SCRIPT_HASH; @@ -45,6 +43,7 @@ use dash_sdk::dpp::identity::identity_public_key::accessors::v0::IdentityPublicK use dash_sdk::dpp::identity::identity_public_key::contract_bounds::ContractBounds; use dash_sdk::dpp::key_wallet::bip32::DerivationPath; use dash_sdk::dpp::platform_value::string_encoding::Encoding; +use dash_sdk::dpp::secp256k1::SecretKey; use dash_sdk::platform::IdentityPublicKey; use eframe::egui::{self}; use egui::{Color32, RichText, ScrollArea}; @@ -543,9 +542,9 @@ impl ScreenLike for KeyInfoScreen { .num_columns(2) .spacing([10.0, 10.0]) .show(ui, |ui| { - if let Ok(secret_key) = SecretKey::from_slice(clear) { - let private_key = - PrivateKey::new(secret_key, self.app_context.network); + if let Ok(private_key) = + PrivateKey::from_byte_array(clear, self.app_context.network) + { ui.label( RichText::new("Private Key (WIF):") .strong() @@ -600,8 +599,8 @@ impl ScreenLike for KeyInfoScreen { && self.view_private_key_even_if_encrypted_or_in_wallet && self.selected_wallet.is_some() { - if let Some(private_key) = self.decrypted_private_key { - Self::render_decrypted_key_grid(ui, &private_key); + if let Some(private_key) = &self.decrypted_private_key { + Self::render_decrypted_key_grid(ui, private_key); } else { Self::queue_key_display( &mut self.pending_key_display_request, @@ -622,8 +621,8 @@ impl ScreenLike for KeyInfoScreen { self.view_private_key_even_if_encrypted_or_in_wallet = true; self.view_wallet_unlock = true; } - if let Some(private_key) = self.decrypted_private_key { - Self::render_decrypted_key_grid(ui, &private_key); + if let Some(private_key) = &self.decrypted_private_key { + Self::render_decrypted_key_grid(ui, private_key); } else { Self::queue_key_display( &mut self.pending_key_display_request, @@ -661,8 +660,8 @@ impl ScreenLike for KeyInfoScreen { .color(text_primary), ); ui.add_space(10.0); - if let Some(private_key) = self.decrypted_private_key { - Self::render_decrypted_key_grid(ui, &private_key); + if let Some(private_key) = &self.decrypted_private_key { + Self::render_decrypted_key_grid(ui, private_key); } else if ui.button("View Private Key").clicked() { self.pending_identity_key_display = true; self.key_display_requested = true; @@ -1109,7 +1108,7 @@ impl KeyInfoScreen { return; } Err(_) => match PrivateKey::from_wif(self.private_key_input.text()) { - Ok(key) => key.inner.secret_bytes(), + Ok(key) => *key.inner.to_bytes(), Err(_) => { MessageBanner::set_global( self.app_context.egui_ctx(), @@ -1294,13 +1293,9 @@ impl KeyInfoScreen { /// are compressed by convention, so a verifier can recover the signer's /// public key and address from the signature alone. fn sign_ecdsa_local(private_key_bytes: &[u8; 32], message: &str) -> String { - let secp = Secp256k1::new(); - let message_hash = signed_msg_hash(message); - let digest = Message::from_digest(*message_hash.as_byte_array()); - let secret_key = SecretKey::from_byte_array(private_key_bytes) + let secret_key = SecretKey::from_secret_bytes(*private_key_bytes) .expect("clear private key is a valid 32-byte secret"); - let recoverable = secp.sign_ecdsa_recoverable(&digest, &secret_key); - MessageSignature::new(recoverable, true).to_base64() + crate::backend_task::wallet::dash_signed_message(message, &secret_key, true) } /// Render the WIF + hex of an already-derived private key. The key is @@ -1325,7 +1320,7 @@ impl KeyInfoScreen { .strong() .color(ui.visuals().text_color()), ); - let private_key_hex = Secret::new(hex::encode(private_key.inner.secret_bytes())); + let private_key_hex = Secret::new(hex::encode(private_key.inner.to_bytes())); ui.label( RichText::new(private_key_hex.expose_secret()).color(ui.visuals().text_color()), ); @@ -2284,10 +2279,11 @@ mod tests { /// passes and the placement check is what decides the outcome. fn keypair(id: KeyID, purpose: Purpose) -> (IdentityPublicKey, [u8; 32]) { let secret_bytes = [0x2A; 32]; - let secp = Secp256k1::new(); - let secret = SecretKey::from_byte_array(&secret_bytes).expect("a valid secret key"); - let data = PrivateKey::new(secret, Network::Testnet) - .public_key(&secp) + + let secret = SecretKey::from_secret_bytes(secret_bytes).expect("a valid secret key"); + let data = PrivateKey::from_byte_array(secret.as_secret_bytes(), Network::Testnet) + .expect("valid secret key") + .public_key() .to_bytes(); ( IdentityPublicKey::V0(IdentityPublicKeyV0 { diff --git a/src/ui/state/account_summary.rs b/src/ui/state/account_summary.rs index 6f004b36e..a38b29c59 100644 --- a/src/ui/state/account_summary.rs +++ b/src/ui/state/account_summary.rs @@ -365,14 +365,12 @@ mod tests { /// valid address to stand in for a funded-but-unindexed chain address. fn addr_from_byte(b: u8) -> Address { use dash_sdk::dpp::dashcore::PublicKey; - use dash_sdk::dpp::dashcore::secp256k1::{ - PublicKey as SecpPublicKey, Secp256k1, SecretKey, - }; - let secp = Secp256k1::new(); + use dash_sdk::dpp::secp256k1::{PublicKey as SecpPublicKey, SecretKey}; + let mut sk_bytes = [0u8; 32]; sk_bytes[0] = if b == 0 { 1 } else { b }; - let sk = SecretKey::from_slice(&sk_bytes).expect("valid secret key"); - let pubkey = PublicKey::from_slice(&SecpPublicKey::from_secret_key(&secp, &sk).serialize()) + let sk = SecretKey::from_secret_bytes(sk_bytes).expect("valid secret key"); + let pubkey = PublicKey::from_slice(&SecpPublicKey::from_secret_key(&sk).serialize()) .expect("valid pubkey"); Address::p2pkh(&pubkey, Network::Testnet) } diff --git a/src/ui/wallets/send_screen.rs b/src/ui/wallets/send_screen.rs index 9c0cad012..754662548 100644 --- a/src/ui/wallets/send_screen.rs +++ b/src/ui/wallets/send_screen.rs @@ -5141,10 +5141,10 @@ mod tests { use crate::database::test_helpers::create_database_at_path; use crate::utils::tasks::TaskManager; use crate::wallet_backend::AssetLockInputState; - use dash_sdk::dashcore_rpc::dashcore::secp256k1::{Secp256k1, SecretKey}; use dash_sdk::dashcore_rpc::dashcore::{Network, PrivateKey, PublicKey}; use dash_sdk::dpp::dashcore::{OutPoint, Txid, hashes::Hash}; use dash_sdk::dpp::key_wallet::bip32::{ChildNumber, DerivationPath}; + use dash_sdk::dpp::secp256k1::SecretKey; use egui_kittest::Harness; use egui_kittest::kittest::Queryable; use std::cell::RefCell; @@ -5158,10 +5158,11 @@ mod tests { } fn testnet_core_address(key_byte: u8) -> Address { - let secp = Secp256k1::new(); - let secret_key = SecretKey::from_slice(&[key_byte; 32]).expect("valid secret key"); - let private_key = PrivateKey::new(secret_key, Network::Testnet); - let public_key = PublicKey::from_private_key(&secp, &private_key); + let secret_key = SecretKey::from_secret_bytes([key_byte; 32]).expect("valid secret key"); + let private_key = + PrivateKey::from_byte_array(secret_key.as_secret_bytes(), Network::Testnet) + .expect("valid secret key"); + let public_key = PublicKey::from_private_key(&private_key); Address::p2pkh(&public_key, Network::Testnet) } diff --git a/src/ui/wallets/wallets_screen/address_table.rs b/src/ui/wallets/wallets_screen/address_table.rs index 71f50c5e3..ed000b95c 100644 --- a/src/ui/wallets/wallets_screen/address_table.rs +++ b/src/ui/wallets/wallets_screen/address_table.rs @@ -573,16 +573,16 @@ impl WalletsBalancesScreen { mod tests { use super::*; use dash_sdk::dpp::dashcore::PublicKey; - use dash_sdk::dpp::dashcore::secp256k1::{Secp256k1, SecretKey}; + use dash_sdk::dpp::secp256k1::SecretKey; /// A distinct testnet p2pkh address keyed off `n` (derived from a valid /// secret key so the pubkey is a real curve point). fn addr(n: u8) -> Address { let mut sk_bytes = [1u8; 32]; sk_bytes[31] = n.max(1); - let secp = Secp256k1::new(); - let sk = SecretKey::from_slice(&sk_bytes).unwrap(); - let pubkey = PublicKey::new(sk.public_key(&secp)); + + let sk = SecretKey::from_secret_bytes(sk_bytes).unwrap(); + let pubkey = PublicKey::new(sk.public_key()); Address::p2pkh(&pubkey, Network::Testnet) } diff --git a/src/wallet_backend/auth_pubkey_cache.rs b/src/wallet_backend/auth_pubkey_cache.rs index f88a3ad6f..e53c854bc 100644 --- a/src/wallet_backend/auth_pubkey_cache.rs +++ b/src/wallet_backend/auth_pubkey_cache.rs @@ -214,9 +214,7 @@ mod tests { use dash_sdk::dpp::dashcore::PublicKey; use dash_sdk::dpp::dashcore::base58; - use dash_sdk::dpp::dashcore::secp256k1::{ - PublicKey as Secp256k1PublicKey, Secp256k1, SecretKey, - }; + use dash_sdk::dpp::secp256k1::{PublicKey as Secp256k1PublicKey, SecretKey}; use crate::wallet_backend::kv_test_support::InMemoryKv; @@ -225,9 +223,8 @@ mod tests { } fn pubkey(seed: u8) -> PublicKey { - let secp = Secp256k1::new(); - let sk = SecretKey::from_slice(&[seed.max(1); 32]).expect("valid secret key"); - PublicKey::new(Secp256k1PublicKey::from_secret_key(&secp, &sk)) + let sk = SecretKey::from_secret_bytes([seed.max(1); 32]).expect("valid secret key"); + PublicKey::new(Secp256k1PublicKey::from_secret_key(&sk)) } /// AUTH-CACHE-VIEW-001 — a written cache round-trips through `get` diff --git a/src/wallet_backend/contact_profile_cache.rs b/src/wallet_backend/contact_profile_cache.rs index ec1a47df5..4df5156e3 100644 --- a/src/wallet_backend/contact_profile_cache.rs +++ b/src/wallet_backend/contact_profile_cache.rs @@ -21,7 +21,6 @@ use std::sync::Arc; -use dash_sdk::dpp::dashcore::base58; use dash_sdk::platform::Identifier; use crate::backend_task::error::TaskError; @@ -62,7 +61,13 @@ impl CachedContactProfile { /// Build the canonical k/v key for a contact's cached profile. fn key_for(contact: &Identifier) -> String { - format!("{KEY_PREFIX}{}", base58::encode_slice(&contact.to_buffer())) + format!( + "{KEY_PREFIX}{}", + dash_sdk::dpp::platform_value::string_encoding::encode( + &contact.to_buffer(), + dash_sdk::dpp::platform_value::string_encoding::Encoding::Base58 + ) + ) } /// View borrowing a shared [`DetKv`] handle. Cheap to construct. diff --git a/src/wallet_backend/dashpay.rs b/src/wallet_backend/dashpay.rs index 20bc155ab..35edcc0c7 100644 --- a/src/wallet_backend/dashpay.rs +++ b/src/wallet_backend/dashpay.rs @@ -166,41 +166,23 @@ pub(crate) fn derive_contact_info_encryption_keys( let root_path = DerivationPath::from_str(&format!("m/9'/{coin_type}'/15'/0'")).map_err(derive_err)?; - let secp = dash_sdk::dpp::dashcore::secp256k1::Secp256k1::new(); - let root = master_xprv - .derive_priv(&secp, &root_path) - .map_err(derive_err)?; + let root = master_xprv.derive_priv(&root_path).map_err(derive_err)?; // Key1 (encToUserId): root/(2^16)'/index' let key1 = root - .derive_priv( - &secp, - &[ChildNumber::from_hardened_idx(65536).map_err(derive_err)?], - ) + .derive_priv(&[ChildNumber::from_hardened_idx(65536).map_err(derive_err)?]) .map_err(derive_err)? - .derive_priv( - &secp, - &[ChildNumber::from_hardened_idx(derivation_index).map_err(derive_err)?], - ) + .derive_priv(&[ChildNumber::from_hardened_idx(derivation_index).map_err(derive_err)?]) .map_err(derive_err)?; // Key2 (privateData): root/(2^16 + 1)'/index' let key2 = root - .derive_priv( - &secp, - &[ChildNumber::from_hardened_idx(65537).map_err(derive_err)?], - ) + .derive_priv(&[ChildNumber::from_hardened_idx(65537).map_err(derive_err)?]) .map_err(derive_err)? - .derive_priv( - &secp, - &[ChildNumber::from_hardened_idx(derivation_index).map_err(derive_err)?], - ) + .derive_priv(&[ChildNumber::from_hardened_idx(derivation_index).map_err(derive_err)?]) .map_err(derive_err)?; - Ok(( - Zeroizing::new(key1.private_key.secret_bytes()), - Zeroizing::new(key2.private_key.secret_bytes()), - )) + Ok((key1.private_key.to_bytes(), key2.private_key.to_bytes())) } // --------------------------------------------------------------------------- @@ -3338,7 +3320,7 @@ mod tests { assert_eq!( seam.public_key, - local.public_key.serialize(), + local.public_key.to_compressed(), "published public key must match the receive-side xpub on mainnet" ); assert_eq!( @@ -3379,7 +3361,7 @@ mod tests { assert_eq!( seam.public_key, - local.public_key.serialize(), + local.public_key.to_compressed(), "published public key must match the receive-side xpub on testnet" ); assert_eq!( @@ -3421,7 +3403,7 @@ mod tests { assert_eq!( seam.public_key, - local.public_key.serialize(), + local.public_key.to_compressed(), "public key must match send↔receive on {network:?}" ); assert_eq!( @@ -3567,7 +3549,7 @@ mod tests { .expect("receive-side xpub"); assert_eq!( published.public_key, - scanned.public_key.serialize(), + scanned.public_key.to_compressed(), "published xpub must equal the scanned xpub for the selected wallet" ); @@ -3577,7 +3559,7 @@ mod tests { .expect("other-side xpub"); assert_ne!( published.public_key, - other_scanned.public_key.serialize(), + other_scanned.public_key.to_compressed(), "the unselected wallet's seed must derive a different xpub" ); } diff --git a/src/wallet_backend/det_platform_signer.rs b/src/wallet_backend/det_platform_signer.rs index 54b52695c..5f7c4ec63 100644 --- a/src/wallet_backend/det_platform_signer.rs +++ b/src/wallet_backend/det_platform_signer.rs @@ -154,8 +154,9 @@ impl<'a> DetPlatformSigner<'a> { .map_err(|e| ProtocolError::Generic(e.to_string()))?; let private_key = extended_private_key.to_priv(); - let signature = dash_sdk::dpp::dashcore::signer::sign(data, private_key.inner.as_ref()) - .map_err(|e| ProtocolError::Generic(format!("Failed to sign: {}", e)))?; + let signature = + dash_sdk::dpp::dashcore::signer::sign(data, private_key.inner.to_bytes().as_slice()) + .map_err(|e| ProtocolError::Generic(format!("Failed to sign: {}", e)))?; Ok(signature.to_vec()) } @@ -202,7 +203,7 @@ mod tests { use super::*; use crate::model::wallet::{AddressInfo, DerivationPathReference, DerivationPathType}; use dash_sdk::dpp::dashcore::Address; - use dash_sdk::dpp::dashcore::secp256k1::Secp256k1; + use zeroize::Zeroizing; /// Reference platform-address signing — reproduces exactly what the @@ -223,7 +224,7 @@ mod tests { .derive_priv_ecdsa_for_master_seed(seed, network) .expect("derive") .to_priv(); - dash_sdk::dpp::dashcore::signer::sign(data, private_key.inner.as_ref()) + dash_sdk::dpp::dashcore::signer::sign(data, private_key.inner.to_bytes().as_slice()) .expect("reference sign") .to_vec() } @@ -241,8 +242,8 @@ mod tests { let xprv = path .derive_priv_ecdsa_for_master_seed(&*seed, network) .expect("derive platform key"); - let secp = Secp256k1::new(); - let address = Address::p2pkh(&xprv.to_priv().public_key(&secp), network); + + let address = Address::p2pkh(&xprv.to_priv().public_key(), network); let platform_address = PlatformAddress::try_from(address.clone()).expect("to platform address"); @@ -318,10 +319,12 @@ mod tests { .expect("derive") .to_priv(); let data = b"key-parity-vector"; - let reference_sig = - dash_sdk::dpp::dashcore::signer::sign(data, reference_key.inner.as_ref()) - .expect("reference sign") - .to_vec(); + let reference_sig = dash_sdk::dpp::dashcore::signer::sign( + data, + reference_key.inner.to_bytes().as_slice(), + ) + .expect("reference sign") + .to_vec(); // The signer's view of the same address must yield the same key. let det = DetPlatformSigner::from_held(&seed, network, &index); @@ -349,8 +352,8 @@ mod tests { let other_xprv = other_path .derive_priv_ecdsa_for_master_seed(&*seed, network) .unwrap(); - let secp = Secp256k1::new(); - let other_address = Address::p2pkh(&other_xprv.to_priv().public_key(&secp), network); + + let other_address = Address::p2pkh(&other_xprv.to_priv().public_key(), network); let other_platform = PlatformAddress::try_from(other_address).unwrap(); assert!(!det.can_sign_with(&other_platform)); diff --git a/src/wallet_backend/det_signer.rs b/src/wallet_backend/det_signer.rs index e75ff6c43..d0cba01ba 100644 --- a/src/wallet_backend/det_signer.rs +++ b/src/wallet_backend/det_signer.rs @@ -35,9 +35,10 @@ use async_trait::async_trait; use dash_sdk::dpp::dashcore::Network; -use dash_sdk::dpp::dashcore::secp256k1::{self, Message, PublicKey, Secp256k1, ecdsa}; +use dash_sdk::dpp::dashcore::ecdsa::{EcdsaPublicKey, EcdsaSignature}; use dash_sdk::dpp::key_wallet::bip32::{DerivationPath, ExtendedPrivKey, ExtendedPubKey}; use dash_sdk::dpp::key_wallet::signer::{ExtendedPubKeySigner, Signer, SignerMethod}; +use dash_sdk::dpp::secp256k1::{self, Message, PublicKey, ecdsa}; use zeroize::Zeroizing; use crate::wallet_backend::secret_access::SecretPlaintext; @@ -63,7 +64,7 @@ pub enum DetSignerError { /// upstream digest-length error as a typed signer error rather than /// panicking. fn message_from_digest(digest: [u8; 32]) -> Result { - Message::from_digest_slice(&digest).map_err(DetSignerError::Sign) + Ok(Message::from_digest(digest)) } /// JIT [`Signer`] backed by a **borrowed** held secret. @@ -123,8 +124,13 @@ impl<'a> DetSigner<'a> { /// Derive the secp256k1 secret at `path` from the held HD seed. Errors /// with [`DetSignerError::WrongSecretKind`] if the held secret is a /// single key (no derivation tree). + /// + /// Signing stays on the secp256k1 primitive (plain RFC 6979 nonce), so + /// signatures are byte-identical to earlier builds; the newer + /// `EcdsaSecretKey::sign` grinds for a low R and would change them. fn derive_secret(&self, path: &DerivationPath) -> Result { - Ok(self.derive_xprv(path)?.private_key) + let scalar = self.derive_xprv(path)?.private_key.to_bytes(); + secp256k1::SecretKey::from_secret_bytes(*scalar).map_err(DetSignerError::Sign) } /// Sign `msg` (a 32-byte digest) with the held **single key** directly, @@ -137,10 +143,10 @@ impl<'a> DetSigner<'a> { ) -> Result { match &self.secret { HeldSecret::SingleKey(key) => { - let sk = - secp256k1::SecretKey::from_byte_array(key).map_err(DetSignerError::Sign)?; + let sk = secp256k1::SecretKey::from_secret_bytes(***key) + .map_err(DetSignerError::Sign)?; let message = message_from_digest(*msg)?; - Ok(Secp256k1::signing_only().sign_ecdsa(&message, &sk)) + Ok(dash_sdk::dpp::secp256k1::ecdsa::sign(message, &sk)) } HeldSecret::HdSeed(_) => Err(DetSignerError::WrongSecretKind), } @@ -172,21 +178,18 @@ impl Signer for DetSigner<'_> { &self, path: &DerivationPath, sighash: [u8; 32], - ) -> Result<(ecdsa::Signature, PublicKey), Self::Error> { + ) -> Result<(EcdsaSignature, EcdsaPublicKey), Self::Error> { let secret = self.derive_secret(path)?; - let secp = Secp256k1::signing_only(); + let msg = message_from_digest(sighash)?; - let signature = secp.sign_ecdsa(&msg, &secret); - let public_key = PublicKey::from_secret_key(&secp, &secret); - Ok((signature, public_key)) + let signature = dash_sdk::dpp::secp256k1::ecdsa::sign(msg, &secret); + let public_key = PublicKey::from_secret_key(&secret); + Ok((signature.into(), public_key.into())) } - async fn public_key(&self, path: &DerivationPath) -> Result { + async fn public_key(&self, path: &DerivationPath) -> Result { let secret = self.derive_secret(path)?; - Ok(PublicKey::from_secret_key( - &Secp256k1::signing_only(), - &secret, - )) + Ok(PublicKey::from_secret_key(&secret).into()) } } @@ -204,7 +207,7 @@ impl ExtendedPubKeySigner for DetSigner<'_> { path: &DerivationPath, ) -> Result { let xprv = self.derive_xprv(path)?; - Ok(ExtendedPubKey::from_priv(&Secp256k1::signing_only(), &xprv)) + Ok(ExtendedPubKey::from_priv(&xprv)) } } @@ -311,14 +314,21 @@ mod tests { // Independent reference: derive the key directly from the seed at // the path, then sign + recover the public key with the same // secp256k1 primitive `DetSigner` uses internally. - let secp = Secp256k1::new(); - let reference_sk = path - .derive_priv_ecdsa_for_master_seed(&PARITY_SEED, network) - .expect("reference derive") - .private_key; - let reference_msg = Message::from_digest_slice(&sighash).expect("reference digest"); - let reference_sig = secp.sign_ecdsa(&reference_msg, &reference_sk); - let reference_pk = PublicKey::from_secret_key(&secp, &reference_sk); + + let reference_sk = secp256k1::SecretKey::from_secret_bytes( + *path + .derive_priv_ecdsa_for_master_seed(&PARITY_SEED, network) + .expect("reference derive") + .private_key + .to_bytes(), + ) + .expect("valid scalar"); + let reference_msg = Message::from_digest(sighash); + let reference_sig = EcdsaSignature::from(dash_sdk::dpp::secp256k1::ecdsa::sign( + reference_msg, + &reference_sk, + )); + let reference_pk = EcdsaPublicKey::from(PublicKey::from_secret_key(&reference_sk)); // The JIT signer over the same seed, held open through the // chokepoint, must match byte-for-byte. @@ -359,11 +369,11 @@ mod tests { for network in [Network::Testnet, Network::Mainnet] { // Independent reference: derive the xprv straight from the seed at // the path, then convert to the xpub with the same primitive. - let secp = Secp256k1::new(); + let reference_xprv = path .derive_priv_ecdsa_for_master_seed(&PARITY_SEED, network) .expect("reference derive"); - let reference_xpub = ExtendedPubKey::from_priv(&secp, &reference_xprv); + let reference_xpub = ExtendedPubKey::from_priv(&reference_xprv); let held = Zeroizing::new(PARITY_SEED); let signer = DetSigner::from_held(SecretPlaintext::HdSeed(&held), network); @@ -402,14 +412,14 @@ mod tests { #[test] fn hd_derivation_matches_pinned_vector() { let path: DerivationPath = "m/44'/1'/0'/0/0".parse().unwrap(); - let secp = Secp256k1::new(); - let sk = path + + let pk = path .derive_priv_ecdsa_for_master_seed(&PARITY_SEED, Network::Testnet) .expect("derive") - .private_key; - let pk = PublicKey::from_secret_key(&secp, &sk); + .private_key + .public_key(); assert_eq!( - hex::encode(pk.serialize()), + hex::encode(pk.to_compressed()), PINNED_TESTNET_PUBKEY_HEX, "derived pubkey drifted from the pinned vector — a derivation regression" ); @@ -433,8 +443,10 @@ mod tests { ); let mut key_bytes = [0u8; 32]; key_bytes[31] = 1; - let sk = secp256k1::SecretKey::from_byte_array(&key_bytes).unwrap(); - let wif = PrivateKey::new(sk, Network::Testnet).to_wif(); + let sk = secp256k1::SecretKey::from_secret_bytes(key_bytes).unwrap(); + let wif = PrivateKey::from_byte_array(sk.as_secret_bytes(), Network::Testnet) + .expect("valid secret key") + .to_wif(); let imported = view .import_wif_with_passphrase( &wif, diff --git a/src/wallet_backend/secret_access.rs b/src/wallet_backend/secret_access.rs index 5d676afab..2038b5317 100644 --- a/src/wallet_backend/secret_access.rs +++ b/src/wallet_backend/secret_access.rs @@ -1730,11 +1730,13 @@ mod tests { fn known_testnet_wif() -> String { use dash_sdk::dpp::dashcore::PrivateKey; - use dash_sdk::dpp::dashcore::secp256k1::SecretKey; + use dash_sdk::dpp::secp256k1::SecretKey; let mut bytes = [0u8; 32]; bytes[31] = 1; - let sk = SecretKey::from_byte_array(&bytes).unwrap(); - PrivateKey::new(sk, Network::Testnet).to_wif() + let sk = SecretKey::from_secret_bytes(bytes).unwrap(); + PrivateKey::from_byte_array(sk.as_secret_bytes(), Network::Testnet) + .expect("valid secret key") + .to_wif() } /// Write a legacy DET AES-GCM `SingleKeyEntry` straight to the vault Tier-1 — @@ -1742,19 +1744,17 @@ mod tests { /// import time, so this is how the legacy→Tier-2 lazy migration path stays /// covered. fn write_legacy_protected_key(store: &Arc, passphrase: &str) -> String { - use dash_sdk::dpp::dashcore::secp256k1::Secp256k1; use dash_sdk::dpp::dashcore::{Address, PrivateKey, PublicKey}; let priv_key = PrivateKey::from_wif(&known_testnet_wif()).expect("wif"); - let raw: Zeroizing<[u8; 32]> = - Zeroizing::new(priv_key.inner[..].try_into().expect("32 bytes")); - let secp = Secp256k1::new(); + let raw: Zeroizing<[u8; 32]> = priv_key.inner.to_bytes(); + let pub_key = PublicKey { compressed: priv_key.compressed, - inner: priv_key.inner.public_key(&secp), + inner: priv_key.inner.public_key(), }; let address = Address::p2pkh(&pub_key, Network::Testnet).to_string(); - let pub_bytes = pub_key.inner.serialize().to_vec(); + let pub_bytes = pub_key.inner.to_compressed().to_vec(); let entry = SingleKeyEntry::protected(&raw, passphrase, Some("the usual".into()), pub_bytes) .expect("build legacy protected entry"); @@ -1819,9 +1819,10 @@ mod tests { let dir = tempfile::tempdir().unwrap(); let store = fresh_store(dir.path()); let address = write_legacy_protected_key(&store, SENTINEL_PASSPHRASE); - let expected: [u8; 32] = PrivateKey::from_wif(&known_testnet_wif()).unwrap().inner[..] - .try_into() - .unwrap(); + let expected: [u8; 32] = *PrivateKey::from_wif(&known_testnet_wif()) + .unwrap() + .inner + .to_bytes(); // First resolve: one passphrase, re-wraps to Tier-2. let prompt = Arc::new(TestPrompt::new([ScriptedAnswer::once(SENTINEL_PASSPHRASE)])); diff --git a/src/wallet_backend/sidecar.rs b/src/wallet_backend/sidecar.rs index f399b831d..9f87cb0c3 100644 --- a/src/wallet_backend/sidecar.rs +++ b/src/wallet_backend/sidecar.rs @@ -32,7 +32,10 @@ pub(crate) fn sidecar_key(network: Network, infix: &str, id: &SidecarId) -> Stri format!( "{}{infix}{}", network_prefix(network), - base58::encode_slice(id) + dash_sdk::dpp::platform_value::string_encoding::encode( + id, + dash_sdk::dpp::platform_value::string_encoding::Encoding::Base58 + ) ) } diff --git a/src/wallet_backend/single_key.rs b/src/wallet_backend/single_key.rs index 7c8424fa9..2fb1c85f9 100644 --- a/src/wallet_backend/single_key.rs +++ b/src/wallet_backend/single_key.rs @@ -14,11 +14,11 @@ use std::sync::Arc; #[cfg(test)] -use dash_sdk::dpp::dashcore::secp256k1::Message; -use dash_sdk::dpp::dashcore::secp256k1::Secp256k1; -#[cfg(test)] -use dash_sdk::dpp::dashcore::secp256k1::ecdsa::Signature; +use dash_sdk::dpp::secp256k1::Message; + use dash_sdk::dpp::dashcore::{Address, Network, PrivateKey, PublicKey}; +#[cfg(test)] +use dash_sdk::dpp::secp256k1::ecdsa::Signature; use platform_wallet_storage::secrets::{ SecretBytes, SecretStore, SecretStoreError, SecretString, WalletId as SecretWalletId, }; @@ -256,10 +256,10 @@ impl<'a> SingleKeyView<'a> { if !priv_key.compressed { return Err(TaskError::UncompressedWifUnsupported); } - let secp = Secp256k1::new(); + let pub_key = PublicKey { compressed: priv_key.compressed, - inner: priv_key.inner.public_key(&secp), + inner: priv_key.inner.public_key(), }; let address = Address::p2pkh(&pub_key, self.network); let address_str = address.to_string(); @@ -289,13 +289,9 @@ impl<'a> SingleKeyView<'a> { }; // Extracted WIF bytes wrapped in `Zeroizing` so the stack copy wipes // on drop instead of lingering after the entry is built. - let raw: Zeroizing<[u8; 32]> = Zeroizing::new( - priv_key.inner[..] - .try_into() - .map_err(|_| TaskError::SingleKeyCryptoFailure)?, - ); + let raw: Zeroizing<[u8; 32]> = priv_key.inner.to_bytes(); - let pub_bytes = pub_key.inner.serialize().to_vec(); + let pub_bytes = pub_key.inner.to_compressed().to_vec(); let password = passphrase .passphrase .as_ref() @@ -714,8 +710,8 @@ impl<'a> SingleKeyView<'a> { return Ok(None); } }; - let secp = Secp256k1::new(); - let public_key = priv_key.public_key(&secp); + + let public_key = priv_key.public_key(); let address = Address::p2pkh(&public_key, meta.network); let key_hash = ClosedSingleKey::compute_key_hash(&key_bytes); @@ -869,7 +865,7 @@ fn parse_locked_address_and_pubkey( ); return None; } - let inner = match dash_sdk::dpp::dashcore::secp256k1::PublicKey::from_slice(public_key_bytes) { + let inner = match dash_sdk::dpp::secp256k1::PublicKey::from_slice(public_key_bytes) { Ok(p) => p, Err(e) => { tracing::warn!( @@ -885,7 +881,7 @@ fn parse_locked_address_and_pubkey( address, PublicKey { compressed: true, - inner, + inner: inner.into(), }, )) } @@ -1031,10 +1027,10 @@ impl<'a> SingleKeyView<'a> { /// [`DetSigner`](crate::wallet_backend::det_signer::DetSigner). #[cfg(test)] fn sign_message_with_raw_key(bytes: &[u8; 32], msg: &[u8; 32]) -> Result { - let sk = dash_sdk::dpp::dashcore::secp256k1::SecretKey::from_byte_array(bytes) + let sk = dash_sdk::dpp::secp256k1::SecretKey::from_secret_bytes(*bytes) .map_err(|_| TaskError::ImportedKeyNotFound)?; let message = Message::from_digest(*msg); - Ok(Secp256k1::new().sign_ecdsa(&message, &sk)) + Ok(dash_sdk::dpp::secp256k1::ecdsa::sign(message, &sk)) } #[cfg(test)] @@ -1137,7 +1133,7 @@ mod tests { fn known_wif() -> &'static str { // Testnet WIF with deterministic, all-zero-except-last-byte key - // bytes. Generated locally with `PrivateKey::new(SecretKey::from_byte_array(&[0;31].chain(&[1])).unwrap(), Testnet).to_wif()` + // bytes. Generated locally with `PrivateKey::new(SecretKey::from_secret_bytes([0;31].chain(&[1])).unwrap(), Testnet).to_wif()` // and pinned here to keep tests offline + reproducible. "cMahea7zqjxrtgAbB7LSGbcQUr1uX1ojuat9jZodMN8rFTv2sfUK" } @@ -1251,8 +1247,6 @@ mod tests { /// lookup path. #[test] fn tc_sk_008_sign_uses_secret_store_path_not_bip32() { - use dash_sdk::dpp::dashcore::secp256k1::Secp256k1; - let dir = tempfile::tempdir().expect("tempdir"); let (store, index, network) = fresh_view(dir.path(), Network::Testnet); let view = SingleKeyView { @@ -1274,9 +1268,9 @@ mod tests { // the signer hit the imported-key path (not some other key the // backend might fall back to) without referencing BIP-32 at all. let priv_key = PrivateKey::from_wif(known_wif()).expect("wif"); - let secp = Secp256k1::new(); - let pk = priv_key.inner.public_key(&secp); - secp.verify_ecdsa(&Message::from_digest(msg), &sig, &pk) + + let pk = dash_sdk::dpp::secp256k1::PublicKey::from(priv_key.inner.public_key()); + dash_sdk::dpp::secp256k1::ecdsa::verify(&sig, Message::from_digest(msg), &pk) .expect("signature verifies against WIF-derived pubkey"); // Forgetting the key removes both the index entry and the @@ -1928,9 +1922,9 @@ mod tests { .expect("chokepoint signs after the right passphrase"); let priv_key = PrivateKey::from_wif(known_wif()).unwrap(); - let secp = Secp256k1::new(); - let pk = priv_key.inner.public_key(&secp); - secp.verify_ecdsa(&Message::from_digest(msg), &sig, &pk) + + let pk = dash_sdk::dpp::secp256k1::PublicKey::from(priv_key.inner.public_key()); + dash_sdk::dpp::secp256k1::ecdsa::verify(&sig, Message::from_digest(msg), &pk) .expect("chokepoint signature verifies"); assert_eq!(prompt.ask_count(), 2, "one wrong + one right passphrase"); } @@ -2107,9 +2101,11 @@ mod tests { /// A deterministic throwaway testnet WIF distinct per `byte`. fn wif_for_byte(byte: u8) -> String { - let secret = dash_sdk::dpp::dashcore::secp256k1::SecretKey::from_byte_array(&[byte; 32]) + let secret = dash_sdk::dpp::secp256k1::SecretKey::from_secret_bytes([byte; 32]) .expect("valid scalar"); - PrivateKey::new(secret, Network::Testnet).to_wif() + PrivateKey::from_byte_array(secret.as_secret_bytes(), Network::Testnet) + .expect("valid secret key") + .to_wif() } fn transient_view<'a>( @@ -2553,11 +2549,11 @@ mod tests { let priv_key = PrivateKey::from_wif(known_wif()).unwrap(); let pub_key = PublicKey { compressed: priv_key.compressed, - inner: priv_key.inner.public_key(&Secp256k1::new()), + inner: priv_key.inner.public_key(), }; let address = Address::p2pkh(&pub_key, network).to_string(); let label = label_for_address(&address); - let raw = SecretBytes::from_slice(&priv_key.inner[..]); + let raw = SecretBytes::from_slice(&priv_key.inner.to_bytes()[..]); store .set(&single_key_namespace_id(), &label, &raw) .expect("write legacy bytes"); @@ -2617,7 +2613,7 @@ mod tests { "raw, not a versioned envelope" ); let priv_key = PrivateKey::from_wif(known_wif()).unwrap(); - assert_eq!(raw.expose_secret(), &priv_key.inner[..]); + assert_eq!(raw.expose_secret(), &priv_key.inner.to_bytes()[..]); // Signs with no passphrase. view.sign_with(&imported.address, &[0x42u8; 32]) @@ -2922,7 +2918,7 @@ mod tests { .expect("secret retained"); assert_eq!( raw.expose_secret(), - &PrivateKey::from_wif(known_wif()).unwrap().inner[..] + &PrivateKey::from_wif(known_wif()).unwrap().inner.to_bytes()[..] ); assert_eq!(view.hydrate_wallets().len(), 1); view.forget(&existing.address) @@ -2997,7 +2993,7 @@ mod tests { .map(|key| { let public = PublicKey { compressed: true, - inner: key.inner.public_key(&Secp256k1::new()), + inner: key.inner.public_key(), }; Address::p2pkh(&public, Network::Testnet).to_string() }) @@ -3032,7 +3028,7 @@ mod tests { .map(|key| { let public = PublicKey { compressed: true, - inner: key.inner.public_key(&Secp256k1::new()), + inner: key.inner.public_key(), }; Address::p2pkh(&public, Network::Testnet).to_string() }) diff --git a/src/wallet_backend/snapshot.rs b/src/wallet_backend/snapshot.rs index 65347cbb6..90807d228 100644 --- a/src/wallet_backend/snapshot.rs +++ b/src/wallet_backend/snapshot.rs @@ -766,6 +766,21 @@ impl SnapshotStore { } } + pub(super) fn unconfirm_instant_locked(&self, wallet_id: &WalletId, txids: &[Txid]) { + let Ok(mut log) = self.tx_log.lock() else { + return; + }; + if let Some(transactions) = log.get_mut(wallet_id) { + for txid in txids { + if let Some(tx) = transactions.get_mut(txid) { + tx.status = TransactionStatus::InstantSendLocked; + tx.height = None; + tx.block_hash = None; + } + } + } + } + /// Upgrade a previously-accumulated record's status to /// `InstantSendLocked`. /// @@ -1030,7 +1045,6 @@ impl SnapshotStore { mod tests { use super::*; use dash_sdk::dpp::dashcore::hashes::Hash; - use dash_sdk::dpp::dashcore::secp256k1::{Secp256k1, SecretKey}; use dash_sdk::dpp::dashcore::{BlockHash, Network, PublicKey, Transaction, TxOut}; use dash_sdk::dpp::key_wallet::account::{AccountType, StandardAccountType}; use dash_sdk::dpp::key_wallet::managed_account::transaction_record::{ @@ -1038,6 +1052,7 @@ mod tests { }; use dash_sdk::dpp::key_wallet::transaction_checking::BlockInfo; use dash_sdk::dpp::key_wallet::transaction_checking::transaction_router::TransactionType; + use dash_sdk::dpp::secp256k1::SecretKey; fn seed(n: u8) -> WalletSeedHash { [n; 32] @@ -1962,6 +1977,34 @@ mod tests { ); } + #[test] + fn truncated_chain_clears_block_evidence_but_preserves_instant_lock() { + let store = SnapshotStore::new(); + let mut rec = record(2, 300); + rec.context = TransactionContext::InChainLockedBlock(BlockInfo::new( + 10, + BlockHash::from_byte_array([1; 32]), + 123, + )); + let txid = rec.txid; + store.accumulate_transactions(&wid(5), [&rec]); + store.accumulate_transactions(&wid(6), [&rec]); + store.unconfirm_instant_locked(&wid(5), &[txid]); + publish_tx_only(&store, seed(5), wid(5)); + publish_tx_only(&store, seed(6), wid(6)); + let snap = store.snapshot(&seed(5)); + assert_eq!( + snap.transactions[0].status, + TransactionStatus::InstantSendLocked + ); + assert_eq!(snap.transactions[0].height, None); + assert_eq!(snap.transactions[0].block_hash, None); + assert_eq!( + store.snapshot(&seed(6)).transactions[0].status, + TransactionStatus::ChainLocked + ); + } + /// A block confirmation may race the InstantLock notification. If the /// stronger status already landed first, the InstantLock must not /// regress it back to `InstantSendLocked`. @@ -2244,9 +2287,9 @@ mod tests { fn addr(n: u8) -> Address { let mut sk_bytes = [1u8; 32]; sk_bytes[31] = n.max(1); - let secp = Secp256k1::new(); - let sk = SecretKey::from_slice(&sk_bytes).unwrap(); - let pubkey = PublicKey::new(sk.public_key(&secp)); + + let sk = SecretKey::from_secret_bytes(sk_bytes).unwrap(); + let pubkey = PublicKey::new(sk.public_key()); Address::p2pkh(&pubkey, Network::Testnet) } diff --git a/src/wallet_backend/wallet_context.rs b/src/wallet_backend/wallet_context.rs index 7c0655ac9..ff6ce79a2 100644 --- a/src/wallet_backend/wallet_context.rs +++ b/src/wallet_backend/wallet_context.rs @@ -489,7 +489,7 @@ impl WalletContext { network, has_passphrase: w.uses_password, passphrase_hint: None, - public_key_bytes: w.public_key.inner.serialize().to_vec(), + public_key_bytes: w.public_key.inner.to_compressed().to_vec(), } }; let _writer = self.lock_writer(); @@ -853,7 +853,7 @@ mod tests { network, has_passphrase: false, passphrase_hint: None, - public_key_bytes: open.public_key.inner.serialize().to_vec(), + public_key_bytes: open.public_key.inner.to_compressed().to_vec(), }; (meta, open, protected) } diff --git a/tests/backend-e2e/dashpay_tasks.rs b/tests/backend-e2e/dashpay_tasks.rs index c1ae089f8..98386d3a8 100644 --- a/tests/backend-e2e/dashpay_tasks.rs +++ b/tests/backend-e2e/dashpay_tasks.rs @@ -627,14 +627,16 @@ async fn step_update_contact_info( key_type: KeyType::ECDSA_SECP256K1, read_only: false, data: { - use dash_sdk::dashcore_rpc::dashcore::key::Secp256k1; use dash_sdk::dpp::dashcore::PrivateKey; - let secp = Secp256k1::new(); let secret_key = - dash_sdk::dpp::dashcore::secp256k1::SecretKey::from_slice(&private_key_bytes) + dash_sdk::dpp::secp256k1::SecretKey::from_secret_bytes(private_key_bytes) .expect("Step 5: invalid secret key"); - let pk = PrivateKey::new(secret_key, dash_sdk::dpp::dashcore::Network::Testnet); - pk.public_key(&secp).to_bytes().into() + let pk = PrivateKey::from_byte_array( + secret_key.as_secret_bytes(), + dash_sdk::dpp::dashcore::Network::Testnet, + ) + .expect("valid secret key"); + pk.public_key().to_bytes().into() }, disabled_at: None, }); @@ -850,11 +852,10 @@ async fn tc_045_detect_incoming_contact_payment() { // secret keys so the addresses stay stable across runs while remaining valid // curve points — secp256k1 now rejects raw bytes that are not on the curve, // so a hand-written `[0x02; 33]` is no longer a usable public key. - let secp = dash_sdk::dpp::dashcore::secp256k1::Secp256k1::new(); let derive_pubkey = |seed: [u8; 32]| { - let secret_key = dash_sdk::dpp::dashcore::secp256k1::SecretKey::from_slice(&seed) + let secret_key = dash_sdk::dpp::secp256k1::SecretKey::from_secret_bytes(seed) .expect("fixed test secret key is a valid scalar"); - dash_sdk::dpp::dashcore::PublicKey::new(secret_key.public_key(&secp)) + dash_sdk::dpp::dashcore::PublicKey::new(secret_key.public_key()) }; let pubkey_0 = derive_pubkey([0x01; 32]); let pubkey_1 = derive_pubkey([0x02; 32]); diff --git a/tests/backend-e2e/identity_in_vault_sign.rs b/tests/backend-e2e/identity_in_vault_sign.rs index 3f85cf0dc..142909c22 100644 --- a/tests/backend-e2e/identity_in_vault_sign.rs +++ b/tests/backend-e2e/identity_in_vault_sign.rs @@ -102,14 +102,13 @@ async fn ts_sign_e2e_01_in_vault_identity_signs_and_broadcasts() { // MASTER key via the JIT free-rider path. let new_private_key_bytes: [u8; 32] = rand::random(); let new_public_key_data = { - use dash_sdk::dashcore_rpc::dashcore::key::Secp256k1; use dash_sdk::dpp::dashcore::PrivateKey; - let secp = Secp256k1::new(); let secret_key = - dash_sdk::dpp::dashcore::secp256k1::SecretKey::from_slice(&new_private_key_bytes) + dash_sdk::dpp::secp256k1::SecretKey::from_secret_bytes(new_private_key_bytes) .expect("valid secret"); - PrivateKey::new(secret_key, Network::Testnet) - .public_key(&secp) + PrivateKey::from_byte_array(secret_key.as_secret_bytes(), Network::Testnet) + .expect("valid secret key") + .public_key() .to_bytes() }; let mut new_ipk = IdentityPublicKey::V0(IdentityPublicKeyV0 { @@ -263,7 +262,7 @@ async fn materialize_master_key_as_clear( let xprv = master_path .derive_priv_ecdsa_for_master_seed(seed, network) .expect("derive master private key from seed"); - Ok(xprv.to_priv().inner.secret_bytes()) + Ok(*xprv.to_priv().inner.to_bytes()) }, ) .await diff --git a/tests/backend-e2e/identity_tasks.rs b/tests/backend-e2e/identity_tasks.rs index 60d40dc36..6d476ce42 100644 --- a/tests/backend-e2e/identity_tasks.rs +++ b/tests/backend-e2e/identity_tasks.rs @@ -194,14 +194,13 @@ async fn step_add_key( key_type: KeyType::ECDSA_SECP256K1, read_only: false, data: { - use dash_sdk::dashcore_rpc::dashcore::key::Secp256k1; use dash_sdk::dpp::dashcore::PrivateKey; - let secp = Secp256k1::new(); let secret_key = - dash_sdk::dpp::dashcore::secp256k1::SecretKey::from_slice(&private_key_bytes) + dash_sdk::dpp::secp256k1::SecretKey::from_secret_bytes(private_key_bytes) .expect("invalid secret key"); - let pk = PrivateKey::new(secret_key, Network::Testnet); - pk.public_key(&secp).to_bytes().into() + let pk = PrivateKey::from_byte_array(secret_key.as_secret_bytes(), Network::Testnet) + .expect("valid secret key"); + pk.public_key().to_bytes().into() }, disabled_at: None, }); diff --git a/tests/backend-e2e/wallet_reregistration.rs b/tests/backend-e2e/wallet_reregistration.rs index 32f2a7cd9..d899ddea2 100644 --- a/tests/backend-e2e/wallet_reregistration.rs +++ b/tests/backend-e2e/wallet_reregistration.rs @@ -140,7 +140,6 @@ async fn cold_process_boot_from_migrated_state_registers_and_shows_balance() { use dash_evo_tool::utils::egui_mpsc::EguiMpscAsync; use dash_evo_tool::utils::tasks::TaskManager; use dash_sdk::dpp::dashcore::Network; - use dash_sdk::dpp::dashcore::secp256k1::Secp256k1; use dash_sdk::dpp::key_wallet::bip32::{ ChildNumber, DerivationPath, ExtendedPrivKey, ExtendedPubKey, }; @@ -175,15 +174,14 @@ async fn cold_process_boot_from_migrated_state_registers_and_shows_balance() { w.seed_hash() }; let epk = { - let secp = Secp256k1::new(); let master = ExtendedPrivKey::new_master(Network::Testnet, &seed).expect("master key"); let path = DerivationPath::from(vec![ ChildNumber::Hardened { index: 44 }, ChildNumber::Hardened { index: 1 }, ChildNumber::Hardened { index: 0 }, ]); - let account = master.derive_priv(&secp, &path).expect("derive account"); - ExtendedPubKey::from_priv(&secp, &account).encode().to_vec() + let account = master.derive_priv(&path).expect("derive account"); + ExtendedPubKey::from_priv(&account).encode().to_vec() }; seed_legacy_unprotected_hd_wallet_row( &db, diff --git a/tests/backend-e2e/z_broadcast_st_tasks.rs b/tests/backend-e2e/z_broadcast_st_tasks.rs index e3076e1c3..97f0f6330 100644 --- a/tests/backend-e2e/z_broadcast_st_tasks.rs +++ b/tests/backend-e2e/z_broadcast_st_tasks.rs @@ -51,14 +51,13 @@ async fn step_broadcast_valid( let new_private_key_bytes: [u8; 32] = rand::random(); let new_public_key_data = { - use dash_sdk::dashcore_rpc::dashcore::key::Secp256k1; use dash_sdk::dpp::dashcore::PrivateKey; - let secp = Secp256k1::new(); let secret_key = - dash_sdk::dpp::dashcore::secp256k1::SecretKey::from_slice(&new_private_key_bytes) + dash_sdk::dpp::secp256k1::SecretKey::from_secret_bytes(new_private_key_bytes) .expect("invalid secret key bytes"); - let pk = PrivateKey::new(secret_key, Network::Testnet); - pk.public_key(&secp).to_bytes() + let pk = PrivateKey::from_byte_array(secret_key.as_secret_bytes(), Network::Testnet) + .expect("valid secret key"); + pk.public_key().to_bytes() }; let mut new_ipk = IdentityPublicKey::V0(IdentityPublicKeyV0 { @@ -212,14 +211,13 @@ async fn step_broadcast_invalid( let new_private_key_bytes: [u8; 32] = rand::random(); let new_public_key_data = { - use dash_sdk::dashcore_rpc::dashcore::key::Secp256k1; use dash_sdk::dpp::dashcore::PrivateKey; - let secp = Secp256k1::new(); let secret_key = - dash_sdk::dpp::dashcore::secp256k1::SecretKey::from_slice(&new_private_key_bytes) + dash_sdk::dpp::secp256k1::SecretKey::from_secret_bytes(new_private_key_bytes) .expect("invalid secret key bytes"); - let pk = PrivateKey::new(secret_key, Network::Testnet); - pk.public_key(&secp).to_bytes() + let pk = PrivateKey::from_byte_array(secret_key.as_secret_bytes(), Network::Testnet) + .expect("valid secret key"); + pk.public_key().to_bytes() }; let mut new_ipk = IdentityPublicKey::V0(IdentityPublicKeyV0 { diff --git a/tests/kittest/address_input.rs b/tests/kittest/address_input.rs index 2cca9378f..d4864c470 100644 --- a/tests/kittest/address_input.rs +++ b/tests/kittest/address_input.rs @@ -30,18 +30,17 @@ use std::sync::{Arc, RwLock}; /// and exactly one BIP-44 receive address, distinct per `seed`. The snapshot /// paths — not the wallet's own bookkeeping — are what the component offers. fn wallet(seed: u8, alias: &str) -> WalletWithSnapshot { - use dash_sdk::dpp::dashcore::secp256k1::{Secp256k1, SecretKey}; use dash_sdk::dpp::dashcore::{Address, PublicKey}; use dash_sdk::dpp::key_wallet::bip32::{ChildNumber, DerivationPath}; let wallet = Wallet::new_from_seed([seed; 64], Network::Testnet, Some(alias.to_string()), None) .expect("wallet from seed"); - let secp = Secp256k1::new(); let mut sk_bytes = [1u8; 32]; sk_bytes[31] = seed.max(1); - let sk = SecretKey::from_slice(&sk_bytes).expect("valid secret key"); - let address = Address::p2pkh(&PublicKey::new(sk.public_key(&secp)), Network::Testnet); + let sk = + dash_sdk::dpp::secp256k1::SecretKey::from_secret_bytes(sk_bytes).expect("valid secret key"); + let address = Address::p2pkh(&PublicKey::new(sk.public_key()), Network::Testnet); // m/44'/1'/0'/0/0 — a BIP-44 external receive path. let path = DerivationPath::from( diff --git a/tests/kittest/send_screen.rs b/tests/kittest/send_screen.rs index 678c84a13..caa9feb3d 100644 --- a/tests/kittest/send_screen.rs +++ b/tests/kittest/send_screen.rs @@ -11,7 +11,6 @@ use dash_evo_tool::model::wallet::{Wallet, WalletSeedHash}; use dash_evo_tool::ui::wallets::send_screen::{SourceSelection, WalletSendScreen}; use dash_sdk::dpp::address_funds::PlatformAddress; use dash_sdk::dpp::balances::credits::CREDITS_PER_DUFF; -use dash_sdk::dpp::dashcore::secp256k1::{Secp256k1, SecretKey}; use dash_sdk::dpp::dashcore::{Address, Network, PublicKey}; use dash_sdk::dpp::identity::Identity; use dash_sdk::dpp::identity::core_script::CoreScript; @@ -27,11 +26,11 @@ const ONE_DASH_CREDITS: u64 = CREDITS_PER_DUFF * 100_000_000; fn core_address() -> Address { let secret_key = loop { let bytes: [u8; 32] = rand::random(); - if let Ok(key) = SecretKey::from_slice(&bytes) { + if let Ok(key) = dash_sdk::dpp::secp256k1::SecretKey::from_secret_bytes(bytes) { break key; } }; - let public_key = PublicKey::new(secret_key.public_key(&Secp256k1::new())); + let public_key = PublicKey::new(secret_key.public_key()); Address::p2pkh(&public_key, Network::Testnet) } diff --git a/tests/kittest/top_up_identity_screen.rs b/tests/kittest/top_up_identity_screen.rs index 327dfdfeb..fb125cc6b 100644 --- a/tests/kittest/top_up_identity_screen.rs +++ b/tests/kittest/top_up_identity_screen.rs @@ -25,7 +25,6 @@ use dash_evo_tool::ui::wallets::send_screen::{ use dash_evo_tool::ui::{RootScreenType, Screen, ScreenType}; use dash_sdk::dpp::address_funds::PlatformAddress; use dash_sdk::dpp::balances::credits::CREDITS_PER_DUFF; -use dash_sdk::dpp::dashcore::secp256k1::{Secp256k1, SecretKey}; use dash_sdk::dpp::dashcore::{Address, Network, PublicKey}; use dash_sdk::dpp::identity::Identity; use dash_sdk::dpp::identity::accessors::{IdentityGettersV0, IdentitySettersV0}; @@ -502,9 +501,9 @@ fn confirm_top_up(harness: &mut App, identity: &QualifiedIdentity) -> BackendTas fn prepare_top_up(harness: &mut App, identity: &QualifiedIdentity) -> Result { let core = Address::p2pkh( &PublicKey::new( - SecretKey::from_slice(&[0x31; 32]) + dash_sdk::dpp::secp256k1::SecretKey::from_secret_bytes([0x31; 32]) .expect("secret key") - .public_key(&Secp256k1::new()), + .public_key(), ), identity.network, ); diff --git a/tests/migration-matrix/assertions.rs b/tests/migration-matrix/assertions.rs index cf14e42af..a341cece9 100644 --- a/tests/migration-matrix/assertions.rs +++ b/tests/migration-matrix/assertions.rs @@ -532,7 +532,7 @@ pub fn legacy_wallet_registrations( "SELECT EXISTS (SELECT 1 FROM account_registrations \ WHERE account_type = 'standard_bip44' AND account_index = 0 \ AND instr(account_xpub_bytes, CAST(?1 AS BLOB)) > 0)", - [base58::encode_check(&account_xpub)], + [base58::Base58CkString::encode_unbounded(&account_xpub).to_string()], |row| row.get::<_, bool>(0), ) .map_err(|e| { @@ -1062,7 +1062,7 @@ mod tests { ) .expect("test vector hex"); assert_eq!( - base58::encode_check(&serialized), + base58::Base58CkString::encode_unbounded(&serialized).to_string(), "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8" ); } @@ -1100,7 +1100,7 @@ mod tests { account_xpub_bytes BLOB NOT NULL);", ) .expect("create registrations"); - let text = base58::encode_check(&plain_xpub); + let text = base58::Base58CkString::encode_unbounded(&plain_xpub).to_string(); let mut stored = (text.len() as u32).to_be_bytes().to_vec(); stored.extend_from_slice(text.as_bytes()); store diff --git a/tests/migration-matrix/public_identities.rs b/tests/migration-matrix/public_identities.rs index a4cec78a7..ab8fbbb8d 100644 --- a/tests/migration-matrix/public_identities.rs +++ b/tests/migration-matrix/public_identities.rs @@ -163,7 +163,12 @@ pub fn check_source( for row in rows { let (id, bytes, status, identity_type, alias) = row.map_err(|e| e.to_string())?; let record = snapshot(&bytes, status, &identity_type)?; - if record.id != dash_sdk::dpp::dashcore::base58::encode_slice(&id) || record.alias != alias + if record.id + != dash_sdk::dpp::platform_value::string_encoding::encode( + &id, + dash_sdk::dpp::platform_value::string_encoding::Encoding::Base58, + ) + || record.alias != alias { return Err("legacy identity columns disagree with the serialized identity".into()); } @@ -191,7 +196,12 @@ pub fn check_migrated( return Err("unexpected wallet binding in public identity record".into()); } let record = snapshot(&stored.qi_bytes, stored.status, &stored.identity_type)?; - if record.id != dash_sdk::dpp::dashcore::base58::encode_slice(&id) { + if record.id + != dash_sdk::dpp::platform_value::string_encoding::encode( + &id, + dash_sdk::dpp::platform_value::string_encoding::Encoding::Base58, + ) + { return Err("identity storage key differs from its serialized identity".into()); } found.push(record); From e18e7f24e03c4fdcbfcd32f8d81a6f680c168c74 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Sat, 24 Oct 2026 18:21:52 +0000 Subject: [PATCH 03/27] fix: adapt to protocol version 14 upstream types Handle the dpp and SDK types the new pin adds, keeping today's behaviour: - Document replace keeps moderated_at and moderated_by; new documents start without them. - The document editor refuses typed-array fields instead of dropping their contents, and keeps referenced key ids as numbers. - Shielded token events show read-only in group actions, and the shielded pool token settings are refused before anything is signed. - Contested document creation states the protocol's base contest fund, so the SDK never auto-quotes a larger one. - Masternode vote reads use ResourceVoteWithCount, the SDK's new vote type; the vote count is not used yet. - New query fields, wallet events, wallet error variants and asset-lock funding modes are handled where the SDK now requires them. - Entropy-only document ids are documented as placeholders from protocol 14, and deferred upstream features link to their issues. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- src/backend_task/contested_names/mod.rs | 46 +++---- .../query_dpns_contested_resources.rs | 19 ++- .../contested_names/refresh_vote_states.rs | 9 +- .../contested_names/vote_on_dpns_name.rs | 8 +- src/backend_task/contract.rs | 3 +- src/backend_task/dashpay.rs | 5 +- src/backend_task/dashpay/contact_info.rs | 15 ++- src/backend_task/dashpay/contact_requests.rs | 14 +- src/backend_task/dashpay/profile.rs | 25 ++-- src/backend_task/document.rs | 14 +- src/backend_task/identity/dpns_usernames.rs | 18 ++- .../refresh_loaded_identities_dpns_names.rs | 5 +- .../identity/register_dpns_name.rs | 42 +++++- src/backend_task/identity/top_up_identity.rs | 3 +- src/backend_task/shielded/mod.rs | 3 + src/backend_task/tokens/mod.rs | 4 +- .../tokens/update_token_config.rs | 36 +++++ src/context/test_support.rs | 14 ++ src/model/asset_lock.rs | 1 + src/model/dashpay.rs | 2 + src/model/fee_estimation.rs | 64 ++++++++- src/model/token.rs | 13 ++ .../document_action_screen.rs | 125 +++++++++++++++++- .../group_actions_screen.rs | 109 +++++++++++++-- src/ui/tokens/tokens_screen/mod.rs | 8 +- src/ui/tokens/update_token_config.rs | 40 ++++++ src/ui/tokens/view_token_claims_screen.rs | 3 +- src/wallet_backend/event_bridge.rs | 11 ++ src/wallet_backend/mod.rs | 22 ++- src/wallet_backend/payments.rs | 18 ++- .../backend-e2e/measure_dpns_registration.rs | 3 + 31 files changed, 602 insertions(+), 100 deletions(-) diff --git a/src/backend_task/contested_names/mod.rs b/src/backend_task/contested_names/mod.rs index 3a59001e4..236476e73 100644 --- a/src/backend_task/contested_names/mod.rs +++ b/src/backend_task/contested_names/mod.rs @@ -25,10 +25,10 @@ use dash_sdk::Sdk; use dash_sdk::dpp::dashcore::Network; use dash_sdk::dpp::identity::accessors::IdentityGettersV0; use dash_sdk::dpp::voting::vote_choices::resource_vote_choice::ResourceVoteChoice; -use dash_sdk::dpp::voting::votes::resource_vote::ResourceVote; use dash_sdk::dpp::voting::votes::resource_vote::accessors::v0::ResourceVoteGettersV0; use dash_sdk::drive::query::contested_resource_votes_given_by_identity_query::ContestedResourceVotesGivenByIdentityQuery; use dash_sdk::platform::{FetchMany, Identifier}; +use dash_sdk::query_types::ResourceVoteWithCount; use futures::{StreamExt, stream}; use std::collections::{BTreeMap, BTreeSet}; use std::sync::Arc; @@ -994,12 +994,12 @@ impl AppContext { start_at: Some((poll_id.to_buffer(), true)), order_ascending: true, }; - match ResourceVote::fetch_many(sdk, query).await { + match ResourceVoteWithCount::fetch_many(sdk, query).await { Ok(votes) => { let observed = votes .get(&poll_id) .and_then(Option::as_ref) - .map(ResourceVoteGettersV0::resource_vote_choice); + .map(|vote| vote.resource_vote.resource_vote_choice()); let status = classify_reconciled_vote(observed, outcome.target.requested_choice) .unwrap_or(DpnsVoteTargetStatus::Unconfirmed); @@ -1669,14 +1669,12 @@ mod tests { context.set_det_kv_override_for_test(kv); store.fail_next_gets_containing("det:dpns_vote_operations:v2:", 2); let mut sdk = Sdk::new_mock(); - let document_type = context - .dpns_contract - .document_type_for_name("domain") - .unwrap(); + let dpns_contract = context.dpns_contract(); + let document_type = dpns_contract.document_type_for_name("domain").unwrap(); sdk.mock() .expect_fetch_many::( VotePollsByDocumentTypeQuery { - contract_id: context.dpns_contract.id(), + contract_id: context.dpns_contract().id(), document_type_name: document_type.name().to_owned(), index_name: document_type.find_contested_index().unwrap().name.clone(), start_at_value: None, @@ -2035,7 +2033,7 @@ mod tests { context.seed_dpns_contest_for_test("alice", Some(now + 600_000), false); let mut sdk = Sdk::new_mock(); sdk.mock() - .expect_fetch_many::( + .expect_fetch_many::( ContestedResourceVotesGivenByIdentityQuery { identity_id: reloaded.identity.id(), offset: None, @@ -2333,14 +2331,12 @@ mod tests { use dash_sdk::query_types::{ContestedResource, ContestedResources}; let (_temp, context) = vote_context(); let mut sdk = Sdk::new_mock(); - let document_type = context - .dpns_contract - .document_type_for_name("domain") - .unwrap(); + let dpns_contract = context.dpns_contract(); + let document_type = dpns_contract.document_type_for_name("domain").unwrap(); sdk.mock() .expect_fetch_many::( VotePollsByDocumentTypeQuery { - contract_id: context.dpns_contract.id(), + contract_id: context.dpns_contract().id(), document_type_name: document_type.name().to_owned(), index_name: document_type.find_contested_index().unwrap().name.clone(), start_at_value: None, @@ -2407,27 +2403,31 @@ mod tests { choice: Option, ) { use dash_sdk::dpp::voting::vote_polls::VotePoll; + use dash_sdk::dpp::voting::votes::resource_vote::ResourceVote; use dash_sdk::dpp::voting::votes::resource_vote::v0::ResourceVoteV0; use dash_sdk::query_types::ResourceVotesByIdentity; let votes: ResourceVotesByIdentity = choice .map(|resource_vote_choice| { let vote_poll = VotePoll::ContestedDocumentResourceVotePoll( - crate::model::dpns_voting::dpns_vote_poll(&context.dpns_contract, name) + crate::model::dpns_voting::dpns_vote_poll(&context.dpns_contract(), name) .unwrap(), ); ( key.vote_poll_id, - Some(ResourceVote::V0(ResourceVoteV0 { - vote_poll, - resource_vote_choice, - })), + Some(ResourceVoteWithCount { + resource_vote: ResourceVote::V0(ResourceVoteV0 { + vote_poll, + resource_vote_choice, + }), + vote_count: 1, + }), ) }) .into_iter() .collect(); sdk.mock() - .expect_fetch_many::( + .expect_fetch_many::( ContestedResourceVotesGivenByIdentityQuery { identity_id: key.voter_id, offset: None, @@ -3009,7 +3009,7 @@ mod tests { .unwrap(); let key = &operation.targets[0].target.key; let mut sdk = Sdk::new_mock(); - sdk.mock().expect_fetch_many::( + sdk.mock().expect_fetch_many::( ContestedResourceVotesGivenByIdentityQuery { identity_id: key.voter_id, offset: None, @@ -3473,7 +3473,7 @@ mod tests { } let mut sdk = Sdk::new_mock(); sdk.mock() - .expect_fetch_many::( + .expect_fetch_many::( ContestedResourceVotesGivenByIdentityQuery { identity_id: voters[0], offset: None, @@ -3537,7 +3537,7 @@ mod tests { context.seed_dpns_contest_for_test("alice", Some(1), false); let mut sdk = Sdk::new_mock(); sdk.mock() - .expect_fetch_many::( + .expect_fetch_many::( ContestedResourceVotesGivenByIdentityQuery { identity_id: voter.identity.id(), offset: None, diff --git a/src/backend_task/contested_names/query_dpns_contested_resources.rs b/src/backend_task/contested_names/query_dpns_contested_resources.rs index bf64b8b16..6a5589f5d 100644 --- a/src/backend_task/contested_names/query_dpns_contested_resources.rs +++ b/src/backend_task/contested_names/query_dpns_contested_resources.rs @@ -153,7 +153,7 @@ impl AppContext { // Held to the end of the pass and taken before any other lock, so a // pass that waits here holds nothing a running pass needs. let _pass = self.dpns_contest_refresh_pass.lock().await; - let data_contract = self.dpns_contract.as_ref(); + let data_contract = self.dpns_contract(); let document_type = data_contract .document_type_for_name("domain") .map_err(|_| TaskError::ContractSchemaMismatch { @@ -456,12 +456,12 @@ mod tests { context.set_det_kv_override_for_test(crate::wallet_backend::DetKv::from_store(Arc::new( crate::wallet_backend::kv_test_support::InMemoryKv::default(), ))); - let document_type = context - .dpns_contract + let dpns_contract = context.dpns_contract(); + let document_type = dpns_contract .document_type_for_name("domain") .expect("domain document type"); let page_from = |start_at_value| VotePollsByDocumentTypeQuery { - contract_id: context.dpns_contract.id(), + contract_id: context.dpns_contract().id(), document_type_name: document_type.name().to_owned(), index_name: document_type .find_contested_index() @@ -716,9 +716,8 @@ mod tests { async fn load_node_without_votes(context: &Arc, sdk: &mut Sdk) -> Identifier { use crate::model::qualified_identity::{IdentityStatus, IdentityType, QualifiedIdentity}; use dash_sdk::dpp::identity::accessors::IdentityGettersV0; - use dash_sdk::dpp::voting::votes::resource_vote::ResourceVote; use dash_sdk::drive::query::contested_resource_votes_given_by_identity_query::ContestedResourceVotesGivenByIdentityQuery; - use dash_sdk::query_types::ResourceVotesByIdentity; + use dash_sdk::query_types::{ResourceVoteWithCount, ResourceVotesByIdentity}; let (tx, _rx) = tokio::sync::mpsc::channel::(8); context @@ -754,7 +753,7 @@ mod tests { .expect("load the node"); let node_id = node.identity.id(); sdk.mock() - .expect_fetch_many::( + .expect_fetch_many::( ContestedResourceVotesGivenByIdentityQuery { identity_id: node_id, offset: None, @@ -777,8 +776,8 @@ mod tests { name: &str, winner: Option<(ContestedDocumentVotePollWinnerInfo, BlockInfo)>, ) { - let document_type = context - .dpns_contract + let dpns_contract = context.dpns_contract(); + let document_type = dpns_contract .document_type_for_name("domain") .expect("domain document type"); let query = ContestedDocumentVotePollDriveQuery { @@ -793,7 +792,7 @@ mod tests { .clone(), index_values: vec![Value::from("dash"), Value::Text(name.to_owned())], document_type_name: document_type.name().to_owned(), - contract_id: context.dpns_contract.id(), + contract_id: context.dpns_contract().id(), }, allow_include_locked_and_abstaining_vote_tally: true, result_type: ContestedDocumentVotePollDriveQueryResultType::DocumentsAndVoteTally, diff --git a/src/backend_task/contested_names/refresh_vote_states.rs b/src/backend_task/contested_names/refresh_vote_states.rs index 7892a0416..0cc0eb1d3 100644 --- a/src/backend_task/contested_names/refresh_vote_states.rs +++ b/src/backend_task/contested_names/refresh_vote_states.rs @@ -5,10 +5,10 @@ use crate::context::{AppContext, DpnsVoteRefreshResults, MAX_CONCURRENT_DPNS_VOT use dash_sdk::Sdk; use dash_sdk::dpp::identity::accessors::IdentityGettersV0; use dash_sdk::dpp::voting::vote_choices::resource_vote_choice::ResourceVoteChoice; -use dash_sdk::dpp::voting::votes::resource_vote::ResourceVote; use dash_sdk::dpp::voting::votes::resource_vote::accessors::v0::ResourceVoteGettersV0; use dash_sdk::drive::query::contested_resource_votes_given_by_identity_query::ContestedResourceVotesGivenByIdentityQuery; use dash_sdk::platform::{FetchMany, Identifier}; +use dash_sdk::query_types::ResourceVoteWithCount; use futures::{StreamExt, stream}; use std::collections::{BTreeMap, BTreeSet}; use std::sync::Arc; @@ -91,7 +91,7 @@ async fn fetch_votes_for_voter( let mut votes = BTreeMap::new(); let mut start_at = None; loop { - let page = ResourceVote::fetch_many( + let page = ResourceVoteWithCount::fetch_many( sdk, ContestedResourceVotesGivenByIdentityQuery { identity_id: voter_id, @@ -106,7 +106,10 @@ async fn fetch_votes_for_voter( let last_key = page.last().map(|(id, _)| id.to_buffer()); for (poll_id, vote) in page { if let Some(vote) = vote { - votes.insert(poll_id.to_buffer(), vote.resource_vote_choice()); + votes.insert( + poll_id.to_buffer(), + vote.resource_vote.resource_vote_choice(), + ); } } if page_len < usize::from(VOTE_QUERY_PAGE_SIZE) { diff --git a/src/backend_task/contested_names/vote_on_dpns_name.rs b/src/backend_task/contested_names/vote_on_dpns_name.rs index a0e7e8fb4..5911945f2 100644 --- a/src/backend_task/contested_names/vote_on_dpns_name.rs +++ b/src/backend_task/contested_names/vote_on_dpns_name.rs @@ -28,7 +28,7 @@ use dash_sdk::platform::transition::broadcast::BroadcastStateTransition; use dash_sdk::platform::transition::broadcast_request::BroadcastRequestForStateTransition; use dash_sdk::platform::transition::put_settings::PutSettings; use dash_sdk::platform::transition::waitable::Waitable; -use dash_sdk::query_types::ContestedResource; +use dash_sdk::query_types::{ContestedResource, ResourceVoteWithCount}; use std::sync::Arc; #[derive(Debug)] @@ -114,13 +114,13 @@ pub(super) async fn proved_dpns_vote_choice( start_at: Some((key.vote_poll_id.to_buffer(), true)), order_ascending: true, }; - let votes = ResourceVote::fetch_many(sdk, query) + let votes = ResourceVoteWithCount::fetch_many(sdk, query) .await .map_err(TaskError::from)?; Ok(votes .get(&key.vote_poll_id) .and_then(Option::as_ref) - .map(ResourceVoteGettersV0::resource_vote_choice)) + .map(|vote| vote.resource_vote.resource_vote_choice())) } /// Settle a rejection that `broadcast` reported. @@ -169,7 +169,7 @@ impl AppContext { name: &str, sdk: &Sdk, ) -> Result { - let vote_poll = dpns_vote_poll(&self.dpns_contract, name)?; + let vote_poll = dpns_vote_poll(&self.dpns_contract(), name)?; let normalized_label = convert_to_homograph_safe_chars(name); // Pre-flight: confirm Platform has an open poll for this label before diff --git a/src/backend_task/contract.rs b/src/backend_task/contract.rs index a21cd10bf..2b648da42 100644 --- a/src/backend_task/contract.rs +++ b/src/backend_task/contract.rs @@ -76,8 +76,9 @@ impl AppContext { for data_contract in data_contracts { if let Some(contract) = &data_contract.1 { // Fetch the contract description from the Search Contract - let search_contract = &self.keyword_search_contract; + let search_contract = self.keyword_search_contract(); let document_query = DocumentQuery { + integer_range_clauses: vec![], sub_queries: Vec::new(), select: SelectProjection::documents(), data_contract: search_contract.clone(), diff --git a/src/backend_task/dashpay.rs b/src/backend_task/dashpay.rs index ae474f2fe..afe1d2ee9 100644 --- a/src/backend_task/dashpay.rs +++ b/src/backend_task/dashpay.rs @@ -32,7 +32,7 @@ use errors::DashPayError; /// Every `contactRequest` read in this module goes through here, so the /// contract handle and the error attribution are stated once. fn contact_request_query(app_context: &AppContext) -> Result { - DocumentQuery::new(app_context.dashpay_contract.clone(), "contactRequest").map_err(|e| { + DocumentQuery::new(app_context.dashpay_contract(), "contactRequest").map_err(|e| { DashPayError::QueryCreation { query_target: "DashPay contactRequest", source: Box::new(e.into()), @@ -40,6 +40,9 @@ fn contact_request_query(app_context: &AppContext) -> Result Result { - let dashpay_contract = app_context.dashpay_contract.clone(); + let dashpay_contract = app_context.dashpay_contract(); let identity_id = identity.identity.id(); let mut query = DocumentQuery::new(dashpay_contract, "contactInfo").map_err(|e| { DashPayError::QueryCreation { @@ -591,7 +591,7 @@ pub async fn create_or_update_contact_info( contact_user_id: Identifier, update: ContactInfoUpdate, ) -> Result { - let dashpay_contract = app_context.dashpay_contract.clone(); + let dashpay_contract = app_context.dashpay_contract(); let identity_id = identity.identity.id(); let lookup = lookup_contact_info(app_context, sdk, &identity, contact_user_id).await?; let (found_existing_doc, derivation_index, enc_user_id_key, private_data_key) = @@ -714,6 +714,11 @@ pub async fn create_or_update_contact_info( let mut rng = StdRng::from_entropy(); let entropy = Bytes32::random_with_rng(&mut rng); + // Before protocol version 14 this entropy-only id is the document's id, + // which Platform recomputes from the entropy. From protocol version 14 + // (`Document::document_id_depends_on_nonce`) the id also commits to the + // identity contract nonce, so this one is only a placeholder that building + // the create transition replaces. Read the id from the confirmed document. let document_id = Document::generate_document_id_v0( &dashpay_contract.id(), &identity_id, @@ -722,6 +727,8 @@ pub async fn create_or_update_contact_info( ); let document = DppDocument::V0(DocumentV0 { + moderated_at: None, + moderated_by: None, id: document_id, owner_id: identity_id, creator_id: None, @@ -843,6 +850,8 @@ mod tests { properties.insert("privateData".to_string(), private_data); } DppDocument::V0(DocumentV0 { + moderated_at: None, + moderated_by: None, id: id(1), owner_id: id(2), creator_id: None, @@ -878,6 +887,8 @@ mod tests { ), ]); DppDocument::V0(DocumentV0 { + moderated_at: None, + moderated_by: None, id: id(document_id), owner_id: id(254), creator_id: None, diff --git a/src/backend_task/dashpay/contact_requests.rs b/src/backend_task/dashpay/contact_requests.rs index 50873710e..9a0e6f837 100644 --- a/src/backend_task/dashpay/contact_requests.rs +++ b/src/backend_task/dashpay/contact_requests.rs @@ -289,7 +289,7 @@ pub async fn send_contact_request_with_proof( } // Step 3: Check if a contact request already exists - let dashpay_contract = app_context.dashpay_contract.clone(); + let dashpay_contract = app_context.dashpay_contract(); let mut existing_query = contact_request_query(app_context)?; existing_query = existing_query @@ -520,7 +520,11 @@ pub async fn send_contact_request_with_proof( let mut rng = StdRng::from_entropy(); let entropy = Bytes32::random_with_rng(&mut rng); - // Generate deterministic document ID based on entropy + // Before protocol version 14 this entropy-only id is the document's id, + // which Platform recomputes from the entropy. From protocol version 14 + // (`Document::document_id_depends_on_nonce`) the id also commits to the + // identity contract nonce, so this one is only a placeholder that building + // the create transition replaces. Read the id from the confirmed document. let document_id = Document::generate_document_id_v0( &dashpay_contract.id(), &identity.identity.id(), @@ -530,6 +534,8 @@ pub async fn send_contact_request_with_proof( // Create the document let document = DppDocument::V0(DocumentV0 { + moderated_at: None, + moderated_by: None, id: document_id, owner_id: identity.identity.id(), creator_id: None, @@ -1289,6 +1295,8 @@ mod tests { properties.insert("toUserId".to_string(), Value::Identifier(to.to_buffer())); } DppDocument::V0(DocumentV0 { + moderated_at: None, + moderated_by: None, id: id(99), owner_id: owner, creator_id: None, @@ -1319,7 +1327,7 @@ mod tests { operator: WhereOperator::Equal, value: Value::Text(value.to_string()), }; - let mut query = DocumentQuery::new(ctx.dpns_contract.clone(), "domain") + let mut query = DocumentQuery::new(ctx.dpns_contract(), "domain") .expect("domain query") .with_where(equals("normalizedParentDomainName", "dash")) .with_where(equals("normalizedLabel", normalized)); diff --git a/src/backend_task/dashpay/profile.rs b/src/backend_task/dashpay/profile.rs index f67569e0b..eb6605f89 100644 --- a/src/backend_task/dashpay/profile.rs +++ b/src/backend_task/dashpay/profile.rs @@ -21,7 +21,7 @@ pub async fn load_profile( let identity_id = identity.identity.id(); let _operation = app_context.lock_identity_profile(identity_id).await; let name_revision = app_context.begin_identity_profile_load(identity_id); - let dashpay_contract = app_context.dashpay_contract.clone(); + let dashpay_contract = app_context.dashpay_contract(); // Query for profile document owned by this identity let mut profile_query = DocumentQuery::new(dashpay_contract, "profile").map_err(|e| { @@ -193,13 +193,12 @@ pub async fn update_profile( "Failed to cache the avatar fetched for a profile update" ); } - let mut query = - DocumentQuery::new(app_context.dashpay_contract.clone(), "profile").map_err(|e| { - DashPayError::QueryCreation { - query_target: "DashPay profile", - source: Box::new(e.into()), - } - })?; + let mut query = DocumentQuery::new(app_context.dashpay_contract(), "profile").map_err(|e| { + DashPayError::QueryCreation { + query_target: "DashPay profile", + source: Box::new(e.into()), + } + })?; query = query.with_where(WhereClause { field: "$ownerId".to_string(), operator: WhereOperator::Equal, @@ -321,7 +320,7 @@ pub async fn fetch_contact_profile( _identity: QualifiedIdentity, // May be needed for future privacy features contact_id: Identifier, ) -> Result { - let dashpay_contract = app_context.dashpay_contract.clone(); + let dashpay_contract = app_context.dashpay_contract(); // Query for the contact's profile document let mut query = DocumentQuery::new(dashpay_contract, "profile").map_err(|e| { @@ -358,7 +357,7 @@ pub async fn search_profiles( sdk: &Sdk, search_query: String, ) -> Result { - let dashpay_contract = app_context.dashpay_contract.clone(); + let dashpay_contract = app_context.dashpay_contract(); let mut results: Vec<(Identifier, Option, String)> = Vec::new(); let query_trimmed = search_query.trim(); @@ -432,7 +431,7 @@ mod tests { "Alice", Value::Identifier(holder.to_buffer()), ); - let mut prefix_query = DocumentQuery::new(ctx.dpns_contract.clone(), "domain") + let mut prefix_query = DocumentQuery::new(ctx.dpns_contract(), "domain") .expect("domain query") .with_where(WhereClause { field: "normalizedParentDomainName".to_string(), @@ -563,6 +562,8 @@ mod tests { fn profile_removal_is_rejected_instead_of_silently_retaining_fields() { for field in ["displayName", "publicMessage", "avatarUrl"] { let existing = Document::V0(dash_sdk::dpp::document::DocumentV0 { + moderated_at: None, + moderated_by: None, properties: [(field.to_string(), Value::Text("existing value".into()))].into(), ..Default::default() }); @@ -582,6 +583,8 @@ mod tests { #[test] fn profile_replacement_is_allowed_with_unchanged_optional_fields() { let existing = Document::V0(dash_sdk::dpp::document::DocumentV0 { + moderated_at: None, + moderated_by: None, properties: [("displayName".to_string(), Value::Text("Alice".into()))].into(), ..Default::default() }); diff --git a/src/backend_task/document.rs b/src/backend_task/document.rs index 59c8bfef9..fbb493c30 100644 --- a/src/backend_task/document.rs +++ b/src/backend_task/document.rs @@ -102,6 +102,7 @@ impl AppContext { document_id: Identifier, ) -> Result { let document_query = DocumentQuery { + integer_range_clauses: vec![], sub_queries: Vec::new(), select: SelectProjection::documents(), data_contract, @@ -192,6 +193,12 @@ impl AppContext { qualified_identity, identity_key, } => { + let contest_fund = crate::model::fee_estimation::document_create_contest_fund_cap( + document_type.as_ref(), + &document, + sdk.version(), + ) + .map_err(|error| dash_sdk::Error::Protocol(*error))?; let mut builder = DocumentCreateTransitionBuilder::new( data_contract, document_type.name().to_string(), @@ -203,7 +210,12 @@ impl AppContext { builder = builder.with_token_payment_info(token_payment); } - let maybe_options = self.state_transition_options(); + let mut maybe_options = self.state_transition_options(); + if let Some(cap) = contest_fund { + maybe_options + .get_or_insert_with(Default::default) + .contest_fund = Some(cap); + } if let Some(options) = maybe_options { builder = builder.with_state_transition_creation_options(options); } diff --git a/src/backend_task/identity/dpns_usernames.rs b/src/backend_task/identity/dpns_usernames.rs index 3af5c4d0e..72473b787 100644 --- a/src/backend_task/identity/dpns_usernames.rs +++ b/src/backend_task/identity/dpns_usernames.rs @@ -1,4 +1,8 @@ //! Identity-side username ops: availability checks and own-request status. +//! +//! TODO(dpns-marketplace): selling, buying and transferring usernames through +//! the platform-wallet marketplace are not offered yet. See +//! https://github.com/dashpay/dash-evo-tool/issues/1103 use std::collections::BTreeMap; @@ -91,9 +95,10 @@ impl AppContext { /// The lookup for a registered name with this normalized label. pub(super) fn dpns_domain_query(&self, normalized: &str) -> DocumentQuery { DocumentQuery { + integer_range_clauses: vec![], sub_queries: Vec::new(), select: SelectProjection::documents(), - data_contract: self.dpns_contract.clone(), + data_contract: self.dpns_contract(), document_type_name: "domain".to_string(), where_clauses: vec![ WhereClause { @@ -127,7 +132,7 @@ impl AppContext { /// Convert a proved vote state into the pure snapshot the model decides on. fn contest_snapshot(&self, sdk: &Sdk, contenders: &Contenders) -> ContestSnapshot { let documents = self - .dpns_contract + .dpns_contract() .document_type_cloned_for_name("domain") .ok() .and_then(|document_type| { @@ -440,9 +445,10 @@ mod tests { async fn sdk_with_owned_name(ctx: &AppContext, id: Identifier) -> Sdk { let query = DocumentQuery { + integer_range_clauses: vec![], sub_queries: vec![], select: SelectProjection::documents(), - data_contract: ctx.dpns_contract.clone(), + data_contract: ctx.dpns_contract(), document_type_name: "domain".into(), where_clauses: vec![WhereClause { field: "records.identity".into(), @@ -458,6 +464,8 @@ mod tests { start: None, }; let document = Document::V0(dash_sdk::dpp::document::DocumentV0 { + moderated_at: None, + moderated_by: None, id: Identifier::from([88; 32]), owner_id: id, creator_id: None, @@ -604,7 +612,7 @@ mod tests { .expect_fetch_many::( ContestedDocumentVotePollDriveQuery { vote_poll: crate::model::dpns_voting::dpns_vote_poll( - &ctx.dpns_contract, + &ctx.dpns_contract(), &normalize_dpns_label("Alice"), ) .unwrap(), @@ -798,7 +806,7 @@ mod tests { .expect_fetch_many::( ContestedDocumentVotePollDriveQuery { vote_poll: crate::model::dpns_voting::dpns_vote_poll( - &ctx.dpns_contract, + &ctx.dpns_contract(), "d1ssh", ) .unwrap(), diff --git a/src/backend_task/identity/refresh_loaded_identities_dpns_names.rs b/src/backend_task/identity/refresh_loaded_identities_dpns_names.rs index 863e6b4c8..f0cdc4277 100644 --- a/src/backend_task/identity/refresh_loaded_identities_dpns_names.rs +++ b/src/backend_task/identity/refresh_loaded_identities_dpns_names.rs @@ -97,9 +97,10 @@ impl AppContext { /// The lookup for every DPNS name whose records point at `identity_id`. pub(super) fn owned_dpns_names_query(&self, identity_id: Identifier) -> DocumentQuery { DocumentQuery { + integer_range_clauses: vec![], sub_queries: Vec::new(), select: SelectProjection::documents(), - data_contract: self.dpns_contract.clone(), + data_contract: self.dpns_contract(), document_type_name: "domain".to_string(), where_clauses: vec![WhereClause { field: "records.identity".to_string(), @@ -177,6 +178,8 @@ mod tests { /// Mock the owned-names lookup of `owner` to return one name. async fn expect_owned_name(sdk: &mut dash_sdk::Sdk, ctx: &AppContext, owner: Identifier) { let document = Document::V0(dash_sdk::dpp::document::DocumentV0 { + moderated_at: None, + moderated_by: None, id: owner, owner_id: owner, creator_id: None, diff --git a/src/backend_task/identity/register_dpns_name.rs b/src/backend_task/identity/register_dpns_name.rs index 6fb7313a8..f3f80cd6a 100644 --- a/src/backend_task/identity/register_dpns_name.rs +++ b/src/backend_task/identity/register_dpns_name.rs @@ -98,6 +98,7 @@ async fn broadcast_name_registration( identity: &QualifiedIdentity, signing_key: IdentityPublicKey, label: &str, + approved_contest_fee: u64, register: F, ) -> Result where @@ -108,6 +109,7 @@ where let preorder_confirmed = Arc::new(AtomicBool::new(false)); let confirmed = Arc::clone(&preorder_confirmed); let input = SdkRegistrationInput { + contest_fund: Some(approved_contest_fee), label: label.to_owned(), identity: identity.identity.clone(), identity_public_key: signing_key, @@ -151,7 +153,7 @@ impl AppContext { return Err(TaskError::InvalidDpnsName { validation }); } - let dpns_contract = self.dpns_contract.clone(); + let dpns_contract = self.dpns_contract(); let qualified_identity = input.qualified_identity; @@ -189,7 +191,7 @@ impl AppContext { .identity .get_public_key_by_id(key_id) .filter(|key| { - key_can_sign_documents(key, dpns_signing_requirement(&self.dpns_contract)) + key_can_sign_documents(key, dpns_signing_requirement(&self.dpns_contract())) }), None => qualified_identity.document_signing_key(&preorder_document_type), } @@ -214,6 +216,7 @@ impl AppContext { &qualified_identity, public_key.clone(), &input.name_input, + input.approved_contest_fee, register, ) .await @@ -760,7 +763,7 @@ mod tests { let confirmed_preorder = domain_document(identity.identity.id(), "alice-123"); let failure = - broadcast_name_registration(&identity, key, "alice-123", |input| async move { + broadcast_name_registration(&identity, key, "alice-123", 0, |input| async move { // As the SDK does: report the confirmed first document if asked to. if let Some(report_confirmed) = input.preorder_callback { report_confirmed(&confirmed_preorder); @@ -776,6 +779,33 @@ mod tests { ); } + #[tokio::test] + async fn registration_never_auto_quotes_above_the_approved_contest_fund() { + for approved in [0, 50_000_000_000] { + let identity = bare_identity(4); + let key = dash_sdk::platform::IdentityPublicKey::random_key( + 1, + Some(4), + PlatformVersion::latest(), + ); + let _ = broadcast_name_registration( + &identity, + key, + "alice", + approved, + |input| async move { + assert_eq!( + input.contest_fund, + Some(approved), + "The SDK must receive the exact approved cap, including zero." + ); + Err(unproven_broadcast_failure()) + }, + ) + .await; + } + } + /// With no node to confirm the first document, the broadcast stops there. #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn broadcast_that_cannot_confirm_the_first_document_reports_that_stage() { @@ -789,7 +819,7 @@ mod tests { PlatformVersion::latest(), ); - let failure = broadcast_name_registration(&identity, key, "alice-123", |input| { + let failure = broadcast_name_registration(&identity, key, "alice-123", 0, |input| { sdk.register_dpns_name(input) }) .await @@ -907,6 +937,8 @@ mod tests { /// A registered-name document as the network returns it for `label`. fn domain_document(owner: Identifier, label: &str) -> Document { Document::V0(DocumentV0 { + moderated_at: None, + moderated_by: None, id: Identifier::from([0x5D; 32]), owner_id: owner, creator_id: None, @@ -950,7 +982,7 @@ mod tests { .expect_fetch_many::( ContestedDocumentVotePollDriveQuery { vote_poll: crate::model::dpns_voting::dpns_vote_poll( - &ctx.dpns_contract, + &ctx.dpns_contract(), &normalize_dpns_label(label), ) .expect("vote poll"), diff --git a/src/backend_task/identity/top_up_identity.rs b/src/backend_task/identity/top_up_identity.rs index 8a6915f62..8303e2afb 100644 --- a/src/backend_task/identity/top_up_identity.rs +++ b/src/backend_task/identity/top_up_identity.rs @@ -259,7 +259,8 @@ impl AppContext { // Unreachable from here — the caller builds only the two modes // above — but this is a funds path, so an upstream funding mode // this path cannot build fails closed instead of panicking. - AssetLockFunding::DrainAccountBalance { .. } => { + AssetLockFunding::DrainAccountBalance { .. } + | AssetLockFunding::DrainWalletBalance { .. } => { return Err(TaskError::TopUpFundingMethodUnsupported); } }; diff --git a/src/backend_task/shielded/mod.rs b/src/backend_task/shielded/mod.rs index c988bfd56..be0c365db 100644 --- a/src/backend_task/shielded/mod.rs +++ b/src/backend_task/shielded/mod.rs @@ -11,6 +11,9 @@ use dash_sdk::dpp::address_funds::{OrchardAddress, PlatformAddress}; use dash_sdk::dpp::dashcore::Address; use std::sync::Arc; +// TODO(shielded-identity-transfers): moving credits between identities and the +// shielded pool (protocol version 14), and shielding to another wallet, are not +// offered yet. See https://github.com/dashpay/dash-evo-tool/issues/1105 /// The shielded-pool operations DET dispatches into the upstream /// `platform-wallet` coordinator. /// diff --git a/src/backend_task/tokens/mod.rs b/src/backend_task/tokens/mod.rs index 9d3b3304c..e7f66aee8 100644 --- a/src/backend_task/tokens/mod.rs +++ b/src/backend_task/tokens/mod.rs @@ -761,7 +761,9 @@ impl AppContext { token_config_v0.distribution_rules = distribution_rules; token_config_v0.description = token_description; - // All tokens are created NotTradeable; a future SDK will add more modes. + // Tokens are created NotTradeable. Tradeable needs protocol version 15 + // (TOKEN_MARKETPLACE_INITIAL_PROTOCOL_VERSION) and a transferable token, + // and DET has no way to list a token or make an offer for it yet. token_config_v0.marketplace_rules = TokenMarketplaceRules::V0(TokenMarketplaceRulesV0 { trade_mode: TokenTradeMode::NotTradeable, trade_mode_change_rules: marketplace_rules, diff --git a/src/backend_task/tokens/update_token_config.rs b/src/backend_task/tokens/update_token_config.rs index c81668bc2..9d16d7a95 100644 --- a/src/backend_task/tokens/update_token_config.rs +++ b/src/backend_task/tokens/update_token_config.rs @@ -27,6 +27,7 @@ impl AppContext { group_info: Option, sdk: &Sdk, ) -> Result { + validate_supported_change(&change_item)?; tracing::trace!( ?group_info, ?identity_token_info, @@ -126,3 +127,38 @@ impl AppContext { )) } } + +fn validate_supported_change(item: &TokenConfigurationChangeItem) -> Result<(), TaskError> { + if crate::model::token::token_change_item_is_supported(item) { + Ok(()) + } else { + Err(TaskError::TokenShieldedConfigurationUnsupported) + } +} + +#[cfg(test)] +mod sdk_adapter_tests { + use super::*; + use dash_sdk::dpp::data_contract::change_control_rules::authorized_action_takers::AuthorizedActionTakers; + + #[test] + fn backend_rejects_shielded_configuration_before_submission() { + for item in [ + TokenConfigurationChangeItem::MinimumPoolNotesForOutgoing(17), + TokenConfigurationChangeItem::MinimumPoolNotesForOutgoingControlGroup( + AuthorizedActionTakers::NoOne, + ), + TokenConfigurationChangeItem::MinimumPoolNotesForOutgoingAdminGroup( + AuthorizedActionTakers::NoOne, + ), + ] { + assert!(matches!( + validate_supported_change(&item), + Err(TaskError::TokenShieldedConfigurationUnsupported) + )); + } + assert!( + validate_supported_change(&TokenConfigurationChangeItem::MaxSupply(Some(17))).is_ok() + ); + } +} diff --git a/src/context/test_support.rs b/src/context/test_support.rs index dda777c98..b75e9bd1d 100644 --- a/src/context/test_support.rs +++ b/src/context/test_support.rs @@ -174,6 +174,18 @@ fn test_app_context_with_kv_and_network( .expect("AppContext") } +/// Put `ctx` at `protocol_version`, as its SDK stands once a proven response +/// ratcheted it there. The replacement SDK is a mock: nothing reaches a network. +pub(crate) fn set_sdk_protocol_version(ctx: &AppContext, protocol_version: u32) { + let version = dash_sdk::dpp::version::PlatformVersion::get(protocol_version) + .expect("a protocol version this build knows"); + let sdk = dash_sdk::SdkBuilder::new_mock() + .with_initial_version(version) + .build() + .expect("mock sdk"); + ctx.sdk.store(Arc::new(sdk)); +} + /// A mock SDK that reads data contracts through `ctx`, as the app's own SDK does. pub(crate) fn mock_sdk_for(ctx: &AppContext) -> Sdk { let sdk = Sdk::new_mock(); @@ -191,6 +203,8 @@ pub(crate) fn mock_sdk_for(ctx: &AppContext) -> Sdk { pub(crate) fn dpns_name_document(owner: Identifier, label: &str, identity: Value) -> Document { let text = |value: &str| Value::Text(value.to_string()); Document::V0(DocumentV0 { + moderated_at: None, + moderated_by: None, id: Identifier::from([0x5D; 32]), owner_id: owner, creator_id: None, diff --git a/src/model/asset_lock.rs b/src/model/asset_lock.rs index 0ed4c3904..aabbac967 100644 --- a/src/model/asset_lock.rs +++ b/src/model/asset_lock.rs @@ -84,6 +84,7 @@ pub(crate) fn confirmed_funding_for_test( use platform_wallet::wallet::asset_lock::tracked::{AssetLockStatus, TrackedAssetLock}; let out_point = OutPoint::new(Txid::from_byte_array([txid_byte; 32]), 0); TrackedAssetLock { + operation_id: None, out_point, transaction: Transaction { version: 3, diff --git a/src/model/dashpay.rs b/src/model/dashpay.rs index 56de2a557..3965a56d9 100644 --- a/src/model/dashpay.rs +++ b/src/model/dashpay.rs @@ -443,6 +443,8 @@ mod tests { properties.insert("toUserId".to_string(), Value::Identifier(to.to_buffer())); } DppDocument::V0(DocumentV0 { + moderated_at: None, + moderated_by: None, id: id(99), owner_id: id(1), creator_id: None, diff --git a/src/model/fee_estimation.rs b/src/model/fee_estimation.rs index 9919d97d6..53deabbfb 100644 --- a/src/model/fee_estimation.rs +++ b/src/model/fee_estimation.rs @@ -33,6 +33,23 @@ use dash_sdk::dpp::state_transition::identity_topup_transition::IdentityTopUpTra use dash_sdk::dpp::state_transition::identity_topup_transition::v0::IdentityTopUpTransitionV0; use dash_sdk::dpp::state_transition::public_key_in_creation::IdentityPublicKeyInCreation; +/// Keep contested document creation within the protocol's base fund, without auto-quoting a larger fund. +pub fn document_create_contest_fund_cap( + document_type: dash_sdk::dpp::data_contract::document_type::DocumentTypeRef<'_>, + document: &dash_sdk::dpp::document::Document, + platform_version: &PlatformVersion, +) -> Result, Box> { + use dash_sdk::dpp::data_contract::document_type::methods::{ + DocumentTypeBasicMethods, DocumentTypeV0Methods, + }; + use dash_sdk::dpp::document::DocumentV0Getters; + let mut document = document.clone(); + document_type.regenerate_generated_properties(document.properties_mut(), platform_version)?; + Ok(document_type + .contested_vote_poll_for_document(&document, platform_version)? + .map(|_| contest_fee_credits(platform_version))) +} + /// Subtract an estimated fee from a credit balance without floating-point conversion. pub const fn max_spendable_credits(balance: u64, estimated_fee: u64) -> u64 { balance.saturating_sub(estimated_fee) @@ -1337,6 +1354,43 @@ pub fn shield_from_balance_fee_headroom( mod tests { use super::*; + #[test] + fn document_contest_cap_preserves_ordinary_creates_and_caps_contested_names() { + use dash_sdk::dpp::data_contract::accessors::v0::DataContractV0Getters; + use dash_sdk::dpp::platform_value::Value; + use dash_sdk::dpp::system_data_contracts::{SystemDataContract, load_system_data_contract}; + use dash_sdk::platform::Identifier; + for version in [13, 14] { + let pv = PlatformVersion::get(version).unwrap(); + let contract = load_system_data_contract(SystemDataContract::DPNS, pv).unwrap(); + let owner = Identifier::from([1; 32]); + let document = crate::context::test_support::dpns_name_document( + owner, + "alice", + Value::Identifier(owner.to_buffer()), + ); + let domain = contract.document_type_for_name("domain").unwrap(); + assert_eq!( + document_create_contest_fund_cap(domain, &document, pv).unwrap(), + Some(contest_fee_credits(pv)) + ); + let preorder = contract.document_type_for_name("preorder").unwrap(); + assert_eq!( + document_create_contest_fund_cap(preorder, &document, pv).unwrap(), + None + ); + let ordinary = crate::context::test_support::dpns_name_document( + owner, + "alice-123", + Value::Identifier(owner.to_buffer()), + ); + assert_eq!( + document_create_contest_fund_cap(domain, &ordinary, pv).unwrap(), + None + ); + } + } + #[test] fn contest_fee_follows_protocol_version() { // USR-TC-005 @@ -2080,12 +2134,10 @@ mod tests { fn any_core_address() -> Address { use dash_sdk::dpp::dashcore::Network; use dash_sdk::dpp::dashcore::PublicKey; - use dash_sdk::dpp::dashcore::secp256k1::{ - PublicKey as SecpPublicKey, Secp256k1, SecretKey, - }; - let secp = Secp256k1::new(); - let sk = SecretKey::from_slice(&[1u8; 32]).expect("valid secret key"); - let pubkey = PublicKey::from_slice(&SecpPublicKey::from_secret_key(&secp, &sk).serialize()) + use dash_sdk::dpp::secp256k1::{PublicKey as SecpPublicKey, SecretKey}; + + let sk = SecretKey::from_secret_bytes([1u8; 32]).expect("valid secret key"); + let pubkey = PublicKey::from_slice(&SecpPublicKey::from_secret_key(&sk).serialize()) .expect("valid pubkey"); Address::p2pkh(&pubkey, Network::Testnet) } diff --git a/src/model/token.rs b/src/model/token.rs index de3669c7d..3989dc219 100644 --- a/src/model/token.rs +++ b/src/model/token.rs @@ -14,6 +14,19 @@ pub fn validate_contract_keyword(keyword: &str) -> Result<(), TextLengthError> { ) } +/// Whether this application supports editing the requested token setting. +pub(crate) fn token_change_item_is_supported( + item: &dash_sdk::dpp::data_contract::associated_token::token_configuration_item::TokenConfigurationChangeItem, +) -> bool { + use dash_sdk::dpp::data_contract::associated_token::token_configuration_item::TokenConfigurationChangeItem; + !matches!( + item, + TokenConfigurationChangeItem::MinimumPoolNotesForOutgoing(_) + | TokenConfigurationChangeItem::MinimumPoolNotesForOutgoingControlGroup(_) + | TokenConfigurationChangeItem::MinimumPoolNotesForOutgoingAdminGroup(_) + ) +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/ui/contracts_documents/document_action_screen.rs b/src/ui/contracts_documents/document_action_screen.rs index 744c22a91..56db4e99a 100644 --- a/src/ui/contracts_documents/document_action_screen.rs +++ b/src/ui/contracts_documents/document_action_screen.rs @@ -732,6 +732,7 @@ impl DocumentActionScreen { | DocumentPropertyType::U64 | DocumentPropertyType::I64 | DocumentPropertyType::U32 + | DocumentPropertyType::KeyIdWithReference(_) | DocumentPropertyType::I32 | DocumentPropertyType::U16 | DocumentPropertyType::I16 @@ -804,6 +805,11 @@ impl DocumentActionScreen { .background_color(DashColors::input_background(dark_mode)), ); } + DocumentPropertyType::TypedArray(_) => { + ui.label( + "This field cannot be edited here. Use another document editor.", + ); + } DocumentPropertyType::Object(_) | DocumentPropertyType::Array(_) | DocumentPropertyType::VariableTypeArray(_) => { @@ -1255,6 +1261,10 @@ impl DocumentActionScreen { .as_ref() .ok_or("No identity selected")?; + if !Self::supports_field_editor(doc_type) { + return Err("This document contains fields that cannot be edited here. Use another document editor.".to_string()); + } + for (prop, schema) in doc_type.properties() { let entry = self.field_inputs.get(prop).map(|s| s.trim()); if schema.required && entry.unwrap_or("").is_empty() { @@ -1300,7 +1310,7 @@ impl DocumentActionScreen { .map_err(|_| format!("{} must be a signed 64-bit integer", name))?; Value::I64(n) } - DocumentPropertyType::U32 => { + DocumentPropertyType::U32 | DocumentPropertyType::KeyIdWithReference(_) => { let n = input_str .parse::() .map_err(|_| format!("{} must be an unsigned 32-bit integer", name))?; @@ -1372,6 +1382,11 @@ impl DocumentActionScreen { .map_err(|_| format!("{} (Date) expects unix-ms integer", name))?; Value::U64(ts) } + DocumentPropertyType::TypedArray(_) => { + return Err(format!( + "Field {name} cannot be edited here. Use another document editor." + )); + } DocumentPropertyType::Object(_) => { return Err(format!( "Object field {} must be supplied via JSON textarea", @@ -1393,6 +1408,11 @@ impl DocumentActionScreen { let entropy: [u8; 32] = rng.r#gen(); let owner_id = qi.identity.id(); + // Before protocol version 14 this entropy-only id is the document's id, + // which Platform recomputes from the entropy. From protocol version 14 + // (`Document::document_id_depends_on_nonce`) the id also commits to the + // identity contract nonce, so this one is only a placeholder that building + // the create transition replaces. Read the id from the confirmed document. let id = Document::generate_document_id_v0( &contract.contract.id(), &owner_id, @@ -1411,6 +1431,8 @@ impl DocumentActionScreen { properties, owner_id, creator_id: None, + moderated_at: None, + moderated_by: None, revision, created_at: None, updated_at: None, @@ -1434,6 +1456,10 @@ impl DocumentActionScreen { .as_ref() .ok_or("No document-type selected")?; + if !Self::supports_field_editor(doc_type) { + return Err("This document contains fields that cannot be edited here. Use another document editor.".to_string()); + } + for (prop, schema) in doc_type.properties() { let entry = self.field_inputs.get(prop).map(|s| s.trim()); if schema.required && entry.unwrap_or("").is_empty() { @@ -1479,7 +1505,7 @@ impl DocumentActionScreen { .map_err(|_| format!("{} must be a signed 64-bit integer", name))?; Value::I64(n) } - DocumentPropertyType::U32 => { + DocumentPropertyType::U32 | DocumentPropertyType::KeyIdWithReference(_) => { let n = input_str .parse::() .map_err(|_| format!("{} must be an unsigned 32-bit integer", name))?; @@ -1551,6 +1577,11 @@ impl DocumentActionScreen { .map_err(|_| format!("{} (Date) expects unix-ms integer", name))?; Value::U64(ts) } + DocumentPropertyType::TypedArray(_) => { + return Err(format!( + "Field {name} cannot be edited here. Use another document editor." + )); + } DocumentPropertyType::Object(_) => { return Err(format!( "Object field {} must be supplied via JSON textarea", @@ -1582,6 +1613,8 @@ impl DocumentActionScreen { properties, owner_id: original_doc.owner_id(), creator_id: original_doc.creator_id(), + moderated_at: original_doc.moderated_at(), + moderated_by: original_doc.moderated_by(), revision: new_revision, created_at: None, updated_at: None, @@ -1598,7 +1631,26 @@ impl DocumentActionScreen { Ok((updated_doc.into(), entropy)) } + fn supports_field_editor( + doc_type: &dash_sdk::dpp::data_contract::document_type::DocumentType, + ) -> bool { + !doc_type + .properties() + .values() + .any(|schema| matches!(schema.property_type, DocumentPropertyType::TypedArray(_))) + } + fn can_broadcast(&self) -> bool { + if matches!( + self.action_type, + DocumentActionType::Create | DocumentActionType::Replace + ) && self + .selected_document_type + .as_ref() + .is_some_and(|doc_type| !Self::supports_field_editor(doc_type)) + { + return false; + } match self.action_type { DocumentActionType::Create => !self.field_inputs.is_empty(), DocumentActionType::Delete => !self.document_id_input.is_empty(), @@ -1895,3 +1947,72 @@ impl DocumentActionScreen { .inner } } + +#[cfg(test)] +mod tests { + use super::*; + fn editor_document_type(array: bool) -> DocumentType { + use dash_sdk::dpp::data_contract::config::DataContractConfig; + use dash_sdk::dpp::platform_value::platform_value; + use dash_sdk::dpp::version::PlatformVersion; + + let version = PlatformVersion::get(14).expect("protocol 14"); + let config = DataContractConfig::default_for_version(version).unwrap(); + let property = if array { + platform_value!({"type": "array", "items": {"type": "string", "maxLength": 20_u32}, "maxItems": 4_u32, "position": 0_u32}) + } else { + platform_value!({"type": "string", "maxLength": 20_u32, "position": 0_u32}) + }; + DocumentType::try_from_schema( + Identifier::new([1; 32]), 1, config.version(), "entry", + platform_value!({"type": "object", "properties": {"value": property}, "additionalProperties": false}), + None, &BTreeMap::new(), &config, false, &mut Vec::new(), version, + ).unwrap() + } + + #[test] + fn replacing_an_optional_typed_array_does_not_drop_its_contents() { + let dir = tempfile::tempdir().unwrap(); + let context = crate::context::test_support::test_app_context(dir.path()); + let mut screen = DocumentActionScreen::new(context, None, DocumentActionType::Replace); + screen.selected_document_type = Some(editor_document_type(true)); + screen.original_doc = Some( + DocumentV0 { + properties: BTreeMap::from([( + "value".to_string(), + Value::Array(vec![Value::Text("keep".into())]), + )]), + ..Default::default() + } + .into(), + ); + screen.broadcast_status = BroadcastStatus::Fetched; + assert!(!screen.can_broadcast()); + assert!(screen.try_build_document_from_original().is_err()); + assert!(matches!(screen.create_replace_task(), BackendTask::None)); + } + + #[test] + fn replacing_document_fields_preserves_moderation_metadata() { + let dir = tempfile::tempdir().unwrap(); + let context = crate::context::test_support::test_app_context(dir.path()); + let mut screen = DocumentActionScreen::new(context, None, DocumentActionType::Replace); + screen.selected_document_type = Some(editor_document_type(false)); + screen.original_doc = Some( + DocumentV0 { + moderated_at: Some(1234), + moderated_by: Some(Identifier::new([7; 32])), + ..Default::default() + } + .into(), + ); + screen.field_inputs.insert("value".into(), "changed".into()); + let (document, _) = screen.try_build_document_from_original().unwrap(); + assert_eq!(document.moderated_at(), Some(1234)); + assert_eq!(document.moderated_by(), Some(Identifier::new([7; 32]))); + assert_eq!( + document.properties().get("value"), + Some(&Value::Text("changed".into())) + ); + } +} diff --git a/src/ui/contracts_documents/group_actions_screen.rs b/src/ui/contracts_documents/group_actions_screen.rs index f8ccf6ee9..58cb3b087 100644 --- a/src/ui/contracts_documents/group_actions_screen.rs +++ b/src/ui/contracts_documents/group_actions_screen.rs @@ -217,23 +217,28 @@ impl GroupActionsScreen { }); row.col(|ui| { ui.horizontal(|ui| { - ui.label(self.get_token_event_type(token_event)); + ui.label(Self::get_token_event_type(token_event)); ui.add_space(30.0); }); }); row.col(|ui| { ui.horizontal(|ui| { - ui.label(self.get_token_event_info(token_event)); + ui.label(Self::get_token_event_info(token_event)); ui.add_space(30.0); }); }); row.col(|ui| { ui.horizontal(|ui| { - ui.label(self.get_token_event_note(token_event)); + ui.label(Self::get_token_event_note(token_event)); ui.add_space(30.0); }); }); row.col(|ui| { + if !Self::supports_token_event_action(token_event) { + ui.add_enabled(false, egui::Button::new("Take Action")) + .on_hover_text("This action cannot be signed here. Use another token client."); + return; + } if ComponentStyles::add_primary_button( ui, "Take Action", @@ -291,7 +296,7 @@ impl GroupActionsScreen { action } - fn get_token_event_type(&self, token_event: &TokenEvent) -> &'static str { + fn get_token_event_type(token_event: &TokenEvent) -> &'static str { match token_event { TokenEvent::Mint(..) => "Mint", TokenEvent::Burn(..) => "Burn", @@ -304,10 +309,17 @@ impl GroupActionsScreen { TokenEvent::ConfigUpdate(..) => "ConfigUpdate", TokenEvent::ChangePriceForDirectPurchase(..) => "ChangePrice", TokenEvent::DirectPurchase(..) => "DirectPurchase", + TokenEvent::Shield(..) => "Shield", + TokenEvent::Unshield(..) => "Unshield", + TokenEvent::ShieldedTransfer => "ShieldedTransfer", + TokenEvent::MintToPool(..) => "MintToPool", + TokenEvent::BurnFromPool(..) => "BurnFromPool", + TokenEvent::ClaimToPool(..) => "ClaimToPool", + TokenEvent::DirectPurchaseToPool(..) => "DirectPurchaseToPool", } } - fn get_token_event_info(&self, token_event: &TokenEvent) -> String { + fn get_token_event_info(token_event: &TokenEvent) -> String { match token_event { TokenEvent::Mint(amount, identifier, _) => format!("{} to {}", amount, identifier), TokenEvent::Burn(amount, burn_from, _) => format!("{} from {}", amount, burn_from), @@ -323,13 +335,25 @@ impl GroupActionsScreen { TokenEvent::EmergencyAction(action, _) => format!("{:?}", action), TokenEvent::ConfigUpdate(change_item, _) => format!("{:?}", change_item), TokenEvent::ChangePriceForDirectPurchase(schedule, _) => format!("{:?}", schedule), - TokenEvent::DirectPurchase(amount, credits) => { + TokenEvent::DirectPurchase(amount, credits) + | TokenEvent::DirectPurchaseToPool(amount, credits) => { format!("{} for {} credits", amount, credits) } + TokenEvent::Shield(amount) | TokenEvent::ClaimToPool(amount) => { + format!("{amount} to the shielded pool") + } + TokenEvent::Unshield(identifier, amount) => format!("{amount} to {identifier}"), + TokenEvent::ShieldedTransfer => "The amount and participants are private.".to_string(), + TokenEvent::MintToPool(amount, digest, _) => { + format!("{amount} to the shielded pool; action digest {digest}") + } + TokenEvent::BurnFromPool(amount, digest, _) => { + format!("{amount} from the shielded pool; action digest {digest}") + } } } - fn get_token_event_note(&self, token_event: &TokenEvent) -> String { + fn get_token_event_note(token_event: &TokenEvent) -> String { match token_event { TokenEvent::Mint(_, _, note_opt) | TokenEvent::Burn(_, _, note_opt) @@ -339,16 +363,36 @@ impl GroupActionsScreen { | TokenEvent::Claim(_, _, note_opt) | TokenEvent::EmergencyAction(_, note_opt) | TokenEvent::ConfigUpdate(_, note_opt) - | TokenEvent::ChangePriceForDirectPurchase(_, note_opt) => { + | TokenEvent::ChangePriceForDirectPurchase(_, note_opt) + | TokenEvent::MintToPool(_, _, note_opt) + | TokenEvent::BurnFromPool(_, _, note_opt) => { note_opt.clone().unwrap_or_default().to_string() } TokenEvent::Transfer(_, public_note, _, _, _) => { public_note.clone().unwrap_or_default().to_string() } - TokenEvent::DirectPurchase(_, _) => String::new(), + TokenEvent::DirectPurchase(_, _) + | TokenEvent::Shield(_) + | TokenEvent::Unshield(_, _) + | TokenEvent::ShieldedTransfer + | TokenEvent::ClaimToPool(_) + | TokenEvent::DirectPurchaseToPool(_, _) => String::new(), } } + fn supports_token_event_action(token_event: &TokenEvent) -> bool { + !matches!( + token_event, + TokenEvent::Shield(_) + | TokenEvent::Unshield(_, _) + | TokenEvent::ShieldedTransfer + | TokenEvent::MintToPool(_, _, _) + | TokenEvent::BurnFromPool(_, _, _) + | TokenEvent::ClaimToPool(_) + | TokenEvent::DirectPurchaseToPool(_, _) + ) + } + /// On click of the "Take Action" button fn handle_token_event_action( &mut self, @@ -614,3 +658,50 @@ impl ScreenLike for GroupActionsScreen { action } } + +#[cfg(test)] +mod sdk_adapter_tests { + use super::*; + + #[test] + fn shielded_events_are_displayed_without_offering_transparent_actions() { + let identity = Identifier::from([7; 32]); + for (event, label, detail, note) in [ + (TokenEvent::Shield(12), "Shield", "12", ""), + (TokenEvent::Unshield(identity, 13), "Unshield", "13", ""), + ( + TokenEvent::ShieldedTransfer, + "ShieldedTransfer", + "private", + "", + ), + ( + TokenEvent::MintToPool(14, identity, Some("mint note".into())), + "MintToPool", + "action digest", + "mint note", + ), + ( + TokenEvent::BurnFromPool(15, identity, Some("burn note".into())), + "BurnFromPool", + "action digest", + "burn note", + ), + (TokenEvent::ClaimToPool(16), "ClaimToPool", "16", ""), + ( + TokenEvent::DirectPurchaseToPool(17, 18), + "DirectPurchaseToPool", + "17 for 18", + "", + ), + ] { + assert_eq!(GroupActionsScreen::get_token_event_type(&event), label); + assert!(GroupActionsScreen::get_token_event_info(&event).contains(detail)); + assert_eq!(GroupActionsScreen::get_token_event_note(&event), note); + assert!(!GroupActionsScreen::supports_token_event_action(&event)); + } + assert!(GroupActionsScreen::supports_token_event_action( + &TokenEvent::Mint(12, identity, None) + )); + } +} diff --git a/src/ui/tokens/tokens_screen/mod.rs b/src/ui/tokens/tokens_screen/mod.rs index 1beb76bcf..b8cfcfdc6 100644 --- a/src/ui/tokens/tokens_screen/mod.rs +++ b/src/ui/tokens/tokens_screen/mod.rs @@ -3444,7 +3444,9 @@ mod tests { let (token_pos, token_config) = contract_v1.tokens.iter().next().unwrap(); assert_eq!({ *token_pos }, 0, "Should be at position 0 by default"); - let TokenConfiguration::V0(token_v0) = token_config; + let TokenConfiguration::V0(token_v0) = token_config else { + panic!("the existing token builder must retain V0 configuration"); + }; let TokenConfigurationConvention::V0(conv_v0) = &token_v0.conventions; assert_eq!(conv_v0.decimals, 8, "Decimals from UI not matched"); @@ -3647,7 +3649,9 @@ mod tests { .expect("Should build successfully"); let contract_v1 = data_contract.as_v1().expect("Expected DataContract::V1"); - let TokenConfiguration::V0(ref token_v0) = contract_v1.tokens[&0u16]; + let TokenConfiguration::V0(ref token_v0) = contract_v1.tokens[&0u16] else { + panic!("the existing token builder must retain V0 configuration"); + }; let TokenDistributionRules::V0(dist_rules_v0) = &token_v0.distribution_rules else { panic!("rules without a once-per-identity distribution stay V0"); }; diff --git a/src/ui/tokens/update_token_config.rs b/src/ui/tokens/update_token_config.rs index 3e127a965..54b2b6a7b 100644 --- a/src/ui/tokens/update_token_config.rs +++ b/src/ui/tokens/update_token_config.rs @@ -5,6 +5,7 @@ use crate::backend_task::{BackendTask, BackendTaskSuccessResult, FeeResult}; use crate::context::AppContext; use crate::model::fee_estimation::format_credits_as_dash; use crate::model::qualified_identity::QualifiedIdentity; +use crate::model::token::token_change_item_is_supported; use crate::model::user_role::UserRole; use crate::model::wallet::Wallet; use crate::ui::components::left_panel::add_left_panel; @@ -604,6 +605,11 @@ impl UpdateTokenConfigScreen { TokenConfigurationChangeItem::MarketplaceTradeMode(_) => { ui.label("Marketplace settings are not yet supported."); } + TokenConfigurationChangeItem::MinimumPoolNotesForOutgoing(_) + | TokenConfigurationChangeItem::MinimumPoolNotesForOutgoingControlGroup(_) + | TokenConfigurationChangeItem::MinimumPoolNotesForOutgoingAdminGroup(_) => { + ui.label("This setting cannot be changed here. Use another token configuration editor."); + } } }); }); @@ -668,6 +674,7 @@ impl UpdateTokenConfigScreen { ); if (self.app_context.user_role().at_least(UserRole::Power) || !button_text.contains("Test")) + && token_change_item_is_supported(&self.change_item) && self.change_item != TokenConfigurationChangeItem::TokenConfigurationNoChange { ui.add_space(20.0); @@ -1091,5 +1098,38 @@ fn token_change_item_label(item: &TokenConfigurationChangeItem) -> &'static str "Marketplace Trade Mode Admin Group" } TokenConfigurationChangeItem::MainControlGroup(_) => "Main Control Group", + TokenConfigurationChangeItem::MinimumPoolNotesForOutgoing(_) => { + "Minimum Pool Notes for Outgoing Transfers" + } + TokenConfigurationChangeItem::MinimumPoolNotesForOutgoingControlGroup(_) => { + "Minimum Pool Notes Control Group" + } + TokenConfigurationChangeItem::MinimumPoolNotesForOutgoingAdminGroup(_) => { + "Minimum Pool Notes Admin Group" + } + } +} + +#[cfg(test)] +mod sdk_adapter_tests { + use super::*; + + #[test] + fn shielded_pool_configuration_cannot_be_submitted_from_this_editor() { + for item in [ + TokenConfigurationChangeItem::MinimumPoolNotesForOutgoing(17), + TokenConfigurationChangeItem::MinimumPoolNotesForOutgoingControlGroup( + AuthorizedActionTakers::NoOne, + ), + TokenConfigurationChangeItem::MinimumPoolNotesForOutgoingAdminGroup( + AuthorizedActionTakers::NoOne, + ), + ] { + assert!(!token_change_item_is_supported(&item)); + assert!(token_change_item_label(&item).contains("Pool")); + } + assert!(token_change_item_is_supported( + &TokenConfigurationChangeItem::MaxSupply(Some(17)) + )); } } diff --git a/src/ui/tokens/view_token_claims_screen.rs b/src/ui/tokens/view_token_claims_screen.rs index 6264a8720..9a23c0243 100644 --- a/src/ui/tokens/view_token_claims_screen.rs +++ b/src/ui/tokens/view_token_claims_screen.rs @@ -59,9 +59,10 @@ impl ViewTokenClaimsScreen { Self { identity_token_basic_info: identity_token_basic_info.clone(), new_claims_query: DocumentQuery { + integer_range_clauses: vec![], sub_queries: Vec::new(), select: SelectProjection::documents(), - data_contract: app_context.token_history_contract.clone(), + data_contract: app_context.token_history_contract(), document_type_name: "claim".to_string(), where_clauses: vec![ WhereClause { diff --git a/src/wallet_backend/event_bridge.rs b/src/wallet_backend/event_bridge.rs index 3df519f1d..f1ccb4e0e 100644 --- a/src/wallet_backend/event_bridge.rs +++ b/src/wallet_backend/event_bridge.rs @@ -286,6 +286,17 @@ impl EventHandler for EventBridge { *wallet_id } WalletEvent::SyncHeightAdvanced { wallet_id, .. } => *wallet_id, + WalletEvent::ChainTruncated { + wallet_id, + txids, + unconfirmed_txids, + .. + } => { + self.snapshots.remove_transactions(wallet_id, txids); + self.snapshots + .unconfirm_instant_locked(wallet_id, unconfirmed_txids); + *wallet_id + } WalletEvent::TransactionsSwept { wallet_id, txids, .. } => { diff --git a/src/wallet_backend/mod.rs b/src/wallet_backend/mod.rs index f1aeb5fc8..253ee1a0e 100644 --- a/src/wallet_backend/mod.rs +++ b/src/wallet_backend/mod.rs @@ -2326,7 +2326,7 @@ impl WalletBackend { &self, address: &str, msg: &[u8; 32], - ) -> Result { + ) -> Result { let scope = SecretScope::SingleKey { address: address.to_string(), }; @@ -3378,6 +3378,12 @@ fn map_shielded_op_error(e: platform_wallet::error::PlatformWalletError) -> Task } } + // A token operation keeps its SDK cause, so it is classified like any + // SDK error; an unclassified one still names the operation. + P::TokenOperationFailed { operation, source } => { + TaskError::from_token_operation_failure(operation, source) + } + // Every remaining variant → generic WalletBackend wrapper. other @ (P::WalletCreation(_) | P::StaleReservation @@ -3427,8 +3433,6 @@ fn map_shielded_op_error(e: platform_wallet::error::PlatformWalletError) -> Task | P::NoWalletsConfigured | P::SpvError(_) | P::TokenError(_) - // Upstream's typed successor of `TokenError`; same message. - | P::TokenOperationFailed { .. } | P::ShieldedNoUnspentNotes | P::ShieldedInsufficientBalance { .. } | P::ShieldedBuildError(_) @@ -3453,6 +3457,12 @@ fn map_shielded_op_error(e: platform_wallet::error::PlatformWalletError) -> Task | P::MessageSigningMessageInvalid { .. } | P::MessageSigningKeyUnavailable { .. } | P::MessageSigningFailed { .. } + | P::PersisterFlush(_) + | P::IdentityBalanceUnavailable(_) + | P::InvitationNetworkMismatch { .. } + | P::AssetLockTooManyInputs { .. } + | P::QuorumNotVerified { .. } + | P::SpvProcessRestartRequired(_) | P::ShutdownIncomplete(_)) => TaskError::WalletBackend { source: Arc::new(other), }, @@ -3783,6 +3793,12 @@ fn identity_op_error_kind(e: &platform_wallet::error::PlatformWalletError) -> Id // know", so it is not `NotManaged` (which asserts the identity is // absent and must be reloaded); upstream's contract is to retry. | P::IdentityDiscoveryIncomplete { .. } + | P::PersisterFlush(_) + | P::IdentityBalanceUnavailable(_) + | P::InvitationNetworkMismatch { .. } + | P::AssetLockTooManyInputs { .. } + | P::QuorumNotVerified { .. } + | P::SpvProcessRestartRequired(_) // Background sync failed to quiesce — a shutdown fault, unrelated to // whether this op reached Platform. | P::ShutdownIncomplete(_) => IdentityOpErrorKind::Other, diff --git a/src/wallet_backend/payments.rs b/src/wallet_backend/payments.rs index 7c573f995..71a14cfdb 100644 --- a/src/wallet_backend/payments.rs +++ b/src/wallet_backend/payments.rs @@ -1111,6 +1111,14 @@ mod tests { } } + /// A credit output script as long as the P2PKH one a real asset lock pays + /// to. The builder sizes the asset-lock payload, and so its fee, from the + /// actual credit outputs, so a shorter script would make the fee cheaper + /// than in production and let a larger amount build. + fn credit_output_script() -> ScriptBuf { + ScriptBuf::from_bytes(vec![0; super::P2PKH_CREDIT_OUTPUT_SCRIPT_LEN]) + } + #[test] fn asset_lock_max_excludes_unconfirmed_funds_counted_by_snapshot() { const CONFIRMED_DUFFS: u64 = 1_000_000; @@ -1175,7 +1183,7 @@ mod tests { .set_special_payload(TransactionPayload::AssetLockPayloadType( AssetLockPayload::new(vec![TxOut { value: max_amount, - script_pubkey: ScriptBuf::new(), + script_pubkey: credit_output_script(), }]), )) .add_funding(&mut dry_run_account, account) @@ -1193,7 +1201,7 @@ mod tests { .set_special_payload(TransactionPayload::AssetLockPayloadType( AssetLockPayload::new(vec![TxOut { value: max_amount + 1, - script_pubkey: ScriptBuf::new(), + script_pubkey: credit_output_script(), }]), )) .add_funding(&mut one_over_account, account) @@ -1211,7 +1219,7 @@ mod tests { .set_special_payload(TransactionPayload::AssetLockPayloadType( AssetLockPayload::new(vec![TxOut { value: snapshot_balance.spendable(), - script_pubkey: ScriptBuf::new(), + script_pubkey: credit_output_script(), }]), )) .add_funding(&mut overshoot_account, account) @@ -1294,7 +1302,7 @@ mod tests { .set_special_payload(TransactionPayload::AssetLockPayloadType( AssetLockPayload::new(vec![TxOut { value: max_amount, - script_pubkey: ScriptBuf::new(), + script_pubkey: credit_output_script(), }]), )) .add_funding(&mut dry_run_account, account) @@ -1316,7 +1324,7 @@ mod tests { .set_special_payload(TransactionPayload::AssetLockPayloadType( AssetLockPayload::new(vec![TxOut { value: max_amount + 1, - script_pubkey: ScriptBuf::new(), + script_pubkey: credit_output_script(), }]), )) .add_funding(&mut one_over_account, account) diff --git a/tests/backend-e2e/measure_dpns_registration.rs b/tests/backend-e2e/measure_dpns_registration.rs index 29f0c65d9..6a944e268 100644 --- a/tests/backend-e2e/measure_dpns_registration.rs +++ b/tests/backend-e2e/measure_dpns_registration.rs @@ -172,6 +172,9 @@ async fn register_and_log( let (confirmed_tx, confirmed_rx) = tokio::sync::oneshot::channel::<()>(); let (resume_tx, resume_rx) = std::sync::mpsc::channel::<()>(); let input = SdkRegistrationInput { + // States the fund the app approves, as the app's task does: the vote + // fee for a contested name, nothing otherwise. + contest_fund: Some(vote_fee), label: label.clone(), identity: identity.identity.clone(), identity_public_key: signing_key, From 2bb9fede8bd244fd965fe011d8067fa814859e9a Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Sat, 24 Oct 2026 18:21:52 +0000 Subject: [PATCH 04/27] fix(sdk): follow the network's protocol version The SDK was pinned to the protocol 13 table, so it could not build any protocol 14 transition or follow the versioned identity lookup. It now starts at upstream's per-network minimum and ratchets to what proven responses report. - System contracts are cached per protocol version and served through accessors. The context provider serves them at the version the proof verifier asks for and fails rather than serve another version's. - A proved epoch sets the fee multiplier and raises the protocol version. An older epoch than the adopted one is ignored. - Rebuilding the connection resets the protocol version, fee multiplier and adopted epoch under one lock before the new SDK is published, and drops reads begun on the old connection. - Capabilities read the network's version through a helper that stays closed for a version this build does not define. - Contest timing reads the live version; retired vote schedules keep the protocol 13 notice window they were created under. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- .../query_dpns_vote_contenders.rs | 2 +- src/backend_task/dashpay/contacts.rs | 2 +- src/backend_task/mod.rs | 30 +- src/backend_task/platform_info.rs | 422 +++++++++--------- src/backend_task/tokens/query_tokens.rs | 17 +- src/context/contested_names_db.rs | 56 ++- src/context/contract_token_db.rs | 10 +- src/context/dpns_vote_state.rs | 2 +- src/context/feature_gate.rs | 50 ++- src/context/legacy_scheduled_votes.rs | 6 +- src/context/mod.rs | 391 +++++++++++++--- src/context/system_contracts.rs | 156 +++++++ src/context_provider.rs | 133 +++++- src/mcp/resolve.rs | 6 +- src/sdk_wrapper.rs | 11 +- .../contracts_documents_screen.rs | 6 +- .../identity/add_new_identity_screen/mod.rs | 4 +- src/ui/identity/mod.rs | 2 +- src/ui/identity/register_dpns_name_screen.rs | 6 +- src/ui/identity/usernames_card.rs | 2 +- src/ui/tokens/tokens_screen/my_tokens.rs | 2 +- 21 files changed, 963 insertions(+), 353 deletions(-) create mode 100644 src/context/system_contracts.rs diff --git a/src/backend_task/contested_names/query_dpns_vote_contenders.rs b/src/backend_task/contested_names/query_dpns_vote_contenders.rs index 52760f99b..8eab0311a 100644 --- a/src/backend_task/contested_names/query_dpns_vote_contenders.rs +++ b/src/backend_task/contested_names/query_dpns_vote_contenders.rs @@ -20,7 +20,7 @@ impl AppContext { sdk: &Sdk, _sender: crate::utils::egui_mpsc::SenderAsync, ) -> Result<(), TaskError> { - let data_contract = self.dpns_contract.as_ref(); + let data_contract = self.dpns_contract(); let document_type = data_contract .document_type_for_name("domain") .map_err(|_| TaskError::DataContractNotFound)?; diff --git a/src/backend_task/dashpay/contacts.rs b/src/backend_task/dashpay/contacts.rs index 6de7aa6d1..642f1a18b 100644 --- a/src/backend_task/dashpay/contacts.rs +++ b/src/backend_task/dashpay/contacts.rs @@ -145,7 +145,7 @@ pub async fn load_contacts( identity: QualifiedIdentity, ) -> Result { let identity_id = identity.identity.id(); - let dashpay_contract = app_context.dashpay_contract.clone(); + let dashpay_contract = app_context.dashpay_contract(); // Query for contact requests where we are the sender (ownerId) let mut outgoing_query = contact_request_query(app_context)?; diff --git a/src/backend_task/mod.rs b/src/backend_task/mod.rs index b0a3878e7..09811cb07 100644 --- a/src/backend_task/mod.rs +++ b/src/backend_task/mod.rs @@ -1,36 +1,36 @@ use crate::app::TaskResult; -use crate::backend_task::error::{DapiAddressAvailability, TaskError}; use crate::backend_task::contested_names::ContestedResourceTask; use crate::backend_task::contract::ContractTask; use crate::backend_task::core::{CoreItem, CoreTask}; -use crate::backend_task::dashpay::{DashPayTask, ContactData}; +use crate::backend_task::dashpay::{ContactData, DashPayTask}; use crate::backend_task::document::DocumentTask; +use crate::backend_task::error::{DapiAddressAvailability, TaskError}; use crate::backend_task::identity::IdentityTask; use crate::backend_task::platform_info::{PlatformInfoTaskRequestType, PlatformInfoTaskResult}; use crate::backend_task::system_task::SystemTask; use crate::backend_task::wallet::WalletTask; use crate::context::AppContext; use crate::context::identity_load_registry::IdentityLoadToken; -use crate::model::masternode_input::decode_identity_id; use crate::model::dpns_voting::{ DpnsScheduledVoteClearOutcome, DpnsScheduledVoteKey, DpnsVoteOperationId, DpnsVoteTargetKey, }; -use dash_sdk::dpp::address_funds::PlatformAddress; -use dash_sdk::dpp::dashcore::Network; -use dash_sdk::dpp::identity::accessors::IdentityGettersV0; -use dash_sdk::dpp::key_wallet::bip32::DerivationPath; +use crate::model::grovestark_prover::ProofDataOutput; +use crate::model::masternode_input::decode_identity_id; use crate::model::qualified_identity::QualifiedIdentity; use crate::model::wallet::{PlatformAddressUpdates, WalletSeedHash}; -use crate::model::grovestark_prover::ProofDataOutput; use crate::ui::tokens::tokens_screen::{ ContractDescriptionInfo, IdentityTokenIdentifier, TokenInfo, }; use crate::utils::egui_mpsc::SenderAsync; use crate::utils::tasks::TaskManager; use contested_names::ScheduledDPNSVote; +use dash_sdk::dpp::address_funds::PlatformAddress; use dash_sdk::dpp::balances::credits::TokenAmount; -use dash_sdk::dpp::data_contract::associated_token::token_perpetual_distribution::distribution_function::evaluate_interval::IntervalEvaluationExplanation; +use dash_sdk::dpp::dashcore::Network; +use dash_sdk::dpp::data_contract::associated_token::token_perpetual_distribution::distribution_function; use dash_sdk::dpp::group::group_action::GroupAction; +use dash_sdk::dpp::identity::accessors::IdentityGettersV0; +use dash_sdk::dpp::key_wallet::bip32::DerivationPath; use dash_sdk::dpp::prelude::DataContract; use dash_sdk::dpp::state_transition::StateTransition; use dash_sdk::dpp::tokens::token_pricing_schedule::TokenPricingSchedule; @@ -38,15 +38,15 @@ use dash_sdk::platform::proto::get_documents_request::get_documents_request_v0:: use dash_sdk::platform::{Document, DocumentQuery, Identifier}; use dash_sdk::query_types::{Documents, IndexMap}; use futures::future::join_all; +use grovestark::GroveSTARKTask; +use migration::MigrationTask; +use shielded::ShieldedTask; use std::collections::BTreeMap; use std::future::Future; use std::sync::Arc; use std::sync::atomic::{AtomicU64, Ordering}; use std::time::Duration; -use migration::MigrationTask; -use shielded::ShieldedTask; use tokens::TokenTask; -use grovestark::GroveSTARKTask; pub mod broadcast_state_transition; pub mod contested_names; @@ -60,6 +60,8 @@ pub mod grovestark; pub mod identity; pub mod migration; pub mod platform_info; +#[cfg(test)] +mod protocol_13_transitions; pub mod register_contract; pub mod shielded; pub mod system_task; @@ -810,7 +812,7 @@ pub enum BackendTaskSuccessResult { TokenEstimatedNonClaimedPerpetualDistributionAmountWithExplanation( IdentityTokenIdentifier, TokenAmount, - IntervalEvaluationExplanation, + distribution_function::evaluate_interval::IntervalEvaluationExplanation, ), ContractsWithDescriptions( BTreeMap, Vec)>, @@ -1430,7 +1432,7 @@ impl AppContext { Ok(self.run_migration_task(migration_task).await?) } BackendTask::ReinitCoreClientAndSdk => { - Arc::clone(self).reinit_core_client_and_sdk()?; + self.rebuild_connection().await?; Ok(BackendTaskSuccessResult::CoreClientReinitialized) } BackendTask::SwitchNetwork { network, start_spv } => { diff --git a/src/backend_task/platform_info.rs b/src/backend_task/platform_info.rs index fff1f014f..16ad8cc3a 100644 --- a/src/backend_task/platform_info.rs +++ b/src/backend_task/platform_info.rs @@ -1,11 +1,13 @@ use crate::backend_task::BackendTaskSuccessResult; use crate::backend_task::error::TaskError; -use crate::context::{AppContext, DET_PLATFORM_VERSION}; +use crate::context::AppContext; use crate::model::datetime; -use crate::model::fee_estimation::PlatformFeeEstimator; +use chrono::{LocalResult, prelude::*}; +use chrono_humanize::{Accuracy, HumanTime, Tense}; use dash_sdk::Error as SdkError; use dash_sdk::Sdk; use dash_sdk::dashcore_rpc::RpcApi; +use dash_sdk::dpp::address_funds::PlatformAddress; use dash_sdk::dpp::block::extended_epoch_info::{ ExtendedEpochInfo, v0::ExtendedEpochInfoV0Getters, }; @@ -13,22 +15,19 @@ use dash_sdk::dpp::core_types::validator_set::v0::ValidatorSetV0Getters; use dash_sdk::dpp::dashcore::hashes::Hash; use dash_sdk::dpp::dashcore::{Address, Network, ScriptBuf}; use dash_sdk::dpp::data_contract::accessors::v0::DataContractV0Getters; -use dash_sdk::dpp::data_contracts::SystemDataContract; -use dash_sdk::dpp::data_contracts::withdrawals_contract::WithdrawalStatus; -use dash_sdk::dpp::data_contracts::withdrawals_contract::v1::document_types::withdrawal::properties::{ - AMOUNT, STATUS, TRANSACTION_INDEX, +use dash_sdk::dpp::data_contracts::withdrawals_contract::{ + WithdrawalStatus, + v1::document_types::withdrawal::properties::{AMOUNT, STATUS, TRANSACTION_INDEX}, }; use dash_sdk::dpp::document::{Document, DocumentV0Getters}; use dash_sdk::dpp::fee::Credits; use dash_sdk::dpp::platform_value::Value; use dash_sdk::dpp::platform_value::btreemap_extensions::BTreeValueMapHelper; use dash_sdk::dpp::state_transition::identity_credit_withdrawal_transition::fields::OUTPUT_SCRIPT; -use dash_sdk::dpp::system_data_contracts::load_system_data_contract; use dash_sdk::dpp::version::PlatformVersion; use dash_sdk::dpp::withdrawal::daily_withdrawal_limit::daily_withdrawal_limit; use dash_sdk::dpp::{dash_to_credits, version::ProtocolVersionVoteCount}; -use dash_sdk::drive::query::{SelectProjection, OrderClause, WhereClause, WhereOperator}; -use dash_sdk::dpp::address_funds::PlatformAddress; +use dash_sdk::drive::query::{OrderClause, SelectProjection, WhereClause, WhereOperator}; use dash_sdk::platform::fetch_current_no_parameters::FetchCurrent; use dash_sdk::platform::proto::get_documents_request::get_documents_request_v0::Start; use dash_sdk::platform::{ @@ -38,8 +37,6 @@ use dash_sdk::query_types::{ AddressInfo, CurrentQuorumsInfo, NoParamQuery, ProtocolVersionUpgrades, TotalCreditsInPlatform, }; use std::sync::Arc; -use chrono::{LocalResult, prelude::*}; -use chrono_humanize::{Accuracy, HumanTime, Tense}; #[derive(Debug, Clone, PartialEq)] pub enum PlatformInfoTaskRequestType { @@ -252,15 +249,11 @@ fn format_extended_epoch_info( ) } -/// The degraded rendering used when the live epoch fetch fails. -/// +/// The epoch summary shown when the current epoch cannot be read. /// `protocol_version` is `None` while the connected network has not confirmed -/// one. The fee multiplier here is the app's own default, and the copy says so -/// rather than claiming anything about what networks charge: with the fetch -/// failed, this build cannot know that. A user comparing the figure against a -/// network that raised its fees has to be able to tell which of the two the app -/// is charging by. -fn format_hardcoded_current_epoch_info( +/// one; `fee_multiplier_permille` is the last known multiplier, not a fresh +/// network reading. +fn format_unavailable_current_epoch_info( protocol_version: Option, fee_multiplier_permille: u64, ) -> String { @@ -269,16 +262,16 @@ fn format_hardcoded_current_epoch_info( Some(protocol_version) => format!( "Current Epoch Information:\n\ • Protocol Version: {protocol_version}\n\ - • Fee Multiplier: {fee_multiplier}x (a fixed value, not read from the network)\n\n\ - Epoch details could not be read from the network just now. The multiplier shown \ - is this app's default, not a value read from the network. Try again in a moment." + • Fee Multiplier: {fee_multiplier}x (the last known value, not a fresh reading)\n\n\ + The current epoch could not be read from the network. Fee estimates use the last \ + known fee multiplier. Refresh again in a moment to read the current one." ), None => format!( "Current Epoch Information:\n\ • Protocol Version: the connected network has not confirmed one yet.\n\ - • Fee Multiplier: {fee_multiplier}x (a fixed value, not read from the network)\n\n\ - Epoch details could not be read from the network just now. The multiplier shown \ - is this app's default, not a value read from the network. Try again in a moment." + • Fee Multiplier: {fee_multiplier}x (the last known value, not a fresh reading)\n\n\ + The current epoch could not be read from the network. Fee estimates use the last \ + known fee multiplier. Refresh again in a moment to read the current one." ), } } @@ -321,51 +314,26 @@ fn format_current_quorums_info(current_quorums_info: &CurrentQuorumsInfo) -> Str result } -/// The daily withdrawal limit at [`DET_PLATFORM_VERSION`], never at the -/// newest version the upstream crates know. +/// The daily withdrawal limit the network enforces, at `platform_version` — +/// never at whatever version the upstream crates happen to know best. /// /// Protocol 13 uses `daily_withdrawal_limit` v1, a flat 2000 Dash that ignores /// the total. Protocol 14 (v2) derives it from the total credits Platform held a /// day ago, which the SDK cannot query; passing today's total is an accepted -/// gap for this display-only figure once DET moves to protocol 14. -fn local_daily_withdrawal_limit( +/// gap for this display-only figure. +fn network_daily_withdrawal_limit( total_credits_on_platform: Credits, + platform_version: &PlatformVersion, ) -> Result { - daily_withdrawal_limit(Some(total_credits_on_platform), DET_PLATFORM_VERSION) + daily_withdrawal_limit(Some(total_credits_on_platform), platform_version) .map_err(|e| WithdrawalParseError::DailyLimit(Box::new(e))) } -/// The Withdrawals system contract at [`DET_PLATFORM_VERSION`]. -/// The `status` values a finished withdrawal can carry, as the -/// `RecentlyCompletedWithdrawals` filter sends them. -/// -/// `FAILED` (5) exists only in the v2 withdrawals schema (protocol 14); the v1 -/// schema this build loads stops at `EXPIRED` (4). Sending it at protocol 13 is -/// still correct and deliberate: a JSON-schema `enum` constrains documents being -/// created, not query values. Nothing in the query path checks membership — the -/// `status` property is typed as a plain integer, and -/// `DocumentPropertyType::encode_value_for_tree_keys` encodes any value that -/// converts to it. So the out-of-enum member encodes, matches nothing on a -/// protocol-13 network (which cannot produce a failed withdrawal), and leaves -/// `COMPLETE` and `EXPIRED` matching as usual — while keeping the query complete -/// once the network, and DET's version pin, reach protocol 14. -fn completed_withdrawal_statuses() -> Vec { - vec![ - Value::U8(WithdrawalStatus::COMPLETE as u8), - Value::U8(WithdrawalStatus::EXPIRED as u8), - Value::U8(WithdrawalStatus::FAILED as u8), - ] -} - -fn withdrawals_contract() -> Result { - load_system_data_contract(SystemDataContract::Withdrawals, DET_PLATFORM_VERSION) - .map_err(|e| TaskError::from(SdkError::Protocol(e))) -} - fn format_withdrawal_documents_with_daily_limit( withdrawal_documents: &[Document], total_credits_on_platform: Credits, network: Network, + platform_version: &PlatformVersion, ) -> Result { let total_amount: Credits = withdrawal_documents .iter() @@ -384,7 +352,8 @@ fn format_withdrawal_documents_with_daily_limit( .map(|document| format_withdrawal_line(document, network)) .collect::, WithdrawalParseError>>()?; - let daily_withdrawal_limit = local_daily_withdrawal_limit(total_credits_on_platform)?; + let daily_withdrawal_limit = + network_daily_withdrawal_limit(total_credits_on_platform, platform_version)?; Ok(format!( "Withdrawal Information:\n\n\ @@ -594,6 +563,37 @@ fn build_withdrawals_result( } impl AppContext { + /// Fetches the current epoch — authenticated by proof — and adopts its + /// fee multiplier and protocol version. The only path by which the fee + /// multiplier cache takes a network value, and fee estimates follow it, + /// so it must never come from an unproven source. + pub(crate) async fn refresh_current_epoch( + &self, + sdk: &Sdk, + ) -> Result> { + // Captured together before the read: a read on an SDK a rebuild has + // replaced, or begun before it, is never adopted. + let (generation, on_current_sdk) = self.connection_snapshot_for(sdk); + let epoch_info = ExtendedEpochInfo::fetch_current(sdk) + .await + .map_err(Box::new)?; + if on_current_sdk { + self.adopt_epoch_info(generation, &epoch_info); + } + Ok(epoch_info) + } + + /// Adopts the fee multiplier and protocol version of a proved epoch read + /// on connection `generation` (see `AppContext::adopt_epoch`). + fn adopt_epoch_info(&self, generation: u64, epoch_info: &ExtendedEpochInfo) { + self.adopt_epoch( + generation, + epoch_info.index(), + epoch_info.fee_multiplier_permille(), + epoch_info.protocol_version(), + ); + } + pub async fn run_platform_info_task( self: &Arc, request: PlatformInfoTaskRequestType, @@ -624,15 +624,9 @@ impl AppContext { )) } PlatformInfoTaskRequestType::CurrentEpochInfo => { - // The live fetch is proved: `ExtendedEpochInfo::fetch_current` resolves the - // current epoch with two explicit-start queries the proof verifier accepts - // (dashpay/platform#4231, in this repo's pin). Its protocol version is a - // network observation, so it feeds the ratchet directly. - match ExtendedEpochInfo::fetch_current(sdk).await { + match self.refresh_current_epoch(sdk).await { Ok(epoch_info) => { let fee_multiplier = epoch_info.fee_multiplier_permille(); - self.set_fee_multiplier_permille(fee_multiplier); - self.set_platform_protocol_version(epoch_info.protocol_version()); let mut formatted = format_extended_epoch_info(epoch_info, self.network, true); @@ -647,38 +641,36 @@ impl AppContext { Err(error) => { tracing::warn!( %error, - "Current-epoch fetch failed; falling back to the cached fee \ - multiplier and the protocol-version ratchet" + "Current epoch fetch failed; keeping the last known fee multiplier" ); - - // Without the epoch, the network's version is learned from the - // ratchet a proved DPNS fetch drives. Only a successful fetch proves - // it came from the network, not the local seed. - match DataContract::fetch(sdk, self.dpns_contract.id()).await { - Ok(_) => { - self.set_platform_protocol_version(sdk.protocol_version_number()) + // Without the epoch, the network's version is learned from + // the ratchet a proved DPNS fetch drives. Only a successful + // fetch proves it came from the network, not the local seed. + let (generation, on_current_sdk) = self.connection_snapshot_for(sdk); + match DataContract::fetch(sdk, self.dpns_contract().id()).await { + Ok(_) if on_current_sdk => { + self.raise_platform_protocol_version_on( + generation, + sdk.protocol_version_number(), + ); } + Ok(_) => {} Err(error) => tracing::warn!( %error, "Protocol-version ratchet trigger (DPNS contract fetch) failed; \ the network's protocol version stays unconfirmed" ), } - - // 1000 permille (1.0x) is what the network actually stores, not a - // placeholder: an epoch's multiplier is written from - // `platform_version.fee_version.uses_version_fee_multiplier_permille`, - // and both fee schedules in the pinned crate declare `Some(1000)`. - let fee_multiplier = PlatformFeeEstimator::DEFAULT_FEE_MULTIPLIER_PERMILLE; - self.set_fee_multiplier_permille(fee_multiplier); - let confirmed = match self.platform_protocol_version() { 0 => None, version => Some(version), }; Ok(BackendTaskSuccessResult::PlatformInfo( PlatformInfoTaskResult::TextResult( - format_hardcoded_current_epoch_info(confirmed, fee_multiplier), + format_unavailable_current_epoch_info( + confirmed, + self.fee_multiplier_permille(), + ), ), )) } @@ -741,12 +733,11 @@ impl AppContext { } } PlatformInfoTaskRequestType::CurrentWithdrawalsInQueue => { - let withdrawal_contract = withdrawals_contract()?; - let queued_document_query = DocumentQuery { + integer_range_clauses: vec![], sub_queries: Vec::new(), select: SelectProjection::documents(), - data_contract: Arc::new(withdrawal_contract), + data_contract: self.withdraws_contract(), document_type_name: "withdrawal".to_string(), where_clauses: vec![], time_range_clauses: Vec::new(), @@ -771,6 +762,7 @@ impl AppContext { &withdrawal_docs, total_credits.0, self.network, + self.connected_platform_version(), )?; Ok(BackendTaskSuccessResult::PlatformInfo( PlatformInfoTaskResult::TextResult(formatted), @@ -788,17 +780,20 @@ impl AppContext { } } PlatformInfoTaskRequestType::RecentlyCompletedWithdrawals => { - let withdrawal_contract = withdrawals_contract()?; - let completed_document_query = DocumentQuery { + integer_range_clauses: vec![], sub_queries: Vec::new(), select: SelectProjection::documents(), - data_contract: Arc::new(withdrawal_contract), + data_contract: self.withdraws_contract(), document_type_name: "withdrawal".to_string(), where_clauses: vec![WhereClause { field: "status".to_string(), operator: WhereOperator::In, - value: Value::Array(completed_withdrawal_statuses()), + value: Value::Array(vec![ + Value::U8(WithdrawalStatus::COMPLETE as u8), + Value::U8(WithdrawalStatus::EXPIRED as u8), + Value::U8(WithdrawalStatus::FAILED as u8), + ]), }], time_range_clauses: Vec::new(), group_by: Vec::new(), @@ -877,15 +872,17 @@ impl AppContext { limit, start_after, } => { - let withdrawal_contract = withdrawals_contract()?; - // `0` is the upstream sentinel for "default limit"; clamp the // requested page so the cursor heuristic has a known bound. let page_limit = limit.unwrap_or(50).clamp(1, 100); let start = start_after.map(|id| Start::StartAfter(id.to_buffer().to_vec())); let statuses = if completed { - completed_withdrawal_statuses() + vec![ + Value::U8(WithdrawalStatus::COMPLETE as u8), + Value::U8(WithdrawalStatus::EXPIRED as u8), + Value::U8(WithdrawalStatus::FAILED as u8), + ] } else { vec![ Value::U8(WithdrawalStatus::QUEUED as u8), @@ -910,9 +907,10 @@ impl AppContext { ]; let query = DocumentQuery { + integer_range_clauses: vec![], sub_queries: Vec::new(), select: SelectProjection::documents(), - data_contract: Arc::new(withdrawal_contract), + data_contract: self.withdraws_contract(), document_type_name: "withdrawal".to_string(), where_clauses, time_range_clauses: Vec::new(), @@ -995,145 +993,161 @@ impl AppContext { mod tests { use super::*; - /// The withdrawal limit follows DET's protocol version, not the newest one - /// upstream knows: protocol 13 keeps the flat 2000 Dash, where protocol 14 - /// reports 15% of the total, capped at 4000 Dash. - /// - /// `dash_to_credits!` stringifies its argument and parses the text, so a - /// Rust numeric separator is not a digit to it: `dash_to_credits!(1_000_000)` - /// silently evaluates to 0 credits. Never write a separator inside this - /// macro. The total is asserted below, and the protocol-14 expectation is - /// pinned to the 4000 Dash cap — a value only a genuinely large total - /// produces, so a zeroed total fails the test instead of passing it for the - /// wrong reason (at 0 credits, protocol 14 returns its 500 Dash floor). + /// The limit is whatever the network's own protocol version says it is, + /// never a version fixed in this build: protocol 13 reports a flat 2000 + /// Dash whatever the total, protocol 14 derives it from that total (15% of + /// it, `SYSTEM_LIMITS_V4.daily_withdrawal_limit_percent`, never below the + /// 500 Dash maximum withdrawal). #[test] - fn daily_withdrawal_limit_uses_det_platform_version_not_latest() { + fn daily_withdrawal_limit_follows_the_given_platform_version() { + // `dash_to_credits!` stringifies its argument and parses the digits, so + // a `1_000_000` written with separators silently reads as no credits. let total = dash_to_credits!(1000000); - assert_eq!( - total, - 1_000_000 * dash_to_credits!(1), - "the total must really be a million Dash" - ); + let v13 = PlatformVersion::get(13).expect("protocol 13"); + let v14 = PlatformVersion::get(14).expect("protocol 14"); - let det_limit = local_daily_withdrawal_limit(total).expect("limit"); - assert_eq!(det_limit, dash_to_credits!(2000)); assert_eq!( - det_limit, - daily_withdrawal_limit(Some(total), DET_PLATFORM_VERSION).expect("limit") + network_daily_withdrawal_limit(total, v13).expect("limit"), + dash_to_credits!(2000) ); - - let latest_limit = - daily_withdrawal_limit(Some(total), PlatformVersion::latest()).expect("limit"); assert_eq!( - latest_limit, - dash_to_credits!(4000), - "protocol 14 caps a million-Dash total at 4000 Dash" + network_daily_withdrawal_limit(total, v14).expect("limit"), + dash_to_credits!(150000) ); - assert_ne!( - det_limit, latest_limit, - "the limit must not follow PlatformVersion::latest()" + // A small total falls back to the maximum single withdrawal. + assert_eq!( + network_daily_withdrawal_limit(dash_to_credits!(1000), v14).expect("limit"), + dash_to_credits!(500) ); } - /// The Withdrawals contract used for queries is loaded at DET's protocol + /// A proved epoch sets both the fee multiplier cache and the protocol /// version. #[test] - fn withdrawals_contract_uses_det_platform_version() { - let expected = - load_system_data_contract(SystemDataContract::Withdrawals, DET_PLATFORM_VERSION) - .expect("contract"); - assert_eq!(withdrawals_contract().expect("contract"), expected); - assert_eq!(DET_PLATFORM_VERSION.protocol_version, 13); + fn a_proved_epoch_updates_the_fee_multiplier_and_protocol_version() { + use dash_sdk::dpp::block::extended_epoch_info::v0::ExtendedEpochInfoV0; + let temp_dir = tempfile::tempdir().expect("tempdir"); + let ctx = crate::context::test_support::test_app_context(temp_dir.path()); + ctx.set_fee_multiplier_permille(1000); + + ctx.adopt_epoch_info( + ctx.connection_generation(), + &ExtendedEpochInfo::V0(ExtendedEpochInfoV0 { + index: 7, + first_block_time: 0, + first_block_height: 0, + first_core_block_height: 0, + fee_multiplier_permille: 1_500, + protocol_version: 14, + }), + ); + + assert_eq!(ctx.fee_multiplier_permille(), 1_500); + assert_eq!(ctx.platform_protocol_version(), 14); } - /// The completed-withdrawal filter keeps `FAILED` even though the schema - /// this build loads stops at `EXPIRED`: a schema `enum` constrains - /// documents, not query values, and every status encodes into the `status` - /// property's key representation at both schema versions. + /// Epoch reads can finish out of order: an older epoch arriving after a + /// newer one must not put back its fee multiplier or protocol version. #[test] - fn completed_withdrawal_filter_encodes_at_both_schema_versions() { - use dash_sdk::dpp::data_contract::accessors::v0::DataContractV0Getters; - use dash_sdk::dpp::data_contract::document_type::accessors::DocumentTypeV0Getters; + fn an_older_epoch_does_not_overwrite_a_newer_one() { + use dash_sdk::dpp::block::extended_epoch_info::v0::ExtendedEpochInfoV0; + let epoch = |index, fee_multiplier_permille, protocol_version| { + ExtendedEpochInfo::V0(ExtendedEpochInfoV0 { + index, + first_block_time: 0, + first_block_height: 0, + first_core_block_height: 0, + fee_multiplier_permille, + protocol_version, + }) + }; + let temp_dir = tempfile::tempdir().expect("tempdir"); + let ctx = crate::context::test_support::test_app_context(temp_dir.path()); + ctx.adopt_epoch_info(ctx.connection_generation(), &epoch(8, 1_500, 14)); + ctx.adopt_epoch_info(ctx.connection_generation(), &epoch(7, 1_000, 13)); + assert_eq!(ctx.fee_multiplier_permille(), 1_500); + assert_eq!(ctx.platform_protocol_version(), 14); + + // The same or a newer epoch is adopted; the protocol version never + // goes back. + ctx.adopt_epoch_info(ctx.connection_generation(), &epoch(8, 1_600, 14)); + assert_eq!(ctx.fee_multiplier_permille(), 1_600); + ctx.adopt_epoch_info(ctx.connection_generation(), &epoch(9, 1_700, 13)); + assert_eq!(ctx.fee_multiplier_permille(), 1_700); + assert_eq!(ctx.platform_protocol_version(), 14); + } + + /// Rebuilding the connection resets everything learned from the old one: + /// the protocol version goes back to unknown, the fee multiplier to the + /// default, and the next proved epoch is adopted whatever its index. + #[test] + fn a_rebuilt_connection_starts_from_unknown_protocol_and_default_fees() { + let temp_dir = tempfile::tempdir().expect("tempdir"); + let ctx = crate::context::test_support::test_app_context(temp_dir.path()); + ctx.adopt_epoch(ctx.connection_generation(), 8, 1_500, 14); + + ctx.replace_connection(Arc::new(Sdk::new_mock())); + + assert_eq!(ctx.platform_protocol_version(), 0); assert_eq!( - completed_withdrawal_statuses(), - vec![Value::U8(3), Value::U8(4), Value::U8(5)], - "COMPLETE, EXPIRED and FAILED, in that order" + ctx.fee_multiplier_permille(), + crate::model::fee_estimation::PlatformFeeEstimator::DEFAULT_FEE_MULTIPLIER_PERMILLE ); + assert!(ctx.adopt_epoch(ctx.connection_generation(), 2, 1_100, 13)); + assert_eq!(ctx.fee_multiplier_permille(), 1_100); + assert_eq!(ctx.platform_protocol_version(), 13); + } - let status_property = |version| { - let contract = load_system_data_contract(SystemDataContract::Withdrawals, version) - .expect("withdrawals contract"); - contract - .document_type_cloned_for_name("withdrawal") - .expect("withdrawal document type") - .properties() - .get("status") - .expect("status property") - .clone() - }; + /// An epoch read that started on the old connection and finishes after + /// the rebuild is dropped, so it cannot be the new connection's first + /// adoption. + #[test] + fn an_epoch_read_from_before_a_rebuild_is_dropped() { + let temp_dir = tempfile::tempdir().expect("tempdir"); + let ctx = crate::context::test_support::test_app_context(temp_dir.path()); + let before_rebuild = ctx.connection_generation(); - // v1 (protocol 13, what DET loads) and v2 (protocol 14) differ only in - // the enum's last member, so the property type is identical. - let det = status_property(DET_PLATFORM_VERSION); - let latest = status_property(PlatformVersion::latest()); + ctx.replace_connection(Arc::new(Sdk::new_mock())); + + assert!(!ctx.adopt_epoch(before_rebuild, 9, 1_500, 14)); + assert_eq!(ctx.platform_protocol_version(), 0); assert_eq!( - det.property_type, latest.property_type, - "the status property type must not differ across schema versions" + ctx.fee_multiplier_permille(), + crate::model::fee_estimation::PlatformFeeEstimator::DEFAULT_FEE_MULTIPLIER_PERMILLE ); - - // FAILED is outside the v1 enum, yet encodes for the query exactly like - // the statuses that are inside it. - for status in completed_withdrawal_statuses() { - assert!( - det.property_type - .encode_value_for_tree_keys(&status) - .is_ok(), - "status {status:?} must encode against the protocol-13 schema" - ); - } } #[test] - fn epoch_fallback_reports_protocol_version_and_the_default_fee_multiplier() { + fn unavailable_epoch_reports_protocol_version_and_the_last_known_fee_multiplier() { assert_eq!( - format_hardcoded_current_epoch_info(Some(12), 1000), + format_unavailable_current_epoch_info(Some(14), 1000), "Current Epoch Information:\n\ - • Protocol Version: 12\n\ - • Fee Multiplier: 1x (a fixed value, not read from the network)\n\n\ - Epoch details could not be read from the network just now. The multiplier shown \ - is this app's default, not a value read from the network. Try again in a moment." + • Protocol Version: 14\n\ + • Fee Multiplier: 1x (the last known value, not a fresh reading)\n\n\ + The current epoch could not be read from the network. Fee estimates use the last \ + known fee multiplier. Refresh again in a moment to read the current one." ); } - /// The multiplier is presented as this app's default, never as a live - /// reading and never as a claim about what networks charge — with the fetch - /// failed, the app cannot know that. A user comparing the figure against a - /// network that raised its fees must be able to see which of the two the app - /// is charging by. + /// The multiplier is presented as stale, never as a live reading: a user + /// comparing it against a network that raised its fees must be able to see + /// which of the two the app is charging by. #[test] - fn epoch_fallback_never_presents_the_fee_multiplier_as_a_network_reading() { + fn unavailable_epoch_never_presents_the_fee_multiplier_as_a_network_reading() { for protocol_version in [None, Some(12)] { - let formatted = format_hardcoded_current_epoch_info(protocol_version, 1500); + let formatted = format_unavailable_current_epoch_info(protocol_version, 1500); assert!( formatted - .contains("• Fee Multiplier: 1.5x (a fixed value, not read from the network)"), - "the multiplier must be shown as fixed, got: {formatted}" - ); - assert!( - formatted.contains("is this app's default, not a value read from the network"), - "the degraded copy must name the default as the app's own, got: {formatted}" - ); - assert!( - !formatted.contains("every network charges"), - "a failed fetch cannot support a claim about what networks charge: {formatted}" + .contains("• Fee Multiplier: 1.5x (the last known value, not a fresh reading)"), + "the multiplier must be shown as stale, got: {formatted}" ); } } #[test] - fn epoch_fallback_never_reports_an_unconfirmed_protocol_version_as_a_number() { - let formatted = format_hardcoded_current_epoch_info(None, 1000); + fn unavailable_epoch_never_reports_an_unconfirmed_protocol_version_as_a_number() { + let formatted = format_unavailable_current_epoch_info(None, 1000); assert!( formatted .contains("• Protocol Version: the connected network has not confirmed one yet."), @@ -1141,33 +1155,29 @@ mod tests { ); } - /// Fee estimates must not keep running on whatever multiplier a previous - /// refresh happened to leave behind: when the live epoch fetch fails (here, - /// against a mock SDK that answers nothing), the task republishes the - /// default one. + /// A failed epoch fetch must not reset a multiplier a previous live read + /// published: the last known value is closer to the network's than any + /// fixed default, and the report names the value it keeps. #[tokio::test] - async fn epoch_fallback_republishes_the_default_fee_multiplier() { + async fn failed_epoch_fetch_keeps_the_last_known_fee_multiplier() { let temp_dir = tempfile::tempdir().expect("tempdir"); let ctx = crate::context::test_support::test_app_context(temp_dir.path()); let sdk = dash_sdk::Sdk::new_mock(); - ctx.set_fee_multiplier_permille(7_777); + ctx.set_fee_multiplier_permille(1_500); let result = ctx .run_platform_info_task(PlatformInfoTaskRequestType::CurrentEpochInfo, &sdk) .await .expect("a failed epoch fetch degrades to a text result"); - assert_eq!( - ctx.fee_multiplier_permille(), - PlatformFeeEstimator::DEFAULT_FEE_MULTIPLIER_PERMILLE - ); + assert_eq!(ctx.fee_multiplier_permille(), 1_500); let BackendTaskSuccessResult::PlatformInfo(PlatformInfoTaskResult::TextResult(text)) = result else { - panic!("the degraded epoch path returns a text result"); + panic!("a failed epoch fetch returns a text result"); }; assert!( - text.contains("• Fee Multiplier: 1x"), + text.contains("• Fee Multiplier: 1.5x (the last known value"), "the reported multiplier must match the cached one, got: {text}" ); } diff --git a/src/backend_task/tokens/query_tokens.rs b/src/backend_task/tokens/query_tokens.rs index a481fefcd..0767c1c63 100644 --- a/src/backend_task/tokens/query_tokens.rs +++ b/src/backend_task/tokens/query_tokens.rs @@ -26,12 +26,10 @@ impl AppContext { sdk: &Sdk, ) -> Result { // -- 1. fetch keyword -> contractId docs - let mut kw_query = - DocumentQuery::new(self.keyword_search_contract.clone(), "contractKeywords").map_err( - |e| TaskError::TokenQueryError { - detail: format!("Failed to create document query: {}", e), - }, - )?; + let mut kw_query = DocumentQuery::new(self.keyword_search_contract(), "contractKeywords") + .map_err(|e| TaskError::TokenQueryError { + detail: format!("Failed to create document query: {}", e), + })?; kw_query.limit = 100; kw_query.start = cursor.clone(); kw_query = kw_query.with_where(WhereClause { @@ -71,10 +69,11 @@ impl AppContext { for cid in &contract_ids { let mut desc_query = - DocumentQuery::new(self.keyword_search_contract.clone(), "shortDescription") - .map_err(|e| TaskError::TokenQueryError { + DocumentQuery::new(self.keyword_search_contract(), "shortDescription").map_err( + |e| TaskError::TokenQueryError { detail: format!("Failed to create document query: {}", e), - })?; + }, + )?; desc_query.limit = 1; desc_query = desc_query.with_where(WhereClause { field: "contractId".into(), diff --git a/src/context/contested_names_db.rs b/src/context/contested_names_db.rs index aabd34e81..3a219f3e7 100644 --- a/src/context/contested_names_db.rs +++ b/src/context/contested_names_db.rs @@ -21,6 +21,7 @@ use crate::model::dpns_voting::{ use crate::model::qualified_identity::QualifiedIdentity; use crate::utils::time::now_ms; use crate::wallet_backend::{DetScope, KvAdapterError}; +#[cfg(test)] use dash_sdk::dpp::dashcore::Network; use dash_sdk::dpp::data_contract::document_type::DocumentTypeRef; use dash_sdk::dpp::document::DocumentV0Getters; @@ -120,12 +121,7 @@ impl StoredContestedName { self.locked || self.awarded_to.is_some() } - fn to_contested_name(&self, network: Network) -> ContestedName { - let join_window = crate::model::dpns::contest_durations( - network, - crate::context::default_platform_version(&network), - ) - .join; + fn to_contested_name(&self, join_window: Duration) -> ContestedName { let awarded_to_id = self.awarded_to.map(Identifier::from); // Match pre-C6 semantics: state is computed from the latest @@ -234,7 +230,15 @@ impl AppContext { let mut out = Vec::with_capacity(keys.len()); for key in keys { match kv.get::(DetScope::Global, &key) { - Ok(Some(stored)) => out.push(stored.to_contested_name(self.network)), + Ok(Some(stored)) => out.push( + stored.to_contested_name( + crate::model::dpns::contest_durations( + self.network, + self.platform_version(), + ) + .join, + ), + ), Ok(None) => {} Err(e) => tracing::warn!( key = %key, @@ -262,7 +266,13 @@ impl AppContext { Ok(Some(stored)) => match stored.end_time { Some(t) if t <= current_timestamp => {} _ => { - let contest = stored.to_contested_name(self.network); + let contest = stored.to_contested_name( + crate::model::dpns::contest_durations( + self.network, + self.platform_version(), + ) + .join, + ); if !contest.is_unread() { out.push(contest); } @@ -1420,6 +1430,22 @@ mod tests { // Decode: contest state branches the cache resolves at read time. // ---------------------------------------------------------------- + #[test] + fn to_contested_name_uses_the_supplied_protocol_join_window() { + let stored = StoredContestedName { + contestants: vec![contestant(1, Some(now_ms() - 60_000))], + ..Default::default() + }; + assert_eq!( + stored.to_contested_name(Duration::from_secs(120)).state, + ContestState::Joinable + ); + assert_eq!( + stored.to_contested_name(Duration::from_secs(30)).state, + ContestState::Ongoing + ); + } + #[test] fn to_contested_name_reports_won_by_when_awarded() { let stored = StoredContestedName { @@ -1427,7 +1453,7 @@ mod tests { awarded_to: Some([7u8; 32]), ..Default::default() }; - let cn = stored.to_contested_name(Network::Testnet); + let cn = stored.to_contested_name(Duration::from_secs(45 * 60)); assert_eq!(cn.state, ContestState::WonBy(Identifier::from([7u8; 32]))); assert_eq!(cn.awarded_to, Some(Identifier::from([7u8; 32]))); } @@ -1439,7 +1465,7 @@ mod tests { locked: true, ..Default::default() }; - let cn = stored.to_contested_name(Network::Testnet); + let cn = stored.to_contested_name(Duration::from_secs(45 * 60)); assert_eq!(cn.state, ContestState::Locked); assert!(cn.awarded_to.is_none()); } @@ -1455,7 +1481,7 @@ mod tests { ..Default::default() }; assert_eq!( - stored.to_contested_name(Network::Testnet).state, + stored.to_contested_name(Duration::from_secs(45 * 60)).state, ContestState::Locked ); } @@ -1469,7 +1495,7 @@ mod tests { contestants: vec![contestant(1, None), contestant(2, None)], ..Default::default() }; - let cn = stored.to_contested_name(Network::Testnet); + let cn = stored.to_contested_name(Duration::from_secs(45 * 60)); assert_eq!(cn.state, ContestState::Unknown); assert_eq!(cn.contestants.as_ref().map(Vec::len), Some(2)); } @@ -1482,7 +1508,7 @@ mod tests { contestants: vec![contestant(3, Some(100))], ..Default::default() }; - let cn = stored.to_contested_name(Network::Testnet); + let cn = stored.to_contested_name(Duration::from_secs(45 * 60)); let mapped = &cn.contestants.unwrap()[0]; assert_eq!(mapped.id, Identifier::from([3u8; 32])); assert_eq!(mapped.name, "name-3"); @@ -1685,8 +1711,8 @@ mod tests { use dash_sdk::dpp::block::block_info::BlockInfo; use dash_sdk::dpp::data_contract::accessors::v0::DataContractV0Getters; let (_temp_dir, context, _history) = cold_history(3); - let document_type = context - .dpns_contract + let dpns_contract = context.dpns_contract(); + let document_type = dpns_contract .document_type_for_name("domain") .expect("domain document type"); let answer = |winner| Contenders { diff --git a/src/context/contract_token_db.rs b/src/context/contract_token_db.rs index 2bb044e63..b700d67bf 100644 --- a/src/context/contract_token_db.rs +++ b/src/context/contract_token_db.rs @@ -130,11 +130,11 @@ impl AppContext { // Pin the system contracts at the head of the list, in display order. let system_contracts = [ - (&self.dpns_contract, "dpns"), - (&self.token_history_contract, "token_history"), - (&self.withdraws_contract, "withdrawals"), - (&self.keyword_search_contract, "keyword_search"), - (&self.dashpay_contract, "dashpay"), + (self.dpns_contract(), "dpns"), + (self.token_history_contract(), "token_history"), + (self.withdraws_contract(), "withdrawals"), + (self.keyword_search_contract(), "keyword_search"), + (self.dashpay_contract(), "dashpay"), ]; for (index, (contract, alias)) in system_contracts.into_iter().enumerate() { contracts.insert( diff --git a/src/context/dpns_vote_state.rs b/src/context/dpns_vote_state.rs index 5fe5f8dbc..2da1d94da 100644 --- a/src/context/dpns_vote_state.rs +++ b/src/context/dpns_vote_state.rs @@ -333,7 +333,7 @@ impl AppContext { /// Build the exact Platform vote-poll ID for one DPNS label. pub fn dpns_vote_poll_id(&self, name: &str) -> Result { - dpns_vote_poll(&self.dpns_contract, name)? + dpns_vote_poll(&self.dpns_contract(), name)? .unique_id() .map_err(|source| TaskError::SdkError { source_error: Box::new(dash_sdk::Error::Protocol(source)), diff --git a/src/context/feature_gate.rs b/src/context/feature_gate.rs index 51d0e8eea..014d31dcf 100644 --- a/src/context/feature_gate.rs +++ b/src/context/feature_gate.rs @@ -1,5 +1,6 @@ use crate::context::AppContext; use crate::model::user_role::UserRole; +use dash_sdk::dpp::version::PlatformVersion; use dash_sdk::dpp::version::feature_initial_protocol_versions::SHIELDED_POOL_INITIAL_PROTOCOL_VERSION; /// Shielded state transitions activate at upstream rs-platform-version's @@ -28,6 +29,22 @@ pub enum Capability { ShieldedProtocol, } +/// The connected network's protocol version, when this build defines it. +/// +/// `None` before the version is fetched (0) and for a version newer than any +/// this build knows, which a proved epoch can report after a network upgrade. +/// Every capability reads the version through this, so both fail closed: this +/// build cannot tell what an unknown version's rules allow. +fn known_protocol_version(ctx: &AppContext) -> Option<&'static PlatformVersion> { + PlatformVersion::get_optional(ctx.platform_protocol_version()) +} + +/// Whether the connected protocol version is one this build defines and is +/// at least `activation`. +fn known_protocol_version_at_least(ctx: &AppContext, activation: u32) -> bool { + known_protocol_version(ctx).is_some_and(|version| version.protocol_version >= activation) +} + impl Capability { fn is_met(self, ctx: &AppContext) -> bool { match self { @@ -35,10 +52,8 @@ impl Capability { // A closed gate makes the capability unavailable on every network. None => false, // The version fetched from the connected network, not the hardcoded - // default — the capability is per-network. The boot value (0, "not - // fetched yet") is below any activation version, so a network whose - // version is not known yet reads as unmet rather than optimistic. - Some(activation) => ctx.platform_protocol_version() >= activation, + // default — the capability is per-network. + Some(activation) => known_protocol_version_at_least(ctx, activation), }, } } @@ -419,4 +434,31 @@ mod tests { ); } } + + /// A protocol version this build does not define closes every protocol + /// capability: nothing here knows that version's rules. The newest + /// version this build defines keeps them open. + #[test] + fn an_unknown_future_protocol_version_closes_every_protocol_capability() { + use dash_sdk::dpp::version::LATEST_VERSION; + let (_tmp, ctx) = ctx_with_role(UserRole::Developer); + let capabilities = [Capability::ShieldedProtocol]; + + ctx.set_platform_protocol_version(LATEST_VERSION); + for capability in capabilities { + assert!( + capability.is_met(&ctx), + "{capability:?} on protocol {LATEST_VERSION}" + ); + } + + ctx.set_platform_protocol_version(LATEST_VERSION + 1); + for capability in capabilities { + assert!( + !capability.is_met(&ctx), + "{capability:?} on unknown protocol {}", + LATEST_VERSION + 1 + ); + } + } } diff --git a/src/context/legacy_scheduled_votes.rs b/src/context/legacy_scheduled_votes.rs index f1ef5a2e1..208da3bd3 100644 --- a/src/context/legacy_scheduled_votes.rs +++ b/src/context/legacy_scheduled_votes.rs @@ -107,9 +107,11 @@ impl AppContext { now } }; + // Retired schedules were created under protocol 13. Keep their notice + // lifetime stable across SDK ratchets and future protocol upgrades. let contest_duration = crate::model::dpns::contest_durations( self.network, - super::default_platform_version(&self.network), + &dash_sdk::dpp::version::v13::PLATFORM_V13, ) .total; Ok(legacy_notice_is_current( @@ -268,7 +270,7 @@ mod tests { let contest_duration = crate::model::dpns::contest_durations( Network::Testnet, - crate::context::default_platform_version(&Network::Testnet), + &dash_sdk::dpp::version::v13::PLATFORM_V13, ) .total; let long_ago = now_ms() - u64::try_from(contest_duration.as_millis()).unwrap() - 1; diff --git a/src/context/mod.rs b/src/context/mod.rs index d5a2a5139..f2c62f63c 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -17,7 +17,10 @@ pub(crate) mod lock_probe; pub(crate) use identity_db::test_staging; pub(crate) mod identity_load_registry; pub mod migration_status; +#[cfg(test)] +mod platform_version_guard; mod settings_db; +mod system_contracts; #[cfg(test)] pub(crate) mod test_support; mod wallet_balances; @@ -50,15 +53,14 @@ use connection_status::ConnectionStatus; use dash_sdk::Sdk; use dash_sdk::dapi_client::AddressList; use dash_sdk::dashcore_rpc::{Auth, Client}; +use dash_sdk::dpp::block::epoch::EpochIndex; use dash_sdk::dpp::dashcore::Network; #[cfg(any(test, feature = "testing"))] use dash_sdk::dpp::data_contract::accessors::v0::DataContractV0Getters; use dash_sdk::dpp::identity::accessors::IdentityGettersV0; use dash_sdk::dpp::state_transition::StateTransitionSigningOptions; use dash_sdk::dpp::state_transition::batch_transition::methods::StateTransitionCreationOptions; -use dash_sdk::dpp::system_data_contracts::{SystemDataContract, load_system_data_contract}; use dash_sdk::dpp::version::PlatformVersion; -use dash_sdk::dpp::version::v13::PLATFORM_V13; use dash_sdk::platform::DataContract; use dash_sdk::platform::Identifier; use egui::Context; @@ -69,6 +71,7 @@ use std::path::PathBuf; use std::str::FromStr as _; use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering}; use std::sync::{Arc, Mutex, RwLock, RwLockWriteGuard}; +use system_contracts::{SystemContracts, SystemContractsCache}; use crate::model::settings::AppSettings; use crate::model::user_role::{UserRole, UserRoleCell}; @@ -161,6 +164,15 @@ impl DpnsVoteDispatchCoordinator { } } +/// What epoch adoption is keyed on. See [`AppContext::adopt_epoch`]. +#[derive(Debug, Default)] +struct EpochAdoption { + /// The connection generation, bumped by every rebuild. + generation: u64, + /// The index of the last adopted proved epoch on this generation. + epoch_index: Option, +} + #[derive(Debug)] pub struct AppContext { pub(crate) data_dir: PathBuf, @@ -178,11 +190,9 @@ pub struct AppContext { // owned by upstream platform-wallet. spv_context_provider: RwLock, pub(crate) config: Arc>, - pub(crate) dpns_contract: Arc, - pub(crate) withdraws_contract: Arc, - pub(crate) dashpay_contract: Arc, - pub(crate) token_history_contract: Arc, - pub(crate) keyword_search_contract: Arc, + /// The system contracts at the SDK's protocol version; read through + /// [`Self::dpns_contract`] and its siblings. + system_contracts: SystemContractsCache, pub(crate) core_client: RwLock, pub(crate) has_wallet: AtomicBool, /// One-shot-per-session latch for the automatic all-wallets identity sweep. @@ -317,6 +327,9 @@ pub struct AppContext { /// Updated alongside fee_multiplier when epoch info is fetched. /// 0 means not yet fetched from the network. platform_protocol_version: AtomicU32, + /// The connection generation and the last adopted proved epoch. See + /// [`Self::adopt_epoch`] and [`Self::replace_connection`]. + epoch_adoption: Mutex, /// Frame-safe shielded balance snapshot (credits, summed across all Orchard accounts). /// /// Written by the Phase-E `on_shielded_sync_completed` event handler from the @@ -543,22 +556,14 @@ impl AppContext { return None; } }; - let platform_version = sdk.version(); - - let load_contract = |contract: SystemDataContract, label: &str| { - load_system_data_contract(contract, platform_version) - .inspect_err(|e| tracing::error!(?network, "Failed to load {label} contract: {e}")) - .ok() + let system_contracts = match SystemContracts::load(sdk.version()) { + Ok(contracts) => SystemContractsCache::new(contracts), + Err(e) => { + tracing::error!(?network, "Failed to load the system contracts: {e}"); + return None; + } }; - let dpns_contract = load_contract(SystemDataContract::DPNS, "DPNS")?; - let withdrawal_contract = load_contract(SystemDataContract::Withdrawals, "Withdrawals")?; - let token_history_contract = - load_contract(SystemDataContract::TokenHistory, "TokenHistory")?; - let keyword_search_contract = - load_contract(SystemDataContract::KeywordSearch, "KeywordSearch")?; - let dashpay_contract = load_contract(SystemDataContract::Dashpay, "Dashpay")?; - let addr = format!( "http://{}:{}", network_config.rpc_host(), @@ -600,11 +605,7 @@ impl AppContext { sdk: ArcSwap::from_pointee(sdk), spv_context_provider: spv_provider.into(), config: config_lock, - dpns_contract: Arc::new(dpns_contract), - withdraws_contract: Arc::new(withdrawal_contract), - dashpay_contract: Arc::new(dashpay_contract), - token_history_contract: Arc::new(token_history_contract), - keyword_search_contract: Arc::new(keyword_search_contract), + system_contracts, core_client: core_client.into(), has_wallet: false.into(), identity_autodiscovery_fired: AtomicBool::new(false), @@ -654,6 +655,7 @@ impl AppContext { PlatformFeeEstimator::DEFAULT_FEE_MULTIPLIER_PERMILLE, ), platform_protocol_version: AtomicU32::new(0), + epoch_adoption: Mutex::new(EpochAdoption::default()), shielded_balances: Arc::new(Mutex::new(std::collections::HashMap::new())), shielded_addresses: Arc::new(Mutex::new(std::collections::HashMap::new())), platform_balances: Arc::new(Mutex::new(std::collections::HashMap::new())), @@ -874,6 +876,13 @@ impl AppContext { .store(version, Ordering::Relaxed); } + /// Raise the cached platform protocol version to `version`; a lower one + /// is ignored. What the network proves never moves it back. + pub(crate) fn raise_platform_protocol_version(&self, version: u32) { + self.platform_protocol_version + .fetch_max(version, Ordering::Relaxed); + } + /// Synchronous read of the frame-safe shielded balance for `seed_hash`. /// /// Returns the total shielded balance in duffs, summed across all Orchard accounts. @@ -1101,8 +1110,63 @@ impl AppContext { } } + /// The platform version the SDK builds, validates and signs with: the + /// network's minimum until a proven response ratchets it to the version + /// the network runs. pub fn platform_version(&self) -> &'static PlatformVersion { - default_platform_version(&self.network) + self.sdk.load().version() + } + + /// The DPNS contract at [`Self::platform_version`]. + pub(crate) fn dpns_contract(&self) -> Arc { + Arc::clone(&self.system_contracts().dpns) + } + + /// The withdrawals contract at [`Self::platform_version`]. + pub(crate) fn withdraws_contract(&self) -> Arc { + Arc::clone(&self.system_contracts().withdrawals) + } + + /// The DashPay contract at [`Self::platform_version`]. + pub(crate) fn dashpay_contract(&self) -> Arc { + Arc::clone(&self.system_contracts().dashpay) + } + + /// The token history contract at [`Self::platform_version`]. + pub(crate) fn token_history_contract(&self) -> Arc { + Arc::clone(&self.system_contracts().token_history) + } + + /// The keyword search contract at [`Self::platform_version`]. + pub(crate) fn keyword_search_contract(&self) -> Arc { + Arc::clone(&self.system_contracts().keyword_search) + } + + fn system_contracts(&self) -> Arc { + self.system_contracts.get(self.platform_version()) + } + + /// The system contracts exactly as `platform_version` defines them, for a + /// caller that verifies at a version other than [`Self::platform_version`]. + /// Fails rather than serving another version's contracts. + pub(crate) fn system_contracts_at( + &self, + platform_version: &PlatformVersion, + ) -> Result, Box> { + self.system_contracts.try_get(platform_version) + } + + /// The platform version of the connected network, once its protocol + /// version has been fetched; [`Self::platform_version`] until then, or for + /// a protocol version this build does not know. + /// + /// For values that differ between protocol versions and must match the + /// network, such as fees introduced by a newer protocol. + pub fn connected_platform_version(&self) -> &'static PlatformVersion { + match self.platform_protocol_version() { + 0 => self.platform_version(), + version => PlatformVersion::get_optional(version).unwrap_or(self.platform_version()), + } } pub fn state_transition_options(&self) -> Option { @@ -1110,6 +1174,7 @@ impl AppContext { // and purpose checks when signing a state transition. if self.user_role().at_least(UserRole::Developer) { Some(StateTransitionCreationOptions { + contest_fund: None, signing_options: StateTransitionSigningOptions { allow_signing_with_any_security_level: true, allow_signing_with_any_purpose: true, @@ -1117,8 +1182,7 @@ impl AppContext { batch_feature_version: None, method_feature_version: None, base_feature_version: None, - // Action fee agreements are a protocol-14 field; DET builds - // protocol-13 transitions only (`DET_PLATFORM_VERSION`). + // Signing overrides only; DET agrees to no document action fee. action_fee_agreement: None, }) } else { @@ -1126,8 +1190,133 @@ impl AppContext { } } + /// The generation of the connection to the network: bumped each time the + /// connection is rebuilt. Production reads take it together with the SDK + /// check, via [`Self::connection_snapshot_for`]. + #[cfg(test)] + pub(crate) fn connection_generation(&self) -> u64 { + self.epoch_adoption + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .generation + } + + /// Adopt the fee multiplier and protocol version of the proved epoch + /// `epoch_index`, read on connection `generation`. Returns whether it was + /// adopted. + /// + /// Dropped when the connection was rebuilt since the read was sent, or + /// when an epoch newer than it was adopted already: epoch reads run + /// concurrently (platform info, fee refusals) and can finish out of + /// order. The protocol version only moves up. The lock covers the checks + /// and both stores, so an adoption and a rebuild cannot interleave; + /// nothing is awaited under it. + pub(crate) fn adopt_epoch( + &self, + generation: u64, + epoch_index: EpochIndex, + fee_multiplier_permille: u64, + protocol_version: u32, + ) -> bool { + let mut adoption = self + .epoch_adoption + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + if generation != adoption.generation + || adoption + .epoch_index + .is_some_and(|adopted| epoch_index < adopted) + { + return false; + } + adoption.epoch_index = Some(epoch_index); + self.set_fee_multiplier_permille(fee_multiplier_permille); + self.raise_platform_protocol_version(protocol_version); + true + } + + /// Raise the protocol version to `protocol_version`, proved on connection + /// `generation`; dropped if the connection was rebuilt since. Returns + /// whether it was applied. + pub(crate) fn raise_platform_protocol_version_on( + &self, + generation: u64, + protocol_version: u32, + ) -> bool { + let adoption = self + .epoch_adoption + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + if generation != adoption.generation { + return false; + } + self.raise_platform_protocol_version(protocol_version); + true + } + + /// Replace the connection's SDK with `new_sdk` and forget everything + /// learned from the old connection: bump the generation, so reads still in + /// flight on it are dropped, forget the adopted epoch, and put the + /// protocol version back to unknown and the fee multiplier to the default. + /// The new endpoints may serve another protocol version or a reset devnet; + /// capabilities stay closed until the new connection proves its epoch. + /// + /// The reset is written before the new SDK is published, under the + /// adoption lock: whoever loads the new SDK sees the reset state, and a + /// [`Self::connection_snapshot_for`] sees either the old generation and + /// SDK or the new ones. A task still holding the old SDK may see the reset + /// state, which closes capabilities and uses default fees: fail closed. + pub(crate) fn replace_connection(&self, new_sdk: Arc) { + self.replace_connection_observed(new_sdk, |_| {}); + } + + /// [`Self::replace_connection`]; `observe` runs where the two halves meet (test seam). + fn replace_connection_observed(&self, new_sdk: Arc, observe: impl Fn(&Self)) { + let mut adoption = self + .epoch_adoption + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + adoption.generation += 1; + adoption.epoch_index = None; + self.set_fee_multiplier_permille(PlatformFeeEstimator::DEFAULT_FEE_MULTIPLIER_PERMILLE); + self.set_platform_protocol_version(0); + observe(self); + self.sdk.store(new_sdk); + } + + /// The connection generation and whether `sdk` is the current SDK (not + /// one a rebuild replaced, nor a copy), read together under the adoption + /// lock. An epoch read takes this before it is sent and passes the + /// generation to [`Self::adopt_epoch`]. + pub(crate) fn connection_snapshot_for(&self, sdk: &Sdk) -> (u64, bool) { + let adoption = self + .epoch_adoption + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + let current = std::ptr::eq(sdk, Arc::as_ptr(&self.sdk.load_full())); + (adoption.generation, current) + } + + /// Rebuild the connection ([`Self::reinit_core_client_and_sdk`]) and read + /// the new connection's epoch, so its protocol version and fee multiplier + /// are known again. A failed epoch read only leaves them unknown until + /// the next one; it does not fail the rebuild. + pub(crate) async fn rebuild_connection(self: &Arc) -> Result<(), TaskError> { + Arc::clone(self).reinit_core_client_and_sdk()?; + let sdk = self.sdk.load_full(); + if let Err(error) = self.refresh_current_epoch(&sdk).await { + tracing::warn!( + %error, + "Could not read the current epoch after rebuilding the connection; \ + protocol features stay closed until the next read" + ); + } + Ok(()) + } + /// Rebuild both the Dash RPC `core_client` and the `Sdk` using the - /// updated `NetworkConfig` from `self.config`. + /// updated `NetworkConfig` from `self.config`, and forget the state + /// learned from the old connection ([`Self::replace_connection`]). pub fn reinit_core_client_and_sdk(self: Arc) -> Result<(), TaskError> { // 1. Grab a fresh snapshot of your NetworkConfig let cfg = { @@ -1160,6 +1349,9 @@ impl AppContext { }; let provider = self.spv_context_provider.read()?.clone(); + // Like a cold start: the network minimum, raised by the new + // connection's first proven response. Nothing learned from the old + // endpoints is carried over. let new_sdk = initialize_sdk(address_list, self.network, provider) .map_err(|e| TaskError::SdkInitializationFailed { detail: e })?; @@ -1168,7 +1360,7 @@ impl AppContext { let mut client_lock = self.core_client.write()?; *client_lock = new_client; } - self.sdk.store(Arc::new(new_sdk)); + self.replace_connection(Arc::new(new_sdk)); // Rebind the provider to hold the new AppContext reference. // bind_app_context also registers the provider with the SDK. @@ -1770,34 +1962,10 @@ impl AppContext { /// Returns the DashPay contract identifier. pub fn dashpay_contract_id(&self) -> Identifier { - self.dashpay_contract.id() + self.dashpay_contract().id() } } -/// The platform version DET uses locally: validation, building and signing -/// state transitions, loading system contracts, and the SDK's initial -/// protocol-version seed. -/// -/// Protocol 13 is what mainnet and testnet run. The pinned upstream crates -/// already know protocol 14 (`PlatformVersion::latest()`), but DET must not -/// build or validate protocol-14 structures until it supports them, so every -/// local use goes through this constant instead of `latest()`. -// TODO(pv14): raise to PLATFORM_V14 together with protocol-14 support. -pub(crate) const DET_PLATFORM_VERSION: &PlatformVersion = &PLATFORM_V13; - -/// Returns the default platform version for the given network. -// Seeded (`with_initial_version`), not pinned, so `Sdk`'s protocol-version -// ratchet stays active and follows the network upward. -// TODO(platform#4231): the fix is merged upstream and included in the current -// platform pin; restore the live `ExtendedEpochInfo::fetch_current` in -// `PlatformInfoTaskRequestType::CurrentEpochInfo` and reconsider this seeding. -// TODO(pv14): devnet should seed at PV14 per upstream rs-sdk `min_protocol_version` -// (devnets run the development line); DET seeds every network at -// `DET_PLATFORM_VERSION` until it supports protocol 14. -pub(crate) const fn default_platform_version(_network: &Network) -> &'static PlatformVersion { - DET_PLATFORM_VERSION -} - #[cfg(test)] mod tests { use super::*; @@ -1835,33 +2003,124 @@ mod tests { } } + /// Before any proven response, a context builds, validates and signs at + /// the lowest protocol version its network still runs (upstream's + /// per-network minimum): 13 on mainnet, not an older table. #[test] - fn platform_version_is_v13_for_every_network() { + fn platform_version_starts_at_the_network_minimum() { + use crate::context::test_support::test_app_context_for_network; + use dash_sdk::sdk::min_protocol_version; + + assert_eq!(min_protocol_version(Network::Mainnet), 13); for network in [ Network::Mainnet, Network::Testnet, Network::Devnet, Network::Regtest, ] { + let tmp = tempfile::tempdir().expect("tempdir"); + let ctx = test_app_context_for_network(tmp.path(), network); assert_eq!( - default_platform_version(&network).protocol_version, - PLATFORM_V13.protocol_version + ctx.platform_version().protocol_version, + min_protocol_version(network), + "{network:?}" ); } } - /// Contest timing is read from this pin on the voting side and from the live - /// SDK version on the identity side. The SDK only ratchets upward from the - /// pin, so both sides describe one contest as long as every newer version - /// this build knows keeps the pinned windows. + /// Once the SDK runs at protocol 14, the context builds with 14 and serves + /// the system contracts protocol 14 defines — not the ones loaded at boot. + #[test] + fn platform_version_and_system_contracts_follow_the_sdk() { + use crate::context::test_support::test_app_context_for_network; + use dash_sdk::dpp::system_data_contracts::{SystemDataContract, load_system_data_contract}; + + let tmp = tempfile::tempdir().expect("tempdir"); + let ctx = test_app_context_for_network(tmp.path(), Network::Mainnet); + let v14 = PlatformVersion::get(14).expect("protocol 14"); + let boot_dashpay = ctx.dashpay_contract(); + assert_ne!( + *boot_dashpay, + load_system_data_contract(SystemDataContract::Dashpay, v14).unwrap(), + "precondition: protocol 14 redefines the DashPay contract" + ); + + crate::context::test_support::set_sdk_protocol_version(&ctx, 14); + + assert_eq!(ctx.platform_version().protocol_version, 14); + for (served, contract) in [ + (ctx.dashpay_contract(), SystemDataContract::Dashpay), + (ctx.withdraws_contract(), SystemDataContract::Withdrawals), + ( + ctx.token_history_contract(), + SystemDataContract::TokenHistory, + ), + (ctx.dpns_contract(), SystemDataContract::DPNS), + ( + ctx.keyword_search_contract(), + SystemDataContract::KeywordSearch, + ), + ] { + assert_eq!(*served, load_system_data_contract(contract, v14).unwrap()); + } + assert_eq!(ctx.dashpay_contract().id(), boot_dashpay.id()); + } + + /// A task that loads the new SDK always sees the reset connection state: + /// the new SDK never pairs with the old connection's protocol version or + /// fee multiplier. #[test] - fn contest_durations_match_the_pin_for_every_version_the_sdk_can_report() { + fn the_new_sdk_is_published_only_after_the_connection_state_is_reset() { + let temp_dir = tempfile::tempdir().expect("tempdir"); + let ctx = crate::context::test_support::test_app_context(temp_dir.path()); + ctx.adopt_epoch(ctx.connection_generation(), 8, 1_500, 14); + let new_sdk = Arc::new(dash_sdk::Sdk::new_mock()); + let new_sdk_ptr = Arc::as_ptr(&new_sdk); + + ctx.replace_connection_observed(new_sdk, |ctx| { + if Arc::as_ptr(&ctx.sdk.load_full()) == new_sdk_ptr { + assert_eq!(ctx.platform_protocol_version(), 0, "new SDK, old protocol"); + assert_eq!( + ctx.fee_multiplier_permille(), + crate::model::fee_estimation::PlatformFeeEstimator::DEFAULT_FEE_MULTIPLIER_PERMILLE, + "new SDK, old fee multiplier" + ); + } + }); + + assert_eq!(Arc::as_ptr(&ctx.sdk.load_full()), new_sdk_ptr); + assert_eq!(ctx.platform_protocol_version(), 0); + } + + /// Until the network's version is fetched, the connected version is the + /// one the SDK builds with. + #[test] + fn connected_platform_version_falls_back_to_the_sdk_version() { + use crate::context::test_support::test_app_context_for_network; + + let tmp = tempfile::tempdir().expect("tempdir"); + let ctx = test_app_context_for_network(tmp.path(), Network::Mainnet); + assert_eq!(ctx.platform_protocol_version(), 0); + assert_eq!( + ctx.connected_platform_version().protocol_version, + ctx.sdk.load().protocol_version_number() + ); + } + + /// Contest windows stay consistent as the SDK ratchets upward from each + /// network minimum to a newer supported protocol. + #[test] + fn contest_durations_match_the_network_minimum_for_every_supported_version() { use crate::model::dpns::contest_durations; let newest = PlatformVersion::latest().protocol_version; for network in [Network::Mainnet, Network::Testnet] { - let pinned = contest_durations(network, default_platform_version(&network)); - for protocol_version in DET_PLATFORM_VERSION.protocol_version..=newest { + let minimum = dash_sdk::sdk::min_protocol_version(network); + let pinned = contest_durations( + network, + PlatformVersion::get(minimum).expect("network minimum"), + ); + for protocol_version in minimum..=newest { let live = PlatformVersion::get(protocol_version).expect("known version"); assert_eq!( contest_durations(network, live), diff --git a/src/context/system_contracts.rs b/src/context/system_contracts.rs new file mode 100644 index 000000000..7a6db8a37 --- /dev/null +++ b/src/context/system_contracts.rs @@ -0,0 +1,156 @@ +//! The system data contracts, as defined at the protocol version in use. + +use std::sync::Arc; +use std::sync::atomic::{AtomicU32, Ordering}; + +use arc_swap::ArcSwap; +use dash_sdk::dpp::ProtocolError; +use dash_sdk::dpp::system_data_contracts::{SystemDataContract, load_system_data_contract}; +use dash_sdk::dpp::version::PlatformVersion; +use dash_sdk::platform::DataContract; + +/// The system contracts DET uses, all loaded at one protocol version. +#[derive(Debug)] +pub(crate) struct SystemContracts { + protocol_version: u32, + pub(crate) dpns: Arc, + pub(crate) withdrawals: Arc, + pub(crate) dashpay: Arc, + pub(crate) token_history: Arc, + pub(crate) keyword_search: Arc, +} + +impl SystemContracts { + /// Load every system contract as `platform_version` defines it. + pub(crate) fn load(platform_version: &PlatformVersion) -> Result> { + let load = |contract| { + load_system_data_contract(contract, platform_version) + .map(Arc::new) + .map_err(Box::new) + }; + Ok(Self { + protocol_version: platform_version.protocol_version, + dpns: load(SystemDataContract::DPNS)?, + withdrawals: load(SystemDataContract::Withdrawals)?, + dashpay: load(SystemDataContract::Dashpay)?, + token_history: load(SystemDataContract::TokenHistory)?, + keyword_search: load(SystemDataContract::KeywordSearch)?, + }) + } +} + +/// [`SystemContracts`] that follow the protocol version in use. +/// +/// Lock-free: the set lives in an [`ArcSwap`]. Two callers reloading at once +/// store equivalent sets; a set stored late for an older version is replaced +/// on the next access, so the cache converges on the current version. +#[derive(Debug)] +pub(crate) struct SystemContractsCache { + current: ArcSwap, + /// The last version whose reload failed, so the failure is logged once. + failed_version: AtomicU32, +} + +impl SystemContractsCache { + pub(crate) fn new(contracts: SystemContracts) -> Self { + Self { + current: ArcSwap::from_pointee(contracts), + failed_version: AtomicU32::new(0), + } + } + + /// The set for exactly `platform_version`, reloaded if the cached one was + /// built for another version. A failed reload is an error, never the set + /// of another version: what verifies a proof must not mix schemas. + pub(crate) fn try_get( + &self, + platform_version: &PlatformVersion, + ) -> Result, Box> { + let cached = self.current.load_full(); + if cached.protocol_version == platform_version.protocol_version { + return Ok(cached); + } + let fresh = Arc::new(SystemContracts::load(platform_version)?); + self.current.store(Arc::clone(&fresh)); + Ok(fresh) + } + + /// The set for `platform_version`, for the app's own reads (names, + /// documents, forms). A failed reload keeps the cached set, so the app + /// keeps working on its last loaded contracts; proof verification uses + /// [`Self::try_get`] instead. + pub(crate) fn get(&self, platform_version: &PlatformVersion) -> Arc { + match self.try_get(platform_version) { + Ok(contracts) => contracts, + Err(error) => { + let cached = self.current.load_full(); + let version = platform_version.protocol_version; + if self.failed_version.swap(version, Ordering::Relaxed) != version { + tracing::error!( + protocol_version = version, + kept_protocol_version = cached.protocol_version, + ?error, + "Could not load the system contracts for the network's protocol version; keeping the loaded ones" + ); + } + cached + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use dash_sdk::dpp::data_contract::accessors::v0::DataContractV0Getters; + + fn version(protocol_version: u32) -> &'static PlatformVersion { + PlatformVersion::get(protocol_version).expect("known protocol version") + } + + /// Protocol 14 redefines some system contracts, so a set loaded at 13 + /// must not be served once the network runs 14. + #[test] + fn a_new_protocol_version_reloads_the_contracts() { + let v13 = version(13); + let v14 = version(14); + let dashpay_v13 = load_system_data_contract(SystemDataContract::Dashpay, v13).unwrap(); + let dashpay_v14 = load_system_data_contract(SystemDataContract::Dashpay, v14).unwrap(); + assert_ne!( + dashpay_v13, dashpay_v14, + "precondition: protocol 14 redefines the DashPay contract" + ); + + let cache = SystemContractsCache::new(SystemContracts::load(v13).unwrap()); + assert_eq!(*cache.get(v13).dashpay, dashpay_v13); + let reloaded = cache.get(v14); + assert_eq!(*reloaded.dashpay, dashpay_v14); + assert_eq!(reloaded.dpns.id(), cache.get(v13).dpns.id()); + assert_eq!( + *cache.get(v14).withdrawals, + load_system_data_contract(SystemDataContract::Withdrawals, v14).unwrap() + ); + } + + /// A version whose contracts cannot be loaded is an error for the exact + /// lookup; the app's lookup keeps the set it has. + #[test] + fn an_unloadable_version_fails_the_exact_lookup() { + let v13 = version(13); + let cache = SystemContractsCache::new(SystemContracts::load(v13).unwrap()); + let mut unloadable = v13.clone(); + unloadable.protocol_version = u32::MAX; + + assert!(cache.try_get(&unloadable).is_err()); + assert_eq!(cache.get(&unloadable).protocol_version, 13); + assert!(Arc::ptr_eq(&cache.get(v13), &cache.try_get(v13).unwrap())); + } + + /// The same version serves the cached set itself, not a reload. + #[test] + fn the_same_protocol_version_keeps_the_cached_set() { + let v13 = version(13); + let cache = SystemContractsCache::new(SystemContracts::load(v13).unwrap()); + assert!(Arc::ptr_eq(&cache.get(v13), &cache.get(v13))); + } +} diff --git a/src/context_provider.rs b/src/context_provider.rs index 6e83a488b..8ba42fa9c 100644 --- a/src/context_provider.rs +++ b/src/context_provider.rs @@ -21,25 +21,41 @@ pub(crate) const MASTERNODE_LIST_NOT_READY_DETAIL: &str = /// Resolve a data contract by ID: check cached system contracts first, then DB. /// +/// System contracts are served as `platform_version` defines them: the proof +/// verifier passes the version it verifies with, which during an upgrade can +/// be newer than the SDK's stored version. +/// /// All system contracts are listed in `cached` — adding a new one is a single /// array edit. The array size is tied to [`SYSTEM_CONTRACT_COUNT`] so the /// compiler enforces completeness. fn resolve_data_contract( app_ctx: &AppContext, data_contract_id: &Identifier, + platform_version: &PlatformVersion, ) -> Result>, ContextProviderError> { + // Never another version's contracts: the verifier would check the proof + // against the wrong schema. + let system_contracts = app_ctx + .system_contracts_at(platform_version) + .map_err(|error| { + ContextProviderError::DataContractFailure(format!( + "system contracts for protocol version {} could not be loaded: {error}", + platform_version.protocol_version + )) + })?; let cached: [&Arc; SYSTEM_CONTRACT_COUNT] = [ - &app_ctx.dpns_contract, - &app_ctx.dashpay_contract, - &app_ctx.token_history_contract, - &app_ctx.withdraws_contract, - &app_ctx.keyword_search_contract, + &system_contracts.dpns, + &system_contracts.dashpay, + &system_contracts.token_history, + &system_contracts.withdrawals, + &system_contracts.keyword_search, ]; - for contract in &cached { - if data_contract_id == &contract.id() { - return Ok(Some(Arc::clone(contract))); - } + if let Some(contract) = cached + .into_iter() + .find(|contract| data_contract_id == &contract.id()) + { + return Ok(Some(Arc::clone(contract))); } // K/V fallback for user-added / non-system contracts @@ -107,10 +123,10 @@ impl ContextProvider for SpvProvider { fn get_data_contract( &self, data_contract_id: &Identifier, - _platform_version: &PlatformVersion, + platform_version: &PlatformVersion, ) -> Result>, ContextProviderError> { let app_ctx = self.app_context()?; - resolve_data_contract(&app_ctx, data_contract_id) + resolve_data_contract(&app_ctx, data_contract_id, platform_version) } fn get_token_configuration( @@ -171,7 +187,7 @@ impl ContextProvider for SpvProvider { core_chain_locked_height, )) }) - .map_err(|e| ContextProviderError::Generic(e.to_string())) + .map_err(quorum_lookup_error) } fn get_platform_activation_height( @@ -187,3 +203,96 @@ impl ContextProvider for SpvProvider { }) } } + +fn quorum_lookup_error(error: crate::backend_task::error::TaskError) -> ContextProviderError { + match error { + crate::backend_task::error::TaskError::WalletBackend { source } => { + source.quorum_lookup_context_error() + } + other => ContextProviderError::Generic(other.to_string()), + } +} + +#[cfg(test)] +mod quorum_error_tests { + use super::*; + use crate::backend_task::error::TaskError; + use dash_sdk::dpp::dashcore::sml::llmq_entry_verification::{ + LLMQEntryVerificationSkipStatus, LLMQEntryVerificationStatus, + }; + + #[test] + fn unverified_local_quorum_does_not_blame_the_dapi_endpoint() { + let error = TaskError::WalletBackend { + source: Arc::new( + platform_wallet::error::PlatformWalletError::QuorumNotVerified { + quorum_type: 6, + quorum_hash: [0xab; 32], + status: LLMQEntryVerificationStatus::Skipped( + LLMQEntryVerificationSkipStatus::NotMarkedForVerification, + ), + }, + ), + }; + assert!(matches!( + quorum_lookup_error(error), + ContextProviderError::QuorumSourceUnavailable(_) + )); + } + + #[test] + fn unrelated_quorum_lookup_errors_are_not_reclassified_as_readiness() { + assert!(matches!( + quorum_lookup_error(TaskError::NoIdentitiesFound), + ContextProviderError::Generic(_) + )); + } +} + +#[cfg(test)] +mod data_contract_tests { + use super::*; + use dash_sdk::dpp::system_data_contracts::{SystemDataContract, load_system_data_contract}; + + /// The proof verifier asks for a contract at the version it verifies + /// with, which during an upgrade can be newer than the SDK's stored + /// version. A system contract is served as that version defines it. + #[test] + fn system_contracts_resolve_at_the_requested_platform_version() { + let dir = tempfile::tempdir().expect("tempdir"); + let ctx = crate::context::test_support::test_app_context(dir.path()); + for protocol_version in [13, 14] { + let platform_version = PlatformVersion::get(protocol_version).expect("known version"); + let expected = + load_system_data_contract(SystemDataContract::Dashpay, platform_version).unwrap(); + let resolved = resolve_data_contract(&ctx, &expected.id(), platform_version) + .expect("lookup") + .expect("a cached system contract"); + assert_eq!(*resolved, expected, "protocol {protocol_version}"); + } + } + + /// A version whose system contracts cannot be loaded is refused, not + /// answered with the set loaded for another version. + #[test] + fn system_contracts_that_cannot_load_at_the_requested_version_are_refused() { + let dir = tempfile::tempdir().expect("tempdir"); + let ctx = crate::context::test_support::test_app_context(dir.path()); + let dpns_id = SystemDataContract::DPNS.id(); + // Load a set first, so a fallback would have something to serve. + let latest = PlatformVersion::latest(); + assert!(matches!( + resolve_data_contract(&ctx, &dpns_id, latest), + Ok(Some(_)) + )); + + let mut unloadable = latest.clone(); + unloadable.protocol_version = u32::MAX; + let resolved = resolve_data_contract(&ctx, &dpns_id, &unloadable); + + assert!( + matches!(resolved, Err(ContextProviderError::DataContractFailure(_))), + "got {resolved:?}" + ); + } +} diff --git a/src/mcp/resolve.rs b/src/mcp/resolve.rs index 23c6abd12..9dfef54aa 100644 --- a/src/mcp/resolve.rs +++ b/src/mcp/resolve.rs @@ -448,12 +448,12 @@ mod tests { use dash_sdk::platform::DataContract; /// Mocks the proved DPNS-contract fetch that drives the protocol-version - /// ratchet — the only network read the epoch task still performs while - /// dashpay/platform#4231 is unresolved. The mock reports `LATEST_VERSION` in + /// ratchet — the epoch task's fallback when the current epoch cannot be + /// read (the mock has no epoch response). The mock reports `LATEST_VERSION` in /// the response metadata, so a successful fetch ratchets the SDK exactly as a /// live network would; a failed one leaves the version unconfirmed. async fn mock_sdk_with_dpns_contract(ctx: &Arc) -> Sdk { - let contract = DataContract::clone(&ctx.dpns_contract); + let contract = DataContract::clone(&ctx.dpns_contract()); let mut sdk = Sdk::new_mock(); sdk.mock() .expect_fetch(contract.id(), Some(contract)) diff --git a/src/sdk_wrapper.rs b/src/sdk_wrapper.rs index 1f6f53879..82b0a6ea8 100644 --- a/src/sdk_wrapper.rs +++ b/src/sdk_wrapper.rs @@ -1,4 +1,3 @@ -use crate::context::default_platform_version; use dash_sdk::dapi_client::AddressList; use dash_sdk::dpp::dashcore::Network; use dash_sdk::platform::ContextProvider; @@ -17,16 +16,20 @@ fn request_settings() -> RequestSettings { } } +/// Build an SDK for `network` that tracks the network's protocol version. +/// +/// The SDK is never pinned: it starts at upstream's per-network minimum (the +/// lowest version the network still runs) and ratchets upward to whatever the +/// network reports in proven responses. A rebuilt connection starts there +/// too: nothing learned from the endpoints it replaces is carried over. pub fn initialize_sdk( address_list: AddressList, network: Network, context_provider: P, ) -> Result { let request_settings = request_settings(); - let platform_version = default_platform_version(&network); let sdk = SdkBuilder::new(address_list) - .with_initial_version(platform_version) .with_network(network) .with_context_provider(context_provider) .with_settings(request_settings) @@ -35,7 +38,7 @@ pub fn initialize_sdk( info!( ?network, - protocol_version = platform_version.protocol_version, + protocol_version = sdk.protocol_version_number(), "SDK initialized successfully" ); diff --git a/src/ui/contracts_documents/contracts_documents_screen.rs b/src/ui/contracts_documents/contracts_documents_screen.rs index 5550c5a30..04f07943d 100644 --- a/src/ui/contracts_documents/contracts_documents_screen.rs +++ b/src/ui/contracts_documents/contracts_documents_screen.rs @@ -120,7 +120,7 @@ pub enum DocumentDisplayMode { impl DocumentQueryScreen { pub fn new(app_context: &Arc) -> Self { let dpns_contract = QualifiedContract { - contract: Arc::clone(&app_context.dpns_contract).as_ref().clone(), + contract: Arc::clone(&app_context.dpns_contract()).as_ref().clone(), alias: Some("dpns".to_string()), }; @@ -614,7 +614,9 @@ impl ScreenLike for DocumentQueryScreen { // Reset the selected contract and document type let dpns_contract = QualifiedContract { - contract: Arc::clone(&self.app_context.dpns_contract).as_ref().clone(), + contract: Arc::clone(&self.app_context.dpns_contract()) + .as_ref() + .clone(), alias: Some("dpns".to_string()), }; self.selected_data_contract = dpns_contract.clone(); diff --git a/src/ui/identity/add_new_identity_screen/mod.rs b/src/ui/identity/add_new_identity_screen/mod.rs index 4d2e6cecb..2b6c6f194 100644 --- a/src/ui/identity/add_new_identity_screen/mod.rs +++ b/src/ui/identity/add_new_identity_screen/mod.rs @@ -221,7 +221,7 @@ impl AddNewIdentityScreen { /// Default number of keys (master + additional) the chooser warms and reads /// from the auth-pubkey cache. fn default_key_count(&self) -> u32 { - let dashpay_contract_id = self.app_context.dashpay_contract.id(); + let dashpay_contract_id = self.app_context.dashpay_contract().id(); // master (index 0) + the default additional keys. default_identity_key_specs(dashpay_contract_id).len() as u32 + 1 } @@ -255,7 +255,7 @@ impl AddNewIdentityScreen { let network = self.app_context.network; let identity_index = self.identity_id_number; - let dashpay_contract_id = self.app_context.dashpay_contract.id(); + let dashpay_contract_id = self.app_context.dashpay_contract().id(); let default_keys = default_identity_key_specs(dashpay_contract_id); let Ok(backend) = self.app_context.wallet_backend() else { diff --git a/src/ui/identity/mod.rs b/src/ui/identity/mod.rs index c31274ebb..c0c2dbd4f 100644 --- a/src/ui/identity/mod.rs +++ b/src/ui/identity/mod.rs @@ -204,7 +204,7 @@ pub fn get_selected_wallet( ) -> Result>>, String> { // If `app_context` is provided, use the DPNS-based approach. let public_key = if let Some(context) = app_context { - let dpns_contract = &context.dpns_contract; + let dpns_contract = &context.dpns_contract(); // Attempt to fetch the `preorder` document type from the DPNS contract. let preorder_document_type = dpns_contract diff --git a/src/ui/identity/register_dpns_name_screen.rs b/src/ui/identity/register_dpns_name_screen.rs index fe2015201..437f21b60 100644 --- a/src/ui/identity/register_dpns_name_screen.rs +++ b/src/ui/identity/register_dpns_name_screen.rs @@ -197,7 +197,7 @@ impl RegisterDpnsNameScreen { let Some(identity) = &self.selected_qualified_identity else { return; }; - let required = dpns_signing_requirement(&self.app_context.dpns_contract); + let required = dpns_signing_requirement(&self.app_context.dpns_contract()); let wallets = self.app_context.wallet_context(); let arrived = missing_registration_wallets(identity, required) .any(|seed_hash| wallets.contains_hd(seed_hash)); @@ -590,7 +590,7 @@ impl RegisterDpnsNameScreen { .show(ui, |ui| { let document_type = self .app_context - .dpns_contract + .dpns_contract() .document_type_cloned_for_name("domain") .ok(); if self.selected_key.is_none() { @@ -913,7 +913,7 @@ impl ScreenLike for RegisterDpnsNameScreen { // and Keys screen; this page only says what helps. let key_status = registration_key_status( identity, - dpns_signing_requirement(&self.app_context.dpns_contract), + dpns_signing_requirement(&self.app_context.dpns_contract()), ); let updated_install = key_status.key_is_unusable_here() && self.app_context.has_legacy_identities(); diff --git a/src/ui/identity/usernames_card.rs b/src/ui/identity/usernames_card.rs index 9c3c2d5d6..5e163557d 100644 --- a/src/ui/identity/usernames_card.rs +++ b/src/ui/identity/usernames_card.rs @@ -403,7 +403,7 @@ impl UsernamesCard { app_context: &Arc, identity: &QualifiedIdentity, ) -> Option { - let required = dpns_signing_requirement(&app_context.dpns_contract); + let required = dpns_signing_requirement(&app_context.dpns_contract()); let status = registration_key_status(identity, required); let identity_id = identity.identity.id(); let key_missing = status.key_is_unusable_here(); diff --git a/src/ui/tokens/tokens_screen/my_tokens.rs b/src/ui/tokens/tokens_screen/my_tokens.rs index 7cf834325..b67b54872 100644 --- a/src/ui/tokens/tokens_screen/my_tokens.rs +++ b/src/ui/tokens/tokens_screen/my_tokens.rs @@ -1116,7 +1116,7 @@ mod tests { let mut screen = TokensScreen::new(&context, TokensSubscreen::MyTokens); let token_id = Identifier::from([42; 32]); let config = TokenConfigurationV0::default_most_restrictive().into(); - let contract = context.dpns_contract.as_ref().clone(); + let contract = context.dpns_contract().as_ref().clone(); screen.all_known_tokens.insert( token_id, TokenInfoWithDataContract { From 173a135ab4662c09b1422f6e0ff4a0dea3644de2 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Sat, 24 Oct 2026 18:21:52 +0000 Subject: [PATCH 05/27] test(platform): guard the protocol 13 tables and transitions Compare the protocol 13 client tables with a recorded snapshot, so an upstream change to them is reviewed, and check that keys and documents DET builds on a protocol 13 network use encodings protocol 13 decodes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- src/backend_task/protocol_13_transitions.rs | 236 ++++++ .../fixtures/platform_v13_client_tables.txt | 798 ++++++++++++++++++ src/context/platform_version_guard.rs | 59 ++ 3 files changed, 1093 insertions(+) create mode 100644 src/backend_task/protocol_13_transitions.rs create mode 100644 src/context/fixtures/platform_v13_client_tables.txt create mode 100644 src/context/platform_version_guard.rs diff --git a/src/backend_task/protocol_13_transitions.rs b/src/backend_task/protocol_13_transitions.rs new file mode 100644 index 000000000..5811df3f0 --- /dev/null +++ b/src/backend_task/protocol_13_transitions.rs @@ -0,0 +1,236 @@ +//! State transitions built the way DET builds them on a protocol 13 network +//! (mainnet and testnet today): only encodings protocol 13 decodes. + +use std::collections::BTreeMap; +use std::sync::Arc; + +use async_trait::async_trait; +use dash_sdk::dpp::ProtocolError; +use dash_sdk::dpp::address_funds::AddressWitness; +use dash_sdk::dpp::dashcore::Network; +use dash_sdk::dpp::data_contract::accessors::v0::DataContractV0Getters; +use dash_sdk::dpp::data_contract::config::DataContractConfig; +use dash_sdk::dpp::data_contract::document_type::DocumentType; +use dash_sdk::dpp::document::{Document, DocumentV0}; +use dash_sdk::dpp::identity::accessors::IdentityGettersV0; +use dash_sdk::dpp::identity::identity_public_key::v0::IdentityPublicKeyV0; +use dash_sdk::dpp::identity::signer::Signer; +use dash_sdk::dpp::identity::{Identity, IdentityPublicKey, KeyType, Purpose, SecurityLevel}; +use dash_sdk::dpp::platform_value::{BinaryData, Identifier, platform_value}; +use dash_sdk::dpp::prelude::UserFeeIncrease; +use dash_sdk::dpp::state_transition::StateTransition; +use dash_sdk::dpp::state_transition::batch_transition::BatchTransition; +use dash_sdk::dpp::state_transition::batch_transition::batched_transition::BatchedTransition; +use dash_sdk::dpp::state_transition::batch_transition::batched_transition::document_transition::DocumentTransitionV0Methods; +use dash_sdk::dpp::state_transition::batch_transition::document_base_transition::DocumentBaseTransition; +use dash_sdk::dpp::state_transition::batch_transition::methods::StateTransitionCreationOptions; +use dash_sdk::dpp::state_transition::batch_transition::methods::v0::DocumentsBatchTransitionMethodsV0; +use dash_sdk::dpp::state_transition::identity_update_transition::IdentityUpdateTransition; +use dash_sdk::dpp::state_transition::identity_update_transition::methods::IdentityUpdateTransitionMethodsV0; +use dash_sdk::dpp::state_transition::public_key_in_creation::IdentityPublicKeyInCreation; +use dash_sdk::dpp::version::PlatformVersion; + +use crate::context::AppContext; +use crate::context::test_support::test_app_context_for_network; + +/// Signs everything with placeholder bytes: these tests check what is +/// encoded, not signature validity. +#[derive(Debug)] +struct PlaceholderSigner; + +#[async_trait] +impl Signer for PlaceholderSigner { + async fn sign( + &self, + _key: &IdentityPublicKey, + _data: &[u8], + ) -> Result { + Ok(BinaryData::new(vec![0; 65])) + } + + async fn sign_create_witness( + &self, + _key: &IdentityPublicKey, + _data: &[u8], + ) -> Result { + Err(ProtocolError::Generic( + "no address witnesses here".to_string(), + )) + } + + fn can_sign_with(&self, _key: &IdentityPublicKey) -> bool { + true + } +} + +/// A mainnet context, which builds at protocol 13 until the network says +/// otherwise. +fn mainnet_context() -> (tempfile::TempDir, Arc, &'static PlatformVersion) { + let tmp = tempfile::tempdir().expect("tempdir"); + let ctx = test_app_context_for_network(tmp.path(), Network::Mainnet); + let platform_version = ctx.platform_version(); + assert_eq!(platform_version.protocol_version, 13); + (tmp, ctx, platform_version) +} + +fn plain_key(id: u32, security_level: SecurityLevel) -> IdentityPublicKey { + IdentityPublicKey::from(IdentityPublicKeyV0 { + id, + purpose: Purpose::AUTHENTICATION, + security_level, + contract_bounds: None, + key_type: KeyType::ECDSA_SECP256K1, + read_only: false, + data: vec![2; 33].into(), + disabled_at: None, + }) +} + +/// A key without limits, added on a protocol 13 network, goes out as the +/// version 0 key encoding protocol 13 decodes. +#[tokio::test] +async fn a_plain_key_is_added_in_the_protocol_13_encoding() { + let (_tmp, _ctx, platform_version) = mainnet_context(); + let mut identity = Identity::create_basic_identity(Identifier::from([4; 32]), platform_version) + .expect("identity"); + identity.add_public_key(plain_key(0, SecurityLevel::MASTER)); + + let transition = IdentityUpdateTransition::try_from_identity_with_signer( + &identity, + &0, + vec![plain_key(1, SecurityLevel::HIGH)], + vec![], + 1, + UserFeeIncrease::default(), + &PlaceholderSigner, + platform_version, + None, + ) + .await + .expect("identity update"); + + let StateTransition::IdentityUpdate(IdentityUpdateTransition::V0(update)) = transition else { + panic!("expected a version 0 identity update, got {transition:?}"); + }; + assert_eq!(update.add_public_keys.len(), 1); + assert!(matches!( + update.add_public_keys[0], + IdentityPublicKeyInCreation::V0(_) + )); +} + +fn note_contract(platform_version: &PlatformVersion) -> dash_sdk::platform::DataContract { + use dash_sdk::dpp::data_contract::v1::DataContractV1; + + let id = Identifier::from([8; 32]); + let config = DataContractConfig::default_for_version(platform_version).expect("config"); + let schema = platform_value!({ + "type": "object", + "properties": {"name": {"type": "string", "position": 0, "maxLength": 63_u32}}, + "required": ["name"], + "additionalProperties": false, + }); + let note = DocumentType::try_from_schema( + id, + 1, + config.version(), + "note", + schema, + None, + &BTreeMap::new(), + &config, + true, + &mut Vec::new(), + platform_version, + ) + .expect("document type"); + dash_sdk::platform::DataContract::V1(DataContractV1 { + id, + version: 1, + owner_id: Identifier::from([7; 32]), + document_types: BTreeMap::from([("note".to_string(), note)]), + config, + schema_defs: None, + groups: BTreeMap::new(), + tokens: BTreeMap::new(), + keywords: vec![], + created_at: None, + updated_at: None, + created_at_block_height: None, + updated_at_block_height: None, + created_at_epoch: None, + updated_at_epoch: None, + description: None, + }) +} + +fn note(owner_id: Identifier) -> Document { + Document::V0(DocumentV0 { + moderated_at: None, + moderated_by: None, + id: Identifier::from([9; 32]), + owner_id, + creator_id: None, + properties: BTreeMap::from([("name".to_string(), "hello".into())]), + revision: Some(1), + created_at: None, + updated_at: None, + transferred_at: None, + created_at_block_height: None, + updated_at_block_height: None, + transferred_at_block_height: None, + created_at_core_block_height: None, + updated_at_core_block_height: None, + transferred_at_core_block_height: None, + contract_version: None, + }) +} + +async fn create_note( + platform_version: &PlatformVersion, + options: Option, +) -> Result> { + let contract = note_contract(platform_version); + let document_type = contract.document_type_for_name("note").expect("note type"); + let owner_id = Identifier::from([4; 32]); + BatchTransition::new_document_creation_transition_from_document( + note(owner_id), + document_type, + [3; 32], + &plain_key(1, SecurityLevel::HIGH), + 1, + UserFeeIncrease::default(), + None, + &PlaceholderSigner, + platform_version, + options, + ) + .await + .map_err(Box::new) +} + +/// A document created on a protocol 13 network uses a document base that +/// protocol 13 decodes: not the protocol 14 one that carries a fee agreement. +#[tokio::test] +async fn a_document_is_created_in_the_protocol_13_encoding() { + let (_tmp, _ctx, platform_version) = mainnet_context(); + + let transition = create_note(platform_version, None) + .await + .expect("document create"); + + let StateTransition::Batch(batch) = transition else { + panic!("expected a batch, got {transition:?}"); + }; + let base = match &batch { + BatchTransition::V0(v0) => v0.transitions[0].base().clone(), + BatchTransition::V1(v1) => match &v1.transitions[0] { + BatchedTransition::Document(document) => document.base().clone(), + other => panic!("expected a document transition, got {other:?}"), + }, + }; + assert!( + !matches!(base, DocumentBaseTransition::V2(_)), + "protocol 13 cannot decode {base:?}" + ); +} diff --git a/src/context/fixtures/platform_v13_client_tables.txt b/src/context/fixtures/platform_v13_client_tables.txt new file mode 100644 index 000000000..219368253 --- /dev/null +++ b/src/context/fixtures/platform_v13_client_tables.txt @@ -0,0 +1,798 @@ +protocol_version: 13 +dpp: DPPVersion { + costs: DPPCostsVersions { + signature_verify: 0, + }, + validation: DPPValidationVersions { + json_schema_validator: JsonSchemaValidatorVersions { + new: 0, + validate: 0, + compile: 0, + compile_and_validate: 0, + }, + data_contract: DataContractValidationVersions { + validate: 0, + validate_config_update: 1, + validate_token_config_update: 0, + validate_index_definitions: 0, + validate_index_naming_duplicates: 0, + validate_not_defined_properties: 0, + validate_property_definition: 0, + validate_token_config_groups_exist: 1, + validate_localizations: 0, + validate_once_per_identity_distribution: None, + }, + document_type: DocumentTypeValidationVersions { + validate_update: 0, + unique_index_limit: 10, + contested_index_limit: 1, + }, + voting: VotingValidationVersions { + allow_other_contenders_time_mainnet_ms: 604800000, + allow_other_contenders_time_testing_ms: 2700000, + votes_allowed_per_masternode: 5, + }, + validation_result: ValidationResultMethodVersions { + flatten: 1, + merge_many: 1, + }, + }, + state_transition_serialization_versions: DPPStateTransitionSerializationVersions { + identity_public_key_in_creation: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + identity_create_from_addresses_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + identity_create_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + identity_update_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + identity_key_limits_update_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + identity_top_up_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + identity_top_up_from_addresses_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + identity_credit_withdrawal_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + identity_credit_transfer_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + identity_credit_transfer_to_addresses_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + masternode_vote_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + contract_create_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + contract_update_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + contract_user_moderation_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + contract_fee_claim_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + batch_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 1, + default_current_version: 1, + }, + document_base_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 1, + }, + document_create_state_transition: DocumentFeatureVersionBounds { + bounds: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + }, + document_replace_state_transition: DocumentFeatureVersionBounds { + bounds: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + }, + document_delete_state_transition: DocumentFeatureVersionBounds { + bounds: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + }, + document_index_only_delete_state_transition: None, + document_transfer_state_transition: DocumentFeatureVersionBounds { + bounds: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + }, + document_update_price_state_transition: DocumentFeatureVersionBounds { + bounds: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + }, + document_purchase_state_transition: DocumentFeatureVersionBounds { + bounds: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + }, + address_funds_transfer_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + address_funding_from_asset_lock_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + address_credit_withdrawal_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + shield_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + shielded_transfer_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + unshield_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + shield_from_asset_lock_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + shielded_withdrawal_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + identity_create_from_shielded_pool_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + shield_from_identity_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + identity_top_up_from_shielded_pool_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + token_shielded_transfer_with_shielded_fee_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + token_unshield_with_shielded_fee_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + token_purchase_from_shielded_pool_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + }, + state_transition_conversion_versions: DPPStateTransitionConversionVersions { + identity_to_identity_create_transition: 0, + identity_to_identity_top_up_transition: 0, + identity_to_identity_transfer_transition: 0, + identity_to_identity_withdrawal_transition: 1, + identity_to_identity_create_transition_with_signer: 0, + inputs_to_identity_create_from_addresses_transition_with_signer: 0, + address_funds_to_address_funds_transfer_transition: 0, + identity_to_identity_top_up_from_addresses_transition: 0, + address_funding_from_asset_lock_transition: 0, + }, + state_transition_method_versions: DPPStateTransitionMethodVersions { + public_key_in_creation_methods: PublicKeyInCreationMethodVersions { + from_public_key_signed_with_private_key: 0, + from_public_key_signed_external: 0, + hash: 0, + duplicated_key_ids_witness: 0, + duplicated_keys_witness: 0, + validate_identity_public_keys_structure: 0, + }, + verify_identity_signed_signature: 0, + }, + state_transitions: DPPStateTransitionVersions { + documents: DocumentTransitionVersions { + documents_batch_transition: DocumentsBatchTransitionVersions { + validation: DocumentsBatchTransitionValidationVersions { + find_duplicates_by_id: 0, + validate_base_structure: 0, + }, + }, + }, + identities: IdentityTransitionVersions { + max_public_keys_in_creation: 6, + asset_locks: IdentityTransitionAssetLockVersions { + required_asset_lock_duff_balance_for_processing_start_for_identity_create: 200000, + required_asset_lock_duff_balance_for_processing_start_for_identity_top_up: 50000, + required_asset_lock_duff_balance_for_processing_start_for_address_funding: 50000, + validate_asset_lock_transaction_structure: 0, + validate_instant_asset_lock_proof_structure: 0, + max_asset_lock_transaction_inputs: 100, + }, + credit_withdrawal: IdentityCreditWithdrawalTransitionVersions { + default_constructor: 1, + }, + calculate_min_required_fee_on_identity_create_transition: 1, + calculate_min_required_fee_on_identity_top_up_transition: 1, + }, + contract: ContractTransitionVersions { + contract_create_transition_default_version: 0, + contract_update_transition_default_version: 0, + }, + address_funds: AddressFundsTransitionVersions { + address_funds_transition_default_version: 0, + credit_withdrawal: 0, + validate_credit_withdrawal_structure: 0, + min_output_amount: 500000, + min_input_amount: 100000, + min_identity_funding_amount: 200000, + }, + shielded: ShieldedTransitionVersions { + validate_withdrawal_structure: 0, + }, + max_address_inputs: 16, + max_address_outputs: 128, + max_address_fee_strategies: 4, + }, + contract_versions: DPPContractVersions { + max_serialized_size: 65000, + contract_serialization_version: FeatureVersionBounds { + min_version: 0, + max_version: 1, + default_current_version: 1, + }, + contract_structure_version: 1, + created_data_contract_structure: 0, + config: FeatureVersionBounds { + min_version: 1, + max_version: 1, + default_current_version: 1, + }, + methods: DataContractMethodVersions { + validate_document: 0, + validate_update: 0, + schema: 0, + validate_groups: 0, + validate_moderation_config: 0, + equal_ignoring_time_fields: 0, + registration_cost: 1, + }, + document_type_versions: DocumentTypeVersions { + index_versions: DocumentTypeIndexVersions { + index_levels_from_indices: 0, + }, + class_method_versions: DocumentTypeClassMethodVersions { + try_from_schema: 2, + create_document_types_from_document_schemas: 1, + }, + structure_version: 0, + schema: DocumentTypeSchemaVersions { + document_type_schema: 2, + should_add_creator_id: 1, + enrich_with_base_schema: 1, + find_identifier_and_binary_paths: 0, + apply_property_reference: None, + apply_required_since: None, + apply_distinct_from: None, + apply_encrypted_for: None, + apply_max_bytes: None, + parse_typed_array: None, + parse_property_constraints: None, + apply_generated_from: None, + expand_property_type_shorthands: None, + validate_max_depth: 0, + max_depth: 256, + recursive_schema_validator_versions: RecursiveSchemaValidatorVersions { + traversal_validator: 0, + }, + validate_schema_compatibility: 0, + }, + methods: DocumentTypeMethodVersions { + create_document_from_data: 0, + create_document_with_prevalidated_properties: 0, + prefunded_voting_balance_for_document: 0, + contested_vote_poll_for_document: 0, + estimated_size: 0, + index_for_types: 0, + max_size: 0, + serialize_value_for_key: 0, + deserialize_value_for_key: 0, + validate_distinct_from: None, + validate_encrypted_property_shapes: None, + validate_max_bytes: None, + validate_property_constraints: None, + canonical_contested_index_values: None, + fill_generated_properties: None, + validate_generated_from: None, + }, + }, + token_versions: TokenVersions { + validate_structure_interval: 0, + validate_pre_programmed_distribution_amounts: 0, + token_configuration_format: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + }, + }, + document_versions: DPPDocumentVersions { + document_structure_version: 0, + document_serialization_version: FeatureVersionBounds { + min_version: 0, + max_version: 2, + default_current_version: 2, + }, + document_cbor_serialization_version: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + extended_document_structure_version: 0, + extended_document_serialization_version: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + document_method_versions: DocumentMethodVersions { + is_equal_ignoring_timestamps: 0, + hash: 0, + get_raw_for_contract: 0, + get_raw_for_document_type: 0, + try_into_asset_unlock_base_transaction_info: 0, + generate_document_id: 0, + get_raw_array_elements_for_document_type: None, + }, + }, + identity_versions: DPPIdentityVersions { + identity_structure_version: 0, + identity_key_structure_version: 0, + identity_key_type_method_versions: IdentityKeyTypeMethodVersions { + random_public_key_data: 0, + random_public_and_private_key_data: 0, + }, + }, + voting_versions: DPPVotingVersions { + default_vote_poll_time_duration_mainnet_ms: 1209600000, + default_vote_poll_time_duration_test_network_ms: 5400000, + contested_document_vote_poll_stored_info_version: 0, + }, + token_versions: DPPTokenVersions { + identity_token_info_default_structure_version: 0, + identity_token_status_default_structure_version: 0, + token_contract_info_default_structure_version: 0, + token_config_update_action_id_version: 1, + token_set_price_action_id_version: 1, + distribution_function_evaluate_version: 0, + reward_distribution_max_cycle_moment_version: 0, + distribution_function_cycle_epochs_version: 0, + }, + asset_lock_versions: DPPAssetLockVersions { + reduced_asset_lock_value: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, + }, + methods: DPPMethodVersions { + epoch_core_reward_credits_for_distribution: 0, + daily_withdrawal_limit: 1, + deduct_fee_from_outputs_or_remaining_balance_of_inputs: 0, + compute_minimum_shielded_fee: 0, + shielded_extra_sighash_data: 0, + core_credit_pool_unlock_limit: None, + credit_pool_bundle_binding: None, + }, + factory_versions: DPPFactoryVersions { + data_contract_factory_structure_version: 0, + document_factory_structure_version: 0, + }, +} +drive.methods.verify: DriveVerifyMethodVersions { + contract: DriveVerifyContractMethodVersions { + verify_contract: 0, + verify_contract_history: 0, + verify_contract_return_serialization: 0, + verify_contracts_by_range: 0, + verify_contracts_versions: 0, + }, + document: DriveVerifyDocumentMethodVersions { + verify_proof: 0, + verify_proof_keep_serialized: 0, + verify_document_history: 0, + verify_start_at_document_in_proof: 0, + }, + chained_document: DriveVerifyChainedDocumentMethodVersions { + verify_chained_documents_proof: 0, + }, + composite_document: DriveVerifyCompositeDocumentMethodVersions { + verify_composite_documents_proof: 0, + }, + document_count: DriveVerifyDocumentCountMethodVersions { + verify_aggregate_count_proof: 0, + verify_carrier_aggregate_count_proof: 0, + verify_distinct_count_proof: 0, + verify_point_lookup_count_proof: 0, + verify_primary_key_count_tree_proof: 0, + }, + document_sum: DriveVerifyDocumentSumMethodVersions { + verify_aggregate_sum_proof: 0, + verify_carrier_aggregate_sum_proof: 0, + verify_carrier_aggregate_count_and_sum_proof: 0, + verify_aggregate_count_and_sum_proof: 0, + verify_primary_key_sum_tree_proof: 0, + verify_primary_key_count_sum_tree_proof: 0, + verify_point_lookup_sum_proof: 0, + verify_distinct_sum_proof: 0, + verify_distinct_count_and_sum_proof: 0, + verify_point_lookup_count_and_sum_proof: 0, + }, + document_ranked: DriveVerifyDocumentRankedMethodVersions { + verify_ranked_top_k_proof: 0, + verify_having_range_proof: 0, + }, + identity: DriveVerifyIdentityMethodVersions { + verify_full_identities_by_public_key_hashes: 0, + verify_full_identity_by_identity_id: 0, + verify_full_identity_by_public_key_hash: 0, + verify_identity_balance_for_identity_id: 0, + verify_identity_balances_for_identity_ids: 0, + verify_identity_balance_revision_and_addresses_from_inputs: 0, + verify_identity_id_by_unique_public_key_hash: 0, + verify_identity_ids_by_unique_public_key_hashes: 0, + verify_identity_keys_by_identity_id: 0, + verify_identity_nonce: 0, + verify_identity_contract_nonce: 0, + verify_identities_contract_keys: 0, + verify_identity_revision_for_identity_id: 0, + verify_full_identity_by_non_unique_public_key_hash: 0, + verify_full_identity_by_non_unique_public_key_hash_two_proofs: 0, + verify_identity_id_by_non_unique_public_key_hash: 0, + verify_identity_keys_remaining_budgets: 0, + }, + group: DriveVerifyGroupMethodVersions { + verify_group_info: 0, + verify_group_infos_in_contract: 0, + verify_action_infos: 0, + verify_action_signers: 0, + verify_action_signers_total_power: 0, + }, + contract_group: DriveVerifyContractGroupMethodVersions { + verify_contract_group_info: 0, + verify_contract_group_members: 0, + verify_contract_group_memberships_for_contract: 0, + }, + contract_moderation: DriveVerifyContractModerationMethodVersions { + verify_contract_moderation_status: 0, + verify_contract_moderation_entries: 0, + verify_contract_fee_pots: 0, + verify_contract_document_removals: 0, + verify_contract_team_actions: 0, + verify_contract_team_action_signers: 0, + verify_contract_team_action_signature: 0, + verify_contract_moderation_action_counts: 0, + }, + token: DriveVerifyTokenMethodVersions { + verify_token_balances_for_identity_ids: 0, + verify_token_balances_for_identity_id: 0, + verify_token_infos_for_identity_ids: 0, + verify_token_infos_for_identity_id: 0, + verify_token_statuses: 0, + verify_token_total_supply_and_aggregated_identity_balance: 0, + verify_token_balance_for_identity_id: 0, + verify_token_info_for_identity_id: 0, + verify_token_status: 0, + verify_token_pre_programmed_distributions: 0, + verify_token_direct_selling_price: 0, + verify_token_direct_selling_prices: 0, + verify_token_perpetual_distribution_last_paid_time: 0, + verify_token_contract_info: 0, + }, + single_document: DriveVerifySingleDocumentMethodVersions { + verify_proof: 0, + verify_proof_keep_serialized: 0, + }, + system: DriveVerifySystemMethodVersions { + verify_epoch_infos: 0, + verify_epoch_proposers: 0, + verify_elements: 0, + verify_total_credits_in_system: 0, + verify_upgrade_state: 0, + verify_upgrade_vote_status: 0, + verify_finalized_epoch_infos: 0, + }, + voting: DriveVerifyVoteMethodVersions { + verify_masternode_vote: 0, + verify_vote_poll_votes_proof: 0, + verify_identity_votes_given_proof: 0, + verify_vote_poll_vote_state_proof: 0, + verify_contests_proof: 0, + verify_vote_polls_by_end_date_proof: 0, + verify_specialized_balance: 0, + }, + address_funds: DriveVerifyAddressFundsMethodVersions { + verify_address_info: 0, + verify_addresses_infos: 0, + verify_address_funds_trunk_query: 0, + verify_address_funds_branch_query: 0, + verify_recent_address_balance_changes: 0, + verify_compacted_address_balance_changes: 1, + }, + state_transition: DriveVerifyStateTransitionMethodVersions { + verify_state_transition_was_executed_with_proof: 0, + }, + shielded: DriveVerifyShieldedMethodVersions { + verify_shielded_pool_state: 0, + verify_shielded_anchors: 0, + verify_most_recent_shielded_anchor: 0, + verify_shielded_encrypted_notes: 0, + verify_shielded_notes_count: 0, + verify_shielded_nullifiers: 0, + }, +} +fee_version: FeeVersion { + fee_version_number: 1, + uses_version_fee_multiplier_permille: Some( + 1000, + ), + storage: FeeStorageVersion { + storage_disk_usage_credit_per_byte: 27000, + storage_processing_credit_per_byte: 400, + storage_load_credit_per_byte: 20, + non_storage_load_credit_per_byte: 10, + storage_seek_cost: 2000, + ttl_ephemeral_disk_usage_credit_per_byte: 270, + }, + signature: FeeSignatureVersion { + verify_signature_ecdsa_secp256k1: 15000, + verify_signature_bls12_381: 300000, + verify_signature_ecdsa_hash160: 15500, + verify_signature_bip13_script_hash: 300000, + verify_signature_eddsa25519_hash160: 3500, + }, + hashing: FeeHashingVersion { + blake3_base: 100, + blake3_per_block: 300, + sha256_per_block: 5000, + sha256_ripe_md160_base: 6000, + single_sha256_base: 100, + ripemd160_per_block: 5000, + sinsemilla_base: 40000, + }, + processing: FeeProcessingVersion { + fetch_identity_balance_processing_cost: 10000, + fetch_identity_revision_processing_cost: 9000, + fetch_identity_balance_and_revision_processing_cost: 15000, + fetch_identity_cost_per_look_up_key_by_id: 9000, + fetch_identity_token_balance_processing_cost: 10000, + fetch_prefunded_specialized_balance_processing_cost: 10000, + fetch_key_with_type_nonce_and_balance_cost: 12000, + fetch_single_identity_key_processing_cost: 10000, + validate_key_structure: 50, + perform_network_threshold_signing: 100000000, + }, + data_contract_validation: FeeDataContractValidationVersion { + document_type_base_fee: 500, + document_type_schema_size_fee: 10, + document_type_per_property_fee: 40, + document_type_base_non_unique_index_fee: 50, + document_type_non_unique_index_per_property_fee: 30, + document_type_base_unique_index_fee: 100, + document_type_unique_index_per_property_fee: 60, + }, + data_contract_registration: FeeDataContractRegistrationVersion { + base_contract_registration_fee: 10000000000, + document_type_registration_fee: 2000000000, + document_type_base_non_unique_index_registration_fee: 1000000000, + document_type_base_unique_index_registration_fee: 1000000000, + document_type_base_contested_index_registration_fee: 100000000000, + token_registration_fee: 10000000000, + token_uses_perpetual_distribution_fee: 10000000000, + token_uses_pre_programmed_distribution_fee: 10000000000, + token_uses_once_per_identity_distribution_fee: 0, + search_keyword_fee: 10000000000, + }, + state_transition_min_fees: StateTransitionMinFees { + credit_transfer: 100000, + credit_transfer_to_addresses: 500000, + credit_withdrawal: 400000000, + identity_update: 100000, + document_batch_sub_transition: 100000, + contract_create: 100000, + contract_update: 100000, + masternode_vote: 100000, + address_credit_withdrawal: 400000000, + address_funds_transfer_input_cost: 500000, + address_funds_transfer_output_cost: 6000000, + identity_create_base_cost: 2000000, + identity_topup_base_cost: 500000, + identity_key_in_creation_cost: 6500000, + }, + vote_resolution_fund_fees: VoteResolutionFundFees { + contested_document_vote_resolution_fund_required_amount: 20000000000, + contested_document_vote_resolution_unlock_fund_required_amount: 400000000000, + contested_document_single_vote_cost: 10000000, + moderation_vote_resolution_fund_required_amount: 20000000000, + contested_document_contenders_before_fund_doubling: 0, + contested_document_contenders_per_fund_doubling: 0, + }, + document_ttl: FeeDocumentTtlVersion { + tiers: [ + DocumentTtlFeeTier { + max_ttl_seconds: 3600, + credit_per_byte: 1, + }, + DocumentTtlFeeTier { + max_ttl_seconds: 86400, + credit_per_byte: 4, + }, + DocumentTtlFeeTier { + max_ttl_seconds: 172800, + credit_per_byte: 8, + }, + DocumentTtlFeeTier { + max_ttl_seconds: 345600, + credit_per_byte: 15, + }, + DocumentTtlFeeTier { + max_ttl_seconds: 604800, + credit_per_byte: 26, + }, + ], + credit_per_byte_per_period: 34, + pricing_period_seconds: 788400, + cleanup_base_processing_cost: 1200000, + cleanup_processing_cost_per_index_level: 400000, + cleanup_processing_cost_per_document_byte: 420, + }, + document_reference: FeeDocumentReferenceVersion { + choose_first_credits_per_candidate: 100000, + where_list_credits_per_element: 100, + }, +} +system_data_contracts: SystemDataContractVersions { + withdrawals: 1, + dpns: 2, + dashpay: 1, + masternode_reward_shares: 1, + wallet: 1, + token_history: 1, + keyword_search: 1, + document_history: 1, + app_connect: 0, + moderation_charters: 0, +} +system_limits: SystemLimits { + credit_pool_nullifier_backfill_page_size: 0, + credit_pool_nullifier_backfill_batch_size: 0, + estimated_contract_max_serialized_size: 16384, + max_field_value_size: 5120, + max_document_value_depth: Some( + 256, + ), + max_typed_array_items: 1024, + max_references_per_document: 256, + max_reference_operands: 4, + max_reference_expression_depth: 4, + max_property_constraints: 16, + max_property_constraint_nodes: 32, + max_property_constraint_aggregates: 4, + max_state_transition_size: 20480, + max_transitions_in_documents_batch: 1, + withdrawal_transactions_per_block_limit: 4, + retry_signing_expired_withdrawal_documents_per_block_limit: 1, + max_withdrawal_amount: 50000000000000, + daily_withdrawal_limit_percent: None, + core_credit_pool_unlock_limit_percent: None, + core_credit_pool_unlock_limit_floor: None, + core_credit_pool_window_blocks: None, + regtest_core_credit_pool_window_blocks: None, + min_withdrawal_amount: 1000000, + core_dust_relay_fee_per_kb: None, + max_core_fee_per_byte: None, + max_group_member_count: 256, + max_contract_group_memberships_per_contract: 16, + max_contract_group_admins: 16, + max_contract_group_name_length: 64, + max_contract_group_description_length: 256, + max_contract_moderators: 16, + max_contract_suspension_until: 9007199254740991, + max_contract_moderation_reason_length: 1024, + max_contract_warnings_per_identity: 16, + max_contract_moderation_reason_documents: 16, + min_mainnet_contract_moderation_election_window_seconds: 86400, + max_contract_moderation_election_window_seconds: 2419200, + min_contract_moderation_challenge_cool_down_seconds: 1209600, + max_contract_moderation_challenge_cool_down_seconds: 94608000, + contract_document_restore_window_ms: 604800000, + max_contract_moderation_added_moderators: 15, + max_moderation_charter_elected_members: 15, + max_contenders_per_contest: 1000, + max_token_redemption_cycles: 128, + max_evonode_reward_claim_epochs: 100, + max_shielded_transition_actions: 16, + max_token_pool_notes_for_outgoing: 250, + max_time_range_overlap_factor: None, + max_time_range_ttl_seconds: None, + min_time_range_ttl_drop_operations_per_write: None, + min_document_ttl_seconds: None, + max_document_ttl_seconds: None, + max_document_expirations_per_block: 0, + max_document_expiration_weight_per_block: 0, + max_contested_summed_value_magnitude: None, + max_expiring_signed_summed_value_magnitude: None, + max_indexed_array_items: None, + max_windowed_indexed_array_items: None, + max_index_entries_per_document: None, + max_choose_first_candidates: 1000, + max_rekey_batch_moves: None, +} diff --git a/src/context/platform_version_guard.rs b/src/context/platform_version_guard.rs new file mode 100644 index 000000000..d93d8af00 --- /dev/null +++ b/src/context/platform_version_guard.rs @@ -0,0 +1,59 @@ +//! Guards the protocol version 13 tables DET builds, validates and verifies +//! with on mainnet and testnet against drift across platform pin bumps. +//! +//! Protocol 13 is live, so table changes require compatibility review. The snapshot +//! covers what the client side reads: serialization and validation (`dpp`), +//! proof verification (`drive.methods.verify`), fees, system contract versions +//! and system limits. On a mismatch, diff the fixture, confirm the change is +//! additive (new fields at 0/None) or otherwise safe for a protocol 13 network, +//! then regenerate with `DET_BLESS_PLATFORM_VERSION_SNAPSHOT=1`. +//! +//! Platform #5294 enforces GroveDB proof envelope V1 outside these tables, even +//! for protocol 13. The snapshot does not establish wire compatibility: #5361 +//! removed the legacy discovery response field 2, still served by sampled +//! mainnet nodes, and #5515 brings it back as the answer to a version 0 lookup +//! request, checked by `verify_full_identity_by_non_unique_public_key_hash_two_proofs`. +//! Only a live check against those nodes shows the SDK accepts their proofs. + +use dash_sdk::dpp::dashcore::Network; +use dash_sdk::dpp::version::PlatformVersion; +use dash_sdk::sdk::min_protocol_version; + +const SNAPSHOT_PATH: &str = "src/context/fixtures/platform_v13_client_tables.txt"; +const SNAPSHOT: &str = include_str!("fixtures/platform_v13_client_tables.txt"); + +fn client_tables(platform_version: &PlatformVersion) -> String { + format!( + "protocol_version: {}\ndpp: {:#?}\ndrive.methods.verify: {:#?}\nfee_version: {:#?}\nsystem_data_contracts: {:#?}\nsystem_limits: {:#?}\n", + platform_version.protocol_version, + platform_version.dpp, + platform_version.drive.methods.verify, + platform_version.fee_version, + platform_version.system_data_contracts, + platform_version.system_limits, + ) +} + +#[test] +fn protocol_13_client_tables_match_the_snapshot() { + let actual = client_tables(PlatformVersion::get(13).expect("protocol 13")); + if std::env::var_os("DET_BLESS_PLATFORM_VERSION_SNAPSHOT").is_some() { + let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join(SNAPSHOT_PATH); + std::fs::write(&path, &actual).expect("write snapshot"); + return; + } + assert!( + actual == SNAPSHOT, + "The protocol 13 tables changed with the platform pin. Diff {SNAPSHOT_PATH} (rerun with \ + DET_BLESS_PLATFORM_VERSION_SNAPSHOT=1 to rewrite it), check the change is safe for \ + mainnet, and commit the new snapshot." + ); +} + +/// Mainnet and testnet run protocol 13, and DET seeds its SDK from upstream's +/// per-network minimum: the snapshot above is the table DET starts with there. +#[test] +fn mainnet_and_testnet_start_at_protocol_13() { + assert_eq!(min_protocol_version(Network::Mainnet), 13); + assert_eq!(min_protocol_version(Network::Testnet), 13); +} From 6a4e6e75d43cde8a35438285322e5900f2dc67c1 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Sat, 24 Oct 2026 18:21:52 +0000 Subject: [PATCH 06/27] fix(errors): explain protocol 14 refusals that existing actions can hit Map the refusals the new protocol adds to flows DET already offers to dedicated TaskError variants: a full username contest, a vote choice the contest does not allow, a name someone else asked for first, a token that cannot be moved, a changed immutable document field, a reference to a document type that cannot be deleted, and a moderators' fee on a contract without moderation. Names supplied by the node stay in the typed source only, never in the message. Token operations that fail inside the wallet runtime keep their SDK cause, so they are classified like any SDK error; an unclassified one names the operation that did not complete. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- src/backend_task/error.rs | 590 +++++++++++++++++++++++++++++++++++++- 1 file changed, 586 insertions(+), 4 deletions(-) diff --git a/src/backend_task/error.rs b/src/backend_task/error.rs index fb6cb61a6..42006aefc 100644 --- a/src/backend_task/error.rs +++ b/src/backend_task/error.rs @@ -43,6 +43,108 @@ impl DapiAddressAvailability { } } +/// The token operation a wallet-runtime token failure belongs to. +/// +/// Upstream `PlatformWalletError::TokenOperationFailed` names the operation with +/// a fixed `&'static str` label (a discriminator, not a message); this is its +/// typed form, so each failure gets a complete, translatable sentence. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum TokenOperationKind { + Claim, + Mint, + Burn, + Transfer, + Freeze, + Unfreeze, + DestroyFrozenFunds, + Pause, + Resume, + SetPrice, + Purchase, + ConfigUpdate, + /// A label this build does not know (a newer upstream operation). The + /// raw label is kept for `Debug` and logs, never shown to the user. + Unrecognized { + label: &'static str, + }, +} + +impl TokenOperationKind { + /// The kind for the operation label upstream puts in + /// `PlatformWalletError::TokenOperationFailed::operation`. + /// + /// Upstream exposes the operation only as this `&'static str` (no typed + /// enum), so every label it uses is matched here; an unknown one is + /// logged at warn and kept as [`Self::Unrecognized`], never silently + /// folded into another operation. + pub fn from_upstream_label(label: &'static str) -> Self { + match label { + "claim" => Self::Claim, + "mint" => Self::Mint, + "burn" => Self::Burn, + "transfer" => Self::Transfer, + "freeze" => Self::Freeze, + "unfreeze" => Self::Unfreeze, + "destroy frozen funds" => Self::DestroyFrozenFunds, + "pause" => Self::Pause, + "resume" => Self::Resume, + "set price" => Self::SetPrice, + "purchase" => Self::Purchase, + "config update" => Self::ConfigUpdate, + label => { + tracing::warn!( + label, + "Unrecognized upstream token operation label; showing a generic message" + ); + Self::Unrecognized { label } + } + } + } + + /// The user-facing sentence for a failure of this operation. + fn failure_message(self) -> &'static str { + match self { + Self::Claim => "The token claim did not complete. Check your connection and try again.", + Self::Mint => { + "Minting the tokens did not complete. Check your connection and try again." + } + Self::Burn => { + "Burning the tokens did not complete. Check your connection and try again." + } + Self::Transfer => { + "The token transfer did not complete. Check your connection and try again." + } + Self::Freeze => { + "Freezing the token account did not complete. Check your connection and try again." + } + Self::Unfreeze => { + "Unfreezing the token account did not complete. Check your connection and try again." + } + Self::DestroyFrozenFunds => { + "Destroying the frozen tokens did not complete. Check your connection and try again." + } + Self::Pause => { + "Pausing the token did not complete. Check your connection and try again." + } + Self::Resume => { + "Resuming the token did not complete. Check your connection and try again." + } + Self::SetPrice => { + "Setting the token price did not complete. Check your connection and try again." + } + Self::Purchase => { + "The token purchase did not complete. Check your connection and try again." + } + Self::ConfigUpdate => { + "Updating the token settings did not complete. Check your connection and try again." + } + Self::Unrecognized { .. } => { + "The token operation did not complete. Check your connection and try again." + } + } + } +} + /// Why an existing DashPay `contactInfo` payload could not be preserved. #[derive(Debug, Error)] pub enum ContactInfoReadError { @@ -2396,13 +2498,113 @@ pub enum TaskError { source_error: Box, }, + // ────────────────────────────────────────────────────────────────────────── + // Username, vote and token transfer refusals (protocol versions 14 and 15) + // ────────────────────────────────────────────────────────────────────────── + /// The contest for the requested name already holds the most contenders it + /// accepts. + #[error( + "Too many people have already asked for this username, so no one else can join. Choose a different username." + )] + DocumentContestContendersLimitReached { + max_contenders: u16, + #[source] + source_error: Box, + }, + + /// The token cannot be moved to another identity, so a transfer, or a + /// payment that moves it, is refused. + #[error( + "Token {} cannot be moved to another identity, so this action is not possible. Use a different token.", + token_id.to_string(Encoding::Base58) + )] + TokenNotTransferable { + token_id: Identifier, + #[source] + source_error: Box, + }, + + /// The vote choice is not allowed for this contest: a lock on a name that + /// cannot be locked, or a vote towards an identity that is not competing. + #[error( + "This vote is not allowed for this username. Vote for one of the identities that asked for it, or choose another option, then vote again." + )] + VoteChoiceNotAllowed { + #[source] + source_error: Box, + }, + + /// Another identity committed to the same name earlier, so its request + /// comes first. + #[error("Someone else asked for this username first. Choose a different username.")] + ReferencedDocumentPreceded { + document_id: Identifier, + preceding_document_id: Identifier, + #[source] + source_error: Box, + }, + + // ────────────────────────────────────────────────────────────────────────── + // Contract authoring (protocol version 14) + // ────────────────────────────────────────────────────────────────────────── + /// A replace changed a property the document type declares immutable. + #[error( + "One of the fields you changed cannot be changed after the document is created. Keep the original values of its fixed fields, then try again." + )] + DocumentImmutablePropertyChanged { + // The node's names for the document type and the property stay in + // `source_error` only: unproven text, never copied into the variant. + document_id: Identifier, + #[source] + source_error: Box, + }, + + /// A `deletableDocument` reference points at a document type whose + /// documents cannot be deleted. + #[error( + "This contract refers to documents that can never be deleted as if they could be. Make that reference a permanent document reference, then try again." + )] + ReferencedDocumentTypeNotDeletable { + // The node's names for the document type and the reference path stay + // in `source_error` only: unproven text, never copied into the variant. + contract_id: Identifier, + #[source] + source_error: Box, + }, + + /// A document type charges moderators a fee the contract has no + /// moderation for. + #[error( + "A document type in this contract charges a fee for moderators, but the contract has no moderation. Remove the moderators' part of the fee, then try again." + )] + DocumentActionFeesWithoutModeration { + // The node's name for the document type stays in `source_error` only: + // unproven text, never copied into the variant. + #[source] + source_error: Box, + }, + // ────────────────────────────────────────────────────────────────────────── // Token query errors // ────────────────────────────────────────────────────────────────────────── + #[error("Shielded token settings cannot be changed here yet. Use a supported token setting instead.")] + TokenShieldedConfigurationUnsupported, + /// Querying token data from the platform failed. #[error("Could not retrieve token information from the platform. Please retry.")] TokenQueryError { detail: String }, + /// A token operation run by the wallet runtime failed for a reason the + /// SDK error classification has no specific variant for. Built by + /// [`TaskError::from_token_operation_failure`], which prefers the specific + /// variant whenever there is one. + #[error("{}", operation.failure_message())] + TokenOperationFailed { + operation: TokenOperationKind, + #[source] + source_error: Box, + }, + /// The token does not have a perpetual distribution configured — no rewards to claim. #[error("This token does not have perpetual distribution, so there are no rewards to claim.")] TokenNoPerpetualDistribution, @@ -3828,6 +4030,26 @@ impl From for TaskError { } } +impl TaskError { + /// The error for a token operation the wallet runtime reported as failed + /// (`PlatformWalletError::TokenOperationFailed`). + /// + /// The SDK source is classified exactly like any other SDK error, so a + /// rejection with a specific meaning (insufficient balance, a claim already + /// taken, …) keeps its specific message. Only an otherwise unclassified + /// failure becomes [`TaskError::TokenOperationFailed`], which at least names + /// the operation. + pub fn from_token_operation_failure(operation: &'static str, source: SdkError) -> Self { + match TaskError::from(source) { + TaskError::SdkError { source_error } => TaskError::TokenOperationFailed { + operation: TokenOperationKind::from_upstream_label(operation), + source_error, + }, + classified => classified, + } + } +} + /// Why the DET identity signer could not resolve a signing key. /// /// The upstream `Signer` trait can only return `ProtocolError`, which has no @@ -4138,6 +4360,69 @@ impl From for TaskError { source_error, })) } + ConsensusError::StateError( + StateError::DocumentContestMaximumContendersReachedError(e), + ) => { + let max_contenders = e.max_contenders(); + Some(Box::new(move |source_error| { + TaskError::DocumentContestContendersLimitReached { + max_contenders, + source_error, + } + })) + } + ConsensusError::StateError(StateError::TokenNotTransferableError(e)) => { + let token_id = *e.token_id(); + Some(Box::new(move |source_error| { + TaskError::TokenNotTransferable { + token_id, + source_error, + } + })) + } + ConsensusError::StateError( + StateError::VoteChoiceNotAllowedForVotePollError(_), + ) => Some(Box::new(|source_error| TaskError::VoteChoiceNotAllowed { + source_error, + })), + ConsensusError::StateError(StateError::ReferencedDocumentPrecededError(e)) => { + let (document_id, preceding_document_id) = + (*e.document_id(), *e.preceding_document_id()); + Some(Box::new(move |source_error| { + TaskError::ReferencedDocumentPreceded { + document_id, + preceding_document_id, + source_error, + } + })) + } + ConsensusError::StateError( + StateError::DocumentImmutablePropertyChangedError(e), + ) => { + let document_id = e.document_id(); + Some(Box::new(move |source_error| { + TaskError::DocumentImmutablePropertyChanged { + document_id, + source_error, + } + })) + } + ConsensusError::StateError( + StateError::ReferencedDocumentTypeNotDeletableError(e), + ) => { + let contract_id = *e.contract_id(); + Some(Box::new(move |source_error| { + TaskError::ReferencedDocumentTypeNotDeletable { + contract_id, + source_error, + } + })) + } + ConsensusError::BasicError( + BasicError::DocumentActionFeesWithoutModerationError(_), + ) => Some(Box::new(|source_error| { + TaskError::DocumentActionFeesWithoutModeration { source_error } + })), _ => None, } }); @@ -4301,10 +4586,12 @@ mod tests { InvalidTokenNameCharacterError, InvalidTokenNameLengthError, }; use dash_sdk::dpp::consensus::basic::identity::InvalidInstantAssetLockProofSignatureError; - use dash_sdk::dpp::consensus::state::identity::duplicated_identity_public_key_id_state_error::DuplicatedIdentityPublicKeyIdStateError; - use dash_sdk::dpp::consensus::state::identity::duplicated_identity_public_key_state_error::DuplicatedIdentityPublicKeyStateError; - use dash_sdk::dpp::consensus::state::identity::IdentityInsufficientBalanceError; - use dash_sdk::dpp::consensus::state::identity::identity_public_key_already_exists_for_unique_contract_bounds_error::IdentityPublicKeyAlreadyExistsForUniqueContractBoundsError; + use dash_sdk::dpp::consensus::state::identity::{ + IdentityInsufficientBalanceError, + duplicated_identity_public_key_id_state_error::DuplicatedIdentityPublicKeyIdStateError, + duplicated_identity_public_key_state_error::DuplicatedIdentityPublicKeyStateError, + identity_public_key_already_exists_for_unique_contract_bounds_error::IdentityPublicKeyAlreadyExistsForUniqueContractBoundsError, + }; use dash_sdk::dpp::consensus::state::voting::masternode_voted_too_many_times::MasternodeVotedTooManyTimesError; use dash_sdk::dpp::identity::Purpose; use dash_sdk::platform::Identifier; @@ -6547,6 +6834,301 @@ mod tests { ); } + fn broadcast_rejection(cause: ConsensusError) -> SdkError { + SdkError::StateTransitionBroadcastError(dash_sdk::error::StateTransitionBroadcastError { + code: 1, + message: "rejected".to_string(), + cause: Some(cause), + }) + } + + /// The protocol-version-14 refusals of contract and document authoring map + /// to their own variants, keep the SDK error as their source, and read as + /// complete sentences free of protocol jargon. + #[test] + fn protocol_v14_consensus_errors_map_to_their_variants() { + use dash_sdk::dpp::consensus::basic::contract_moderation::DocumentActionFeesWithoutModerationError; + use dash_sdk::dpp::consensus::state::document::{ + document_immutable_property_changed_error::DocumentImmutablePropertyChangedError, + referenced_document_type_not_deletable_error::ReferencedDocumentTypeNotDeletableError, + }; + + let contract_id = Identifier::random(); + type Expectation = fn(&TaskError) -> bool; + let cases: Vec<(ConsensusError, Expectation)> = vec![ + ( + DocumentImmutablePropertyChangedError::new( + Identifier::random(), + "note".to_string(), + "title".to_string(), + ) + .into(), + |e| matches!(e, TaskError::DocumentImmutablePropertyChanged { .. }), + ), + ( + ReferencedDocumentTypeNotDeletableError::new( + contract_id, + "note".to_string(), + "properties.ref".to_string(), + ) + .into(), + |e| matches!(e, TaskError::ReferencedDocumentTypeNotDeletable { .. }), + ), + ( + DocumentActionFeesWithoutModerationError::new("note".to_string()).into(), + |e| matches!(e, TaskError::DocumentActionFeesWithoutModeration { .. }), + ), + ]; + + for (cause, expected) in cases { + let debug = format!("{cause:?}"); + let error = TaskError::from(broadcast_rejection(cause)); + assert!(expected(&error), "{debug} mapped to {error:?}"); + assert!( + std::error::Error::source(&error).is_some(), + "{error:?} must keep the SDK error as its source" + ); + let message = error.to_string(); + assert!(message.ends_with('.'), "not a sentence: {message}"); + for jargon in ["consensus", "state transition", "nonce", "SDK", "permille"] { + assert!( + !message.contains(jargon), + "{message} contains jargon {jargon}" + ); + } + } + } + + /// The refusals protocol versions 14 and 15 add on username, vote and + /// token flows map to their own variants, keep the SDK error as their + /// source and tell the user what to do instead. + #[test] + fn contest_vote_and_token_transfer_refusals_map_to_their_variants() { + use dash_sdk::dpp::consensus::state::document::document_contest_maximum_contenders_reached_error::DocumentContestMaximumContendersReachedError; + use dash_sdk::dpp::consensus::state::document::referenced_document_preceded_error::ReferencedDocumentPrecededError; + use dash_sdk::dpp::consensus::state::token::TokenNotTransferableError; + use dash_sdk::dpp::consensus::state::voting::vote_choice_not_allowed_for_vote_poll_error::VoteChoiceNotAllowedForVotePollError; + use dash_sdk::dpp::voting::vote_choices::resource_vote_choice::ResourceVoteChoice; + use dash_sdk::dpp::voting::vote_polls::VotePoll; + use dash_sdk::dpp::voting::vote_polls::contested_document_resource_vote_poll::ContestedDocumentResourceVotePoll; + + type Expectation = fn(&TaskError) -> bool; + let token_id = Identifier::random(); + let poll = ContestedDocumentResourceVotePoll { + contract_id: Identifier::random(), + document_type_name: "domain".to_string(), + index_name: "parentNameAndLabel".to_string(), + index_values: vec![ + dash_sdk::dpp::platform_value::Value::from("dash"), + dash_sdk::dpp::platform_value::Value::from("alice"), + ], + }; + let cases: Vec<(ConsensusError, Expectation, &str)> = vec![ + ( + DocumentContestMaximumContendersReachedError::new(poll.clone(), 1000).into(), + |e| { + matches!( + e, + TaskError::DocumentContestContendersLimitReached { + max_contenders: 1000, + .. + } + ) + }, + "Choose a different username.", + ), + ( + TokenNotTransferableError::new(token_id, "transfer".to_string()).into(), + |e| matches!(e, TaskError::TokenNotTransferable { .. }), + "Use a different token.", + ), + ( + VoteChoiceNotAllowedForVotePollError::new( + VotePoll::ContestedDocumentResourceVotePoll(poll), + ResourceVoteChoice::Lock, + ) + .into(), + |e| matches!(e, TaskError::VoteChoiceNotAllowed { .. }), + "then vote again.", + ), + ( + ReferencedDocumentPrecededError::new( + Identifier::random(), + Identifier::random(), + "normalizedLabel".to_string(), + "preorder".to_string(), + ) + .into(), + |e| matches!(e, TaskError::ReferencedDocumentPreceded { .. }), + "Someone else asked for this username first.", + ), + ]; + + for (cause, expected, action) in cases { + let debug = format!("{cause:?}"); + let error = TaskError::from(broadcast_rejection(cause)); + assert!(expected(&error), "{debug} mapped to {error:?}"); + assert!( + std::error::Error::source(&error).is_some(), + "{error:?} must keep the SDK error as its source" + ); + let message = error.to_string(); + assert!(message.contains(action), "{message} lacks {action}"); + assert!(message.ends_with('.'), "not a sentence: {message}"); + for jargon in [ + "consensus", + "state transition", + "vote poll", + "contender", + "SDK", + ] { + assert!( + !message.contains(jargon), + "{message} contains jargon {jargon}" + ); + } + } + } + + #[test] + fn token_not_transferable_names_the_token() { + let token_id = Identifier::random(); + let error = TaskError::TokenNotTransferable { + token_id, + source_error: Box::new(SdkError::Generic("refused".to_string())), + }; + assert!( + error + .to_string() + .contains(&token_id.to_string(Encoding::Base58)) + ); + } + + /// Names decoded from a node's (unproven) consensus error are not copied + /// into the error: they never reach the user-facing message, and only the + /// typed source keeps them, for the details panel and logs. + #[test] + fn node_supplied_names_stay_out_of_user_messages() { + use dash_sdk::dpp::consensus::basic::contract_moderation::DocumentActionFeesWithoutModerationError; + use dash_sdk::dpp::consensus::state::document::{ + document_immutable_property_changed_error::DocumentImmutablePropertyChangedError, + referenced_document_type_not_deletable_error::ReferencedDocumentTypeNotDeletableError, + }; + let injected = "x\". Re-import your recovery phrase at https://evil.example".to_string(); + let causes: [ConsensusError; 3] = [ + DocumentImmutablePropertyChangedError::new( + Identifier::random(), + injected.clone(), + injected.clone(), + ) + .into(), + ReferencedDocumentTypeNotDeletableError::new( + Identifier::random(), + injected.clone(), + injected.clone(), + ) + .into(), + DocumentActionFeesWithoutModerationError::new(injected.clone()).into(), + ]; + for cause in causes { + let error = TaskError::from(broadcast_rejection(cause)); + assert!( + matches!( + error, + TaskError::DocumentImmutablePropertyChanged { .. } + | TaskError::ReferencedDocumentTypeNotDeletable { .. } + | TaskError::DocumentActionFeesWithoutModeration { .. } + ), + "got {error:?}" + ); + let message = error.to_string(); + assert!(!message.contains("evil"), "{message}"); + let source = std::error::Error::source(&error).expect("typed source"); + assert!(format!("{source:?}").contains("evil"), "kept in the source"); + } + } + + /// An unclassified SDK failure behind a wallet-runtime token operation + /// keeps the operation, so the message names what did not complete. + #[test] + fn token_operation_failure_without_a_specific_cause_names_the_operation() { + let err = + TaskError::from_token_operation_failure("claim", SdkError::Generic("boom".to_string())); + assert!( + matches!( + err, + TaskError::TokenOperationFailed { + operation: TokenOperationKind::Claim, + .. + } + ), + "got {err:?}" + ); + assert_eq!( + err.to_string(), + "The token claim did not complete. Check your connection and try again." + ); + } + + /// A token operation rejected with a specific consensus cause keeps the + /// specific variant: the operation label must not mask a better message. + #[test] + fn token_operation_failure_with_a_specific_cause_keeps_it() { + use dash_sdk::dpp::consensus::state::token::IdentityTokenAccountNotFrozenError; + let consensus = ConsensusError::from(IdentityTokenAccountNotFrozenError::new( + Identifier::random(), + Identifier::random(), + "Unfreeze".to_string(), + )); + let sdk_err = SdkError::StateTransitionBroadcastError( + dash_sdk::error::StateTransitionBroadcastError { + code: 40703, + message: "identity token account is not frozen".to_string(), + cause: Some(consensus), + }, + ); + let err = TaskError::from_token_operation_failure("unfreeze", sdk_err); + assert!( + matches!(err, TaskError::TokenAccountNotFrozen { .. }), + "got {err:?}" + ); + } + + /// Every label upstream emits today maps to its own kind; an unknown one + /// degrades to the generic kind instead of failing. + #[test] + fn token_operation_labels_map_to_kinds() { + for (label, kind) in [ + ("claim", TokenOperationKind::Claim), + ("mint", TokenOperationKind::Mint), + ("burn", TokenOperationKind::Burn), + ("transfer", TokenOperationKind::Transfer), + ("freeze", TokenOperationKind::Freeze), + ("unfreeze", TokenOperationKind::Unfreeze), + ( + "destroy frozen funds", + TokenOperationKind::DestroyFrozenFunds, + ), + ("pause", TokenOperationKind::Pause), + ("resume", TokenOperationKind::Resume), + ("set price", TokenOperationKind::SetPrice), + ("purchase", TokenOperationKind::Purchase), + ("config update", TokenOperationKind::ConfigUpdate), + ( + "something new", + TokenOperationKind::Unrecognized { + label: "something new", + }, + ), + ] { + assert_eq!( + TokenOperationKind::from_upstream_label(label), + kind, + "{label}" + ); + } + } + #[test] fn test_identity_token_account_not_frozen_from_consensus_error() { use dash_sdk::dpp::consensus::state::token::IdentityTokenAccountNotFrozenError; From d75a1591ac8af704e9ea2d9ae3a43d8b0d215ba2 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Sat, 24 Oct 2026 18:21:52 +0000 Subject: [PATCH 07/27] docs: record the Platform v5.1-dev bump and protocol tracking Add a dated design note for the pin, the protocol version sources, the connection state rules and the protocol 13 guards, and point AGENTS.md at From for TaskError as the SDK error reference. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- AGENTS.md | 2 +- .../README.md | 67 +++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 docs/ai-design/2026-10-24-platform-v5.1-dev-bump/README.md diff --git a/AGENTS.md b/AGENTS.md index 07f0babe7..207af94d8 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -128,7 +128,7 @@ User-facing error messages (shown in `MessageBanner` via `Display`) must follow 2. **Structure**: *What happened* + *what to do*. Every message must include a concrete action the user can take themselves: retry, wait, try a different approach. Never redirect to "contact support" — users must be able to self-resolve. 3. **Tone**: Calm, direct, brief. Not apologetic ("Sorry!"), not alarming ("Something went wrong!"), not vague ("An error occurred"). 4. **Technical details**: Never in the message itself — no raw error strings, stack traces, SDK internals, or error codes. Attach via `BannerHandle::with_details(e)` — the `Debug` repr goes to the collapsible details panel and logs. Never refer users to "details" or "details panel" — these are not visible in basic mode. Exception: Base58 identifiers (see rule 6) are not technical details — they are user-meaningful handles. -5. **Reference implementation**: `sdk_error_user_message()` in `src/backend_task/error.rs` demonstrates the pattern for SDK errors. New `TaskError` variants should follow the same style. +5. **Reference implementation**: `impl From for TaskError` in `src/backend_task/error.rs` demonstrates the pattern for SDK errors. New `TaskError` variants should follow the same style. 6. **Base58 IDs are allowed in messages**: Contract IDs, identity IDs, document IDs, and similar Base58-encoded identifiers may appear in user-facing messages when they help the user identify which object is involved (e.g., *"This key conflicts with an existing key bound to contract `Abc123…`."*). They are not jargon — they are opaque-but-copyable handles the user can look up. 7. **Use dedicated `TaskError` variants**: Every error should get a dedicated `TaskError` variant with a `#[source]` field that preserves the error chain, enables structural matching, and keeps `Display` / `Debug` separation explicit. For `#[source]` fields in SDK-originated error variants, use `Box` — convert upstream types (e.g. `ProtocolError`) via `SdkError::Protocol(e)`. Use the concrete domain type directly for non-SDK errors (e.g. `rusqlite::Error`). Omit `#[source]` entirely when the upstream error carries no useful diagnostic information (e.g. a channel `SendError`). **Never store user-facing strings in error variants** — error variants must not contain `String` fields that hold messages for the user. The `#[error("...")]` attribute on the variant provides the user-facing message; `String` fields (regardless of name) break this separation. Instead, create a dedicated variant with typed `#[source]` fields, or a fieldless variant if no upstream error exists. When encountering existing variants that store stringified errors, replace them with properly typed variants as part of the change. diff --git a/docs/ai-design/2026-10-24-platform-v5.1-dev-bump/README.md b/docs/ai-design/2026-10-24-platform-v5.1-dev-bump/README.md new file mode 100644 index 000000000..fe5b8bb3a --- /dev/null +++ b/docs/ai-design/2026-10-24-platform-v5.1-dev-bump/README.md @@ -0,0 +1,67 @@ +# Platform v5.1-dev pin and protocol version tracking + +A point-in-time record of the update to Platform `v5.1-dev` +(`063589bf0adbe1a8823a943cee95385b423b0e3f`, 5.0.0-rc.8). The update keeps +today's behaviour on protocol 13 and protocol 14 networks and adds no new +user-facing feature. Protocol 14 features build on it in separate changes. +It supersedes the open "Devnet protocol seed" item of +[`2026-09-14-platform-4.2-dev-bump`](../2026-09-14-platform-4.2-dev-bump/upgrade-notes.md). + +## Which protocol version DET builds for + +There are two version sources, and they can disagree while a network moves +from 13 to 14: + +- **The SDK's version** (`AppContext::platform_version()`). It starts at + upstream's per-network `dash_sdk::sdk::min_protocol_version` (13 on mainnet, + testnet and regtest, 14 on devnet) and ratchets to what verified responses + report. Transitions are encoded at this version. The SDK is no longer pinned + to a fixed table: a pinned SDK cannot build any protocol 14 transition, and + cannot follow the versioned identity lookup below. +- **The network's confirmed version** (`AppContext::platform_protocol_version()`). + It comes from a proved epoch read (or, when that fails, a proved DPNS fetch). + It is `0` until confirmed. Capabilities (`context/feature_gate.rs`) read only + this one, through `known_protocol_version`, so an unconfirmed network, or a + version this build does not define, never unlocks a protocol feature. + `connected_platform_version()` selects that version's tables for estimates, + such as the daily withdrawal limit. + +## System contracts + +`SystemContractsCache` serves the system contracts (DPNS, DashPay, withdrawals, +token history, keyword search) at the SDK's version and reloads them when it +changes. The context provider serves them at the version the proof verifier +asks for (`verifier_version`), which can be newer than the SDK's. `try_get` +fails rather than serving another version's contracts, so a proof is never +checked against the wrong schema. + +## Connection state + +- **Epoch adoption.** A proved epoch sets the fee multiplier and raises the + protocol version (`AppContext::adopt_epoch`). Reads can finish out of order, + so an epoch older than the adopted one is ignored, and the protocol version + only moves up. +- **Rebuilding the connection** (`replace_connection`, `rebuild_connection`) + bumps a generation counter, forgets the adopted epoch, and puts the protocol + version back to unknown and the fee multiplier to the default, under one + lock, before the new SDK is published. A read begun on the old connection is + dropped. Capabilities stay closed until the new connection proves its epoch. + +## Protocol 13 guards + +- `context/platform_version_guard.rs` compares the protocol 13 client tables + with a recorded snapshot, so an upstream change to them is reviewed. +- `backend_task/protocol_13_transitions.rs` checks that transitions DET builds + on a protocol 13 network use encodings protocol 13 decodes. + +## Mainnet compatibility + +dashpay/platform#5515 versions the non-unique identity lookup: protocol 13 +nodes answer the V0 request with the legacy two-proof response, and the SDK +verifies it. Proof verification stays strict; nothing skips or weakens it. + +## Existing data + +The upstream V019 wallet-storage migration adds an asset-lock operation id. +V001–V018 are unchanged. The upgrade is forward only: going back to an older +build needs a copy of the data directory taken before the upgrade. From 74695e7d4f0eefc95126d220afe1a5a2f0b5c4fc Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Sat, 24 Oct 2026 19:36:54 +0000 Subject: [PATCH 08/27] fix(errors): explain moderation, gas sponsorship and distribution limit refusals Protocol version 14 refusals that flows already in DET can hit get dedicated, actionable messages, each keeping the SDK error as its source: - an identity banned or suspended (until when) on a moderated contract; - a gas sponsor short of credits, a gas payer the document type does not offer, and mixed gas payers in one batch; - pre-programmed token distributions of one time over the limit. An overflowing shielded note total is logged at warn: it means corrupt or hostile sync data, not routine noise. Tests cover contracts saved at protocol 12 loading unchanged at 13 and 14, and contracts saved at 14 decoding at 13 and in full again at 14. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- src/backend_task/error.rs | 239 ++++++++++++++++++++++++++++- src/context/contract_token_db.rs | 154 +++++++++++++++++++ src/wallet_backend/event_bridge.rs | 4 +- 3 files changed, 395 insertions(+), 2 deletions(-) diff --git a/src/backend_task/error.rs b/src/backend_task/error.rs index 42006aefc..86c4bd4e4 100644 --- a/src/backend_task/error.rs +++ b/src/backend_task/error.rs @@ -17,6 +17,7 @@ use dash_sdk::dpp::consensus::basic::basic_error::BasicError; use dash_sdk::dpp::consensus::state::state_error::StateError; use dash_sdk::dpp::dashcore; use dash_sdk::dpp::dashcore::Network; +use dash_sdk::dpp::fee::Credits; use dash_sdk::dpp::platform_value::string_encoding::Encoding; use dash_sdk::platform::Identifier; use platform_wallet::wallet::asset_lock::tracked::TrackedAssetLock; @@ -2498,6 +2499,66 @@ pub enum TaskError { source_error: Box, }, + // ────────────────────────────────────────────────────────────────────────── + // Document action fees, moderation and gas sponsorship (protocol version 14) + // ────────────────────────────────────────────────────────────────────────── + /// The identity is banned on the contract. + #[error( + "This identity is banned from contract {} and cannot act on its documents. Use a different identity for this contract.", + contract_id.to_string(Encoding::Base58) + )] + ContractUserBanned { + contract_id: Identifier, + identity_id: Identifier, + #[source] + source_error: Box, + }, + + /// The identity is suspended on the contract until a given time. + #[error( + "This identity is suspended from contract {} until {}. Wait until then, or use a different identity for this contract.", + contract_id.to_string(Encoding::Base58), + crate::model::datetime::local_date_time_from_unix_millis(*until_ms) + )] + ContractUserSuspended { + contract_id: Identifier, + identity_id: Identifier, + /// End of the suspension, in milliseconds since the Unix epoch. + until_ms: u64, + #[source] + source_error: Box, + }, + + /// The contract owner sponsoring the gas cannot cover it. + #[error( + "The contract owner pays the network fee for this action but does not have enough credits for it right now. Try again later." + )] + GasSponsorInsufficientBalance { + sponsor_id: Identifier, + balance: Credits, + required_balance: Credits, + #[source] + source_error: Box, + }, + + /// The transition asked for a gas payer the document type does not offer. + #[error( + "This document type does not let that party pay the network fee for this action. Pay the fee as the document type allows, then try again." + )] + GasFeesPaidByNotAllowed { + #[source] + source_error: Box, + }, + + /// The transitions of one batch named different gas payers. + #[error( + "The actions in this request name different payers for the network fee. Send them one at a time, then try again." + )] + InconsistentGasFeesPayerInBatch { + #[source] + source_error: Box, + }, + // ────────────────────────────────────────────────────────────────────────── // Username, vote and token transfer refusals (protocol versions 14 and 15) // ────────────────────────────────────────────────────────────────────────── @@ -2584,6 +2645,20 @@ pub enum TaskError { source_error: Box, }, + /// The pre-programmed distributions of one time add up to more than the + /// maximum. + #[error( + "The scheduled token distributions at {} add up to more than the allowed maximum. Lower those amounts, then try again.", + crate::model::datetime::local_date_time_from_unix_millis(*timestamp_ms) + )] + PreProgrammedDistributionOverLimit { + token_position: u16, + /// The distribution time, in milliseconds since the Unix epoch. + timestamp_ms: u64, + #[source] + source_error: Box, + }, + // ────────────────────────────────────────────────────────────────────────── // Token query errors // ────────────────────────────────────────────────────────────────────────── @@ -4360,6 +4435,52 @@ impl From for TaskError { source_error, })) } + ConsensusError::StateError(StateError::ContractUserBannedError(e)) => { + let (contract_id, identity_id) = (e.contract_id(), e.identity_id()); + Some(Box::new(move |source_error| { + TaskError::ContractUserBanned { + contract_id, + identity_id, + source_error, + } + })) + } + ConsensusError::StateError(StateError::ContractUserSuspendedError(e)) => { + let (contract_id, identity_id, until_ms) = + (e.contract_id(), e.identity_id(), e.until()); + Some(Box::new(move |source_error| { + TaskError::ContractUserSuspended { + contract_id, + identity_id, + until_ms, + source_error, + } + })) + } + ConsensusError::StateError(StateError::GasSponsorInsufficientBalanceError( + e, + )) => { + let (sponsor_id, balance, required_balance) = + (*e.sponsor_id(), e.balance(), e.required_balance()); + Some(Box::new(move |source_error| { + TaskError::GasSponsorInsufficientBalance { + sponsor_id, + balance, + required_balance, + source_error, + } + })) + } + ConsensusError::StateError(StateError::GasFeesPaidByNotAllowedError(_)) => { + Some(Box::new(|source_error| { + TaskError::GasFeesPaidByNotAllowed { source_error } + })) + } + ConsensusError::StateError( + StateError::InconsistentGasFeesPaidByInBatchError(_), + ) => Some(Box::new(|source_error| { + TaskError::InconsistentGasFeesPayerInBatch { source_error } + })), ConsensusError::StateError( StateError::DocumentContestMaximumContendersReachedError(e), ) => { @@ -4423,6 +4544,18 @@ impl From for TaskError { ) => Some(Box::new(|source_error| { TaskError::DocumentActionFeesWithoutModeration { source_error } })), + ConsensusError::BasicError( + BasicError::PreProgrammedDistributionAmountOverLimitError(e), + ) => { + let (token_position, timestamp_ms) = (e.token_position(), e.timestamp()); + Some(Box::new(move |source_error| { + TaskError::PreProgrammedDistributionOverLimit { + token_position, + timestamp_ms, + source_error, + } + })) + } _ => None, } }); @@ -6848,14 +6981,66 @@ mod tests { #[test] fn protocol_v14_consensus_errors_map_to_their_variants() { use dash_sdk::dpp::consensus::basic::contract_moderation::DocumentActionFeesWithoutModerationError; + use dash_sdk::dpp::consensus::basic::data_contract::PreProgrammedDistributionAmountOverLimitError; + use dash_sdk::dpp::consensus::state::contract_moderation::{ + ContractUserBannedError, ContractUserSuspendedError, + }; use dash_sdk::dpp::consensus::state::document::{ document_immutable_property_changed_error::DocumentImmutablePropertyChangedError, referenced_document_type_not_deletable_error::ReferencedDocumentTypeNotDeletableError, }; + use dash_sdk::dpp::consensus::state::identity::gas_sponsor_insufficient_balance_error::GasSponsorInsufficientBalanceError; + use dash_sdk::dpp::consensus::state::token::{ + GasFeesPaidByNotAllowedError, InconsistentGasFeesPaidByInBatchError, + }; + use dash_sdk::dpp::tokens::gas_fees_paid_by::GasFeesPaidBy; - let contract_id = Identifier::random(); + let (contract_id, identity_id) = (Identifier::random(), Identifier::random()); type Expectation = fn(&TaskError) -> bool; let cases: Vec<(ConsensusError, Expectation)> = vec![ + ( + ContractUserBannedError::new(contract_id, identity_id).into(), + |e| matches!(e, TaskError::ContractUserBanned { .. }), + ), + ( + ContractUserSuspendedError::new(contract_id, identity_id, 1_758_140_722_000).into(), + |e| { + matches!( + e, + TaskError::ContractUserSuspended { + until_ms: 1_758_140_722_000, + .. + } + ) + }, + ), + ( + GasSponsorInsufficientBalanceError::new(contract_id, 1, 2).into(), + |e| { + matches!( + e, + TaskError::GasSponsorInsufficientBalance { + balance: 1, + required_balance: 2, + .. + } + ) + }, + ), + ( + GasFeesPaidByNotAllowedError::new( + "note".to_string(), + "create".to_string(), + GasFeesPaidBy::ContractOwner, + GasFeesPaidBy::DocumentOwner, + ) + .into(), + |e| matches!(e, TaskError::GasFeesPaidByNotAllowed { .. }), + ), + ( + InconsistentGasFeesPaidByInBatchError::new(Some(identity_id), None).into(), + |e| matches!(e, TaskError::InconsistentGasFeesPayerInBatch { .. }), + ), ( DocumentImmutablePropertyChangedError::new( Identifier::random(), @@ -6878,6 +7063,18 @@ mod tests { DocumentActionFeesWithoutModerationError::new("note".to_string()).into(), |e| matches!(e, TaskError::DocumentActionFeesWithoutModeration { .. }), ), + ( + PreProgrammedDistributionAmountOverLimitError::new(1, 1_758_140_722_000).into(), + |e| { + matches!( + e, + TaskError::PreProgrammedDistributionOverLimit { + token_position: 1, + .. + } + ) + }, + ), ]; for (cause, expected) in cases { @@ -7048,6 +7245,46 @@ mod tests { } } + #[test] + fn contract_user_errors_name_the_contract_and_the_suspension_end() { + let contract_id = Identifier::random(); + let banned = TaskError::ContractUserBanned { + contract_id, + identity_id: Identifier::random(), + source_error: Box::new(SdkError::Generic("banned".to_string())), + }; + assert!( + banned + .to_string() + .contains(&contract_id.to_string(Encoding::Base58)) + ); + let suspended = TaskError::ContractUserSuspended { + contract_id, + identity_id: Identifier::random(), + until_ms: 1_758_140_722_000, + source_error: Box::new(SdkError::Generic("suspended".to_string())), + }; + // Dates in messages follow the model's formatter: the user's time zone, + // like every other date the app shows. + let until = crate::model::datetime::local_date_time_from_unix_millis(1_758_140_722_000); + assert!(!until.is_empty()); + assert!( + suspended.to_string().contains(&format!("until {until}.")), + "got: {suspended}" + ); + let over_limit = TaskError::PreProgrammedDistributionOverLimit { + token_position: 0, + timestamp_ms: 1_758_140_722_000, + source_error: Box::new(SdkError::Generic("over".to_string())), + }; + assert!( + over_limit + .to_string() + .contains(&format!("at {until} add up")), + "got: {over_limit}" + ); + } + /// An unclassified SDK failure behind a wallet-runtime token operation /// keeps the operation, so the message names what did not complete. #[test] diff --git a/src/context/contract_token_db.rs b/src/context/contract_token_db.rs index b700d67bf..23f6aaae9 100644 --- a/src/context/contract_token_db.rs +++ b/src/context/contract_token_db.rs @@ -854,6 +854,7 @@ where mod tests { use super::*; use crate::wallet_backend::kv_test_support::{InMemoryKv, StallingReadKv}; + use dash_sdk::dpp::version::PlatformVersion; use platform_wallet_storage::{KvError, KvStore, ObjectId}; use std::sync::{Arc, Mutex}; @@ -926,6 +927,159 @@ mod tests { assert_eq!(decoded, token); } + /// A user contract as DET builds one at `platform_version`: a document + /// type with a unique index (charging `action_fees`, if given), and a token. + fn user_contract_at( + platform_version: &PlatformVersion, + action_fees: Option, + ) -> DataContract { + use dash_sdk::dpp::data_contract::associated_token::token_configuration::v0::TokenConfigurationV0; + use dash_sdk::dpp::data_contract::config::DataContractConfig; + use dash_sdk::dpp::data_contract::document_type::DocumentType; + use dash_sdk::dpp::data_contract::v1::DataContractV1; + use dash_sdk::dpp::platform_value::platform_value; + use std::collections::BTreeMap; + + let id = ident(8); + let config = DataContractConfig::default_for_version(platform_version).expect("config"); + let mut schema = platform_value!({ + "type": "object", + "properties": {"name": {"type": "string", "position": 0, "maxLength": 63_u32}}, + "required": ["name"], + "indices": [{"name": "byName", "properties": [{"name": "asc"}], "unique": true}], + "additionalProperties": false, + }); + if let Some(action_fees) = action_fees { + schema + .insert("actionFees".to_string(), action_fees) + .expect("a map schema"); + } + let note = DocumentType::try_from_schema( + id, + 1, + config.version(), + "note", + schema, + None, + &BTreeMap::new(), + &config, + true, + &mut Vec::new(), + platform_version, + ) + .expect("document type"); + DataContract::V1(DataContractV1 { + id, + version: 1, + owner_id: ident(7), + document_types: BTreeMap::from([("note".to_string(), note)]), + config, + schema_defs: None, + groups: BTreeMap::new(), + tokens: BTreeMap::from([( + 0, + TokenConfiguration::V0(TokenConfigurationV0::default_most_restrictive()), + )]), + keywords: vec![], + created_at: None, + updated_at: None, + created_at_block_height: None, + updated_at_block_height: None, + created_at_epoch: None, + updated_at_epoch: None, + description: None, + }) + } + + /// The contracts a user may hold locally, encoded as DET encodes them at + /// `protocol_version`. + fn stored_contracts_at(protocol_version: u32) -> Vec<(DataContract, StoredContract)> { + use dash_sdk::dpp::system_data_contracts::{SystemDataContract, load_system_data_contract}; + + let pv = PlatformVersion::get(protocol_version).expect("known version"); + [ + user_contract_at(pv, None), + load_system_data_contract(SystemDataContract::DPNS, pv).expect("dpns"), + load_system_data_contract(SystemDataContract::Dashpay, pv).expect("dashpay"), + load_system_data_contract(SystemDataContract::TokenHistory, pv).expect("history"), + ] + .into_iter() + .map(|contract| { + let stored = StoredContract { + contract_bytes: contract + .serialize_to_bytes_with_platform_version(pv) + .expect("encode"), + alias: Some("saved".to_string()), + }; + (contract, stored) + }) + .collect() + } + + /// Contracts saved while DET built at protocol 12 still load, unchanged, + /// once DET builds at the network's 13 or 14. + #[test] + fn contracts_stored_at_protocol_12_decode_at_13_and_14() { + let v12 = PlatformVersion::get(12).expect("v12"); + let tmp = tempfile::tempdir().expect("tempdir"); + let ctx = crate::context::test_support::test_app_context(tmp.path()); + for version in [13, 14] { + crate::context::test_support::set_sdk_protocol_version(&ctx, version); + for (original, stored) in stored_contracts_at(12) { + let bytes = stored.contract_bytes.clone(); + let decoded = ctx + .decode_stored_contract(stored) + .unwrap_or_else(|e| panic!("protocol {version}: {e:?}")); + assert_eq!(decoded.alias.as_deref(), Some("saved")); + assert_eq!(decoded.contract.id(), original.id()); + assert_eq!( + decoded + .contract + .serialize_to_bytes_with_platform_version(v12) + .expect("re-encode"), + bytes, + "protocol {version}: contract {} lost data", + original.id() + ); + } + } + } + + /// A contract saved at protocol 14 and read back while DET still builds at + /// 13 (after a restart, before the first proven response) decodes without + /// error. Protocol 14 features (here `actionFees`) are not parsed at 13, + /// but the saved bytes are untouched and decode in full once DET builds at + /// 14 again. + #[test] + fn contracts_stored_at_protocol_14_decode_at_13_and_recover_at_14() { + use dash_sdk::dpp::platform_value::platform_value; + + let tmp = tempfile::tempdir().expect("tempdir"); + let ctx = crate::context::test_support::test_app_context(tmp.path()); + crate::context::test_support::set_sdk_protocol_version(&ctx, 13); + for (original, stored) in stored_contracts_at(14) { + let decoded = ctx + .decode_stored_contract(stored) + .unwrap_or_else(|e| panic!("contract {}: {e:?}", original.id())); + assert_eq!(decoded.contract, original); + } + + let v14 = PlatformVersion::get(14).expect("v14"); + let charging = user_contract_at(v14, Some(platform_value!({"create": {"owner": 5_u64}}))); + let stored = || StoredContract { + contract_bytes: charging + .serialize_to_bytes_with_platform_version(v14) + .expect("encode"), + alias: None, + }; + let at_13 = ctx.decode_stored_contract(stored()).expect("decodes at 13"); + assert_eq!(at_13.contract.id(), charging.id()); + + crate::context::test_support::set_sdk_protocol_version(&ctx, 14); + let at_14 = ctx.decode_stored_contract(stored()).expect("decodes at 14"); + assert_eq!(at_14.contract, charging); + } + #[test] fn contract_keys_carry_a_base58_suffix_that_round_trips_to_the_id() { let contract = ident(6); diff --git a/src/wallet_backend/event_bridge.rs b/src/wallet_backend/event_bridge.rs index f1ccb4e0e..29b81fbed 100644 --- a/src/wallet_backend/event_bridge.rs +++ b/src/wallet_backend/event_bridge.rs @@ -584,7 +584,9 @@ fn summary_ok_balances(summary: &ShieldedSyncPassSummary) -> Vec<([u8; 32], u64) WalletShieldedOutcome::Ok(sync) => match sync.balance_total() { Ok(balance) => Some((*wallet_id, balance)), Err(error) => { - tracing::debug!( + // An overflowing note total means corrupt or hostile sync + // data, not routine noise: surface it at warn. + tracing::warn!( ?wallet_id, ?error, "Shielded sync balance total is invalid; preserving the cached balance" From 53d3fb22e44bc3a829586fe4c48970a6513f2676 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Sun, 25 Oct 2026 10:42:24 +0000 Subject: [PATCH 09/27] fix(context): keep one system contract set per protocol version A lookup at another version, as the proof verifier makes at a response's version, swapped out the app's set, so each such lookup reloaded the system contracts and the next app read reloaded them again. Each version now keeps its own set; the app's reads keep the set they last got for a failed reload. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- .../README.md | 7 +- src/context/system_contracts.rs | 72 ++++++++++++++----- 2 files changed, 58 insertions(+), 21 deletions(-) diff --git a/docs/ai-design/2026-10-24-platform-v5.1-dev-bump/README.md b/docs/ai-design/2026-10-24-platform-v5.1-dev-bump/README.md index fe5b8bb3a..50e72fb63 100644 --- a/docs/ai-design/2026-10-24-platform-v5.1-dev-bump/README.md +++ b/docs/ai-design/2026-10-24-platform-v5.1-dev-bump/README.md @@ -31,9 +31,10 @@ from 13 to 14: `SystemContractsCache` serves the system contracts (DPNS, DashPay, withdrawals, token history, keyword search) at the SDK's version and reloads them when it changes. The context provider serves them at the version the proof verifier -asks for (`verifier_version`), which can be newer than the SDK's. `try_get` -fails rather than serving another version's contracts, so a proof is never -checked against the wrong schema. +passes in (`AppContext::system_contracts_at`), which can be newer than the +SDK's. Each version keeps its own set, so the two lookups do not evict each +other. `try_get` fails rather than serving another version's contracts, so a +proof is never checked against the wrong schema. ## Connection state diff --git a/src/context/system_contracts.rs b/src/context/system_contracts.rs index 7a6db8a37..522682fc9 100644 --- a/src/context/system_contracts.rs +++ b/src/context/system_contracts.rs @@ -1,7 +1,8 @@ //! The system data contracts, as defined at the protocol version in use. -use std::sync::Arc; +use std::collections::BTreeMap; use std::sync::atomic::{AtomicU32, Ordering}; +use std::sync::{Arc, PoisonError, RwLock}; use arc_swap::ArcSwap; use dash_sdk::dpp::ProtocolError; @@ -39,13 +40,17 @@ impl SystemContracts { } } -/// [`SystemContracts`] that follow the protocol version in use. +/// [`SystemContracts`], one set per protocol version. /// -/// Lock-free: the set lives in an [`ArcSwap`]. Two callers reloading at once -/// store equivalent sets; a set stored late for an older version is replaced -/// on the next access, so the cache converges on the current version. +/// The proof verifier asks for the set at the version a response reports, the +/// app's own reads at the version the SDK runs; each version keeps its own +/// set, so neither lookup evicts the other's. The map holds at most one set +/// per protocol version table this build defines. Its lock is held only to +/// read or insert, never across a load. #[derive(Debug)] pub(crate) struct SystemContractsCache { + sets: RwLock>>, + /// The set the app's own reads last got, served when a reload fails. current: ArcSwap, /// The last version whose reload failed, so the failure is logged once. failed_version: AtomicU32, @@ -53,35 +58,51 @@ pub(crate) struct SystemContractsCache { impl SystemContractsCache { pub(crate) fn new(contracts: SystemContracts) -> Self { + let contracts = Arc::new(contracts); Self { - current: ArcSwap::from_pointee(contracts), + sets: RwLock::new(BTreeMap::from([( + contracts.protocol_version, + Arc::clone(&contracts), + )])), + current: ArcSwap::new(contracts), failed_version: AtomicU32::new(0), } } - /// The set for exactly `platform_version`, reloaded if the cached one was - /// built for another version. A failed reload is an error, never the set - /// of another version: what verifies a proof must not mix schemas. + /// The set for exactly `platform_version`, loaded on first use. A failed + /// load is an error, never the set of another version: what verifies a + /// proof must not mix schemas. pub(crate) fn try_get( &self, platform_version: &PlatformVersion, ) -> Result, Box> { - let cached = self.current.load_full(); - if cached.protocol_version == platform_version.protocol_version { - return Ok(cached); + let version = platform_version.protocol_version; + if let Some(cached) = self + .sets + .read() + .unwrap_or_else(PoisonError::into_inner) + .get(&version) + { + return Ok(Arc::clone(cached)); } let fresh = Arc::new(SystemContracts::load(platform_version)?); - self.current.store(Arc::clone(&fresh)); - Ok(fresh) + // Two callers loading at once keep the first set inserted. + let mut sets = self.sets.write().unwrap_or_else(PoisonError::into_inner); + Ok(Arc::clone(sets.entry(version).or_insert(fresh))) } /// The set for `platform_version`, for the app's own reads (names, - /// documents, forms). A failed reload keeps the cached set, so the app - /// keeps working on its last loaded contracts; proof verification uses - /// [`Self::try_get`] instead. + /// documents, forms). A failed load keeps the set these reads last got, + /// so the app keeps working on its last loaded contracts; proof + /// verification uses [`Self::try_get`] instead. pub(crate) fn get(&self, platform_version: &PlatformVersion) -> Arc { match self.try_get(platform_version) { - Ok(contracts) => contracts, + Ok(contracts) => { + if !Arc::ptr_eq(&self.current.load(), &contracts) { + self.current.store(Arc::clone(&contracts)); + } + contracts + } Err(error) => { let cached = self.current.load_full(); let version = platform_version.protocol_version; @@ -146,6 +167,21 @@ mod tests { assert!(Arc::ptr_eq(&cache.get(v13), &cache.try_get(v13).unwrap())); } + /// Regression: a lookup at another version (the proof verifier follows a + /// response's version) swapped out the app's set, so every such lookup + /// reloaded the contracts twice. Each version keeps its own set. + #[test] + fn a_lookup_at_another_version_keeps_the_current_set() { + let v13 = version(13); + let v14 = version(14); + let cache = SystemContractsCache::new(SystemContracts::load(v13).unwrap()); + let current = cache.get(v13); + + let other = cache.try_get(v14).unwrap(); + assert!(Arc::ptr_eq(¤t, &cache.get(v13))); + assert!(Arc::ptr_eq(&other, &cache.try_get(v14).unwrap())); + } + /// The same version serves the cached set itself, not a reload. #[test] fn the_same_protocol_version_keeps_the_cached_set() { From e698c7ac14856680ebd50b05e2941ad1b9562523 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Sun, 25 Oct 2026 10:42:24 +0000 Subject: [PATCH 10/27] fix(platform): confirm only a verified protocol version without the epoch When the epoch cannot be read, the proved DPNS fallback confirmed the SDK's version, which starts at the network's upstream minimum (14 on devnet), as the network's. It now confirms the version of the responses the SDK verified, and nothing before any. Restores the completed-withdrawal status list with the reason sending FAILED to a protocol 13 network is safe, and its test, now comparing the protocol 13 schema with the latest one. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- src/backend_task/platform_info.rs | 119 ++++++++++++++++++++++++++---- 1 file changed, 103 insertions(+), 16 deletions(-) diff --git a/src/backend_task/platform_info.rs b/src/backend_task/platform_info.rs index 16ad8cc3a..1aa2ad953 100644 --- a/src/backend_task/platform_info.rs +++ b/src/backend_task/platform_info.rs @@ -644,15 +644,14 @@ impl AppContext { "Current epoch fetch failed; keeping the last known fee multiplier" ); // Without the epoch, the network's version is learned from - // the ratchet a proved DPNS fetch drives. Only a successful - // fetch proves it came from the network, not the local seed. + // a proved DPNS fetch: the version of the responses the SDK + // verified, never its local seed. let (generation, on_current_sdk) = self.connection_snapshot_for(sdk); match DataContract::fetch(sdk, self.dpns_contract().id()).await { Ok(_) if on_current_sdk => { - self.raise_platform_protocol_version_on( - generation, - sdk.protocol_version_number(), - ); + if let Some(version) = network_proved_protocol_version(sdk) { + self.raise_platform_protocol_version_on(generation, version); + } } Ok(_) => {} Err(error) => tracing::warn!( @@ -789,11 +788,7 @@ impl AppContext { where_clauses: vec![WhereClause { field: "status".to_string(), operator: WhereOperator::In, - value: Value::Array(vec![ - Value::U8(WithdrawalStatus::COMPLETE as u8), - Value::U8(WithdrawalStatus::EXPIRED as u8), - Value::U8(WithdrawalStatus::FAILED as u8), - ]), + value: Value::Array(completed_withdrawal_statuses()), }], time_range_clauses: Vec::new(), group_by: Vec::new(), @@ -878,11 +873,7 @@ impl AppContext { let start = start_after.map(|id| Start::StartAfter(id.to_buffer().to_vec())); let statuses = if completed { - vec![ - Value::U8(WithdrawalStatus::COMPLETE as u8), - Value::U8(WithdrawalStatus::EXPIRED as u8), - Value::U8(WithdrawalStatus::FAILED as u8), - ] + completed_withdrawal_statuses() } else { vec![ Value::U8(WithdrawalStatus::QUEUED as u8), @@ -989,10 +980,106 @@ impl AppContext { } } +/// The `status` values a finished withdrawal can carry, as the completed +/// withdrawal queries send them. +/// +/// `FAILED` (5) exists only in the v2 withdrawals schema (protocol 14); the v1 +/// schema of a protocol 13 network stops at `EXPIRED` (4). Sending it at +/// protocol 13 is still correct and deliberate: a JSON-schema `enum` constrains +/// documents being created, not query values. Nothing in the query path checks +/// membership — the `status` property is typed as a plain integer, and +/// `DocumentPropertyType::encode_value_for_tree_keys` encodes any value that +/// converts to it. So the out-of-enum member encodes, matches nothing on a +/// protocol 13 network (which cannot produce a failed withdrawal), and leaves +/// `COMPLETE` and `EXPIRED` matching as usual, while the same query stays +/// complete on protocol 14. +fn completed_withdrawal_statuses() -> Vec { + vec![ + Value::U8(WithdrawalStatus::COMPLETE as u8), + Value::U8(WithdrawalStatus::EXPIRED as u8), + Value::U8(WithdrawalStatus::FAILED as u8), + ] +} + +/// The protocol version `sdk` has seen in a response whose proof and quorum +/// signature were verified, or `None` before any such response. Never the +/// SDK's local seed: the SDK's own version starts at the network's upstream +/// minimum and only rises, so a successful read does not prove it. +fn network_proved_protocol_version(sdk: &Sdk) -> Option { + match sdk.query_settings().verified_protocol_version { + 0 => None, + version => Some(version), + } +} + #[cfg(test)] mod tests { use super::*; + /// The completed-withdrawal filter keeps `FAILED` even though the protocol + /// 13 schema stops at `EXPIRED`: a schema `enum` constrains documents, not + /// query values, and every status encodes into the `status` property's key + /// representation at both schema versions. + #[test] + fn completed_withdrawal_filter_encodes_at_both_schema_versions() { + use dash_sdk::dpp::data_contract::accessors::v0::DataContractV0Getters; + use dash_sdk::dpp::data_contract::document_type::accessors::DocumentTypeV0Getters; + use dash_sdk::dpp::system_data_contracts::{SystemDataContract, load_system_data_contract}; + use dash_sdk::dpp::version::v13::PLATFORM_V13; + + assert_eq!( + completed_withdrawal_statuses(), + vec![Value::U8(3), Value::U8(4), Value::U8(5)], + "COMPLETE, EXPIRED and FAILED, in that order" + ); + + let status_property = |version| { + let contract = load_system_data_contract(SystemDataContract::Withdrawals, version) + .expect("withdrawals contract"); + contract + .document_type_cloned_for_name("withdrawal") + .expect("withdrawal document type") + .properties() + .get("status") + .expect("status property") + .clone() + }; + + // v1 (protocol 13) and v2 (protocol 14) differ only in the enum's last + // member, so the property type is identical. + let v13 = status_property(&PLATFORM_V13); + let latest = status_property(PlatformVersion::latest()); + assert_eq!( + v13.property_type, latest.property_type, + "the status property type must not differ across schema versions" + ); + + // FAILED is outside the v1 enum, yet encodes for the query exactly like + // the statuses that are inside it. + for status in completed_withdrawal_statuses() { + assert!( + v13.property_type + .encode_value_for_tree_keys(&status) + .is_ok(), + "status {status:?} must encode against the protocol 13 schema" + ); + } + } + + /// Regression: the DPNS fallback confirmed the SDK's version, which starts + /// at the local seed (14 on devnet), as the network's proved version. A + /// fresh SDK has verified nothing, so nothing is proved. + #[test] + fn a_fresh_sdk_proves_no_protocol_version() { + let sdk = Sdk::new_mock(); + assert_ne!( + sdk.protocol_version_number(), + 0, + "precondition: a seeded SDK" + ); + assert_eq!(network_proved_protocol_version(&sdk), None); + } + /// The limit is whatever the network's own protocol version says it is, /// never a version fixed in this build: protocol 13 reports a flat 2000 /// Dash whatever the total, protocol 14 derives it from that total (15% of From 4abf4d5bbbaf40057a6ab4cc9e4cb52c4fa905e5 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Sun, 25 Oct 2026 10:42:24 +0000 Subject: [PATCH 11/27] fix(wallet): erase the secp256k1 signing key after use The derived key DET signs with is copied into secp256k1's own type, which keeps its bytes after use. Wrap it so it is erased on drop, as the derived key it comes from would be; signatures stay byte-identical. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- src/wallet_backend/det_signer.rs | 35 ++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/wallet_backend/det_signer.rs b/src/wallet_backend/det_signer.rs index d0cba01ba..8ce7096b0 100644 --- a/src/wallet_backend/det_signer.rs +++ b/src/wallet_backend/det_signer.rs @@ -128,9 +128,9 @@ impl<'a> DetSigner<'a> { /// Signing stays on the secp256k1 primitive (plain RFC 6979 nonce), so /// signatures are byte-identical to earlier builds; the newer /// `EcdsaSecretKey::sign` grinds for a low R and would change them. - fn derive_secret(&self, path: &DerivationPath) -> Result { + fn derive_secret(&self, path: &DerivationPath) -> Result { let scalar = self.derive_xprv(path)?.private_key.to_bytes(); - secp256k1::SecretKey::from_secret_bytes(*scalar).map_err(DetSignerError::Sign) + ErasedSecretKey::from_scalar(&scalar) } /// Sign `msg` (a 32-byte digest) with the held **single key** directly, @@ -143,8 +143,7 @@ impl<'a> DetSigner<'a> { ) -> Result { match &self.secret { HeldSecret::SingleKey(key) => { - let sk = secp256k1::SecretKey::from_secret_bytes(***key) - .map_err(DetSignerError::Sign)?; + let sk = ErasedSecretKey::from_scalar(key)?; let message = message_from_digest(*msg)?; Ok(dash_sdk::dpp::secp256k1::ecdsa::sign(message, &sk)) } @@ -153,6 +152,34 @@ impl<'a> DetSigner<'a> { } } +/// A secp256k1 secret key erased when dropped. secp256k1's own type keeps its +/// bytes after use, while the derived key it is built from would have wiped +/// them; this keeps the signing primitive (byte-identical signatures) without +/// leaving the key behind in freed memory. +struct ErasedSecretKey(secp256k1::SecretKey); + +impl ErasedSecretKey { + fn from_scalar(scalar: &[u8; 32]) -> Result { + secp256k1::SecretKey::from_secret_bytes(*scalar) + .map(Self) + .map_err(DetSignerError::Sign) + } +} + +impl std::ops::Deref for ErasedSecretKey { + type Target = secp256k1::SecretKey; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl Drop for ErasedSecretKey { + fn drop(&mut self) { + self.0.non_secure_erase(); + } +} + impl std::fmt::Debug for DetSigner<'_> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let kind = match self.secret { From 55367457c53798a4f275d6669992546fe158e58c Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Sun, 25 Oct 2026 10:42:24 +0000 Subject: [PATCH 12/27] test(protocol): guard a contested name built with the contest fund at protocol 13 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- src/backend_task/protocol_13_transitions.rs | 72 +++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/src/backend_task/protocol_13_transitions.rs b/src/backend_task/protocol_13_transitions.rs index 5811df3f0..ffa25e3f4 100644 --- a/src/backend_task/protocol_13_transitions.rs +++ b/src/backend_task/protocol_13_transitions.rs @@ -234,3 +234,75 @@ async fn a_document_is_created_in_the_protocol_13_encoding() { "protocol 13 cannot decode {base:?}" ); } + +/// A contested username is created on a protocol 13 network the way +/// `BroadcastDocument` creates it, with the contest fund DET states: in the +/// protocol 13 document encoding, and stating exactly the protocol's base +/// fund. +#[tokio::test] +async fn a_contested_name_is_created_in_the_protocol_13_encoding_with_the_base_fund() { + use crate::model::fee_estimation::{contest_fee_credits, document_create_contest_fund_cap}; + use dash_sdk::dpp::state_transition::batch_transition::batched_transition::document_create_transition::v0::v0_methods::DocumentCreateTransitionV0Methods; + use dash_sdk::dpp::state_transition::batch_transition::batched_transition::document_transition::DocumentTransition; + use dash_sdk::dpp::system_data_contracts::{SystemDataContract, load_system_data_contract}; + + let (_tmp, _ctx, platform_version) = mainnet_context(); + let contract = load_system_data_contract(SystemDataContract::DPNS, platform_version) + .expect("DPNS contract"); + let domain = contract + .document_type_for_name("domain") + .expect("domain type"); + let owner_id = Identifier::from([4; 32]); + let document = crate::context::test_support::dpns_name_document( + owner_id, + "alice", + dash_sdk::dpp::platform_value::Value::Identifier(owner_id.to_buffer()), + ); + let fund = document_create_contest_fund_cap(domain, &document, platform_version) + .expect("contest check") + .expect("a contested name"); + assert_eq!(fund, contest_fee_credits(platform_version)); + let options = StateTransitionCreationOptions { + contest_fund: Some(fund), + ..Default::default() + }; + + let transition = BatchTransition::new_document_creation_transition_from_document( + document, + domain, + [3; 32], + &plain_key(1, SecurityLevel::HIGH), + 1, + UserFeeIncrease::default(), + None, + &PlaceholderSigner, + platform_version, + Some(options), + ) + .await + .expect("contested name create"); + + let StateTransition::Batch(batch) = transition else { + panic!("expected a batch, got {transition:?}"); + }; + let document_transition = match &batch { + BatchTransition::V0(v0) => v0.transitions[0].clone(), + BatchTransition::V1(v1) => match &v1.transitions[0] { + BatchedTransition::Document(document) => document.clone(), + other => panic!("expected a document transition, got {other:?}"), + }, + }; + assert!( + !matches!(document_transition.base(), DocumentBaseTransition::V2(_)), + "protocol 13 cannot decode {:?}", + document_transition.base() + ); + let DocumentTransition::Create(create) = document_transition else { + panic!("expected a create, got {document_transition:?}"); + }; + let stated = create + .prefunded_voting_balance() + .as_ref() + .map(|(_, credits)| *credits); + assert_eq!(stated, Some(contest_fee_credits(platform_version))); +} From 93cea96772ffd844c6a7bbfe026104af7486fbbc Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Sun, 25 Oct 2026 10:42:24 +0000 Subject: [PATCH 13/27] fix(ui): say plainly what the app cannot edit or sign yet The new refusals sent users to another document editor or token client. They now say the app cannot edit this field type, sign this action or change this token setting yet, in one wording, and the shielded setting refusal sits with the token contract errors. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- src/backend_task/error.rs | 9 ++++--- .../document_action_screen.rs | 26 +++++++++++-------- .../group_actions_screen.rs | 2 +- src/ui/tokens/update_token_config.rs | 2 +- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/backend_task/error.rs b/src/backend_task/error.rs index 86c4bd4e4..6d0d4c0fa 100644 --- a/src/backend_task/error.rs +++ b/src/backend_task/error.rs @@ -2147,6 +2147,12 @@ pub enum TaskError { // ────────────────────────────────────────────────────────────────────────── // Token contract errors // ────────────────────────────────────────────────────────────────────────── + /// A token configuration change the app cannot make yet (the shielded + /// pool settings), refused by `validate_supported_change` in + /// `backend_task/tokens/update_token_config.rs` before anything is signed. + #[error("The app cannot change this token setting yet. Choose another setting to update.")] + TokenShieldedConfigurationUnsupported, + /// A token at the expected position was not found in the contract. #[error( "Token at position {position} was not found in the contract. Please reload the contract and retry." @@ -2662,9 +2668,6 @@ pub enum TaskError { // ────────────────────────────────────────────────────────────────────────── // Token query errors // ────────────────────────────────────────────────────────────────────────── - #[error("Shielded token settings cannot be changed here yet. Use a supported token setting instead.")] - TokenShieldedConfigurationUnsupported, - /// Querying token data from the platform failed. #[error("Could not retrieve token information from the platform. Please retry.")] TokenQueryError { detail: String }, diff --git a/src/ui/contracts_documents/document_action_screen.rs b/src/ui/contracts_documents/document_action_screen.rs index 56db4e99a..76b56bf95 100644 --- a/src/ui/contracts_documents/document_action_screen.rs +++ b/src/ui/contracts_documents/document_action_screen.rs @@ -85,6 +85,12 @@ pub enum BroadcastStatus { Broadcasted, } +/// Shown in place of a field whose type the document editor cannot edit yet. +const UNEDITABLE_FIELD_NOTICE: &str = "The app cannot edit this field type yet."; + +/// Why a document with a field type the editor cannot edit is not sent. +const UNEDITABLE_DOCUMENT_MESSAGE: &str = "This document has a field type the app cannot edit yet, so it cannot be created or changed here."; + pub struct DocumentActionScreen { pub app_context: Arc, pub action_type: DocumentActionType, @@ -806,9 +812,7 @@ impl DocumentActionScreen { ); } DocumentPropertyType::TypedArray(_) => { - ui.label( - "This field cannot be edited here. Use another document editor.", - ); + ui.label(UNEDITABLE_FIELD_NOTICE); } DocumentPropertyType::Object(_) | DocumentPropertyType::Array(_) @@ -1262,7 +1266,7 @@ impl DocumentActionScreen { .ok_or("No identity selected")?; if !Self::supports_field_editor(doc_type) { - return Err("This document contains fields that cannot be edited here. Use another document editor.".to_string()); + return Err(UNEDITABLE_DOCUMENT_MESSAGE.to_string()); } for (prop, schema) in doc_type.properties() { @@ -1383,9 +1387,9 @@ impl DocumentActionScreen { Value::U64(ts) } DocumentPropertyType::TypedArray(_) => { - return Err(format!( - "Field {name} cannot be edited here. Use another document editor." - )); + // Unreachable while `supports_field_editor` refuses the + // document first; kept so a typed array is never encoded. + return Err(UNEDITABLE_DOCUMENT_MESSAGE.to_string()); } DocumentPropertyType::Object(_) => { return Err(format!( @@ -1457,7 +1461,7 @@ impl DocumentActionScreen { .ok_or("No document-type selected")?; if !Self::supports_field_editor(doc_type) { - return Err("This document contains fields that cannot be edited here. Use another document editor.".to_string()); + return Err(UNEDITABLE_DOCUMENT_MESSAGE.to_string()); } for (prop, schema) in doc_type.properties() { @@ -1578,9 +1582,9 @@ impl DocumentActionScreen { Value::U64(ts) } DocumentPropertyType::TypedArray(_) => { - return Err(format!( - "Field {name} cannot be edited here. Use another document editor." - )); + // Unreachable while `supports_field_editor` refuses the + // document first; kept so a typed array is never encoded. + return Err(UNEDITABLE_DOCUMENT_MESSAGE.to_string()); } DocumentPropertyType::Object(_) => { return Err(format!( diff --git a/src/ui/contracts_documents/group_actions_screen.rs b/src/ui/contracts_documents/group_actions_screen.rs index 58cb3b087..a0a6e650b 100644 --- a/src/ui/contracts_documents/group_actions_screen.rs +++ b/src/ui/contracts_documents/group_actions_screen.rs @@ -236,7 +236,7 @@ impl GroupActionsScreen { row.col(|ui| { if !Self::supports_token_event_action(token_event) { ui.add_enabled(false, egui::Button::new("Take Action")) - .on_hover_text("This action cannot be signed here. Use another token client."); + .on_hover_text("The app cannot sign this action yet."); return; } if ComponentStyles::add_primary_button( diff --git a/src/ui/tokens/update_token_config.rs b/src/ui/tokens/update_token_config.rs index 54b2b6a7b..1685218bf 100644 --- a/src/ui/tokens/update_token_config.rs +++ b/src/ui/tokens/update_token_config.rs @@ -608,7 +608,7 @@ impl UpdateTokenConfigScreen { TokenConfigurationChangeItem::MinimumPoolNotesForOutgoing(_) | TokenConfigurationChangeItem::MinimumPoolNotesForOutgoingControlGroup(_) | TokenConfigurationChangeItem::MinimumPoolNotesForOutgoingAdminGroup(_) => { - ui.label("This setting cannot be changed here. Use another token configuration editor."); + ui.label("The app cannot change this token setting yet. Choose another setting to update."); } } }); From 16c1c7fc5d599173c06dc9344ee8c7a59bb67c4f Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Sun, 25 Oct 2026 10:42:24 +0000 Subject: [PATCH 14/27] chore: keep AGENTS.md and the marketplace comment as on v1.0-dev Also corrects the platform compatibility fixtures README: no code reads e3cd7cf.sql any more; it is the V001-V018 checksum reference. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- AGENTS.md | 2 +- src/backend_task/tokens/mod.rs | 4 +--- .../platform_compatibility/fixtures/README.md | 11 +++++++---- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 207af94d8..07f0babe7 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -128,7 +128,7 @@ User-facing error messages (shown in `MessageBanner` via `Display`) must follow 2. **Structure**: *What happened* + *what to do*. Every message must include a concrete action the user can take themselves: retry, wait, try a different approach. Never redirect to "contact support" — users must be able to self-resolve. 3. **Tone**: Calm, direct, brief. Not apologetic ("Sorry!"), not alarming ("Something went wrong!"), not vague ("An error occurred"). 4. **Technical details**: Never in the message itself — no raw error strings, stack traces, SDK internals, or error codes. Attach via `BannerHandle::with_details(e)` — the `Debug` repr goes to the collapsible details panel and logs. Never refer users to "details" or "details panel" — these are not visible in basic mode. Exception: Base58 identifiers (see rule 6) are not technical details — they are user-meaningful handles. -5. **Reference implementation**: `impl From for TaskError` in `src/backend_task/error.rs` demonstrates the pattern for SDK errors. New `TaskError` variants should follow the same style. +5. **Reference implementation**: `sdk_error_user_message()` in `src/backend_task/error.rs` demonstrates the pattern for SDK errors. New `TaskError` variants should follow the same style. 6. **Base58 IDs are allowed in messages**: Contract IDs, identity IDs, document IDs, and similar Base58-encoded identifiers may appear in user-facing messages when they help the user identify which object is involved (e.g., *"This key conflicts with an existing key bound to contract `Abc123…`."*). They are not jargon — they are opaque-but-copyable handles the user can look up. 7. **Use dedicated `TaskError` variants**: Every error should get a dedicated `TaskError` variant with a `#[source]` field that preserves the error chain, enables structural matching, and keeps `Display` / `Debug` separation explicit. For `#[source]` fields in SDK-originated error variants, use `Box` — convert upstream types (e.g. `ProtocolError`) via `SdkError::Protocol(e)`. Use the concrete domain type directly for non-SDK errors (e.g. `rusqlite::Error`). Omit `#[source]` entirely when the upstream error carries no useful diagnostic information (e.g. a channel `SendError`). **Never store user-facing strings in error variants** — error variants must not contain `String` fields that hold messages for the user. The `#[error("...")]` attribute on the variant provides the user-facing message; `String` fields (regardless of name) break this separation. Instead, create a dedicated variant with typed `#[source]` fields, or a fieldless variant if no upstream error exists. When encountering existing variants that store stringified errors, replace them with properly typed variants as part of the change. diff --git a/src/backend_task/tokens/mod.rs b/src/backend_task/tokens/mod.rs index e7f66aee8..9d3b3304c 100644 --- a/src/backend_task/tokens/mod.rs +++ b/src/backend_task/tokens/mod.rs @@ -761,9 +761,7 @@ impl AppContext { token_config_v0.distribution_rules = distribution_rules; token_config_v0.description = token_description; - // Tokens are created NotTradeable. Tradeable needs protocol version 15 - // (TOKEN_MARKETPLACE_INITIAL_PROTOCOL_VERSION) and a transferable token, - // and DET has no way to list a token or make an offer for it yet. + // All tokens are created NotTradeable; a future SDK will add more modes. token_config_v0.marketplace_rules = TokenMarketplaceRules::V0(TokenMarketplaceRulesV0 { trade_mode: TokenTradeMode::NotTradeable, trade_mode_change_rules: marketplace_rules, diff --git a/src/wallet_backend/platform_compatibility/fixtures/README.md b/src/wallet_backend/platform_compatibility/fixtures/README.md index a0f8ae0d4..3def487aa 100644 --- a/src/wallet_backend/platform_compatibility/fixtures/README.md +++ b/src/wallet_backend/platform_compatibility/fixtures/README.md @@ -14,10 +14,13 @@ stored in table/index/trigger order. Refinery history uses its SipHasher13 hash over migration name, i32 version and rendered SQL. Timestamps and the store-generation token are deterministic fixture values. -The production bridge compares these exact histories and materialized -schemas before translating data. The destination itself is created by the -current public `SqlitePersister::open`, not by the fixture SQL. Updating -either schema revision requires reviewing and regenerating these guards. +The production bridge compares the source history and schema in +`67d4ef3.sql`, and the destination guard `033ecae.sql` described below, +before translating data. No code reads `e3cd7cf.sql` any more: it is kept +as the reference for the V001–V018 migration names and checksums. The +destination itself is created by the current public `SqlitePersister::open`, +not by the fixture SQL. Updating either schema revision requires reviewing +and regenerating these guards. The current destination guard is `033ecae.sql`, captured from an empty database opened by `SqlitePersister::open` at Platform revision `033ecaee2f3badc8e04ff8be664062f33516e3e4`. The V001–V018 migration names and From 407bbaa53af040a70857136425d175c7f66010d3 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Sun, 25 Oct 2026 12:03:11 +0000 Subject: [PATCH 15/27] fix(test): prove the protocol version the headless refresh learns The protocol version fallback takes only a version the SDK verified, and a mock expectation's metadata is never recorded as verified, so the three headless refresh tests saw the version stay unconfirmed. The mock now has the SDK accept the version from metadata treated as proved, as after a live network's signed response, and a new test pins that a response the SDK never verified leaves the version unconfirmed. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- src/mcp/resolve.rs | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/src/mcp/resolve.rs b/src/mcp/resolve.rs index 9dfef54aa..652b7c214 100644 --- a/src/mcp/resolve.rs +++ b/src/mcp/resolve.rs @@ -447,12 +447,31 @@ mod tests { use dash_sdk::dpp::version::LATEST_VERSION; use dash_sdk::platform::DataContract; - /// Mocks the proved DPNS-contract fetch that drives the protocol-version - /// ratchet — the epoch task's fallback when the current epoch cannot be - /// read (the mock has no epoch response). The mock reports `LATEST_VERSION` in - /// the response metadata, so a successful fetch ratchets the SDK exactly as a - /// live network would; a failed one leaves the version unconfirmed. + /// Mocks the DPNS-contract fetch that drives the protocol-version ratchet, + /// the epoch task's fallback when the current epoch cannot be read (the + /// mock has no epoch response). The fallback takes only a version the SDK + /// verified, and a mock expectation is made up, never verified, so the + /// SDK first accepts `LATEST_VERSION` from metadata it is told was proved, + /// as it does after a live network's quorum-signed response. A successful + /// fetch then ratchets the app exactly as a live network would; a failed + /// one leaves the version unconfirmed. async fn mock_sdk_with_dpns_contract(ctx: &Arc) -> Sdk { + let sdk = mock_sdk_with_unproved_dpns_contract(ctx).await; + sdk.verify_response_metadata( + "get_data_contract", + &dash_sdk::dapi_grpc::platform::v0::ResponseMetadata { + protocol_version: LATEST_VERSION, + height: 1, + ..Default::default() + }, + ) + .expect("proved metadata is accepted"); + sdk + } + + /// The DPNS-contract fetch alone: its expectation proves nothing about the + /// network's protocol version. + async fn mock_sdk_with_unproved_dpns_contract(ctx: &Arc) -> Sdk { let contract = DataContract::clone(&ctx.dpns_contract()); let mut sdk = Sdk::new_mock(); sdk.mock() @@ -462,6 +481,22 @@ mod tests { sdk } + /// A DPNS fetch whose response the SDK never verified does not confirm + /// the network's protocol version: features stay gated off. + #[tokio::test] + async fn an_unproved_response_leaves_the_protocol_version_unconfirmed() { + let temp_dir = tempfile::tempdir().expect("tempdir"); + let ctx = crate::mcp::tests::legacy_wallet_context(temp_dir.path()); + ctx.sdk + .store(Arc::new(mock_sdk_with_unproved_dpns_contract(&ctx).await)); + + refresh_platform_protocol_version(&ctx) + .await + .expect("refresh platform metadata"); + + assert_eq!(ctx.platform_protocol_version(), 0); + } + #[tokio::test] async fn headless_sync_populates_protocol_version_after_spv_is_running() { let temp_dir = tempfile::tempdir().expect("tempdir"); From 9c54f998c7a89b190e8beef068c616ec11e39521 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Mon, 26 Oct 2026 08:53:02 +0000 Subject: [PATCH 16/27] test: drop the protocol 13 transition encoding tests The SDK picks each transition's encoding from the platform version DET passes in, so these tests checked dpp's version tables rather than DET. The mainnet start version and the contest fund cap stay covered by the platform_info and fee_estimation tests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- .../README.md | 2 - src/backend_task/mod.rs | 2 - src/backend_task/protocol_13_transitions.rs | 308 ------------------ 3 files changed, 312 deletions(-) delete mode 100644 src/backend_task/protocol_13_transitions.rs diff --git a/docs/ai-design/2026-10-24-platform-v5.1-dev-bump/README.md b/docs/ai-design/2026-10-24-platform-v5.1-dev-bump/README.md index 50e72fb63..387c531ac 100644 --- a/docs/ai-design/2026-10-24-platform-v5.1-dev-bump/README.md +++ b/docs/ai-design/2026-10-24-platform-v5.1-dev-bump/README.md @@ -52,8 +52,6 @@ proof is never checked against the wrong schema. - `context/platform_version_guard.rs` compares the protocol 13 client tables with a recorded snapshot, so an upstream change to them is reviewed. -- `backend_task/protocol_13_transitions.rs` checks that transitions DET builds - on a protocol 13 network use encodings protocol 13 decodes. ## Mainnet compatibility diff --git a/src/backend_task/mod.rs b/src/backend_task/mod.rs index 09811cb07..1a2e193b5 100644 --- a/src/backend_task/mod.rs +++ b/src/backend_task/mod.rs @@ -60,8 +60,6 @@ pub mod grovestark; pub mod identity; pub mod migration; pub mod platform_info; -#[cfg(test)] -mod protocol_13_transitions; pub mod register_contract; pub mod shielded; pub mod system_task; diff --git a/src/backend_task/protocol_13_transitions.rs b/src/backend_task/protocol_13_transitions.rs deleted file mode 100644 index ffa25e3f4..000000000 --- a/src/backend_task/protocol_13_transitions.rs +++ /dev/null @@ -1,308 +0,0 @@ -//! State transitions built the way DET builds them on a protocol 13 network -//! (mainnet and testnet today): only encodings protocol 13 decodes. - -use std::collections::BTreeMap; -use std::sync::Arc; - -use async_trait::async_trait; -use dash_sdk::dpp::ProtocolError; -use dash_sdk::dpp::address_funds::AddressWitness; -use dash_sdk::dpp::dashcore::Network; -use dash_sdk::dpp::data_contract::accessors::v0::DataContractV0Getters; -use dash_sdk::dpp::data_contract::config::DataContractConfig; -use dash_sdk::dpp::data_contract::document_type::DocumentType; -use dash_sdk::dpp::document::{Document, DocumentV0}; -use dash_sdk::dpp::identity::accessors::IdentityGettersV0; -use dash_sdk::dpp::identity::identity_public_key::v0::IdentityPublicKeyV0; -use dash_sdk::dpp::identity::signer::Signer; -use dash_sdk::dpp::identity::{Identity, IdentityPublicKey, KeyType, Purpose, SecurityLevel}; -use dash_sdk::dpp::platform_value::{BinaryData, Identifier, platform_value}; -use dash_sdk::dpp::prelude::UserFeeIncrease; -use dash_sdk::dpp::state_transition::StateTransition; -use dash_sdk::dpp::state_transition::batch_transition::BatchTransition; -use dash_sdk::dpp::state_transition::batch_transition::batched_transition::BatchedTransition; -use dash_sdk::dpp::state_transition::batch_transition::batched_transition::document_transition::DocumentTransitionV0Methods; -use dash_sdk::dpp::state_transition::batch_transition::document_base_transition::DocumentBaseTransition; -use dash_sdk::dpp::state_transition::batch_transition::methods::StateTransitionCreationOptions; -use dash_sdk::dpp::state_transition::batch_transition::methods::v0::DocumentsBatchTransitionMethodsV0; -use dash_sdk::dpp::state_transition::identity_update_transition::IdentityUpdateTransition; -use dash_sdk::dpp::state_transition::identity_update_transition::methods::IdentityUpdateTransitionMethodsV0; -use dash_sdk::dpp::state_transition::public_key_in_creation::IdentityPublicKeyInCreation; -use dash_sdk::dpp::version::PlatformVersion; - -use crate::context::AppContext; -use crate::context::test_support::test_app_context_for_network; - -/// Signs everything with placeholder bytes: these tests check what is -/// encoded, not signature validity. -#[derive(Debug)] -struct PlaceholderSigner; - -#[async_trait] -impl Signer for PlaceholderSigner { - async fn sign( - &self, - _key: &IdentityPublicKey, - _data: &[u8], - ) -> Result { - Ok(BinaryData::new(vec![0; 65])) - } - - async fn sign_create_witness( - &self, - _key: &IdentityPublicKey, - _data: &[u8], - ) -> Result { - Err(ProtocolError::Generic( - "no address witnesses here".to_string(), - )) - } - - fn can_sign_with(&self, _key: &IdentityPublicKey) -> bool { - true - } -} - -/// A mainnet context, which builds at protocol 13 until the network says -/// otherwise. -fn mainnet_context() -> (tempfile::TempDir, Arc, &'static PlatformVersion) { - let tmp = tempfile::tempdir().expect("tempdir"); - let ctx = test_app_context_for_network(tmp.path(), Network::Mainnet); - let platform_version = ctx.platform_version(); - assert_eq!(platform_version.protocol_version, 13); - (tmp, ctx, platform_version) -} - -fn plain_key(id: u32, security_level: SecurityLevel) -> IdentityPublicKey { - IdentityPublicKey::from(IdentityPublicKeyV0 { - id, - purpose: Purpose::AUTHENTICATION, - security_level, - contract_bounds: None, - key_type: KeyType::ECDSA_SECP256K1, - read_only: false, - data: vec![2; 33].into(), - disabled_at: None, - }) -} - -/// A key without limits, added on a protocol 13 network, goes out as the -/// version 0 key encoding protocol 13 decodes. -#[tokio::test] -async fn a_plain_key_is_added_in_the_protocol_13_encoding() { - let (_tmp, _ctx, platform_version) = mainnet_context(); - let mut identity = Identity::create_basic_identity(Identifier::from([4; 32]), platform_version) - .expect("identity"); - identity.add_public_key(plain_key(0, SecurityLevel::MASTER)); - - let transition = IdentityUpdateTransition::try_from_identity_with_signer( - &identity, - &0, - vec![plain_key(1, SecurityLevel::HIGH)], - vec![], - 1, - UserFeeIncrease::default(), - &PlaceholderSigner, - platform_version, - None, - ) - .await - .expect("identity update"); - - let StateTransition::IdentityUpdate(IdentityUpdateTransition::V0(update)) = transition else { - panic!("expected a version 0 identity update, got {transition:?}"); - }; - assert_eq!(update.add_public_keys.len(), 1); - assert!(matches!( - update.add_public_keys[0], - IdentityPublicKeyInCreation::V0(_) - )); -} - -fn note_contract(platform_version: &PlatformVersion) -> dash_sdk::platform::DataContract { - use dash_sdk::dpp::data_contract::v1::DataContractV1; - - let id = Identifier::from([8; 32]); - let config = DataContractConfig::default_for_version(platform_version).expect("config"); - let schema = platform_value!({ - "type": "object", - "properties": {"name": {"type": "string", "position": 0, "maxLength": 63_u32}}, - "required": ["name"], - "additionalProperties": false, - }); - let note = DocumentType::try_from_schema( - id, - 1, - config.version(), - "note", - schema, - None, - &BTreeMap::new(), - &config, - true, - &mut Vec::new(), - platform_version, - ) - .expect("document type"); - dash_sdk::platform::DataContract::V1(DataContractV1 { - id, - version: 1, - owner_id: Identifier::from([7; 32]), - document_types: BTreeMap::from([("note".to_string(), note)]), - config, - schema_defs: None, - groups: BTreeMap::new(), - tokens: BTreeMap::new(), - keywords: vec![], - created_at: None, - updated_at: None, - created_at_block_height: None, - updated_at_block_height: None, - created_at_epoch: None, - updated_at_epoch: None, - description: None, - }) -} - -fn note(owner_id: Identifier) -> Document { - Document::V0(DocumentV0 { - moderated_at: None, - moderated_by: None, - id: Identifier::from([9; 32]), - owner_id, - creator_id: None, - properties: BTreeMap::from([("name".to_string(), "hello".into())]), - revision: Some(1), - created_at: None, - updated_at: None, - transferred_at: None, - created_at_block_height: None, - updated_at_block_height: None, - transferred_at_block_height: None, - created_at_core_block_height: None, - updated_at_core_block_height: None, - transferred_at_core_block_height: None, - contract_version: None, - }) -} - -async fn create_note( - platform_version: &PlatformVersion, - options: Option, -) -> Result> { - let contract = note_contract(platform_version); - let document_type = contract.document_type_for_name("note").expect("note type"); - let owner_id = Identifier::from([4; 32]); - BatchTransition::new_document_creation_transition_from_document( - note(owner_id), - document_type, - [3; 32], - &plain_key(1, SecurityLevel::HIGH), - 1, - UserFeeIncrease::default(), - None, - &PlaceholderSigner, - platform_version, - options, - ) - .await - .map_err(Box::new) -} - -/// A document created on a protocol 13 network uses a document base that -/// protocol 13 decodes: not the protocol 14 one that carries a fee agreement. -#[tokio::test] -async fn a_document_is_created_in_the_protocol_13_encoding() { - let (_tmp, _ctx, platform_version) = mainnet_context(); - - let transition = create_note(platform_version, None) - .await - .expect("document create"); - - let StateTransition::Batch(batch) = transition else { - panic!("expected a batch, got {transition:?}"); - }; - let base = match &batch { - BatchTransition::V0(v0) => v0.transitions[0].base().clone(), - BatchTransition::V1(v1) => match &v1.transitions[0] { - BatchedTransition::Document(document) => document.base().clone(), - other => panic!("expected a document transition, got {other:?}"), - }, - }; - assert!( - !matches!(base, DocumentBaseTransition::V2(_)), - "protocol 13 cannot decode {base:?}" - ); -} - -/// A contested username is created on a protocol 13 network the way -/// `BroadcastDocument` creates it, with the contest fund DET states: in the -/// protocol 13 document encoding, and stating exactly the protocol's base -/// fund. -#[tokio::test] -async fn a_contested_name_is_created_in_the_protocol_13_encoding_with_the_base_fund() { - use crate::model::fee_estimation::{contest_fee_credits, document_create_contest_fund_cap}; - use dash_sdk::dpp::state_transition::batch_transition::batched_transition::document_create_transition::v0::v0_methods::DocumentCreateTransitionV0Methods; - use dash_sdk::dpp::state_transition::batch_transition::batched_transition::document_transition::DocumentTransition; - use dash_sdk::dpp::system_data_contracts::{SystemDataContract, load_system_data_contract}; - - let (_tmp, _ctx, platform_version) = mainnet_context(); - let contract = load_system_data_contract(SystemDataContract::DPNS, platform_version) - .expect("DPNS contract"); - let domain = contract - .document_type_for_name("domain") - .expect("domain type"); - let owner_id = Identifier::from([4; 32]); - let document = crate::context::test_support::dpns_name_document( - owner_id, - "alice", - dash_sdk::dpp::platform_value::Value::Identifier(owner_id.to_buffer()), - ); - let fund = document_create_contest_fund_cap(domain, &document, platform_version) - .expect("contest check") - .expect("a contested name"); - assert_eq!(fund, contest_fee_credits(platform_version)); - let options = StateTransitionCreationOptions { - contest_fund: Some(fund), - ..Default::default() - }; - - let transition = BatchTransition::new_document_creation_transition_from_document( - document, - domain, - [3; 32], - &plain_key(1, SecurityLevel::HIGH), - 1, - UserFeeIncrease::default(), - None, - &PlaceholderSigner, - platform_version, - Some(options), - ) - .await - .expect("contested name create"); - - let StateTransition::Batch(batch) = transition else { - panic!("expected a batch, got {transition:?}"); - }; - let document_transition = match &batch { - BatchTransition::V0(v0) => v0.transitions[0].clone(), - BatchTransition::V1(v1) => match &v1.transitions[0] { - BatchedTransition::Document(document) => document.clone(), - other => panic!("expected a document transition, got {other:?}"), - }, - }; - assert!( - !matches!(document_transition.base(), DocumentBaseTransition::V2(_)), - "protocol 13 cannot decode {:?}", - document_transition.base() - ); - let DocumentTransition::Create(create) = document_transition else { - panic!("expected a create, got {document_transition:?}"); - }; - let stated = create - .prefunded_voting_balance() - .as_ref() - .map(|(_, credits)| *credits); - assert_eq!(stated, Some(contest_fee_credits(platform_version))); -} From 0555714f9d68cfe95071dd0dc2d56b62a8c026a4 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Mon, 26 Oct 2026 09:10:58 +0000 Subject: [PATCH 17/27] refactor(context): read the network's protocol version from the SDK The SDK at this pin already tracks the version the network has proven: it records the version of every response whose proof and quorum signature it verified, and a rebuilt connection is a new SDK that starts from nothing. DET's own copy, raised from proved epochs and DPNS reads and reset on every rebuild, duplicated that state. The app now reads the current SDK's verified version; capabilities still stay closed before a version is proven and for one this build does not define, and network info shows the proven number or says none is confirmed. Proved epochs still set the fee multiplier, in epoch order and only on the current connection. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- .../README.md | 45 +++-- src/backend_task/mod.rs | 5 +- src/backend_task/platform_info.rs | 109 +++++------ src/backend_task/shielded/mod.rs | 5 +- src/context/feature_gate.rs | 45 ++--- src/context/mod.rs | 171 ++++++++---------- src/context/test_support.rs | 22 +++ src/mcp/resolve.rs | 42 ++--- tests/backend-e2e/platform_info.rs | 5 +- 9 files changed, 217 insertions(+), 232 deletions(-) diff --git a/docs/ai-design/2026-10-24-platform-v5.1-dev-bump/README.md b/docs/ai-design/2026-10-24-platform-v5.1-dev-bump/README.md index 387c531ac..f6248ec5d 100644 --- a/docs/ai-design/2026-10-24-platform-v5.1-dev-bump/README.md +++ b/docs/ai-design/2026-10-24-platform-v5.1-dev-bump/README.md @@ -9,22 +9,26 @@ It supersedes the open "Devnet protocol seed" item of ## Which protocol version DET builds for -There are two version sources, and they can disagree while a network moves -from 13 to 14: +DET keeps no copy of the network's protocol version. Both values below come +from the current SDK, and a rebuilt connection is a new SDK that starts from +scratch: - **The SDK's version** (`AppContext::platform_version()`). It starts at upstream's per-network `dash_sdk::sdk::min_protocol_version` (13 on mainnet, testnet and regtest, 14 on devnet) and ratchets to what verified responses - report. Transitions are encoded at this version. The SDK is no longer pinned - to a fixed table: a pinned SDK cannot build any protocol 14 transition, and - cannot follow the versioned identity lookup below. -- **The network's confirmed version** (`AppContext::platform_protocol_version()`). - It comes from a proved epoch read (or, when that fails, a proved DPNS fetch). - It is `0` until confirmed. Capabilities (`context/feature_gate.rs`) read only - this one, through `known_protocol_version`, so an unconfirmed network, or a - version this build does not define, never unlocks a protocol feature. - `connected_platform_version()` selects that version's tables for estimates, - such as the daily withdrawal limit. + report, never to a version this build does not define. Transitions are + encoded at this version, and its tables serve estimates such as the daily + withdrawal limit. The SDK is no longer pinned to a fixed table: a pinned SDK + cannot build any protocol 14 transition, and cannot follow the versioned + identity lookup below. +- **The network's proven version** (`AppContext::proven_protocol_version()`), + the SDK's `verified_protocol_version`: the highest version seen in a + response whose proof and quorum signature were verified, or `None` before + any. It can be a version this build does not define. + `AppContext::proven_platform_version()` is `None` then, as it is before + anything is proven. Capabilities (`context/feature_gate.rs`) read only that, + so an unproven network, or an unknown version, never unlocks a protocol + feature. Network info shows the proven number, or says none is confirmed. ## System contracts @@ -38,15 +42,16 @@ proof is never checked against the wrong schema. ## Connection state -- **Epoch adoption.** A proved epoch sets the fee multiplier and raises the - protocol version (`AppContext::adopt_epoch`). Reads can finish out of order, - so an epoch older than the adopted one is ignored, and the protocol version - only moves up. +- **Epoch adoption.** A proved epoch sets the fee multiplier + (`AppContext::adopt_epoch`). Reads can finish out of order, so an epoch older + than the adopted one is ignored. When the epoch cannot be read, a proved + DPNS fetch still lets the SDK learn the network's version. - **Rebuilding the connection** (`replace_connection`, `rebuild_connection`) - bumps a generation counter, forgets the adopted epoch, and puts the protocol - version back to unknown and the fee multiplier to the default, under one - lock, before the new SDK is published. A read begun on the old connection is - dropped. Capabilities stay closed until the new connection proves its epoch. + bumps a generation counter, forgets the adopted epoch and puts the fee + multiplier back to the default, under one lock, before the new SDK is + published. A read begun on the old connection is dropped. The new SDK has + proven no version, so capabilities stay closed until the new connection + proves one. ## Protocol 13 guards diff --git a/src/backend_task/mod.rs b/src/backend_task/mod.rs index 1a2e193b5..59fc1cbed 100644 --- a/src/backend_task/mod.rs +++ b/src/backend_task/mod.rs @@ -2387,7 +2387,10 @@ mod tests { let tmp = tempfile::tempdir().expect("tempdir"); let ctx = test_app_context(tmp.path()); - ctx.set_platform_protocol_version(SHIELDED_POOL_INITIAL_PROTOCOL_VERSION); + crate::context::test_support::prove_protocol_version( + &ctx, + SHIELDED_POOL_INITIAL_PROTOCOL_VERSION, + ); assert!(!FeatureGate::ShieldedOperations.is_available(&ctx)); let (tx, _rx) = tokio::sync::mpsc::channel::(32); let sender = SenderAsync::new(tx, ctx.egui_ctx().clone()); diff --git a/src/backend_task/platform_info.rs b/src/backend_task/platform_info.rs index 1aa2ad953..2d989e428 100644 --- a/src/backend_task/platform_info.rs +++ b/src/backend_task/platform_info.rs @@ -564,9 +564,10 @@ fn build_withdrawals_result( impl AppContext { /// Fetches the current epoch — authenticated by proof — and adopts its - /// fee multiplier and protocol version. The only path by which the fee - /// multiplier cache takes a network value, and fee estimates follow it, - /// so it must never come from an unproven source. + /// fee multiplier. The only path by which the fee multiplier cache takes + /// a network value, and fee estimates follow it, so it must never come + /// from an unproven source. The verified response also ratchets the SDK's + /// protocol version. pub(crate) async fn refresh_current_epoch( &self, sdk: &Sdk, @@ -583,14 +584,13 @@ impl AppContext { Ok(epoch_info) } - /// Adopts the fee multiplier and protocol version of a proved epoch read - /// on connection `generation` (see `AppContext::adopt_epoch`). + /// Adopts the fee multiplier of a proved epoch read on connection + /// `generation` (see `AppContext::adopt_epoch`). fn adopt_epoch_info(&self, generation: u64, epoch_info: &ExtendedEpochInfo) { self.adopt_epoch( generation, epoch_info.index(), epoch_info.fee_multiplier_permille(), - epoch_info.protocol_version(), ); } @@ -643,27 +643,18 @@ impl AppContext { %error, "Current epoch fetch failed; keeping the last known fee multiplier" ); - // Without the epoch, the network's version is learned from - // a proved DPNS fetch: the version of the responses the SDK - // verified, never its local seed. - let (generation, on_current_sdk) = self.connection_snapshot_for(sdk); - match DataContract::fetch(sdk, self.dpns_contract().id()).await { - Ok(_) if on_current_sdk => { - if let Some(version) = network_proved_protocol_version(sdk) { - self.raise_platform_protocol_version_on(generation, version); - } - } - Ok(_) => {} - Err(error) => tracing::warn!( + // Without the epoch, a proved DPNS fetch lets the SDK learn + // the network's version from the response it verifies. + if let Err(error) = + DataContract::fetch(sdk, self.dpns_contract().id()).await + { + tracing::warn!( %error, "Protocol-version ratchet trigger (DPNS contract fetch) failed; \ the network's protocol version stays unconfirmed" - ), + ); } - let confirmed = match self.platform_protocol_version() { - 0 => None, - version => Some(version), - }; + let confirmed = self.proven_protocol_version(); Ok(BackendTaskSuccessResult::PlatformInfo( PlatformInfoTaskResult::TextResult( format_unavailable_current_epoch_info( @@ -761,7 +752,7 @@ impl AppContext { &withdrawal_docs, total_credits.0, self.network, - self.connected_platform_version(), + self.platform_version(), )?; Ok(BackendTaskSuccessResult::PlatformInfo( PlatformInfoTaskResult::TextResult(formatted), @@ -1001,17 +992,6 @@ fn completed_withdrawal_statuses() -> Vec { ] } -/// The protocol version `sdk` has seen in a response whose proof and quorum -/// signature were verified, or `None` before any such response. Never the -/// SDK's local seed: the SDK's own version starts at the network's upstream -/// minimum and only rises, so a successful read does not prove it. -fn network_proved_protocol_version(sdk: &Sdk) -> Option { - match sdk.query_settings().verified_protocol_version { - 0 => None, - version => Some(version), - } -} - #[cfg(test)] mod tests { use super::*; @@ -1071,13 +1051,15 @@ mod tests { /// fresh SDK has verified nothing, so nothing is proved. #[test] fn a_fresh_sdk_proves_no_protocol_version() { - let sdk = Sdk::new_mock(); + let temp_dir = tempfile::tempdir().expect("tempdir"); + let ctx = crate::context::test_support::test_app_context(temp_dir.path()); + ctx.sdk.store(Arc::new(Sdk::new_mock())); assert_ne!( - sdk.protocol_version_number(), + ctx.sdk.load().protocol_version_number(), 0, "precondition: a seeded SDK" ); - assert_eq!(network_proved_protocol_version(&sdk), None); + assert_eq!(ctx.proven_protocol_version(), None); } /// The limit is whatever the network's own protocol version says it is, @@ -1108,10 +1090,9 @@ mod tests { ); } - /// A proved epoch sets both the fee multiplier cache and the protocol - /// version. + /// A proved epoch sets the fee multiplier cache. #[test] - fn a_proved_epoch_updates_the_fee_multiplier_and_protocol_version() { + fn a_proved_epoch_updates_the_fee_multiplier() { use dash_sdk::dpp::block::extended_epoch_info::v0::ExtendedEpochInfoV0; let temp_dir = tempfile::tempdir().expect("tempdir"); let ctx = crate::context::test_support::test_app_context(temp_dir.path()); @@ -1130,11 +1111,10 @@ mod tests { ); assert_eq!(ctx.fee_multiplier_permille(), 1_500); - assert_eq!(ctx.platform_protocol_version(), 14); } /// Epoch reads can finish out of order: an older epoch arriving after a - /// newer one must not put back its fee multiplier or protocol version. + /// newer one must not put back its fee multiplier. #[test] fn an_older_epoch_does_not_overwrite_a_newer_one() { use dash_sdk::dpp::block::extended_epoch_info::v0::ExtendedEpochInfoV0; @@ -1154,36 +1134,34 @@ mod tests { ctx.adopt_epoch_info(ctx.connection_generation(), &epoch(8, 1_500, 14)); ctx.adopt_epoch_info(ctx.connection_generation(), &epoch(7, 1_000, 13)); assert_eq!(ctx.fee_multiplier_permille(), 1_500); - assert_eq!(ctx.platform_protocol_version(), 14); - // The same or a newer epoch is adopted; the protocol version never - // goes back. + // The same or a newer epoch is adopted. ctx.adopt_epoch_info(ctx.connection_generation(), &epoch(8, 1_600, 14)); assert_eq!(ctx.fee_multiplier_permille(), 1_600); ctx.adopt_epoch_info(ctx.connection_generation(), &epoch(9, 1_700, 13)); assert_eq!(ctx.fee_multiplier_permille(), 1_700); - assert_eq!(ctx.platform_protocol_version(), 14); } /// Rebuilding the connection resets everything learned from the old one: - /// the protocol version goes back to unknown, the fee multiplier to the - /// default, and the next proved epoch is adopted whatever its index. + /// the new SDK has proven no protocol version, the fee multiplier goes + /// back to the default, and the next proved epoch is adopted whatever its + /// index. #[test] fn a_rebuilt_connection_starts_from_unknown_protocol_and_default_fees() { let temp_dir = tempfile::tempdir().expect("tempdir"); let ctx = crate::context::test_support::test_app_context(temp_dir.path()); - ctx.adopt_epoch(ctx.connection_generation(), 8, 1_500, 14); + crate::context::test_support::prove_protocol_version(&ctx, 14); + ctx.adopt_epoch(ctx.connection_generation(), 8, 1_500); ctx.replace_connection(Arc::new(Sdk::new_mock())); - assert_eq!(ctx.platform_protocol_version(), 0); + assert_eq!(ctx.proven_protocol_version(), None); assert_eq!( ctx.fee_multiplier_permille(), crate::model::fee_estimation::PlatformFeeEstimator::DEFAULT_FEE_MULTIPLIER_PERMILLE ); - assert!(ctx.adopt_epoch(ctx.connection_generation(), 2, 1_100, 13)); + assert!(ctx.adopt_epoch(ctx.connection_generation(), 2, 1_100)); assert_eq!(ctx.fee_multiplier_permille(), 1_100); - assert_eq!(ctx.platform_protocol_version(), 13); } /// An epoch read that started on the old connection and finishes after @@ -1197,8 +1175,7 @@ mod tests { ctx.replace_connection(Arc::new(Sdk::new_mock())); - assert!(!ctx.adopt_epoch(before_rebuild, 9, 1_500, 14)); - assert_eq!(ctx.platform_protocol_version(), 0); + assert!(!ctx.adopt_epoch(before_rebuild, 9, 1_500)); assert_eq!( ctx.fee_multiplier_permille(), crate::model::fee_estimation::PlatformFeeEstimator::DEFAULT_FEE_MULTIPLIER_PERMILLE @@ -1270,23 +1247,35 @@ mod tests { } /// A failed ratchet trigger leaves the SDK reporting its local seed, which is - /// not a network observation: caching it would open the shielded capability - /// gate and defeat the `0` retry sentinel `mcp::resolve` polls. + /// not a network observation: treating it as proven would open the shielded + /// capability gate and stop the retry `mcp::resolve` makes while nothing is + /// proven. The report names the version as unconfirmed. #[tokio::test] async fn a_failed_ratchet_trigger_leaves_the_protocol_version_unconfirmed() { let temp_dir = tempfile::tempdir().expect("tempdir"); let ctx = crate::context::test_support::test_app_context(temp_dir.path()); - let sdk = dash_sdk::Sdk::new_mock(); + ctx.sdk.store(Arc::new(dash_sdk::Sdk::new_mock())); + let sdk = ctx.sdk.load_full(); assert_ne!( sdk.protocol_version_number(), 0, "precondition: the mock SDK seeds a local version of its own" ); - ctx.run_platform_info_task(PlatformInfoTaskRequestType::CurrentEpochInfo, &sdk) + let result = ctx + .run_platform_info_task(PlatformInfoTaskRequestType::CurrentEpochInfo, &sdk) .await .expect("a failed epoch fetch degrades to a text result"); - assert_eq!(ctx.platform_protocol_version(), 0); + assert_eq!(ctx.proven_protocol_version(), None); + let BackendTaskSuccessResult::PlatformInfo(PlatformInfoTaskResult::TextResult(text)) = + result + else { + panic!("a failed epoch fetch returns a text result"); + }; + assert!( + text.contains("has not confirmed one yet"), + "an unconfirmed version must be named as such, got: {text}" + ); } } diff --git a/src/backend_task/shielded/mod.rs b/src/backend_task/shielded/mod.rs index be0c365db..26a9aae28 100644 --- a/src/backend_task/shielded/mod.rs +++ b/src/backend_task/shielded/mod.rs @@ -342,7 +342,10 @@ mod tests { let tmp = tempfile::tempdir().expect("tempdir"); let ctx = test_app_context(tmp.path()); - ctx.set_platform_protocol_version(SHIELDED_POOL_INITIAL_PROTOCOL_VERSION); + crate::context::test_support::prove_protocol_version( + &ctx, + SHIELDED_POOL_INITIAL_PROTOCOL_VERSION, + ); assert!(!FeatureGate::ShieldedOperations.is_available(&ctx)); assert!(ctx.wallet_backend().is_err(), "precondition"); diff --git a/src/context/feature_gate.rs b/src/context/feature_gate.rs index 014d31dcf..502640abb 100644 --- a/src/context/feature_gate.rs +++ b/src/context/feature_gate.rs @@ -1,6 +1,5 @@ use crate::context::AppContext; use crate::model::user_role::UserRole; -use dash_sdk::dpp::version::PlatformVersion; use dash_sdk::dpp::version::feature_initial_protocol_versions::SHIELDED_POOL_INITIAL_PROTOCOL_VERSION; /// Shielded state transitions activate at upstream rs-platform-version's @@ -29,20 +28,16 @@ pub enum Capability { ShieldedProtocol, } -/// The connected network's protocol version, when this build defines it. +/// Whether the protocol version the connected network has proven is one this +/// build defines and is at least `activation`. /// -/// `None` before the version is fetched (0) and for a version newer than any -/// this build knows, which a proved epoch can report after a network upgrade. -/// Every capability reads the version through this, so both fail closed: this -/// build cannot tell what an unknown version's rules allow. -fn known_protocol_version(ctx: &AppContext) -> Option<&'static PlatformVersion> { - PlatformVersion::get_optional(ctx.platform_protocol_version()) -} - -/// Whether the connected protocol version is one this build defines and is -/// at least `activation`. +/// Every capability reads the version through this, so it fails closed both +/// before the network proves a version and for a version newer than any this +/// build knows ([`AppContext::proven_platform_version`]): this build cannot +/// tell what an unknown version's rules allow. fn known_protocol_version_at_least(ctx: &AppContext, activation: u32) -> bool { - known_protocol_version(ctx).is_some_and(|version| version.protocol_version >= activation) + ctx.proven_platform_version() + .is_some_and(|version| version.protocol_version >= activation) } impl Capability { @@ -188,7 +183,7 @@ impl FeatureGate { #[cfg(test)] mod tests { use super::*; - use crate::context::test_support::test_app_context; + use crate::context::test_support::{prove_protocol_version, test_app_context}; use dash_sdk::dpp::version::PlatformVersion; use std::sync::Arc; @@ -287,7 +282,7 @@ mod tests { fn shielded_capability_is_unmet_before_a_version_is_fetched() { let (_tmp, ctx) = ctx_with_role(UserRole::Developer); - assert_eq!(ctx.platform_protocol_version(), 0, "boot value"); + assert_eq!(ctx.proven_protocol_version(), None, "boot value"); assert!(!Capability::ShieldedProtocol.is_met(&ctx)); assert!( !FeatureGate::ShieldedOperations.is_available(&ctx), @@ -313,7 +308,7 @@ mod tests { // Every protocol version upstream defines — none of which the tab // gate depends on. for version in known_protocol_versions() { - ctx.set_platform_protocol_version(version); + prove_protocol_version(&ctx, version); assert!( FeatureGate::Shielded.is_available(&ctx), "Shielded tab must be available on protocol v{version} for {role:?}" @@ -329,7 +324,7 @@ mod tests { assert!(!versions.is_empty(), "the probe must find some versions"); for version in versions { - ctx.set_platform_protocol_version(version); + prove_protocol_version(&ctx, version); assert_eq!( Capability::ShieldedProtocol.is_met(&ctx), version >= SHIELDED_POOL_INITIAL_PROTOCOL_VERSION, @@ -342,7 +337,7 @@ mod tests { fn shielded_operations_are_available_at_activation_for_unlocked_roles() { for role in [UserRole::Power, UserRole::Developer] { let (_tmp, ctx) = ctx_with_role(role); - ctx.set_platform_protocol_version(SHIELDED_POOL_INITIAL_PROTOCOL_VERSION); + prove_protocol_version(&ctx, SHIELDED_POOL_INITIAL_PROTOCOL_VERSION); assert!( FeatureGate::ShieldedOperations.is_available(&ctx), @@ -357,7 +352,7 @@ mod tests { let version = SHIELDED_POOL_INITIAL_PROTOCOL_VERSION .checked_sub(1) .expect("shielded activation must follow the boot protocol version"); - ctx.set_platform_protocol_version(version); + prove_protocol_version(&ctx, version); assert!(!FeatureGate::ShieldedOperations.is_available(&ctx)); } @@ -365,7 +360,7 @@ mod tests { #[test] fn shielded_operations_are_unavailable_at_activation_for_everyday_user() { let (_tmp, ctx) = ctx_with_role(UserRole::Everyday); - ctx.set_platform_protocol_version(SHIELDED_POOL_INITIAL_PROTOCOL_VERSION); + prove_protocol_version(&ctx, SHIELDED_POOL_INITIAL_PROTOCOL_VERSION); assert!(!FeatureGate::ShieldedOperations.is_available(&ctx)); } @@ -376,21 +371,21 @@ mod tests { let version = SHIELDED_POOL_INITIAL_PROTOCOL_VERSION .checked_sub(1) .expect("shielded activation must follow the boot protocol version"); - ctx.set_platform_protocol_version(version); + prove_protocol_version(&ctx, version); assert_eq!( FeatureGate::ShieldedOperations.first_unmet_check(&ctx), Some(Check::Capability(Capability::ShieldedProtocol)) ); let (_tmp, ctx) = ctx_with_role(UserRole::Everyday); - ctx.set_platform_protocol_version(SHIELDED_POOL_INITIAL_PROTOCOL_VERSION); + prove_protocol_version(&ctx, SHIELDED_POOL_INITIAL_PROTOCOL_VERSION); assert_eq!( FeatureGate::ShieldedOperations.first_unmet_check(&ctx), Some(Check::Experimental(ExperimentalFeature::Shielded)) ); let (_tmp, ctx) = ctx_with_role(UserRole::Developer); - ctx.set_platform_protocol_version(SHIELDED_POOL_INITIAL_PROTOCOL_VERSION); + prove_protocol_version(&ctx, SHIELDED_POOL_INITIAL_PROTOCOL_VERSION); assert_eq!( FeatureGate::ShieldedOperations.first_unmet_check(&ctx), None @@ -444,7 +439,7 @@ mod tests { let (_tmp, ctx) = ctx_with_role(UserRole::Developer); let capabilities = [Capability::ShieldedProtocol]; - ctx.set_platform_protocol_version(LATEST_VERSION); + prove_protocol_version(&ctx, LATEST_VERSION); for capability in capabilities { assert!( capability.is_met(&ctx), @@ -452,7 +447,7 @@ mod tests { ); } - ctx.set_platform_protocol_version(LATEST_VERSION + 1); + prove_protocol_version(&ctx, LATEST_VERSION + 1); for capability in capabilities { assert!( !capability.is_met(&ctx), diff --git a/src/context/mod.rs b/src/context/mod.rs index f2c62f63c..e7aa3c93b 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -69,7 +69,7 @@ use platform_wallet_storage::secrets::SecretStore; use std::collections::{BTreeMap, HashMap, HashSet}; use std::path::PathBuf; use std::str::FromStr as _; -use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering}; +use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use std::sync::{Arc, Mutex, RwLock, RwLockWriteGuard}; use system_contracts::{SystemContracts, SystemContractsCache}; @@ -323,10 +323,6 @@ pub struct AppContext { /// Cached fee multiplier permille from current epoch (1000 = 1x, 2000 = 2x) /// Updated when epoch info is fetched from Platform fee_multiplier_permille: AtomicU64, - /// Cached protocol version from the current epoch on the connected network. - /// Updated alongside fee_multiplier when epoch info is fetched. - /// 0 means not yet fetched from the network. - platform_protocol_version: AtomicU32, /// The connection generation and the last adopted proved epoch. See /// [`Self::adopt_epoch`] and [`Self::replace_connection`]. epoch_adoption: Mutex, @@ -654,7 +650,6 @@ impl AppContext { fee_multiplier_permille: AtomicU64::new( PlatformFeeEstimator::DEFAULT_FEE_MULTIPLIER_PERMILLE, ), - platform_protocol_version: AtomicU32::new(0), epoch_adoption: Mutex::new(EpochAdoption::default()), shielded_balances: Arc::new(Mutex::new(std::collections::HashMap::new())), shielded_addresses: Arc::new(Mutex::new(std::collections::HashMap::new())), @@ -860,29 +855,6 @@ impl AppContext { .store(multiplier, Ordering::Relaxed); } - /// Get the cached platform protocol version from the connected network. - /// Returns 0 if not yet fetched from the network. - pub fn platform_protocol_version(&self) -> u32 { - self.platform_protocol_version.load(Ordering::Relaxed) - } - - /// Cache the platform protocol version fetched from the connected network. - /// - /// Read by the shielded-operations capability check to decide whether the - /// network can settle shielded state transitions. `0` means "not fetched - /// yet", which reads as below any activation version. - pub fn set_platform_protocol_version(&self, version: u32) { - self.platform_protocol_version - .store(version, Ordering::Relaxed); - } - - /// Raise the cached platform protocol version to `version`; a lower one - /// is ignored. What the network proves never moves it back. - pub(crate) fn raise_platform_protocol_version(&self, version: u32) { - self.platform_protocol_version - .fetch_max(version, Ordering::Relaxed); - } - /// Synchronous read of the frame-safe shielded balance for `seed_hash`. /// /// Returns the total shielded balance in duffs, summed across all Orchard accounts. @@ -1156,19 +1128,28 @@ impl AppContext { self.system_contracts.try_get(platform_version) } - /// The platform version of the connected network, once its protocol - /// version has been fetched; [`Self::platform_version`] until then, or for - /// a protocol version this build does not know. - /// - /// For values that differ between protocol versions and must match the - /// network, such as fees introduced by a newer protocol. - pub fn connected_platform_version(&self) -> &'static PlatformVersion { - match self.platform_protocol_version() { - 0 => self.platform_version(), - version => PlatformVersion::get_optional(version).unwrap_or(self.platform_version()), + /// The protocol version the current SDK has seen in a response whose + /// proof and quorum signature were verified, or `None` before any such + /// response. A rebuilt connection is a new SDK and starts from `None`. + /// The number may be one this build does not define; see + /// [`Self::proven_platform_version`]. + pub fn proven_protocol_version(&self) -> Option { + match self.sdk.load().query_settings().verified_protocol_version { + 0 => None, + version => Some(version), } } + /// The platform version the connected network has proven + /// ([`Self::proven_protocol_version`]), when this build defines it. + /// `None` before the network proves a version and for a version newer + /// than any this build knows, so every capability reading it fails + /// closed. + pub fn proven_platform_version(&self) -> Option<&'static PlatformVersion> { + self.proven_protocol_version() + .and_then(PlatformVersion::get_optional) + } + pub fn state_transition_options(&self) -> Option { // Signing override: only the Developer role may relax the security-level // and purpose checks when signing a state transition. @@ -1201,22 +1182,20 @@ impl AppContext { .generation } - /// Adopt the fee multiplier and protocol version of the proved epoch - /// `epoch_index`, read on connection `generation`. Returns whether it was - /// adopted. + /// Adopt the fee multiplier of the proved epoch `epoch_index`, read on + /// connection `generation`. Returns whether it was adopted. /// /// Dropped when the connection was rebuilt since the read was sent, or /// when an epoch newer than it was adopted already: epoch reads run /// concurrently (platform info, fee refusals) and can finish out of - /// order. The protocol version only moves up. The lock covers the checks - /// and both stores, so an adoption and a rebuild cannot interleave; - /// nothing is awaited under it. + /// order. The lock covers the checks and the store, so an adoption and a + /// rebuild cannot interleave; nothing is awaited under it. The protocol + /// version is the SDK's own ([`Self::proven_protocol_version`]). pub(crate) fn adopt_epoch( &self, generation: u64, epoch_index: EpochIndex, fee_multiplier_permille: u64, - protocol_version: u32, ) -> bool { let mut adoption = self .epoch_adoption @@ -1231,41 +1210,22 @@ impl AppContext { } adoption.epoch_index = Some(epoch_index); self.set_fee_multiplier_permille(fee_multiplier_permille); - self.raise_platform_protocol_version(protocol_version); - true - } - - /// Raise the protocol version to `protocol_version`, proved on connection - /// `generation`; dropped if the connection was rebuilt since. Returns - /// whether it was applied. - pub(crate) fn raise_platform_protocol_version_on( - &self, - generation: u64, - protocol_version: u32, - ) -> bool { - let adoption = self - .epoch_adoption - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner); - if generation != adoption.generation { - return false; - } - self.raise_platform_protocol_version(protocol_version); true } /// Replace the connection's SDK with `new_sdk` and forget everything /// learned from the old connection: bump the generation, so reads still in - /// flight on it are dropped, forget the adopted epoch, and put the - /// protocol version back to unknown and the fee multiplier to the default. - /// The new endpoints may serve another protocol version or a reset devnet; - /// capabilities stay closed until the new connection proves its epoch. + /// flight on it are dropped, forget the adopted epoch, and put the fee + /// multiplier back to the default. The new SDK has verified nothing, so + /// the proven protocol version is unknown again and capabilities stay + /// closed until the new connection proves one: the new endpoints may serve + /// another protocol version or a reset devnet. /// /// The reset is written before the new SDK is published, under the /// adoption lock: whoever loads the new SDK sees the reset state, and a /// [`Self::connection_snapshot_for`] sees either the old generation and - /// SDK or the new ones. A task still holding the old SDK may see the reset - /// state, which closes capabilities and uses default fees: fail closed. + /// SDK or the new ones. A task still holding the old SDK may see the + /// default fees: fail closed. pub(crate) fn replace_connection(&self, new_sdk: Arc) { self.replace_connection_observed(new_sdk, |_| {}); } @@ -1279,7 +1239,6 @@ impl AppContext { adoption.generation += 1; adoption.epoch_index = None; self.set_fee_multiplier_permille(PlatformFeeEstimator::DEFAULT_FEE_MULTIPLIER_PERMILLE); - self.set_platform_protocol_version(0); observe(self); self.sdk.store(new_sdk); } @@ -2066,20 +2025,61 @@ mod tests { assert_eq!(ctx.dashpay_contract().id(), boot_dashpay.id()); } + /// The proven protocol version is the current SDK's verified one: none at + /// boot, the proven number once a verified response carried it, and none + /// again on a rebuilt connection, whose SDK has verified nothing. + #[test] + fn the_proven_protocol_version_follows_the_current_sdk() { + use crate::context::test_support::{prove_protocol_version, test_app_context}; + let temp_dir = tempfile::tempdir().expect("tempdir"); + let ctx = test_app_context(temp_dir.path()); + assert_eq!(ctx.proven_protocol_version(), None, "boot"); + assert!(ctx.proven_platform_version().is_none(), "boot"); + + prove_protocol_version(&ctx, 13); + assert_eq!(ctx.proven_protocol_version(), Some(13)); + assert_eq!( + ctx.proven_platform_version().map(|v| v.protocol_version), + Some(13) + ); + + ctx.replace_connection(Arc::new(dash_sdk::Sdk::new_mock())); + assert_eq!(ctx.proven_protocol_version(), None, "rebuilt connection"); + assert!( + ctx.proven_platform_version().is_none(), + "rebuilt connection" + ); + } + + /// A proven version this build does not define is reported as proven, but + /// no platform version is derived from it: nothing here knows its rules. + #[test] + fn an_unknown_proven_version_has_no_platform_version() { + use crate::context::test_support::{prove_protocol_version, test_app_context}; + use dash_sdk::dpp::version::LATEST_VERSION; + let temp_dir = tempfile::tempdir().expect("tempdir"); + let ctx = test_app_context(temp_dir.path()); + + prove_protocol_version(&ctx, LATEST_VERSION + 1); + + assert_eq!(ctx.proven_protocol_version(), Some(LATEST_VERSION + 1)); + assert!(ctx.proven_platform_version().is_none()); + } + /// A task that loads the new SDK always sees the reset connection state: - /// the new SDK never pairs with the old connection's protocol version or - /// fee multiplier. + /// the new SDK never pairs with the old connection's fee multiplier, and + /// it has proven no protocol version. #[test] fn the_new_sdk_is_published_only_after_the_connection_state_is_reset() { let temp_dir = tempfile::tempdir().expect("tempdir"); let ctx = crate::context::test_support::test_app_context(temp_dir.path()); - ctx.adopt_epoch(ctx.connection_generation(), 8, 1_500, 14); + ctx.adopt_epoch(ctx.connection_generation(), 8, 1_500); let new_sdk = Arc::new(dash_sdk::Sdk::new_mock()); let new_sdk_ptr = Arc::as_ptr(&new_sdk); ctx.replace_connection_observed(new_sdk, |ctx| { if Arc::as_ptr(&ctx.sdk.load_full()) == new_sdk_ptr { - assert_eq!(ctx.platform_protocol_version(), 0, "new SDK, old protocol"); + assert_eq!(ctx.proven_protocol_version(), None, "new SDK, old protocol"); assert_eq!( ctx.fee_multiplier_permille(), crate::model::fee_estimation::PlatformFeeEstimator::DEFAULT_FEE_MULTIPLIER_PERMILLE, @@ -2089,22 +2089,7 @@ mod tests { }); assert_eq!(Arc::as_ptr(&ctx.sdk.load_full()), new_sdk_ptr); - assert_eq!(ctx.platform_protocol_version(), 0); - } - - /// Until the network's version is fetched, the connected version is the - /// one the SDK builds with. - #[test] - fn connected_platform_version_falls_back_to_the_sdk_version() { - use crate::context::test_support::test_app_context_for_network; - - let tmp = tempfile::tempdir().expect("tempdir"); - let ctx = test_app_context_for_network(tmp.path(), Network::Mainnet); - assert_eq!(ctx.platform_protocol_version(), 0); - assert_eq!( - ctx.connected_platform_version().protocol_version, - ctx.sdk.load().protocol_version_number() - ); + assert_eq!(ctx.proven_protocol_version(), None); } /// Contest windows stay consistent as the SDK ratchets upward from each diff --git a/src/context/test_support.rs b/src/context/test_support.rs index b75e9bd1d..969d9d07a 100644 --- a/src/context/test_support.rs +++ b/src/context/test_support.rs @@ -186,6 +186,28 @@ pub(crate) fn set_sdk_protocol_version(ctx: &AppContext, protocol_version: u32) ctx.sdk.store(Arc::new(sdk)); } +/// Have `ctx`'s SDK learn `protocol_version` as it does from a response whose +/// proof and quorum signature were verified. Only raises it, as the network +/// does; an unknown version is recorded as proven, as upstream records it. +pub(crate) fn prove_protocol_version(ctx: &AppContext, protocol_version: u32) { + let now_ms = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .expect("clock") + .as_millis() as u64; + ctx.sdk + .load() + .verify_response_metadata( + "test", + &dash_sdk::dapi_grpc::platform::v0::ResponseMetadata { + height: 1, + time_ms: now_ms, + protocol_version, + ..Default::default() + }, + ) + .expect("fresh proved metadata"); +} + /// A mock SDK that reads data contracts through `ctx`, as the app's own SDK does. pub(crate) fn mock_sdk_for(ctx: &AppContext) -> Sdk { let sdk = Sdk::new_mock(); diff --git a/src/mcp/resolve.rs b/src/mcp/resolve.rs index 652b7c214..f6d12dce1 100644 --- a/src/mcp/resolve.rs +++ b/src/mcp/resolve.rs @@ -349,7 +349,7 @@ async fn wait_for_spv_and_refresh_platform_info( } match protocol_refresh { - ProtocolRefresh::BestEffortIfUnpopulated if ctx.platform_protocol_version() == 0 => { + ProtocolRefresh::BestEffortIfUnpopulated if ctx.proven_protocol_version().is_none() => { let _ = refresh_platform_protocol_version(ctx).await; Ok(()) } @@ -376,7 +376,7 @@ async fn refresh_platform_protocol_version(ctx: &Arc) -> Result<(), })?; tracing::trace!( - protocol_version = ctx.platform_protocol_version(), + protocol_version = ?ctx.proven_protocol_version(), "Platform epoch information refreshed for headless feature gating" ); Ok(()) @@ -449,12 +449,10 @@ mod tests { /// Mocks the DPNS-contract fetch that drives the protocol-version ratchet, /// the epoch task's fallback when the current epoch cannot be read (the - /// mock has no epoch response). The fallback takes only a version the SDK + /// mock has no epoch response). The app reads the version the SDK /// verified, and a mock expectation is made up, never verified, so the - /// SDK first accepts `LATEST_VERSION` from metadata it is told was proved, - /// as it does after a live network's quorum-signed response. A successful - /// fetch then ratchets the app exactly as a live network would; a failed - /// one leaves the version unconfirmed. + /// SDK accepts `LATEST_VERSION` from metadata it is told was proved, as + /// it does after a live network's quorum-signed response. async fn mock_sdk_with_dpns_contract(ctx: &Arc) -> Sdk { let sdk = mock_sdk_with_unproved_dpns_contract(ctx).await; sdk.verify_response_metadata( @@ -494,45 +492,31 @@ mod tests { .await .expect("refresh platform metadata"); - assert_eq!(ctx.platform_protocol_version(), 0); + assert_eq!(ctx.proven_protocol_version(), None); } + /// Once SPV runs, the headless sync completes and reads the protocol + /// version the current SDK has proven; the fee multiplier stays at the + /// default without a proved epoch. #[tokio::test] - async fn headless_sync_populates_protocol_version_after_spv_is_running() { + async fn headless_sync_reads_the_proven_protocol_version_after_spv_is_running() { let temp_dir = tempfile::tempdir().expect("tempdir"); let ctx = crate::mcp::tests::legacy_wallet_context(temp_dir.path()); ctx.sdk .store(Arc::new(mock_sdk_with_dpns_contract(&ctx).await)); ctx.connection_status().set_spv_status(SpvStatus::Running); - assert_eq!(ctx.platform_protocol_version(), 0, "boot value"); - wait_for_spv_and_refresh_platform_info(&ctx, ProtocolRefresh::BestEffortIfUnpopulated) .await .expect("headless sync completion"); - assert_eq!(ctx.platform_protocol_version(), LATEST_VERSION); + assert_eq!(ctx.proven_protocol_version(), Some(LATEST_VERSION)); assert_eq!( ctx.fee_multiplier_permille(), crate::model::fee_estimation::PlatformFeeEstimator::DEFAULT_FEE_MULTIPLIER_PERMILLE ); } - #[tokio::test] - async fn protocol_refresh_observes_activation_after_a_pre_activation_epoch() { - let temp_dir = tempfile::tempdir().expect("tempdir"); - let ctx = crate::mcp::tests::legacy_wallet_context(temp_dir.path()); - ctx.set_platform_protocol_version(LATEST_VERSION - 1); - ctx.sdk - .store(Arc::new(mock_sdk_with_dpns_contract(&ctx).await)); - - refresh_platform_protocol_version(&ctx) - .await - .expect("refresh platform metadata"); - - assert_eq!(ctx.platform_protocol_version(), LATEST_VERSION); - } - #[tokio::test] async fn best_effort_protocol_refresh_retries_without_blocking_spv_readiness() { let temp_dir = tempfile::tempdir().expect("tempdir"); @@ -543,7 +527,7 @@ mod tests { wait_for_spv_and_refresh_platform_info(&ctx, ProtocolRefresh::BestEffortIfUnpopulated) .await .expect("SPV readiness survives a Platform metadata failure"); - assert_eq!(ctx.platform_protocol_version(), 0); + assert_eq!(ctx.proven_protocol_version(), None); ctx.sdk .store(Arc::new(mock_sdk_with_dpns_contract(&ctx).await)); @@ -551,7 +535,7 @@ mod tests { .await .expect("SPV readiness retries Platform metadata"); - assert_eq!(ctx.platform_protocol_version(), LATEST_VERSION); + assert_eq!(ctx.proven_protocol_version(), Some(LATEST_VERSION)); } /// Insert a wallet straight into the in-memory map, bypassing registration diff --git a/tests/backend-e2e/platform_info.rs b/tests/backend-e2e/platform_info.rs index fc87b7922..cd5304e75 100644 --- a/tests/backend-e2e/platform_info.rs +++ b/tests/backend-e2e/platform_info.rs @@ -55,9 +55,8 @@ async fn test_pi001_current_epoch_is_fetched_live() { // The fetch is a network observation, so it confirms the protocol version. let ctx = ctx().await; - assert_ne!( - ctx.app_context.platform_protocol_version(), - 0, + assert!( + ctx.app_context.proven_protocol_version().is_some(), "a live epoch fetch must confirm the network's protocol version" ); } From e7a9a451610620847fe7bfcec56b51136b783480 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Mon, 26 Oct 2026 09:17:59 +0000 Subject: [PATCH 18/27] refactor(context): set the fee multiplier from each proved epoch read A connection rebuild never changes the chain, the multiplier changes once per epoch and feeds only fee estimates, so ordering epoch reads and dropping those begun before a rebuild guarded against nothing worth the machinery. A proved epoch read sets the multiplier directly again; an unavailable epoch still keeps the last known value and labels it as such. A rebuild still reads the epoch, so the new SDK proves the network's version again. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- .../README.md | 19 ++- src/backend_task/platform_info.rs | 108 ++------------ src/context/mod.rs | 141 ++---------------- 3 files changed, 31 insertions(+), 237 deletions(-) diff --git a/docs/ai-design/2026-10-24-platform-v5.1-dev-bump/README.md b/docs/ai-design/2026-10-24-platform-v5.1-dev-bump/README.md index f6248ec5d..adb142ed0 100644 --- a/docs/ai-design/2026-10-24-platform-v5.1-dev-bump/README.md +++ b/docs/ai-design/2026-10-24-platform-v5.1-dev-bump/README.md @@ -42,16 +42,15 @@ proof is never checked against the wrong schema. ## Connection state -- **Epoch adoption.** A proved epoch sets the fee multiplier - (`AppContext::adopt_epoch`). Reads can finish out of order, so an epoch older - than the adopted one is ignored. When the epoch cannot be read, a proved - DPNS fetch still lets the SDK learn the network's version. -- **Rebuilding the connection** (`replace_connection`, `rebuild_connection`) - bumps a generation counter, forgets the adopted epoch and puts the fee - multiplier back to the default, under one lock, before the new SDK is - published. A read begun on the old connection is dropped. The new SDK has - proven no version, so capabilities stay closed until the new connection - proves one. +- **Fee multiplier.** A proved epoch read sets the fee multiplier, which feeds + fee estimates only. When the epoch cannot be read, the last known value is + kept and shown as such, never as a fresh network reading, and a proved DPNS + fetch still lets the SDK learn the network's version. +- **Rebuilding the connection** (`rebuild_connection`) builds a new SDK over + the same network, so nothing about the chain changes. The new SDK has proven + no version, so capabilities stay closed until a proved response carries + one; the rebuild reads the epoch right away for that, and the read refreshes + the fee multiplier. ## Protocol 13 guards diff --git a/src/backend_task/platform_info.rs b/src/backend_task/platform_info.rs index 2d989e428..2a1fc82c8 100644 --- a/src/backend_task/platform_info.rs +++ b/src/backend_task/platform_info.rs @@ -563,7 +563,7 @@ fn build_withdrawals_result( } impl AppContext { - /// Fetches the current epoch — authenticated by proof — and adopts its + /// Fetches the current epoch — authenticated by proof — and sets its /// fee multiplier. The only path by which the fee multiplier cache takes /// a network value, and fee estimates follow it, so it must never come /// from an unproven source. The verified response also ratchets the SDK's @@ -572,26 +572,16 @@ impl AppContext { &self, sdk: &Sdk, ) -> Result> { - // Captured together before the read: a read on an SDK a rebuild has - // replaced, or begun before it, is never adopted. - let (generation, on_current_sdk) = self.connection_snapshot_for(sdk); let epoch_info = ExtendedEpochInfo::fetch_current(sdk) .await .map_err(Box::new)?; - if on_current_sdk { - self.adopt_epoch_info(generation, &epoch_info); - } + self.set_fee_multiplier_from(&epoch_info); Ok(epoch_info) } - /// Adopts the fee multiplier of a proved epoch read on connection - /// `generation` (see `AppContext::adopt_epoch`). - fn adopt_epoch_info(&self, generation: u64, epoch_info: &ExtendedEpochInfo) { - self.adopt_epoch( - generation, - epoch_info.index(), - epoch_info.fee_multiplier_permille(), - ); + /// Sets the fee multiplier cache from a proved epoch. + fn set_fee_multiplier_from(&self, epoch_info: &ExtendedEpochInfo) { + self.set_fee_multiplier_permille(epoch_info.fee_multiplier_permille()); } pub async fn run_platform_info_task( @@ -1098,88 +1088,16 @@ mod tests { let ctx = crate::context::test_support::test_app_context(temp_dir.path()); ctx.set_fee_multiplier_permille(1000); - ctx.adopt_epoch_info( - ctx.connection_generation(), - &ExtendedEpochInfo::V0(ExtendedEpochInfoV0 { - index: 7, - first_block_time: 0, - first_block_height: 0, - first_core_block_height: 0, - fee_multiplier_permille: 1_500, - protocol_version: 14, - }), - ); - - assert_eq!(ctx.fee_multiplier_permille(), 1_500); - } - - /// Epoch reads can finish out of order: an older epoch arriving after a - /// newer one must not put back its fee multiplier. - #[test] - fn an_older_epoch_does_not_overwrite_a_newer_one() { - use dash_sdk::dpp::block::extended_epoch_info::v0::ExtendedEpochInfoV0; - let epoch = |index, fee_multiplier_permille, protocol_version| { - ExtendedEpochInfo::V0(ExtendedEpochInfoV0 { - index, - first_block_time: 0, - first_block_height: 0, - first_core_block_height: 0, - fee_multiplier_permille, - protocol_version, - }) - }; - let temp_dir = tempfile::tempdir().expect("tempdir"); - let ctx = crate::context::test_support::test_app_context(temp_dir.path()); + ctx.set_fee_multiplier_from(&ExtendedEpochInfo::V0(ExtendedEpochInfoV0 { + index: 7, + first_block_time: 0, + first_block_height: 0, + first_core_block_height: 0, + fee_multiplier_permille: 1_500, + protocol_version: 14, + })); - ctx.adopt_epoch_info(ctx.connection_generation(), &epoch(8, 1_500, 14)); - ctx.adopt_epoch_info(ctx.connection_generation(), &epoch(7, 1_000, 13)); assert_eq!(ctx.fee_multiplier_permille(), 1_500); - - // The same or a newer epoch is adopted. - ctx.adopt_epoch_info(ctx.connection_generation(), &epoch(8, 1_600, 14)); - assert_eq!(ctx.fee_multiplier_permille(), 1_600); - ctx.adopt_epoch_info(ctx.connection_generation(), &epoch(9, 1_700, 13)); - assert_eq!(ctx.fee_multiplier_permille(), 1_700); - } - - /// Rebuilding the connection resets everything learned from the old one: - /// the new SDK has proven no protocol version, the fee multiplier goes - /// back to the default, and the next proved epoch is adopted whatever its - /// index. - #[test] - fn a_rebuilt_connection_starts_from_unknown_protocol_and_default_fees() { - let temp_dir = tempfile::tempdir().expect("tempdir"); - let ctx = crate::context::test_support::test_app_context(temp_dir.path()); - crate::context::test_support::prove_protocol_version(&ctx, 14); - ctx.adopt_epoch(ctx.connection_generation(), 8, 1_500); - - ctx.replace_connection(Arc::new(Sdk::new_mock())); - - assert_eq!(ctx.proven_protocol_version(), None); - assert_eq!( - ctx.fee_multiplier_permille(), - crate::model::fee_estimation::PlatformFeeEstimator::DEFAULT_FEE_MULTIPLIER_PERMILLE - ); - assert!(ctx.adopt_epoch(ctx.connection_generation(), 2, 1_100)); - assert_eq!(ctx.fee_multiplier_permille(), 1_100); - } - - /// An epoch read that started on the old connection and finishes after - /// the rebuild is dropped, so it cannot be the new connection's first - /// adoption. - #[test] - fn an_epoch_read_from_before_a_rebuild_is_dropped() { - let temp_dir = tempfile::tempdir().expect("tempdir"); - let ctx = crate::context::test_support::test_app_context(temp_dir.path()); - let before_rebuild = ctx.connection_generation(); - - ctx.replace_connection(Arc::new(Sdk::new_mock())); - - assert!(!ctx.adopt_epoch(before_rebuild, 9, 1_500)); - assert_eq!( - ctx.fee_multiplier_permille(), - crate::model::fee_estimation::PlatformFeeEstimator::DEFAULT_FEE_MULTIPLIER_PERMILLE - ); } #[test] diff --git a/src/context/mod.rs b/src/context/mod.rs index e7aa3c93b..7a2346a44 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -53,7 +53,6 @@ use connection_status::ConnectionStatus; use dash_sdk::Sdk; use dash_sdk::dapi_client::AddressList; use dash_sdk::dashcore_rpc::{Auth, Client}; -use dash_sdk::dpp::block::epoch::EpochIndex; use dash_sdk::dpp::dashcore::Network; #[cfg(any(test, feature = "testing"))] use dash_sdk::dpp::data_contract::accessors::v0::DataContractV0Getters; @@ -164,15 +163,6 @@ impl DpnsVoteDispatchCoordinator { } } -/// What epoch adoption is keyed on. See [`AppContext::adopt_epoch`]. -#[derive(Debug, Default)] -struct EpochAdoption { - /// The connection generation, bumped by every rebuild. - generation: u64, - /// The index of the last adopted proved epoch on this generation. - epoch_index: Option, -} - #[derive(Debug)] pub struct AppContext { pub(crate) data_dir: PathBuf, @@ -323,9 +313,6 @@ pub struct AppContext { /// Cached fee multiplier permille from current epoch (1000 = 1x, 2000 = 2x) /// Updated when epoch info is fetched from Platform fee_multiplier_permille: AtomicU64, - /// The connection generation and the last adopted proved epoch. See - /// [`Self::adopt_epoch`] and [`Self::replace_connection`]. - epoch_adoption: Mutex, /// Frame-safe shielded balance snapshot (credits, summed across all Orchard accounts). /// /// Written by the Phase-E `on_shielded_sync_completed` event handler from the @@ -650,7 +637,6 @@ impl AppContext { fee_multiplier_permille: AtomicU64::new( PlatformFeeEstimator::DEFAULT_FEE_MULTIPLIER_PERMILLE, ), - epoch_adoption: Mutex::new(EpochAdoption::default()), shielded_balances: Arc::new(Mutex::new(std::collections::HashMap::new())), shielded_addresses: Arc::new(Mutex::new(std::collections::HashMap::new())), platform_balances: Arc::new(Mutex::new(std::collections::HashMap::new())), @@ -1171,95 +1157,12 @@ impl AppContext { } } - /// The generation of the connection to the network: bumped each time the - /// connection is rebuilt. Production reads take it together with the SDK - /// check, via [`Self::connection_snapshot_for`]. - #[cfg(test)] - pub(crate) fn connection_generation(&self) -> u64 { - self.epoch_adoption - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner) - .generation - } - - /// Adopt the fee multiplier of the proved epoch `epoch_index`, read on - /// connection `generation`. Returns whether it was adopted. - /// - /// Dropped when the connection was rebuilt since the read was sent, or - /// when an epoch newer than it was adopted already: epoch reads run - /// concurrently (platform info, fee refusals) and can finish out of - /// order. The lock covers the checks and the store, so an adoption and a - /// rebuild cannot interleave; nothing is awaited under it. The protocol - /// version is the SDK's own ([`Self::proven_protocol_version`]). - pub(crate) fn adopt_epoch( - &self, - generation: u64, - epoch_index: EpochIndex, - fee_multiplier_permille: u64, - ) -> bool { - let mut adoption = self - .epoch_adoption - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner); - if generation != adoption.generation - || adoption - .epoch_index - .is_some_and(|adopted| epoch_index < adopted) - { - return false; - } - adoption.epoch_index = Some(epoch_index); - self.set_fee_multiplier_permille(fee_multiplier_permille); - true - } - - /// Replace the connection's SDK with `new_sdk` and forget everything - /// learned from the old connection: bump the generation, so reads still in - /// flight on it are dropped, forget the adopted epoch, and put the fee - /// multiplier back to the default. The new SDK has verified nothing, so - /// the proven protocol version is unknown again and capabilities stay - /// closed until the new connection proves one: the new endpoints may serve - /// another protocol version or a reset devnet. - /// - /// The reset is written before the new SDK is published, under the - /// adoption lock: whoever loads the new SDK sees the reset state, and a - /// [`Self::connection_snapshot_for`] sees either the old generation and - /// SDK or the new ones. A task still holding the old SDK may see the - /// default fees: fail closed. - pub(crate) fn replace_connection(&self, new_sdk: Arc) { - self.replace_connection_observed(new_sdk, |_| {}); - } - - /// [`Self::replace_connection`]; `observe` runs where the two halves meet (test seam). - fn replace_connection_observed(&self, new_sdk: Arc, observe: impl Fn(&Self)) { - let mut adoption = self - .epoch_adoption - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner); - adoption.generation += 1; - adoption.epoch_index = None; - self.set_fee_multiplier_permille(PlatformFeeEstimator::DEFAULT_FEE_MULTIPLIER_PERMILLE); - observe(self); - self.sdk.store(new_sdk); - } - - /// The connection generation and whether `sdk` is the current SDK (not - /// one a rebuild replaced, nor a copy), read together under the adoption - /// lock. An epoch read takes this before it is sent and passes the - /// generation to [`Self::adopt_epoch`]. - pub(crate) fn connection_snapshot_for(&self, sdk: &Sdk) -> (u64, bool) { - let adoption = self - .epoch_adoption - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner); - let current = std::ptr::eq(sdk, Arc::as_ptr(&self.sdk.load_full())); - (adoption.generation, current) - } - /// Rebuild the connection ([`Self::reinit_core_client_and_sdk`]) and read - /// the new connection's epoch, so its protocol version and fee multiplier - /// are known again. A failed epoch read only leaves them unknown until - /// the next one; it does not fail the rebuild. + /// the new connection's epoch. The new SDK has proven no protocol version, + /// so protocol capabilities stay closed until a proved response carries + /// one; the epoch read is that response, and it refreshes the fee + /// multiplier too. A failed read only leaves the version unproven until + /// the next proved response; it does not fail the rebuild. pub(crate) async fn rebuild_connection(self: &Arc) -> Result<(), TaskError> { Arc::clone(self).reinit_core_client_and_sdk()?; let sdk = self.sdk.load_full(); @@ -1274,8 +1177,8 @@ impl AppContext { } /// Rebuild both the Dash RPC `core_client` and the `Sdk` using the - /// updated `NetworkConfig` from `self.config`, and forget the state - /// learned from the old connection ([`Self::replace_connection`]). + /// updated `NetworkConfig` from `self.config`. The new SDK starts from + /// nothing learned from the old endpoints. pub fn reinit_core_client_and_sdk(self: Arc) -> Result<(), TaskError> { // 1. Grab a fresh snapshot of your NetworkConfig let cfg = { @@ -1319,7 +1222,7 @@ impl AppContext { let mut client_lock = self.core_client.write()?; *client_lock = new_client; } - self.replace_connection(Arc::new(new_sdk)); + self.sdk.store(Arc::new(new_sdk)); // Rebind the provider to hold the new AppContext reference. // bind_app_context also registers the provider with the SDK. @@ -2043,7 +1946,7 @@ mod tests { Some(13) ); - ctx.replace_connection(Arc::new(dash_sdk::Sdk::new_mock())); + ctx.sdk.store(Arc::new(dash_sdk::Sdk::new_mock())); assert_eq!(ctx.proven_protocol_version(), None, "rebuilt connection"); assert!( ctx.proven_platform_version().is_none(), @@ -2066,32 +1969,6 @@ mod tests { assert!(ctx.proven_platform_version().is_none()); } - /// A task that loads the new SDK always sees the reset connection state: - /// the new SDK never pairs with the old connection's fee multiplier, and - /// it has proven no protocol version. - #[test] - fn the_new_sdk_is_published_only_after_the_connection_state_is_reset() { - let temp_dir = tempfile::tempdir().expect("tempdir"); - let ctx = crate::context::test_support::test_app_context(temp_dir.path()); - ctx.adopt_epoch(ctx.connection_generation(), 8, 1_500); - let new_sdk = Arc::new(dash_sdk::Sdk::new_mock()); - let new_sdk_ptr = Arc::as_ptr(&new_sdk); - - ctx.replace_connection_observed(new_sdk, |ctx| { - if Arc::as_ptr(&ctx.sdk.load_full()) == new_sdk_ptr { - assert_eq!(ctx.proven_protocol_version(), None, "new SDK, old protocol"); - assert_eq!( - ctx.fee_multiplier_permille(), - crate::model::fee_estimation::PlatformFeeEstimator::DEFAULT_FEE_MULTIPLIER_PERMILLE, - "new SDK, old fee multiplier" - ); - } - }); - - assert_eq!(Arc::as_ptr(&ctx.sdk.load_full()), new_sdk_ptr); - assert_eq!(ctx.proven_protocol_version(), None); - } - /// Contest windows stay consistent as the SDK ratchets upward from each /// network minimum to a newer supported protocol. #[test] From 318ef38bf45a2d63f9a65888b00c3f1348a7f4b3 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Mon, 26 Oct 2026 09:21:06 +0000 Subject: [PATCH 19/27] docs(context): say why proved epochs need no ordering Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- src/backend_task/platform_info.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/backend_task/platform_info.rs b/src/backend_task/platform_info.rs index 2a1fc82c8..a57aaeadf 100644 --- a/src/backend_task/platform_info.rs +++ b/src/backend_task/platform_info.rs @@ -575,6 +575,10 @@ impl AppContext { let epoch_info = ExtendedEpochInfo::fetch_current(sdk) .await .map_err(Box::new)?; + // No epoch ordering here: an older epoch can only come from a node + // behind the chain, and the SDK refuses a proved response more than + // `metadata_height_tolerance` blocks (1 by default) below the highest + // it has seen (`StaleNodeError::Height`), then bans that DAPI address. self.set_fee_multiplier_from(&epoch_info); Ok(epoch_info) } From 8a18bff867fa7111df150d9cf0bbabb877f7befc Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Mon, 26 Oct 2026 09:24:25 +0000 Subject: [PATCH 20/27] test: drop the protocol 13 client tables snapshot A released protocol's tables and each network's minimum version are upstream's to keep fixed; DET's start is covered by platform_version_starts_at_the_network_minimum. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- .../README.md | 5 - .../fixtures/platform_v13_client_tables.txt | 798 ------------------ src/context/mod.rs | 2 - src/context/platform_version_guard.rs | 59 -- 4 files changed, 864 deletions(-) delete mode 100644 src/context/fixtures/platform_v13_client_tables.txt delete mode 100644 src/context/platform_version_guard.rs diff --git a/docs/ai-design/2026-10-24-platform-v5.1-dev-bump/README.md b/docs/ai-design/2026-10-24-platform-v5.1-dev-bump/README.md index adb142ed0..cc450f39b 100644 --- a/docs/ai-design/2026-10-24-platform-v5.1-dev-bump/README.md +++ b/docs/ai-design/2026-10-24-platform-v5.1-dev-bump/README.md @@ -52,11 +52,6 @@ proof is never checked against the wrong schema. one; the rebuild reads the epoch right away for that, and the read refreshes the fee multiplier. -## Protocol 13 guards - -- `context/platform_version_guard.rs` compares the protocol 13 client tables - with a recorded snapshot, so an upstream change to them is reviewed. - ## Mainnet compatibility dashpay/platform#5515 versions the non-unique identity lookup: protocol 13 diff --git a/src/context/fixtures/platform_v13_client_tables.txt b/src/context/fixtures/platform_v13_client_tables.txt deleted file mode 100644 index 219368253..000000000 --- a/src/context/fixtures/platform_v13_client_tables.txt +++ /dev/null @@ -1,798 +0,0 @@ -protocol_version: 13 -dpp: DPPVersion { - costs: DPPCostsVersions { - signature_verify: 0, - }, - validation: DPPValidationVersions { - json_schema_validator: JsonSchemaValidatorVersions { - new: 0, - validate: 0, - compile: 0, - compile_and_validate: 0, - }, - data_contract: DataContractValidationVersions { - validate: 0, - validate_config_update: 1, - validate_token_config_update: 0, - validate_index_definitions: 0, - validate_index_naming_duplicates: 0, - validate_not_defined_properties: 0, - validate_property_definition: 0, - validate_token_config_groups_exist: 1, - validate_localizations: 0, - validate_once_per_identity_distribution: None, - }, - document_type: DocumentTypeValidationVersions { - validate_update: 0, - unique_index_limit: 10, - contested_index_limit: 1, - }, - voting: VotingValidationVersions { - allow_other_contenders_time_mainnet_ms: 604800000, - allow_other_contenders_time_testing_ms: 2700000, - votes_allowed_per_masternode: 5, - }, - validation_result: ValidationResultMethodVersions { - flatten: 1, - merge_many: 1, - }, - }, - state_transition_serialization_versions: DPPStateTransitionSerializationVersions { - identity_public_key_in_creation: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - identity_create_from_addresses_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - identity_create_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - identity_update_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - identity_key_limits_update_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - identity_top_up_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - identity_top_up_from_addresses_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - identity_credit_withdrawal_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - identity_credit_transfer_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - identity_credit_transfer_to_addresses_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - masternode_vote_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - contract_create_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - contract_update_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - contract_user_moderation_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - contract_fee_claim_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - batch_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 1, - default_current_version: 1, - }, - document_base_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 1, - }, - document_create_state_transition: DocumentFeatureVersionBounds { - bounds: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - }, - document_replace_state_transition: DocumentFeatureVersionBounds { - bounds: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - }, - document_delete_state_transition: DocumentFeatureVersionBounds { - bounds: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - }, - document_index_only_delete_state_transition: None, - document_transfer_state_transition: DocumentFeatureVersionBounds { - bounds: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - }, - document_update_price_state_transition: DocumentFeatureVersionBounds { - bounds: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - }, - document_purchase_state_transition: DocumentFeatureVersionBounds { - bounds: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - }, - address_funds_transfer_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - address_funding_from_asset_lock_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - address_credit_withdrawal_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - shield_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - shielded_transfer_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - unshield_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - shield_from_asset_lock_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - shielded_withdrawal_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - identity_create_from_shielded_pool_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - shield_from_identity_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - identity_top_up_from_shielded_pool_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - token_shielded_transfer_with_shielded_fee_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - token_unshield_with_shielded_fee_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - token_purchase_from_shielded_pool_state_transition: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - }, - state_transition_conversion_versions: DPPStateTransitionConversionVersions { - identity_to_identity_create_transition: 0, - identity_to_identity_top_up_transition: 0, - identity_to_identity_transfer_transition: 0, - identity_to_identity_withdrawal_transition: 1, - identity_to_identity_create_transition_with_signer: 0, - inputs_to_identity_create_from_addresses_transition_with_signer: 0, - address_funds_to_address_funds_transfer_transition: 0, - identity_to_identity_top_up_from_addresses_transition: 0, - address_funding_from_asset_lock_transition: 0, - }, - state_transition_method_versions: DPPStateTransitionMethodVersions { - public_key_in_creation_methods: PublicKeyInCreationMethodVersions { - from_public_key_signed_with_private_key: 0, - from_public_key_signed_external: 0, - hash: 0, - duplicated_key_ids_witness: 0, - duplicated_keys_witness: 0, - validate_identity_public_keys_structure: 0, - }, - verify_identity_signed_signature: 0, - }, - state_transitions: DPPStateTransitionVersions { - documents: DocumentTransitionVersions { - documents_batch_transition: DocumentsBatchTransitionVersions { - validation: DocumentsBatchTransitionValidationVersions { - find_duplicates_by_id: 0, - validate_base_structure: 0, - }, - }, - }, - identities: IdentityTransitionVersions { - max_public_keys_in_creation: 6, - asset_locks: IdentityTransitionAssetLockVersions { - required_asset_lock_duff_balance_for_processing_start_for_identity_create: 200000, - required_asset_lock_duff_balance_for_processing_start_for_identity_top_up: 50000, - required_asset_lock_duff_balance_for_processing_start_for_address_funding: 50000, - validate_asset_lock_transaction_structure: 0, - validate_instant_asset_lock_proof_structure: 0, - max_asset_lock_transaction_inputs: 100, - }, - credit_withdrawal: IdentityCreditWithdrawalTransitionVersions { - default_constructor: 1, - }, - calculate_min_required_fee_on_identity_create_transition: 1, - calculate_min_required_fee_on_identity_top_up_transition: 1, - }, - contract: ContractTransitionVersions { - contract_create_transition_default_version: 0, - contract_update_transition_default_version: 0, - }, - address_funds: AddressFundsTransitionVersions { - address_funds_transition_default_version: 0, - credit_withdrawal: 0, - validate_credit_withdrawal_structure: 0, - min_output_amount: 500000, - min_input_amount: 100000, - min_identity_funding_amount: 200000, - }, - shielded: ShieldedTransitionVersions { - validate_withdrawal_structure: 0, - }, - max_address_inputs: 16, - max_address_outputs: 128, - max_address_fee_strategies: 4, - }, - contract_versions: DPPContractVersions { - max_serialized_size: 65000, - contract_serialization_version: FeatureVersionBounds { - min_version: 0, - max_version: 1, - default_current_version: 1, - }, - contract_structure_version: 1, - created_data_contract_structure: 0, - config: FeatureVersionBounds { - min_version: 1, - max_version: 1, - default_current_version: 1, - }, - methods: DataContractMethodVersions { - validate_document: 0, - validate_update: 0, - schema: 0, - validate_groups: 0, - validate_moderation_config: 0, - equal_ignoring_time_fields: 0, - registration_cost: 1, - }, - document_type_versions: DocumentTypeVersions { - index_versions: DocumentTypeIndexVersions { - index_levels_from_indices: 0, - }, - class_method_versions: DocumentTypeClassMethodVersions { - try_from_schema: 2, - create_document_types_from_document_schemas: 1, - }, - structure_version: 0, - schema: DocumentTypeSchemaVersions { - document_type_schema: 2, - should_add_creator_id: 1, - enrich_with_base_schema: 1, - find_identifier_and_binary_paths: 0, - apply_property_reference: None, - apply_required_since: None, - apply_distinct_from: None, - apply_encrypted_for: None, - apply_max_bytes: None, - parse_typed_array: None, - parse_property_constraints: None, - apply_generated_from: None, - expand_property_type_shorthands: None, - validate_max_depth: 0, - max_depth: 256, - recursive_schema_validator_versions: RecursiveSchemaValidatorVersions { - traversal_validator: 0, - }, - validate_schema_compatibility: 0, - }, - methods: DocumentTypeMethodVersions { - create_document_from_data: 0, - create_document_with_prevalidated_properties: 0, - prefunded_voting_balance_for_document: 0, - contested_vote_poll_for_document: 0, - estimated_size: 0, - index_for_types: 0, - max_size: 0, - serialize_value_for_key: 0, - deserialize_value_for_key: 0, - validate_distinct_from: None, - validate_encrypted_property_shapes: None, - validate_max_bytes: None, - validate_property_constraints: None, - canonical_contested_index_values: None, - fill_generated_properties: None, - validate_generated_from: None, - }, - }, - token_versions: TokenVersions { - validate_structure_interval: 0, - validate_pre_programmed_distribution_amounts: 0, - token_configuration_format: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - }, - }, - document_versions: DPPDocumentVersions { - document_structure_version: 0, - document_serialization_version: FeatureVersionBounds { - min_version: 0, - max_version: 2, - default_current_version: 2, - }, - document_cbor_serialization_version: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - extended_document_structure_version: 0, - extended_document_serialization_version: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - document_method_versions: DocumentMethodVersions { - is_equal_ignoring_timestamps: 0, - hash: 0, - get_raw_for_contract: 0, - get_raw_for_document_type: 0, - try_into_asset_unlock_base_transaction_info: 0, - generate_document_id: 0, - get_raw_array_elements_for_document_type: None, - }, - }, - identity_versions: DPPIdentityVersions { - identity_structure_version: 0, - identity_key_structure_version: 0, - identity_key_type_method_versions: IdentityKeyTypeMethodVersions { - random_public_key_data: 0, - random_public_and_private_key_data: 0, - }, - }, - voting_versions: DPPVotingVersions { - default_vote_poll_time_duration_mainnet_ms: 1209600000, - default_vote_poll_time_duration_test_network_ms: 5400000, - contested_document_vote_poll_stored_info_version: 0, - }, - token_versions: DPPTokenVersions { - identity_token_info_default_structure_version: 0, - identity_token_status_default_structure_version: 0, - token_contract_info_default_structure_version: 0, - token_config_update_action_id_version: 1, - token_set_price_action_id_version: 1, - distribution_function_evaluate_version: 0, - reward_distribution_max_cycle_moment_version: 0, - distribution_function_cycle_epochs_version: 0, - }, - asset_lock_versions: DPPAssetLockVersions { - reduced_asset_lock_value: FeatureVersionBounds { - min_version: 0, - max_version: 0, - default_current_version: 0, - }, - }, - methods: DPPMethodVersions { - epoch_core_reward_credits_for_distribution: 0, - daily_withdrawal_limit: 1, - deduct_fee_from_outputs_or_remaining_balance_of_inputs: 0, - compute_minimum_shielded_fee: 0, - shielded_extra_sighash_data: 0, - core_credit_pool_unlock_limit: None, - credit_pool_bundle_binding: None, - }, - factory_versions: DPPFactoryVersions { - data_contract_factory_structure_version: 0, - document_factory_structure_version: 0, - }, -} -drive.methods.verify: DriveVerifyMethodVersions { - contract: DriveVerifyContractMethodVersions { - verify_contract: 0, - verify_contract_history: 0, - verify_contract_return_serialization: 0, - verify_contracts_by_range: 0, - verify_contracts_versions: 0, - }, - document: DriveVerifyDocumentMethodVersions { - verify_proof: 0, - verify_proof_keep_serialized: 0, - verify_document_history: 0, - verify_start_at_document_in_proof: 0, - }, - chained_document: DriveVerifyChainedDocumentMethodVersions { - verify_chained_documents_proof: 0, - }, - composite_document: DriveVerifyCompositeDocumentMethodVersions { - verify_composite_documents_proof: 0, - }, - document_count: DriveVerifyDocumentCountMethodVersions { - verify_aggregate_count_proof: 0, - verify_carrier_aggregate_count_proof: 0, - verify_distinct_count_proof: 0, - verify_point_lookup_count_proof: 0, - verify_primary_key_count_tree_proof: 0, - }, - document_sum: DriveVerifyDocumentSumMethodVersions { - verify_aggregate_sum_proof: 0, - verify_carrier_aggregate_sum_proof: 0, - verify_carrier_aggregate_count_and_sum_proof: 0, - verify_aggregate_count_and_sum_proof: 0, - verify_primary_key_sum_tree_proof: 0, - verify_primary_key_count_sum_tree_proof: 0, - verify_point_lookup_sum_proof: 0, - verify_distinct_sum_proof: 0, - verify_distinct_count_and_sum_proof: 0, - verify_point_lookup_count_and_sum_proof: 0, - }, - document_ranked: DriveVerifyDocumentRankedMethodVersions { - verify_ranked_top_k_proof: 0, - verify_having_range_proof: 0, - }, - identity: DriveVerifyIdentityMethodVersions { - verify_full_identities_by_public_key_hashes: 0, - verify_full_identity_by_identity_id: 0, - verify_full_identity_by_public_key_hash: 0, - verify_identity_balance_for_identity_id: 0, - verify_identity_balances_for_identity_ids: 0, - verify_identity_balance_revision_and_addresses_from_inputs: 0, - verify_identity_id_by_unique_public_key_hash: 0, - verify_identity_ids_by_unique_public_key_hashes: 0, - verify_identity_keys_by_identity_id: 0, - verify_identity_nonce: 0, - verify_identity_contract_nonce: 0, - verify_identities_contract_keys: 0, - verify_identity_revision_for_identity_id: 0, - verify_full_identity_by_non_unique_public_key_hash: 0, - verify_full_identity_by_non_unique_public_key_hash_two_proofs: 0, - verify_identity_id_by_non_unique_public_key_hash: 0, - verify_identity_keys_remaining_budgets: 0, - }, - group: DriveVerifyGroupMethodVersions { - verify_group_info: 0, - verify_group_infos_in_contract: 0, - verify_action_infos: 0, - verify_action_signers: 0, - verify_action_signers_total_power: 0, - }, - contract_group: DriveVerifyContractGroupMethodVersions { - verify_contract_group_info: 0, - verify_contract_group_members: 0, - verify_contract_group_memberships_for_contract: 0, - }, - contract_moderation: DriveVerifyContractModerationMethodVersions { - verify_contract_moderation_status: 0, - verify_contract_moderation_entries: 0, - verify_contract_fee_pots: 0, - verify_contract_document_removals: 0, - verify_contract_team_actions: 0, - verify_contract_team_action_signers: 0, - verify_contract_team_action_signature: 0, - verify_contract_moderation_action_counts: 0, - }, - token: DriveVerifyTokenMethodVersions { - verify_token_balances_for_identity_ids: 0, - verify_token_balances_for_identity_id: 0, - verify_token_infos_for_identity_ids: 0, - verify_token_infos_for_identity_id: 0, - verify_token_statuses: 0, - verify_token_total_supply_and_aggregated_identity_balance: 0, - verify_token_balance_for_identity_id: 0, - verify_token_info_for_identity_id: 0, - verify_token_status: 0, - verify_token_pre_programmed_distributions: 0, - verify_token_direct_selling_price: 0, - verify_token_direct_selling_prices: 0, - verify_token_perpetual_distribution_last_paid_time: 0, - verify_token_contract_info: 0, - }, - single_document: DriveVerifySingleDocumentMethodVersions { - verify_proof: 0, - verify_proof_keep_serialized: 0, - }, - system: DriveVerifySystemMethodVersions { - verify_epoch_infos: 0, - verify_epoch_proposers: 0, - verify_elements: 0, - verify_total_credits_in_system: 0, - verify_upgrade_state: 0, - verify_upgrade_vote_status: 0, - verify_finalized_epoch_infos: 0, - }, - voting: DriveVerifyVoteMethodVersions { - verify_masternode_vote: 0, - verify_vote_poll_votes_proof: 0, - verify_identity_votes_given_proof: 0, - verify_vote_poll_vote_state_proof: 0, - verify_contests_proof: 0, - verify_vote_polls_by_end_date_proof: 0, - verify_specialized_balance: 0, - }, - address_funds: DriveVerifyAddressFundsMethodVersions { - verify_address_info: 0, - verify_addresses_infos: 0, - verify_address_funds_trunk_query: 0, - verify_address_funds_branch_query: 0, - verify_recent_address_balance_changes: 0, - verify_compacted_address_balance_changes: 1, - }, - state_transition: DriveVerifyStateTransitionMethodVersions { - verify_state_transition_was_executed_with_proof: 0, - }, - shielded: DriveVerifyShieldedMethodVersions { - verify_shielded_pool_state: 0, - verify_shielded_anchors: 0, - verify_most_recent_shielded_anchor: 0, - verify_shielded_encrypted_notes: 0, - verify_shielded_notes_count: 0, - verify_shielded_nullifiers: 0, - }, -} -fee_version: FeeVersion { - fee_version_number: 1, - uses_version_fee_multiplier_permille: Some( - 1000, - ), - storage: FeeStorageVersion { - storage_disk_usage_credit_per_byte: 27000, - storage_processing_credit_per_byte: 400, - storage_load_credit_per_byte: 20, - non_storage_load_credit_per_byte: 10, - storage_seek_cost: 2000, - ttl_ephemeral_disk_usage_credit_per_byte: 270, - }, - signature: FeeSignatureVersion { - verify_signature_ecdsa_secp256k1: 15000, - verify_signature_bls12_381: 300000, - verify_signature_ecdsa_hash160: 15500, - verify_signature_bip13_script_hash: 300000, - verify_signature_eddsa25519_hash160: 3500, - }, - hashing: FeeHashingVersion { - blake3_base: 100, - blake3_per_block: 300, - sha256_per_block: 5000, - sha256_ripe_md160_base: 6000, - single_sha256_base: 100, - ripemd160_per_block: 5000, - sinsemilla_base: 40000, - }, - processing: FeeProcessingVersion { - fetch_identity_balance_processing_cost: 10000, - fetch_identity_revision_processing_cost: 9000, - fetch_identity_balance_and_revision_processing_cost: 15000, - fetch_identity_cost_per_look_up_key_by_id: 9000, - fetch_identity_token_balance_processing_cost: 10000, - fetch_prefunded_specialized_balance_processing_cost: 10000, - fetch_key_with_type_nonce_and_balance_cost: 12000, - fetch_single_identity_key_processing_cost: 10000, - validate_key_structure: 50, - perform_network_threshold_signing: 100000000, - }, - data_contract_validation: FeeDataContractValidationVersion { - document_type_base_fee: 500, - document_type_schema_size_fee: 10, - document_type_per_property_fee: 40, - document_type_base_non_unique_index_fee: 50, - document_type_non_unique_index_per_property_fee: 30, - document_type_base_unique_index_fee: 100, - document_type_unique_index_per_property_fee: 60, - }, - data_contract_registration: FeeDataContractRegistrationVersion { - base_contract_registration_fee: 10000000000, - document_type_registration_fee: 2000000000, - document_type_base_non_unique_index_registration_fee: 1000000000, - document_type_base_unique_index_registration_fee: 1000000000, - document_type_base_contested_index_registration_fee: 100000000000, - token_registration_fee: 10000000000, - token_uses_perpetual_distribution_fee: 10000000000, - token_uses_pre_programmed_distribution_fee: 10000000000, - token_uses_once_per_identity_distribution_fee: 0, - search_keyword_fee: 10000000000, - }, - state_transition_min_fees: StateTransitionMinFees { - credit_transfer: 100000, - credit_transfer_to_addresses: 500000, - credit_withdrawal: 400000000, - identity_update: 100000, - document_batch_sub_transition: 100000, - contract_create: 100000, - contract_update: 100000, - masternode_vote: 100000, - address_credit_withdrawal: 400000000, - address_funds_transfer_input_cost: 500000, - address_funds_transfer_output_cost: 6000000, - identity_create_base_cost: 2000000, - identity_topup_base_cost: 500000, - identity_key_in_creation_cost: 6500000, - }, - vote_resolution_fund_fees: VoteResolutionFundFees { - contested_document_vote_resolution_fund_required_amount: 20000000000, - contested_document_vote_resolution_unlock_fund_required_amount: 400000000000, - contested_document_single_vote_cost: 10000000, - moderation_vote_resolution_fund_required_amount: 20000000000, - contested_document_contenders_before_fund_doubling: 0, - contested_document_contenders_per_fund_doubling: 0, - }, - document_ttl: FeeDocumentTtlVersion { - tiers: [ - DocumentTtlFeeTier { - max_ttl_seconds: 3600, - credit_per_byte: 1, - }, - DocumentTtlFeeTier { - max_ttl_seconds: 86400, - credit_per_byte: 4, - }, - DocumentTtlFeeTier { - max_ttl_seconds: 172800, - credit_per_byte: 8, - }, - DocumentTtlFeeTier { - max_ttl_seconds: 345600, - credit_per_byte: 15, - }, - DocumentTtlFeeTier { - max_ttl_seconds: 604800, - credit_per_byte: 26, - }, - ], - credit_per_byte_per_period: 34, - pricing_period_seconds: 788400, - cleanup_base_processing_cost: 1200000, - cleanup_processing_cost_per_index_level: 400000, - cleanup_processing_cost_per_document_byte: 420, - }, - document_reference: FeeDocumentReferenceVersion { - choose_first_credits_per_candidate: 100000, - where_list_credits_per_element: 100, - }, -} -system_data_contracts: SystemDataContractVersions { - withdrawals: 1, - dpns: 2, - dashpay: 1, - masternode_reward_shares: 1, - wallet: 1, - token_history: 1, - keyword_search: 1, - document_history: 1, - app_connect: 0, - moderation_charters: 0, -} -system_limits: SystemLimits { - credit_pool_nullifier_backfill_page_size: 0, - credit_pool_nullifier_backfill_batch_size: 0, - estimated_contract_max_serialized_size: 16384, - max_field_value_size: 5120, - max_document_value_depth: Some( - 256, - ), - max_typed_array_items: 1024, - max_references_per_document: 256, - max_reference_operands: 4, - max_reference_expression_depth: 4, - max_property_constraints: 16, - max_property_constraint_nodes: 32, - max_property_constraint_aggregates: 4, - max_state_transition_size: 20480, - max_transitions_in_documents_batch: 1, - withdrawal_transactions_per_block_limit: 4, - retry_signing_expired_withdrawal_documents_per_block_limit: 1, - max_withdrawal_amount: 50000000000000, - daily_withdrawal_limit_percent: None, - core_credit_pool_unlock_limit_percent: None, - core_credit_pool_unlock_limit_floor: None, - core_credit_pool_window_blocks: None, - regtest_core_credit_pool_window_blocks: None, - min_withdrawal_amount: 1000000, - core_dust_relay_fee_per_kb: None, - max_core_fee_per_byte: None, - max_group_member_count: 256, - max_contract_group_memberships_per_contract: 16, - max_contract_group_admins: 16, - max_contract_group_name_length: 64, - max_contract_group_description_length: 256, - max_contract_moderators: 16, - max_contract_suspension_until: 9007199254740991, - max_contract_moderation_reason_length: 1024, - max_contract_warnings_per_identity: 16, - max_contract_moderation_reason_documents: 16, - min_mainnet_contract_moderation_election_window_seconds: 86400, - max_contract_moderation_election_window_seconds: 2419200, - min_contract_moderation_challenge_cool_down_seconds: 1209600, - max_contract_moderation_challenge_cool_down_seconds: 94608000, - contract_document_restore_window_ms: 604800000, - max_contract_moderation_added_moderators: 15, - max_moderation_charter_elected_members: 15, - max_contenders_per_contest: 1000, - max_token_redemption_cycles: 128, - max_evonode_reward_claim_epochs: 100, - max_shielded_transition_actions: 16, - max_token_pool_notes_for_outgoing: 250, - max_time_range_overlap_factor: None, - max_time_range_ttl_seconds: None, - min_time_range_ttl_drop_operations_per_write: None, - min_document_ttl_seconds: None, - max_document_ttl_seconds: None, - max_document_expirations_per_block: 0, - max_document_expiration_weight_per_block: 0, - max_contested_summed_value_magnitude: None, - max_expiring_signed_summed_value_magnitude: None, - max_indexed_array_items: None, - max_windowed_indexed_array_items: None, - max_index_entries_per_document: None, - max_choose_first_candidates: 1000, - max_rekey_batch_moves: None, -} diff --git a/src/context/mod.rs b/src/context/mod.rs index 7a2346a44..b2a133570 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -17,8 +17,6 @@ pub(crate) mod lock_probe; pub(crate) use identity_db::test_staging; pub(crate) mod identity_load_registry; pub mod migration_status; -#[cfg(test)] -mod platform_version_guard; mod settings_db; mod system_contracts; #[cfg(test)] diff --git a/src/context/platform_version_guard.rs b/src/context/platform_version_guard.rs deleted file mode 100644 index d93d8af00..000000000 --- a/src/context/platform_version_guard.rs +++ /dev/null @@ -1,59 +0,0 @@ -//! Guards the protocol version 13 tables DET builds, validates and verifies -//! with on mainnet and testnet against drift across platform pin bumps. -//! -//! Protocol 13 is live, so table changes require compatibility review. The snapshot -//! covers what the client side reads: serialization and validation (`dpp`), -//! proof verification (`drive.methods.verify`), fees, system contract versions -//! and system limits. On a mismatch, diff the fixture, confirm the change is -//! additive (new fields at 0/None) or otherwise safe for a protocol 13 network, -//! then regenerate with `DET_BLESS_PLATFORM_VERSION_SNAPSHOT=1`. -//! -//! Platform #5294 enforces GroveDB proof envelope V1 outside these tables, even -//! for protocol 13. The snapshot does not establish wire compatibility: #5361 -//! removed the legacy discovery response field 2, still served by sampled -//! mainnet nodes, and #5515 brings it back as the answer to a version 0 lookup -//! request, checked by `verify_full_identity_by_non_unique_public_key_hash_two_proofs`. -//! Only a live check against those nodes shows the SDK accepts their proofs. - -use dash_sdk::dpp::dashcore::Network; -use dash_sdk::dpp::version::PlatformVersion; -use dash_sdk::sdk::min_protocol_version; - -const SNAPSHOT_PATH: &str = "src/context/fixtures/platform_v13_client_tables.txt"; -const SNAPSHOT: &str = include_str!("fixtures/platform_v13_client_tables.txt"); - -fn client_tables(platform_version: &PlatformVersion) -> String { - format!( - "protocol_version: {}\ndpp: {:#?}\ndrive.methods.verify: {:#?}\nfee_version: {:#?}\nsystem_data_contracts: {:#?}\nsystem_limits: {:#?}\n", - platform_version.protocol_version, - platform_version.dpp, - platform_version.drive.methods.verify, - platform_version.fee_version, - platform_version.system_data_contracts, - platform_version.system_limits, - ) -} - -#[test] -fn protocol_13_client_tables_match_the_snapshot() { - let actual = client_tables(PlatformVersion::get(13).expect("protocol 13")); - if std::env::var_os("DET_BLESS_PLATFORM_VERSION_SNAPSHOT").is_some() { - let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join(SNAPSHOT_PATH); - std::fs::write(&path, &actual).expect("write snapshot"); - return; - } - assert!( - actual == SNAPSHOT, - "The protocol 13 tables changed with the platform pin. Diff {SNAPSHOT_PATH} (rerun with \ - DET_BLESS_PLATFORM_VERSION_SNAPSHOT=1 to rewrite it), check the change is safe for \ - mainnet, and commit the new snapshot." - ); -} - -/// Mainnet and testnet run protocol 13, and DET seeds its SDK from upstream's -/// per-network minimum: the snapshot above is the table DET starts with there. -#[test] -fn mainnet_and_testnet_start_at_protocol_13() { - assert_eq!(min_protocol_version(Network::Mainnet), 13); - assert_eq!(min_protocol_version(Network::Testnet), 13); -} From 325ec45de72480965a1cf1cf59664b9b7816e348 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Mon, 26 Oct 2026 09:59:51 +0000 Subject: [PATCH 21/27] fix(context): decode saved contracts at the proof verifier's version The proof verifier asks for contracts at the version it verifies with, which can be newer than the SDK's. System contracts were already served at that version; a saved user contract was decoded at the SDK's version instead, dropping newer-protocol settings such as action fees. The fallback now decodes it at the version asked for. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- src/context/contract_token_db.rs | 62 ++++++++++++++++++++++++++++---- src/context_provider.rs | 7 ++-- 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/src/context/contract_token_db.rs b/src/context/contract_token_db.rs index 23f6aaae9..a5333ddd0 100644 --- a/src/context/contract_token_db.rs +++ b/src/context/contract_token_db.rs @@ -17,6 +17,7 @@ use dash_sdk::dpp::serialization::{ PlatformDeserializableWithPotentialValidationFromVersionedStructureUntrusted, PlatformSerializableWithPlatformVersion, }; +use dash_sdk::dpp::version::PlatformVersion; use dash_sdk::platform::{DataContract, Identifier}; use dash_sdk::query_types::IndexMap; use serde::{Deserialize, Serialize}; @@ -183,6 +184,14 @@ impl AppContext { fn decode_stored_contract( &self, stored: StoredContract, + ) -> std::result::Result { + Self::decode_stored_contract_at(stored, self.platform_version()) + } + + /// [`Self::decode_stored_contract`] at `platform_version`. + fn decode_stored_contract_at( + stored: StoredContract, + platform_version: &PlatformVersion, ) -> std::result::Result { // DET wrote these bytes, but the untrusted decoder accepts every valid // stored contract under the same no-limit config and does not @@ -191,7 +200,7 @@ impl AppContext { let contract = DataContract::versioned_deserialize_untrusted( &stored.contract_bytes, false, - self.platform_version(), + platform_version, ) .map_err(|source| TaskError::ContractEncoding { source: Box::new(source), @@ -205,6 +214,17 @@ impl AppContext { pub fn get_contract_by_id( &self, contract_id: &Identifier, + ) -> std::result::Result, TaskError> { + self.get_contract_by_id_at(contract_id, self.platform_version()) + } + + /// [`Self::get_contract_by_id`], decoded at `platform_version`. The proof + /// verifier reads contracts at the version it verifies with, which can be + /// newer than the SDK's. + pub(crate) fn get_contract_by_id_at( + &self, + contract_id: &Identifier, + platform_version: &PlatformVersion, ) -> std::result::Result, TaskError> { let backend = self.wallet_backend()?; let key = contract_key(contract_id); @@ -212,10 +232,9 @@ impl AppContext { .kv() .get(DetScope::Global, &key) .map_err(contract_err)?; - match stored { - Some(stored) => Ok(Some(self.decode_stored_contract(stored)?)), - None => Ok(None), - } + stored + .map(|stored| Self::decode_stored_contract_at(stored, platform_version)) + .transpose() } pub fn get_unqualified_contract_by_id( @@ -854,7 +873,6 @@ where mod tests { use super::*; use crate::wallet_backend::kv_test_support::{InMemoryKv, StallingReadKv}; - use dash_sdk::dpp::version::PlatformVersion; use platform_wallet_storage::{KvError, KvStore, ObjectId}; use std::sync::{Arc, Mutex}; @@ -1080,6 +1098,38 @@ mod tests { assert_eq!(at_14.contract, charging); } + /// The proof verifier reads a saved user contract at the version it + /// verifies with, as it reads system contracts: a contract saved at + /// protocol 14 comes back in full when asked at 14 while the SDK still + /// sits at 13. + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] + async fn the_proof_verifier_reads_a_saved_contract_at_the_version_it_asks_for() { + use dash_sdk::dpp::platform_value::platform_value; + + let tmp = tempfile::tempdir().expect("tempdir"); + let ctx = crate::context::test_support::wired_test_app_context(tmp.path()).await; + crate::context::test_support::set_sdk_protocol_version(&ctx, 13); + let v14 = PlatformVersion::get(14).expect("v14"); + let charging = user_contract_at(v14, Some(platform_value!({"create": {"owner": 5_u64}}))); + let stored = StoredContract { + contract_bytes: charging + .serialize_to_bytes_with_platform_version(v14) + .expect("encode"), + alias: None, + }; + ctx.wallet_backend() + .expect("wired backend") + .kv() + .put(DetScope::Global, &contract_key(&charging.id()), &stored) + .expect("store the contract"); + + let resolved = crate::context_provider::resolve_data_contract(&ctx, &charging.id(), v14) + .expect("resolves") + .expect("the saved contract"); + + assert_eq!(*resolved, charging); + } + #[test] fn contract_keys_carry_a_base58_suffix_that_round_trips_to_the_id() { let contract = ident(6); diff --git a/src/context_provider.rs b/src/context_provider.rs index 8ba42fa9c..171ee8d24 100644 --- a/src/context_provider.rs +++ b/src/context_provider.rs @@ -28,7 +28,7 @@ pub(crate) const MASTERNODE_LIST_NOT_READY_DETAIL: &str = /// All system contracts are listed in `cached` — adding a new one is a single /// array edit. The array size is tied to [`SYSTEM_CONTRACT_COUNT`] so the /// compiler enforces completeness. -fn resolve_data_contract( +pub(crate) fn resolve_data_contract( app_ctx: &AppContext, data_contract_id: &Identifier, platform_version: &PlatformVersion, @@ -58,9 +58,10 @@ fn resolve_data_contract( return Ok(Some(Arc::clone(contract))); } - // K/V fallback for user-added / non-system contracts + // K/V fallback for user-added / non-system contracts, decoded at the + // verifier's version too. let dc = app_ctx - .get_contract_by_id(data_contract_id) + .get_contract_by_id_at(data_contract_id, platform_version) .map_err(|e| ContextProviderError::Generic(e.to_string()))?; Ok(dc.map(|qc| Arc::new(qc.contract))) From 7db8d30091e605e62900610073b32de67be146bf Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Mon, 26 Oct 2026 10:01:17 +0000 Subject: [PATCH 22/27] fix(wallet): report a failed signed-message envelope instead of panicking Building the recoverable-signature envelope returns an error upstream; it is now reported as the existing message-signing failure, with the detail logged, instead of aborting the signing path. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- src/backend_task/wallet/mod.rs | 23 +++++++++++++------ .../wallet/sign_message_with_identity_key.rs | 2 +- .../wallet/sign_message_with_key.rs | 6 +---- src/ui/identity/keys/key_info_screen.rs | 13 +++++++++-- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/backend_task/wallet/mod.rs b/src/backend_task/wallet/mod.rs index 64afbe9ec..1a74dbbdb 100644 --- a/src/backend_task/wallet/mod.rs +++ b/src/backend_task/wallet/mod.rs @@ -36,11 +36,16 @@ use std::sync::Arc; /// (`27 + recId`, `+4` when `compressed`) followed by the 64-byte signature, so /// a verifier can recover the signer's public key from the signature alone. /// Shared by the wallet-key and identity-key message-signing tasks. +/// +/// # Errors +/// +/// [`TaskError::WalletMessageSigningFailed`] if the signature cannot be put +/// in the envelope; the detail is logged. pub(crate) fn dash_signed_message( message: &str, secret_key: &SecretKey, compressed: bool, -) -> String { +) -> Result { let message_hash = signed_msg_hash(message); let digest = Message::from_digest(*message_hash.as_byte_array()); let recoverable = dash_sdk::dpp::secp256k1::ecdsa::RecoverableSignature::sign_ecdsa_recoverable( @@ -52,8 +57,11 @@ pub(crate) fn dash_signed_message( recovery_id.to_u8(), compressed, ) - .expect("a recovery id from signing is in range"); - sign_message::to_base64(envelope) + .map_err(|detail| { + tracing::warn!(error = %detail, "Sign-message envelope construction failed"); + TaskError::WalletMessageSigningFailed + })?; + Ok(sign_message::to_base64(envelope)) } impl AppContext { @@ -519,7 +527,7 @@ mod tests { let expected_pubkey = PublicKey::from_secret_key(&secret_key); let message = "Bilby was here"; - let base64 = dash_signed_message(message, &secret_key, compressed); + let base64 = dash_signed_message(message, &secret_key, compressed).expect("signs"); let parsed = sign_message::from_base64(&base64).expect("valid envelope"); assert_eq!(parsed.is_compressed().expect("valid header"), compressed); @@ -545,9 +553,10 @@ mod tests { let mut expected = [0u8; 65]; expected[0] = 27 + recovery_id.to_u8() + if compressed { 4 } else { 0 }; expected[1..].copy_from_slice(&compact); - let encoded = - sign_message::from_base64(&dash_signed_message(message, &secret_key, compressed)) - .expect("valid envelope"); + let encoded = sign_message::from_base64( + &dash_signed_message(message, &secret_key, compressed).expect("signs"), + ) + .expect("valid envelope"); assert_eq!(encoded.as_bytes(), &expected); } } diff --git a/src/backend_task/wallet/sign_message_with_identity_key.rs b/src/backend_task/wallet/sign_message_with_identity_key.rs index 9de62fb4b..fc383e5ee 100644 --- a/src/backend_task/wallet/sign_message_with_identity_key.rs +++ b/src/backend_task/wallet/sign_message_with_identity_key.rs @@ -34,7 +34,7 @@ impl AppContext { let signature = self .with_identity_secret_key(identity_id, target.clone(), key_id, |secret_key| { // Identity keys are compressed by convention. - Ok(dash_signed_message(message.as_str(), &secret_key, true)) + dash_signed_message(message.as_str(), &secret_key, true) }) .await?; diff --git a/src/backend_task/wallet/sign_message_with_key.rs b/src/backend_task/wallet/sign_message_with_key.rs index c52dd6a58..e2d0bf60d 100644 --- a/src/backend_task/wallet/sign_message_with_key.rs +++ b/src/backend_task/wallet/sign_message_with_key.rs @@ -45,11 +45,7 @@ impl AppContext { tracing::warn!(error = %detail, "Sign-message secret key construction failed"); TaskError::WalletMessageSigningFailed })?; - Ok(dash_signed_message( - message.as_str(), - &secret_key, - private_key.compressed, - )) + dash_signed_message(message.as_str(), &secret_key, private_key.compressed) }, ) .await?; diff --git a/src/ui/identity/keys/key_info_screen.rs b/src/ui/identity/keys/key_info_screen.rs index 9c0e3d69c..e80279a75 100644 --- a/src/ui/identity/keys/key_info_screen.rs +++ b/src/ui/identity/keys/key_info_screen.rs @@ -1261,7 +1261,16 @@ impl KeyInfoScreen { // Keys that carry their own plaintext sign locally — no wallet seed // is involved, so there is nothing to fetch through the chokepoint. PrivateKeyData::Clear(bytes) | PrivateKeyData::AlwaysClear(bytes) => { - self.signed_message = Some(Self::sign_ecdsa_local(bytes, &self.message_input)); + match Self::sign_ecdsa_local(bytes, &self.message_input) { + Ok(signature) => self.signed_message = Some(signature), + Err(error) => { + MessageBanner::set_global( + self.app_context.egui_ctx(), + error.to_string(), + MessageType::Error, + ); + } + } } // Wallet-derived keys sign in the backend: the seed is fetched // just-in-time through the JIT chokepoint and only the public @@ -1292,7 +1301,7 @@ impl KeyInfoScreen { /// `+4` for a compressed key) followed by the 64-byte signature. These keys /// are compressed by convention, so a verifier can recover the signer's /// public key and address from the signature alone. - fn sign_ecdsa_local(private_key_bytes: &[u8; 32], message: &str) -> String { + fn sign_ecdsa_local(private_key_bytes: &[u8; 32], message: &str) -> Result { let secret_key = SecretKey::from_secret_bytes(*private_key_bytes) .expect("clear private key is a valid 32-byte secret"); crate::backend_task::wallet::dash_signed_message(message, &secret_key, true) From 749f8bd1e81e94cae0d66cb0b5a7e88314e9c37d Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Mon, 26 Oct 2026 10:05:03 +0000 Subject: [PATCH 23/27] style: drop blank lines left by the removed secp256k1 context Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- src/backend_task/dashpay/auto_accept_proof.rs | 1 - src/backend_task/identity/add_key_to_identity.rs | 1 - src/backend_task/identity/load_identity.rs | 1 - src/backend_task/identity/mod.rs | 1 - src/backend_task/migration/finish_unwire.rs | 1 - src/backend_task/migration/single_key_restore.rs | 2 -- src/context/wallet_lifecycle/tests.rs | 2 -- src/database/test_helpers.rs | 2 -- src/database/wallet.rs | 2 -- src/model/dashpay_derivation/dip14.rs | 1 - src/model/dashpay_derivation/hd.rs | 1 - src/model/qualified_identity/mod.rs | 2 -- .../qualified_identity/qualified_identity_public_key.rs | 1 - src/model/single_key.rs | 2 -- src/model/wallet/mod.rs | 8 -------- src/model/wallet/single_key.rs | 2 -- src/ui/identity/funding_common.rs | 1 - src/ui/identity/keys/key_info_screen.rs | 1 - src/ui/wallets/wallets_screen/address_table.rs | 1 - src/wallet_backend/det_platform_signer.rs | 3 --- src/wallet_backend/det_signer.rs | 4 ---- src/wallet_backend/secret_access.rs | 1 - src/wallet_backend/single_key.rs | 7 +------ src/wallet_backend/snapshot.rs | 1 - 24 files changed, 1 insertion(+), 48 deletions(-) diff --git a/src/backend_task/dashpay/auto_accept_proof.rs b/src/backend_task/dashpay/auto_accept_proof.rs index 4a3fd5b22..31cd344de 100644 --- a/src/backend_task/dashpay/auto_accept_proof.rs +++ b/src/backend_task/dashpay/auto_accept_proof.rs @@ -274,7 +274,6 @@ pub async fn verify_auto_accept_proof( let mut hasher = Sha256::new(); hasher.update(&message_data); let message_hash = hasher.finalize(); - let message = Message::from_digest(message_hash.into()); // Derive expected pubkey from our seed and key index (timestamp) diff --git a/src/backend_task/identity/add_key_to_identity.rs b/src/backend_task/identity/add_key_to_identity.rs index 91fe6c1fe..e07bcb432 100644 --- a/src/backend_task/identity/add_key_to_identity.rs +++ b/src/backend_task/identity/add_key_to_identity.rs @@ -703,7 +703,6 @@ mod tests { .get_resolve_with_seed(&placement, &wallets, &seed, Network::Testnet) .unwrap() .unwrap(); - let secret = SecretKey::from_secret_bytes(*bytes).unwrap(); let message = Message::from_digest([1; 32]); let signature = dash_sdk::dpp::secp256k1::ecdsa::sign(message, &secret); diff --git a/src/backend_task/identity/load_identity.rs b/src/backend_task/identity/load_identity.rs index b7dc03e53..3c011f6bc 100644 --- a/src/backend_task/identity/load_identity.rs +++ b/src/backend_task/identity/load_identity.rs @@ -19,7 +19,6 @@ use crate::model::wallet::{Wallet, WalletSeedHash}; use crate::ui::identity::add_new_identity_screen::MAX_IDENTITY_INDEX; use dash_sdk::Sdk; use dash_sdk::dashcore_rpc::dashcore::PrivateKey; - use dash_sdk::dpp::dashcore::ProTxHash; use dash_sdk::dpp::dashcore::hashes::Hash; use dash_sdk::dpp::identity::KeyType; diff --git a/src/backend_task/identity/mod.rs b/src/backend_task/identity/mod.rs index 54f6f963a..786bb08e4 100644 --- a/src/backend_task/identity/mod.rs +++ b/src/backend_task/identity/mod.rs @@ -1498,7 +1498,6 @@ mod tests { .expect("resolve master") .expect("master present"); // The resolved private key's public key must match the spec's public key. - let resolved_pub = dash_sdk::dpp::secp256k1::SecretKey::from_secret_bytes(*stored.1) .expect("secret") .public_key(); diff --git a/src/backend_task/migration/finish_unwire.rs b/src/backend_task/migration/finish_unwire.rs index 721eda3de..0d923a32a 100644 --- a/src/backend_task/migration/finish_unwire.rs +++ b/src/backend_task/migration/finish_unwire.rs @@ -3955,7 +3955,6 @@ mod tests { use crate::wallet_backend::single_key::{ SingleKeyView, label_for_address, single_key_namespace_id, }; - use dash_sdk::dpp::dashcore::{Address, Network, PrivateKey, PublicKey}; let dir = tempfile::tempdir().expect("tempdir"); diff --git a/src/backend_task/migration/single_key_restore.rs b/src/backend_task/migration/single_key_restore.rs index dacfc4065..2b2b14476 100644 --- a/src/backend_task/migration/single_key_restore.rs +++ b/src/backend_task/migration/single_key_restore.rs @@ -332,7 +332,6 @@ pub(super) fn derive_p2pkh_address(wif: &str, network: Network) -> Result, ) -> String { use crate::model::wallet::single_key::ClosedSingleKey; - use dash_sdk::dpp::dashcore::{Address, PrivateKey, PublicKey}; let path = ctx.db.db_file_path().expect("data.db path"); @@ -3607,7 +3606,6 @@ fn seed_legacy_protected_single_key( nonce, } = ClosedSingleKey::encrypt_private_key(raw_key, password).expect("encrypt"); let priv_key = PrivateKey::from_byte_array(raw_key, Network::Testnet).expect("priv"); - let pub_key = PublicKey { compressed: priv_key.compressed, inner: priv_key.inner.public_key(), diff --git a/src/database/test_helpers.rs b/src/database/test_helpers.rs index 01248ae89..ed7f2495f 100644 --- a/src/database/test_helpers.rs +++ b/src/database/test_helpers.rs @@ -161,13 +161,11 @@ pub fn legacy_master_epk_bytes( network: dash_sdk::dpp::dashcore::Network, ) -> Vec { use dash_sdk::dpp::dashcore::Network; - use dash_sdk::dpp::key_wallet::bip32::{ ChildNumber, DerivationPath, ExtendedPrivKey, ExtendedPubKey, }; let coin_type = if network == Network::Mainnet { 5 } else { 1 }; - let master = ExtendedPrivKey::new_master(network, seed).expect("master key"); let path = DerivationPath::from(vec![ ChildNumber::Hardened { index: 44 }, diff --git a/src/database/wallet.rs b/src/database/wallet.rs index bbae151d3..75a7b85ee 100644 --- a/src/database/wallet.rs +++ b/src/database/wallet.rs @@ -554,7 +554,6 @@ impl From for rusqlite::Error { mod tests { use super::*; use crate::database::test_helpers::create_test_database; - use dash_sdk::dpp::key_wallet::bip32::DerivationPath; use dash_sdk::dpp::key_wallet::bip32::{ChildNumber, ExtendedPrivKey}; @@ -568,7 +567,6 @@ mod tests { fn create_test_master_epk_bytes(network: Network) -> Vec { let seed = [7u8; 64]; - let master = ExtendedPrivKey::new_master(network, &seed).expect("master key"); let path = DerivationPath::from(vec![ ChildNumber::Hardened { index: 44 }, diff --git a/src/model/dashpay_derivation/dip14.rs b/src/model/dashpay_derivation/dip14.rs index 1009eaf40..7bb826d2b 100644 --- a/src/model/dashpay_derivation/dip14.rs +++ b/src/model/dashpay_derivation/dip14.rs @@ -116,7 +116,6 @@ pub fn derive_dashpay_incoming_xpub_dip14( let base_path = DerivationPath::from_str(&format!("m/9'/{coin_type}'/15'/{account}'"))?; // Derive to the account level using standard BIP32 - let account_xprv = master_xprv.derive_priv(&base_path)?; // Now use DIP-14 256-bit derivation for the identity levels diff --git a/src/model/dashpay_derivation/hd.rs b/src/model/dashpay_derivation/hd.rs index cf4a4862b..9ca674a03 100644 --- a/src/model/dashpay_derivation/hd.rs +++ b/src/model/dashpay_derivation/hd.rs @@ -294,7 +294,6 @@ mod tests { let master_seed = [2u8; 64]; let master_xprv = ExtendedPrivKey::new_master(network, &master_seed).unwrap(); - let xpub = ExtendedPubKey::from_priv(&master_xprv); // Test version 0 diff --git a/src/model/qualified_identity/mod.rs b/src/model/qualified_identity/mod.rs index 4166ab369..3dd57c599 100644 --- a/src/model/qualified_identity/mod.rs +++ b/src/model/qualified_identity/mod.rs @@ -981,7 +981,6 @@ impl QualifiedIdentity { ) -> Result, ProtocolError> { use dash_sdk::dpp::dashcore::PublicKey; use dash_sdk::dpp::dashcore::hashes::{Hash, ripemd160, sha256}; - use dash_sdk::dpp::key_wallet::bip32::{DerivationPath as DP, KeyDerivationType}; let (Some(secret_access), Some(seed_hash)) = @@ -1003,7 +1002,6 @@ impl QualifiedIdentity { let Some(seed) = plaintext.expose_hd_seed() else { return Ok(None); }; - for identity_index in 0..10u32 { let correct_path = DP::identity_authentication_path( network, diff --git a/src/model/qualified_identity/qualified_identity_public_key.rs b/src/model/qualified_identity/qualified_identity_public_key.rs index b5e1e0966..cb042cf9d 100644 --- a/src/model/qualified_identity/qualified_identity_public_key.rs +++ b/src/model/qualified_identity/qualified_identity_public_key.rs @@ -145,7 +145,6 @@ mod tests { fn pubkey(n: u8) -> PublicKey { let mut sk_bytes = [3u8; 32]; sk_bytes[31] = n.max(1); - let sk = SecretKey::from_secret_bytes(sk_bytes).expect("valid secret key"); PublicKey::new(sk.public_key()) } diff --git a/src/model/single_key.rs b/src/model/single_key.rs index 27951c958..a89dd69ca 100644 --- a/src/model/single_key.rs +++ b/src/model/single_key.rs @@ -10,7 +10,6 @@ //! (spending gated) until single-key moves onto the upstream wallet runtime. use dash_sdk::dpp::dashcore::key::Error as KeyError; - use dash_sdk::dpp::dashcore::{Address, Network, PrivateKey, PublicKey}; use serde::{Deserialize, Serialize}; @@ -22,7 +21,6 @@ use serde::{Deserialize, Serialize}; /// task is the authoritative enforcement layer. pub fn validate_wif(wif: &str, network: Network) -> Result { let priv_key = PrivateKey::from_wif(wif)?; - let pub_key = PublicKey { compressed: priv_key.compressed, inner: priv_key.inner.public_key(), diff --git a/src/model/wallet/mod.rs b/src/model/wallet/mod.rs index 600c585e9..d3e593705 100644 --- a/src/model/wallet/mod.rs +++ b/src/model/wallet/mod.rs @@ -485,7 +485,6 @@ impl Wallet { } })?; let bip44_path = Self::bip44_account0_path(network); - let account_priv = master_priv.derive_priv(&bip44_path).map_err(|e| { WalletCreationError::KeyDerivation { source: Box::new(e), @@ -1341,7 +1340,6 @@ impl Wallet { app_context: &AppContext, ) -> Result<(), WalletError> { let coin_type = coin_type_for_network(network); - for (change_flag, max) in [ (false, BOOTSTRAP_BIP44_EXTERNAL_COUNT), (true, BOOTSTRAP_BIP44_CHANGE_COUNT), @@ -1631,7 +1629,6 @@ impl Wallet { let private_key = extended_private_key.to_priv(); // Create a P2PKH address for platform payment - let public_key = private_key.public_key(); let platform_address = Address::p2pkh(&public_key, network); @@ -2012,7 +2009,6 @@ pub(crate) fn derive_platform_payment_account_xpub( ChildNumber::Hardened { index: account }, ChildNumber::Hardened { index: key_class }, ]); - let master = ExtendedPrivKey::new_master(network, seed)?; let account_priv = master.derive_priv(&account_path)?; Ok(ExtendedPubKey::from_priv(&account_priv)) @@ -3111,7 +3107,6 @@ mod tests { .derive_priv_ecdsa_for_master_seed(&seed, network) .expect("reference derive") .to_priv(); - let address = Address::p2pkh(&reference.public_key(), network); let mut wallet = wallet; @@ -3139,7 +3134,6 @@ mod tests { let reference_xprv = reference_path .derive_priv_ecdsa_for_master_seed(&seed, network) .expect("reference derive"); - let reference = Address::p2pkh(&reference_xprv.to_priv().public_key(), network); let param = param_wallet @@ -3783,7 +3777,6 @@ mod tests { /// for. Independent of the reconciliation path under test. fn platform_address_at(index: u32, network: Network) -> Address { let path = DerivationPath::platform_payment_path(network, 0, 0, index); - let xprv = path .derive_priv_ecdsa_for_master_seed(&TEST_SEED, network) .expect("derive platform key"); @@ -4010,7 +4003,6 @@ mod tests { let foreign_seed = [0x11u8; 64]; let path = DerivationPath::platform_payment_path(network, 0, 0, 0); - let xprv = path .derive_priv_ecdsa_for_master_seed(&foreign_seed, network) .expect("derive foreign key"); diff --git a/src/model/wallet/single_key.rs b/src/model/wallet/single_key.rs index 9cb27bbca..0f6c84524 100644 --- a/src/model/wallet/single_key.rs +++ b/src/model/wallet/single_key.rs @@ -10,7 +10,6 @@ use aes_gcm::aead::Aead; use aes_gcm::{Aes256Gcm, KeyInit}; - use dash_sdk::dpp::dashcore::{Address, Network, OutPoint, PrivateKey, PublicKey, TxOut}; use sha2::{Digest, Sha256}; use std::collections::HashMap; @@ -516,7 +515,6 @@ mod tests { // Through a SingleKeyWallet that holds the closed key. let priv_key = PrivateKey::from_byte_array(&secret, Network::Testnet).expect("valid key bytes"); - let public_key = priv_key.public_key(); let address = Address::p2pkh(&public_key, Network::Testnet); let wallet = SingleKeyWallet { diff --git a/src/ui/identity/funding_common.rs b/src/ui/identity/funding_common.rs index 946af984a..b65c76374 100644 --- a/src/ui/identity/funding_common.rs +++ b/src/ui/identity/funding_common.rs @@ -687,7 +687,6 @@ mod tests { fn addr(n: u8) -> Address { let mut sk_bytes = [1u8; 32]; sk_bytes[31] = n.max(1); - let sk = SecretKey::from_secret_bytes(sk_bytes).expect("valid secret key"); let pubkey = PublicKey::new(sk.public_key()); Address::p2pkh(&pubkey, Network::Testnet) diff --git a/src/ui/identity/keys/key_info_screen.rs b/src/ui/identity/keys/key_info_screen.rs index e80279a75..692088e5c 100644 --- a/src/ui/identity/keys/key_info_screen.rs +++ b/src/ui/identity/keys/key_info_screen.rs @@ -2288,7 +2288,6 @@ mod tests { /// passes and the placement check is what decides the outcome. fn keypair(id: KeyID, purpose: Purpose) -> (IdentityPublicKey, [u8; 32]) { let secret_bytes = [0x2A; 32]; - let secret = SecretKey::from_secret_bytes(secret_bytes).expect("a valid secret key"); let data = PrivateKey::from_byte_array(secret.as_secret_bytes(), Network::Testnet) .expect("valid secret key") diff --git a/src/ui/wallets/wallets_screen/address_table.rs b/src/ui/wallets/wallets_screen/address_table.rs index ed000b95c..577ab2e29 100644 --- a/src/ui/wallets/wallets_screen/address_table.rs +++ b/src/ui/wallets/wallets_screen/address_table.rs @@ -580,7 +580,6 @@ mod tests { fn addr(n: u8) -> Address { let mut sk_bytes = [1u8; 32]; sk_bytes[31] = n.max(1); - let sk = SecretKey::from_secret_bytes(sk_bytes).unwrap(); let pubkey = PublicKey::new(sk.public_key()); Address::p2pkh(&pubkey, Network::Testnet) diff --git a/src/wallet_backend/det_platform_signer.rs b/src/wallet_backend/det_platform_signer.rs index 5f7c4ec63..bf8729edb 100644 --- a/src/wallet_backend/det_platform_signer.rs +++ b/src/wallet_backend/det_platform_signer.rs @@ -203,7 +203,6 @@ mod tests { use super::*; use crate::model::wallet::{AddressInfo, DerivationPathReference, DerivationPathType}; use dash_sdk::dpp::dashcore::Address; - use zeroize::Zeroizing; /// Reference platform-address signing — reproduces exactly what the @@ -242,7 +241,6 @@ mod tests { let xprv = path .derive_priv_ecdsa_for_master_seed(&*seed, network) .expect("derive platform key"); - let address = Address::p2pkh(&xprv.to_priv().public_key(), network); let platform_address = PlatformAddress::try_from(address.clone()).expect("to platform address"); @@ -352,7 +350,6 @@ mod tests { let other_xprv = other_path .derive_priv_ecdsa_for_master_seed(&*seed, network) .unwrap(); - let other_address = Address::p2pkh(&other_xprv.to_priv().public_key(), network); let other_platform = PlatformAddress::try_from(other_address).unwrap(); diff --git a/src/wallet_backend/det_signer.rs b/src/wallet_backend/det_signer.rs index 8ce7096b0..84df4f0e4 100644 --- a/src/wallet_backend/det_signer.rs +++ b/src/wallet_backend/det_signer.rs @@ -207,7 +207,6 @@ impl Signer for DetSigner<'_> { sighash: [u8; 32], ) -> Result<(EcdsaSignature, EcdsaPublicKey), Self::Error> { let secret = self.derive_secret(path)?; - let msg = message_from_digest(sighash)?; let signature = dash_sdk::dpp::secp256k1::ecdsa::sign(msg, &secret); let public_key = PublicKey::from_secret_key(&secret); @@ -341,7 +340,6 @@ mod tests { // Independent reference: derive the key directly from the seed at // the path, then sign + recover the public key with the same // secp256k1 primitive `DetSigner` uses internally. - let reference_sk = secp256k1::SecretKey::from_secret_bytes( *path .derive_priv_ecdsa_for_master_seed(&PARITY_SEED, network) @@ -396,7 +394,6 @@ mod tests { for network in [Network::Testnet, Network::Mainnet] { // Independent reference: derive the xprv straight from the seed at // the path, then convert to the xpub with the same primitive. - let reference_xprv = path .derive_priv_ecdsa_for_master_seed(&PARITY_SEED, network) .expect("reference derive"); @@ -439,7 +436,6 @@ mod tests { #[test] fn hd_derivation_matches_pinned_vector() { let path: DerivationPath = "m/44'/1'/0'/0/0".parse().unwrap(); - let pk = path .derive_priv_ecdsa_for_master_seed(&PARITY_SEED, Network::Testnet) .expect("derive") diff --git a/src/wallet_backend/secret_access.rs b/src/wallet_backend/secret_access.rs index 2038b5317..cf2ba069b 100644 --- a/src/wallet_backend/secret_access.rs +++ b/src/wallet_backend/secret_access.rs @@ -1748,7 +1748,6 @@ mod tests { let priv_key = PrivateKey::from_wif(&known_testnet_wif()).expect("wif"); let raw: Zeroizing<[u8; 32]> = priv_key.inner.to_bytes(); - let pub_key = PublicKey { compressed: priv_key.compressed, inner: priv_key.inner.public_key(), diff --git a/src/wallet_backend/single_key.rs b/src/wallet_backend/single_key.rs index 2fb1c85f9..6981a88ff 100644 --- a/src/wallet_backend/single_key.rs +++ b/src/wallet_backend/single_key.rs @@ -13,10 +13,9 @@ use std::sync::Arc; +use dash_sdk::dpp::dashcore::{Address, Network, PrivateKey, PublicKey}; #[cfg(test)] use dash_sdk::dpp::secp256k1::Message; - -use dash_sdk::dpp::dashcore::{Address, Network, PrivateKey, PublicKey}; #[cfg(test)] use dash_sdk::dpp::secp256k1::ecdsa::Signature; use platform_wallet_storage::secrets::{ @@ -256,7 +255,6 @@ impl<'a> SingleKeyView<'a> { if !priv_key.compressed { return Err(TaskError::UncompressedWifUnsupported); } - let pub_key = PublicKey { compressed: priv_key.compressed, inner: priv_key.inner.public_key(), @@ -710,7 +708,6 @@ impl<'a> SingleKeyView<'a> { return Ok(None); } }; - let public_key = priv_key.public_key(); let address = Address::p2pkh(&public_key, meta.network); @@ -1268,7 +1265,6 @@ mod tests { // the signer hit the imported-key path (not some other key the // backend might fall back to) without referencing BIP-32 at all. let priv_key = PrivateKey::from_wif(known_wif()).expect("wif"); - let pk = dash_sdk::dpp::secp256k1::PublicKey::from(priv_key.inner.public_key()); dash_sdk::dpp::secp256k1::ecdsa::verify(&sig, Message::from_digest(msg), &pk) .expect("signature verifies against WIF-derived pubkey"); @@ -1922,7 +1918,6 @@ mod tests { .expect("chokepoint signs after the right passphrase"); let priv_key = PrivateKey::from_wif(known_wif()).unwrap(); - let pk = dash_sdk::dpp::secp256k1::PublicKey::from(priv_key.inner.public_key()); dash_sdk::dpp::secp256k1::ecdsa::verify(&sig, Message::from_digest(msg), &pk) .expect("chokepoint signature verifies"); diff --git a/src/wallet_backend/snapshot.rs b/src/wallet_backend/snapshot.rs index 90807d228..7ab2ac053 100644 --- a/src/wallet_backend/snapshot.rs +++ b/src/wallet_backend/snapshot.rs @@ -2287,7 +2287,6 @@ mod tests { fn addr(n: u8) -> Address { let mut sk_bytes = [1u8; 32]; sk_bytes[31] = n.max(1); - let sk = SecretKey::from_secret_bytes(sk_bytes).unwrap(); let pubkey = PublicKey::new(sk.public_key()); Address::p2pkh(&pubkey, Network::Testnet) From bd23edc93880a6cb0a7926602316e16ea13770fa Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Mon, 26 Oct 2026 10:14:52 +0000 Subject: [PATCH 24/27] fix(security): erase every secp256k1 secret key copy after use secp256k1 0.33's `SecretKey` is `Copy` with no `Drop`, so each plain copy DET built from its self-zeroizing key material outlived the operation in freed memory. Move `DetSigner`'s `ErasedSecretKey` into `model::erased_secret_key` and use it wherever DET turns key bytes into a `secp256k1::SecretKey`: wallet- and identity-key message signing, local key signing on the key info screen, the HASH160 key check, auto-accept proof signing, DashPay ECDH, and the DIP-14 tweak validation. `with_identity_secret_key` lends the key to its closure as `&SecretKey` so the closure can no longer take an un-erased copy. Signatures and ECDH output are unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- src/backend_task/dashpay/auto_accept_proof.rs | 3 +- src/backend_task/dashpay/encryption.rs | 10 +-- src/backend_task/wallet/mod.rs | 15 ++-- .../wallet/sign_message_with_identity_key.rs | 2 +- .../wallet/sign_message_with_key.rs | 4 +- src/model/dashpay_derivation/dip14.rs | 4 +- src/model/erased_secret_key.rs | 69 +++++++++++++++++++ src/model/mod.rs | 1 + src/model/qualified_identity/mod.rs | 4 +- src/ui/identity/keys/key_info_screen.rs | 5 +- src/wallet_backend/det_signer.rs | 33 +-------- 11 files changed, 98 insertions(+), 52 deletions(-) create mode 100644 src/model/erased_secret_key.rs diff --git a/src/backend_task/dashpay/auto_accept_proof.rs b/src/backend_task/dashpay/auto_accept_proof.rs index 31cd344de..a757fbc6a 100644 --- a/src/backend_task/dashpay/auto_accept_proof.rs +++ b/src/backend_task/dashpay/auto_accept_proof.rs @@ -1,5 +1,6 @@ use crate::backend_task::error::TaskError; use crate::model::dashpay_derivation::derive_auto_accept_key; +use crate::model::erased_secret_key::ErasedSecretKey; use crate::model::qualified_identity::QualifiedIdentity; use dash_sdk::dpp::identity::accessors::IdentityGettersV0; use dash_sdk::dpp::identity::{KeyType, Purpose, SecurityLevel}; @@ -213,7 +214,7 @@ pub fn create_auto_accept_proof_bytes_with_key( let message = Message::from_digest(message_hash.into()); - let secret_key = dash_sdk::dpp::secp256k1::SecretKey::from_secret_bytes(*(signing_key_bytes)) + let secret_key = ErasedSecretKey::from_secret_bytes(signing_key_bytes) .map_err(|e| format!("Failed to create secret key: {}", e))?; let signature = dash_sdk::dpp::secp256k1::ecdsa::sign(message, &secret_key); diff --git a/src/backend_task/dashpay/encryption.rs b/src/backend_task/dashpay/encryption.rs index e6f11be17..f07dd7d4b 100644 --- a/src/backend_task/dashpay/encryption.rs +++ b/src/backend_task/dashpay/encryption.rs @@ -4,10 +4,12 @@ use cbc; use dash_sdk::dpp::identity::IdentityPublicKey; use dash_sdk::dpp::identity::KeyType; use dash_sdk::dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0; -use dash_sdk::dpp::secp256k1::{PublicKey, SecretKey}; +use dash_sdk::dpp::secp256k1::PublicKey; use sha2::{Digest, Sha256}; use zeroize::Zeroizing; +use crate::model::erased_secret_key::ErasedSecretKey; + /// Generate ECDH shared key according to DashPay DIP-15 /// Uses libsecp256k1_ecdh method: SHA256((y[31]&0x1|0x2) || x) /// @@ -19,10 +21,8 @@ pub fn generate_ecdh_shared_key( public_key: &IdentityPublicKey, ) -> Result, String> { // Parse the private key - let secret_key = SecretKey::from_secret_bytes( - private_key - .try_into() - .map_err(|_| "Invalid private key length".to_string())?, + let secret_key = ErasedSecretKey::from_secret_bytes( + <&[u8; 32]>::try_from(private_key).map_err(|_| "Invalid private key length".to_string())?, ) .map_err(|e| format!("Invalid private key: {}", e))?; diff --git a/src/backend_task/wallet/mod.rs b/src/backend_task/wallet/mod.rs index 1a74dbbdb..5558f454a 100644 --- a/src/backend_task/wallet/mod.rs +++ b/src/backend_task/wallet/mod.rs @@ -14,6 +14,7 @@ mod withdraw_from_platform_address; use crate::backend_task::error::TaskError; use crate::context::AppContext; +use crate::model::erased_secret_key::ErasedSecretKey; use crate::model::qualified_identity::PrivateKeyTarget; use crate::model::wallet::WalletSeedHash; use dash_sdk::dpp::address_funds::PlatformAddress; @@ -118,10 +119,10 @@ impl AppContext { } /// Resolve the vault-backed identity key at `(identity_id, target, - /// key_id)` through the JIT chokepoint and hand its `SecretKey` to `f`. - /// The raw key zeroizes when the closure returns — only `f`'s result - /// crosses back to the caller. Shared by the identity-key sign and - /// display tasks. + /// key_id)` through the JIT chokepoint and lend its `SecretKey` to `f`. + /// The raw key and its secp256k1 copy are wiped when the closure returns + /// — only `f`'s result crosses back to the caller. Shared by the + /// identity-key sign and display tasks. /// /// The placement is the caller's word, and it addresses the vault label /// directly, so the bytes are matched against the public key the stored @@ -150,7 +151,7 @@ impl AppContext { identity_id: Identifier, target: PrivateKeyTarget, key_id: KeyID, - f: impl FnOnce(SecretKey) -> Result, + f: impl FnOnce(&SecretKey) -> Result, ) -> Result { let identity = self .get_local_qualified_identity(&identity_id)? @@ -200,7 +201,7 @@ impl AppContext { .ok_or(TaskError::IdentityKeyMissing)?; // Present-but-malformed key bytes are distinct from a // genuinely absent key and from a signing/derivation failure. - let secret_key = SecretKey::from_secret_bytes(*key).map_err(|detail| { + let secret_key = ErasedSecretKey::from_secret_bytes(key).map_err(|detail| { tracing::warn!(error = %detail, "Identity-key secret construction failed"); TaskError::IdentityKeyMalformed })?; @@ -216,7 +217,7 @@ impl AppContext { ); return Err(TaskError::IdentityKeyMismatch); } - f(secret_key) + f(&secret_key) }) .await } diff --git a/src/backend_task/wallet/sign_message_with_identity_key.rs b/src/backend_task/wallet/sign_message_with_identity_key.rs index fc383e5ee..96cb87598 100644 --- a/src/backend_task/wallet/sign_message_with_identity_key.rs +++ b/src/backend_task/wallet/sign_message_with_identity_key.rs @@ -34,7 +34,7 @@ impl AppContext { let signature = self .with_identity_secret_key(identity_id, target.clone(), key_id, |secret_key| { // Identity keys are compressed by convention. - dash_signed_message(message.as_str(), &secret_key, true) + dash_signed_message(message.as_str(), secret_key, true) }) .await?; diff --git a/src/backend_task/wallet/sign_message_with_key.rs b/src/backend_task/wallet/sign_message_with_key.rs index e2d0bf60d..82a2b2f62 100644 --- a/src/backend_task/wallet/sign_message_with_key.rs +++ b/src/backend_task/wallet/sign_message_with_key.rs @@ -5,10 +5,10 @@ use crate::backend_task::BackendTaskSuccessResult; use crate::backend_task::error::TaskError; use crate::backend_task::wallet::dash_signed_message; use crate::context::AppContext; +use crate::model::erased_secret_key::ErasedSecretKey; use crate::model::wallet::WalletSeedHash; use dash_sdk::dpp::identity::KeyType; use dash_sdk::dpp::key_wallet::bip32::DerivationPath; -use dash_sdk::dpp::secp256k1::SecretKey; use std::sync::Arc; impl AppContext { @@ -40,7 +40,7 @@ impl AppContext { &derivation_path, TaskError::WalletMessageSigningFailed, |private_key| { - let secret_key = SecretKey::from_secret_bytes(*private_key.inner.to_bytes()) + let secret_key = ErasedSecretKey::from_secret_bytes(&private_key.inner.to_bytes()) .map_err(|detail| { tracing::warn!(error = %detail, "Sign-message secret key construction failed"); TaskError::WalletMessageSigningFailed diff --git a/src/model/dashpay_derivation/dip14.rs b/src/model/dashpay_derivation/dip14.rs index 7bb826d2b..837e2d9cf 100644 --- a/src/model/dashpay_derivation/dip14.rs +++ b/src/model/dashpay_derivation/dip14.rs @@ -8,10 +8,10 @@ use dash_sdk::dpp::dashcore::hashes::sha512; /// as specified in DIP-0014 for DashPay contact relationships. use dash_sdk::dpp::dashcore::hashes::{Hash, HashEngine}; use dash_sdk::dpp::key_wallet::bip32::{ChainCode, ExtendedPrivKey, ExtendedPubKey, Fingerprint}; -use dash_sdk::dpp::secp256k1::SecretKey; use dash_sdk::platform::Identifier; use super::DerivationError; +use crate::model::erased_secret_key::ErasedSecretKey; /// Perform DIP-14 compliant 256-bit child key derivation for private keys /// @@ -66,7 +66,7 @@ pub fn ckd_priv_256( let i_l: &[u8; 32] = i_l .try_into() .map_err(|_| dash_sdk::dpp::secp256k1::Error::InvalidSecretKey)?; - SecretKey::from_secret_bytes(*i_l)?; + ErasedSecretKey::from_secret_bytes(i_l)?; // k_i = parse_256(I_L) + k_par (mod n) let child_key = parent_key.private_key.add_tweak(i_l)?; diff --git a/src/model/erased_secret_key.rs b/src/model/erased_secret_key.rs new file mode 100644 index 000000000..c9c0dce8b --- /dev/null +++ b/src/model/erased_secret_key.rs @@ -0,0 +1,69 @@ +//! A secp256k1 secret key that erases itself when dropped. + +use dash_sdk::dpp::secp256k1; + +/// A secp256k1 secret key erased when dropped. +/// +/// `secp256k1::SecretKey` is `Copy` with no `Drop`, so every plain copy DET +/// builds from its self-zeroizing key material would otherwise outlive the +/// operation in freed memory. Wrapping the copy keeps the secp256k1 signing and +/// ECDH primitives (byte-identical output) while overwriting the key once the +/// operation finishes. +/// +/// The erase is best effort: it uses secp256k1's volatile `non_secure_erase`, +/// and the compiler may still have moved the bytes elsewhere before. Not +/// `Clone`/`Copy` on purpose, so the only copy is the one that gets erased. +pub(crate) struct ErasedSecretKey(secp256k1::SecretKey); + +impl ErasedSecretKey { + /// Build a secret key from a 32-byte scalar, refusing zero and values not + /// below the curve order. + pub(crate) fn from_secret_bytes(scalar: &[u8; 32]) -> Result { + secp256k1::SecretKey::from_secret_bytes(*scalar).map(Self) + } + + /// Overwrite the held key. Called on drop. + fn erase(&mut self) { + self.0.non_secure_erase(); + } +} + +impl std::ops::Deref for ErasedSecretKey { + type Target = secp256k1::SecretKey; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl Drop for ErasedSecretKey { + fn drop(&mut self) { + self.erase(); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// The erase that `Drop` runs leaves none of the original key bytes + /// behind. Checked through `erase` itself, because reading a value after + /// its drop has run is not allowed. + #[test] + fn erase_overwrites_the_key() { + let scalar = [0x5a; 32]; + let mut key = ErasedSecretKey::from_secret_bytes(&scalar).expect("valid scalar"); + assert_eq!(key.to_secret_bytes(), scalar); + + key.erase(); + + assert_ne!(key.to_secret_bytes(), scalar); + } + + /// The wrapper refuses the same invalid scalars secp256k1 does. + #[test] + fn rejects_invalid_scalar() { + assert!(ErasedSecretKey::from_secret_bytes(&[0; 32]).is_err()); + assert!(ErasedSecretKey::from_secret_bytes(&[0xff; 32]).is_err()); + } +} diff --git a/src/model/mod.rs b/src/model/mod.rs index 7648d0387..ee4825665 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -12,6 +12,7 @@ pub mod derived_identity_key; pub mod dpns; pub mod dpns_usernames; pub mod dpns_voting; +pub(crate) mod erased_secret_key; pub mod fee_estimation; pub mod grovestark_prover; pub mod identity_discovery; diff --git a/src/model/qualified_identity/mod.rs b/src/model/qualified_identity/mod.rs index 3dd57c599..5622e7f58 100644 --- a/src/model/qualified_identity/mod.rs +++ b/src/model/qualified_identity/mod.rs @@ -442,13 +442,13 @@ impl Signer for QualifiedIdentity { // For ECDSA_HASH160, verify that the private key matches the public key hash on Platform // If there's a mismatch (due to incorrect stored derivation path), regenerate the correct path if identity_public_key.key_type() == KeyType::ECDSA_HASH160 { + use crate::model::erased_secret_key::ErasedSecretKey; use dash_sdk::dpp::dashcore::PublicKey; use dash_sdk::dpp::dashcore::hashes::{Hash, ripemd160, sha256}; - use dash_sdk::dpp::secp256k1::SecretKey; let platform_key_data = identity_public_key.data().as_slice(); - if let Ok(secret_key) = SecretKey::from_secret_bytes(*private_key) { + if let Ok(secret_key) = ErasedSecretKey::from_secret_bytes(&private_key) { let derived_pubkey = PublicKey::new(secret_key.public_key()); let pubkey_bytes = derived_pubkey.to_bytes(); let sha256_hash = sha256::Hash::hash(&pubkey_bytes); diff --git a/src/ui/identity/keys/key_info_screen.rs b/src/ui/identity/keys/key_info_screen.rs index 692088e5c..7a66c26a2 100644 --- a/src/ui/identity/keys/key_info_screen.rs +++ b/src/ui/identity/keys/key_info_screen.rs @@ -4,6 +4,7 @@ use crate::backend_task::identity::IdentityTask; use crate::backend_task::wallet::WalletTask; use crate::backend_task::{BackendTask, BackendTaskContext, BackendTaskSuccessResult}; use crate::context::AppContext; +use crate::model::erased_secret_key::ErasedSecretKey; use crate::model::identity_key_protection::validate_protection_password; use crate::model::legacy_recovery::RecoveryItem; use crate::model::qualified_identity::encrypted_key_storage::{ @@ -43,7 +44,6 @@ use dash_sdk::dpp::identity::identity_public_key::accessors::v0::IdentityPublicK use dash_sdk::dpp::identity::identity_public_key::contract_bounds::ContractBounds; use dash_sdk::dpp::key_wallet::bip32::DerivationPath; use dash_sdk::dpp::platform_value::string_encoding::Encoding; -use dash_sdk::dpp::secp256k1::SecretKey; use dash_sdk::platform::IdentityPublicKey; use eframe::egui::{self}; use egui::{Color32, RichText, ScrollArea}; @@ -1302,7 +1302,7 @@ impl KeyInfoScreen { /// are compressed by convention, so a verifier can recover the signer's /// public key and address from the signature alone. fn sign_ecdsa_local(private_key_bytes: &[u8; 32], message: &str) -> Result { - let secret_key = SecretKey::from_secret_bytes(*private_key_bytes) + let secret_key = ErasedSecretKey::from_secret_bytes(private_key_bytes) .expect("clear private key is a valid 32-byte secret"); crate::backend_task::wallet::dash_signed_message(message, &secret_key, true) } @@ -1785,6 +1785,7 @@ mod tests { use dash_sdk::dpp::identity::identity_public_key::v0::IdentityPublicKeyV0; use dash_sdk::dpp::identity::{Identity, KeyID, KeyType, Purpose, SecurityLevel}; use dash_sdk::dpp::platform_value::BinaryData; + use dash_sdk::dpp::secp256k1::SecretKey; use dash_sdk::dpp::version::PlatformVersion; use dash_sdk::platform::Identifier; use std::collections::BTreeMap; diff --git a/src/wallet_backend/det_signer.rs b/src/wallet_backend/det_signer.rs index 84df4f0e4..642fd7c6c 100644 --- a/src/wallet_backend/det_signer.rs +++ b/src/wallet_backend/det_signer.rs @@ -41,6 +41,7 @@ use dash_sdk::dpp::key_wallet::signer::{ExtendedPubKeySigner, Signer, SignerMeth use dash_sdk::dpp::secp256k1::{self, Message, PublicKey, ecdsa}; use zeroize::Zeroizing; +use crate::model::erased_secret_key::ErasedSecretKey; use crate::wallet_backend::secret_access::SecretPlaintext; /// Errors returned by [`DetSigner`]. Wired with `#[source]` so callers can @@ -130,7 +131,7 @@ impl<'a> DetSigner<'a> { /// `EcdsaSecretKey::sign` grinds for a low R and would change them. fn derive_secret(&self, path: &DerivationPath) -> Result { let scalar = self.derive_xprv(path)?.private_key.to_bytes(); - ErasedSecretKey::from_scalar(&scalar) + ErasedSecretKey::from_secret_bytes(&scalar).map_err(DetSignerError::Sign) } /// Sign `msg` (a 32-byte digest) with the held **single key** directly, @@ -143,7 +144,7 @@ impl<'a> DetSigner<'a> { ) -> Result { match &self.secret { HeldSecret::SingleKey(key) => { - let sk = ErasedSecretKey::from_scalar(key)?; + let sk = ErasedSecretKey::from_secret_bytes(key).map_err(DetSignerError::Sign)?; let message = message_from_digest(*msg)?; Ok(dash_sdk::dpp::secp256k1::ecdsa::sign(message, &sk)) } @@ -152,34 +153,6 @@ impl<'a> DetSigner<'a> { } } -/// A secp256k1 secret key erased when dropped. secp256k1's own type keeps its -/// bytes after use, while the derived key it is built from would have wiped -/// them; this keeps the signing primitive (byte-identical signatures) without -/// leaving the key behind in freed memory. -struct ErasedSecretKey(secp256k1::SecretKey); - -impl ErasedSecretKey { - fn from_scalar(scalar: &[u8; 32]) -> Result { - secp256k1::SecretKey::from_secret_bytes(*scalar) - .map(Self) - .map_err(DetSignerError::Sign) - } -} - -impl std::ops::Deref for ErasedSecretKey { - type Target = secp256k1::SecretKey; - - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl Drop for ErasedSecretKey { - fn drop(&mut self) { - self.0.non_secure_erase(); - } -} - impl std::fmt::Debug for DetSigner<'_> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let kind = match self.secret { From ae646281e5595ab9f02cbb1c1b0a7ff5aab302f2 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Mon, 26 Oct 2026 12:08:30 +0000 Subject: [PATCH 25/27] refactor(security): sign with the self-erasing EcdsaSecretKey Replace DET's local `ErasedSecretKey` with the upstream `EcdsaSecretKey` (dash-pkc via dashcore), which erases its scalar on drop, at every site that signs, derives a public key, or range-checks a scalar. HD signing in `DetSigner` and wallet-key message signing now sign with the derived key itself, so no copy is made at all. DIP-15 ECDH is the one site `EcdsaSecretKey` cannot serve, so it keeps a private `EcdhSecretKey` wrapper in the encryption module. The shared `model::erased_secret_key` module and the now-unused `DerivationError::Secp256k1` variant are removed. `EcdsaSecretKey::sign` grinds a low R as Dash Core does, so non-recoverable signatures (Core transactions, single-key signing, auto-accept proofs) change bytes for about half of all digests. They stay valid, low-S and deterministic. Recoverable message signatures and ECDH output are byte-identical. The DetSigner parity reference now signs with `sign_low_r`, and new tests check that signatures verify and are in standard low-S, low-R form. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- src/backend_task/dashpay/auto_accept_proof.rs | 70 ++++++++-- src/backend_task/dashpay/encryption.rs | 61 ++++++++- .../wallet/derive_identity_key_for_display.rs | 6 +- src/backend_task/wallet/mod.rs | 68 +++++----- .../wallet/sign_message_with_key.rs | 12 +- src/model/dashpay_derivation/dip14.rs | 9 +- src/model/dashpay_derivation/mod.rs | 4 - src/model/erased_secret_key.rs | 69 ---------- src/model/mod.rs | 1 - src/model/qualified_identity/mod.rs | 4 +- src/ui/identity/keys/key_info_screen.rs | 4 +- src/wallet_backend/det_signer.rs | 124 ++++++++++++------ 12 files changed, 249 insertions(+), 183 deletions(-) delete mode 100644 src/model/erased_secret_key.rs diff --git a/src/backend_task/dashpay/auto_accept_proof.rs b/src/backend_task/dashpay/auto_accept_proof.rs index a757fbc6a..cfa8e52a7 100644 --- a/src/backend_task/dashpay/auto_accept_proof.rs +++ b/src/backend_task/dashpay/auto_accept_proof.rs @@ -1,7 +1,7 @@ use crate::backend_task::error::TaskError; use crate::model::dashpay_derivation::derive_auto_accept_key; -use crate::model::erased_secret_key::ErasedSecretKey; use crate::model::qualified_identity::QualifiedIdentity; +use dash_sdk::dpp::dashcore::ecdsa::EcdsaSecretKey; use dash_sdk::dpp::identity::accessors::IdentityGettersV0; use dash_sdk::dpp::identity::{KeyType, Purpose, SecurityLevel}; use dash_sdk::dpp::secp256k1::Message; @@ -210,15 +210,13 @@ pub fn create_auto_accept_proof_bytes_with_key( hasher.update(&message_data); let message_hash = hasher.finalize(); - // Create secp256k1 message and sign - - let message = Message::from_digest(message_hash.into()); - - let secret_key = ErasedSecretKey::from_secret_bytes(signing_key_bytes) + // Sign the digest. The key erases itself when it drops. The signature is + // low-S with a low R ground as Dash Core grinds it; any ECDSA verifier + // accepts it. + let secret_key = EcdsaSecretKey::from_bytes(signing_key_bytes) .map_err(|e| format!("Failed to create secret key: {}", e))?; - let signature = dash_sdk::dpp::secp256k1::ecdsa::sign(message, &secret_key); - let sig_bytes = signature.serialize_compact(); + let sig_bytes = secret_key.sign(&message_hash.into()).to_bytes(); // Build the proof bytes let mut proof_bytes = Vec::new(); @@ -309,3 +307,59 @@ pub async fn verify_auto_accept_proof( } // No local persistence required + +#[cfg(test)] +mod tests { + use super::*; + use dash_sdk::dpp::secp256k1::{PublicKey, SecretKey, ecdsa}; + + /// The proof's signature verifies, the way `verify_auto_accept_proof` + /// checks it, against the public key of the key it was signed with. The + /// signature is low-S with a low R ground as Dash Core grinds it; R lands + /// low half the time on its own, so this signs for many accounts. + #[test] + fn proof_signature_verifies_and_has_low_r() { + let key_bytes = [0x2Bu8; 32]; + let public_key = + PublicKey::from_secret_key(&SecretKey::from_secret_bytes(key_bytes).expect("valid")); + let sender = Identifier::from([0x01; 32]); + let recipient = Identifier::from([0x02; 32]); + let expires_at = 1_900_000_000u64; + + for account_reference in 0..32u32 { + let proof = create_auto_accept_proof_bytes_with_key( + expires_at, + &key_bytes, + &sender, + &recipient, + account_reference, + ) + .expect("proof"); + + assert_eq!(proof[0], 0, "key type"); + assert_eq!( + &proof[1..5], + &(expires_at as u32).to_be_bytes(), + "key index" + ); + assert_eq!(proof[5], 64, "signature size"); + assert_eq!(proof.len(), 6 + 64); + + let mut message_data = Vec::new(); + message_data.extend_from_slice(&sender.to_buffer()); + message_data.extend_from_slice(&recipient.to_buffer()); + message_data.extend_from_slice(&account_reference.to_le_bytes()); + let digest: [u8; 32] = Sha256::digest(&message_data).into(); + + let signature = ecdsa::Signature::from_compact(&proof[6..]).expect("compact"); + let mut normalized = signature; + normalized.normalize_s(); + assert_eq!(normalized, signature, "high S for {account_reference}"); + assert!( + ecdsa::verify(&signature, Message::from_digest(digest), &public_key).is_ok(), + "proof does not verify for {account_reference}" + ); + assert!(proof[6] < 0x80, "R is not low for {account_reference}"); + } + } +} diff --git a/src/backend_task/dashpay/encryption.rs b/src/backend_task/dashpay/encryption.rs index f07dd7d4b..b03cf7a61 100644 --- a/src/backend_task/dashpay/encryption.rs +++ b/src/backend_task/dashpay/encryption.rs @@ -4,11 +4,40 @@ use cbc; use dash_sdk::dpp::identity::IdentityPublicKey; use dash_sdk::dpp::identity::KeyType; use dash_sdk::dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0; -use dash_sdk::dpp::secp256k1::PublicKey; +use dash_sdk::dpp::secp256k1::{self, PublicKey}; use sha2::{Digest, Sha256}; use zeroize::Zeroizing; -use crate::model::erased_secret_key::ErasedSecretKey; +/// A secp256k1 secret key erased when dropped, for the ECDH below. +/// +/// `secp256k1::SecretKey` is `Copy` with no `Drop`, so a plain one would +/// outlive the exchange in freed memory. Everywhere else DET uses the +/// self-erasing `EcdsaSecretKey`, which has no ECDH; this keeps the raw +/// secp256k1 ECDH (unchanged output) and overwrites the key once it is done. +/// +/// The erase is best effort: it uses secp256k1's volatile `non_secure_erase`, +/// and the compiler may still have moved the bytes elsewhere before. Not +/// `Clone`/`Copy` on purpose, so the only copy is the one that gets erased. +struct EcdhSecretKey(secp256k1::SecretKey); + +impl EcdhSecretKey { + /// Build a secret key from a 32-byte scalar, refusing zero and values not + /// below the curve order. + fn from_secret_bytes(scalar: &[u8; 32]) -> Result { + secp256k1::SecretKey::from_secret_bytes(*scalar).map(Self) + } + + /// Overwrite the held key. Called on drop. + fn erase(&mut self) { + self.0.non_secure_erase(); + } +} + +impl Drop for EcdhSecretKey { + fn drop(&mut self) { + self.erase(); + } +} /// Generate ECDH shared key according to DashPay DIP-15 /// Uses libsecp256k1_ecdh method: SHA256((y[31]&0x1|0x2) || x) @@ -21,7 +50,7 @@ pub fn generate_ecdh_shared_key( public_key: &IdentityPublicKey, ) -> Result, String> { // Parse the private key - let secret_key = ErasedSecretKey::from_secret_bytes( + let secret_key = EcdhSecretKey::from_secret_bytes( <&[u8; 32]>::try_from(private_key).map_err(|_| "Invalid private key length".to_string())?, ) .map_err(|e| format!("Invalid private key: {}", e))?; @@ -36,10 +65,7 @@ pub fn generate_ecdh_shared_key( // Perform ECDH to get shared secret. The 64-byte point is secret; // hold it in `Zeroizing` so it is wiped once the key is hashed out. let shared_secret = Zeroizing::new( - dash_sdk::dpp::secp256k1::ecdh::shared_secret_point( - &public_key, - &secret_key, - ), + secp256k1::ecdh::shared_secret_point(&public_key, &secret_key.0), ); // Extract x and y coordinates (64 bytes total: 32 + 32) @@ -299,6 +325,27 @@ mod tests { use bip39::rand::{self, RngCore}; use dash_sdk::dpp::secp256k1::{PublicKey, SecretKey}; + /// The erase that `Drop` runs leaves none of the original key bytes + /// behind. Checked through `erase` itself, because reading a value after + /// its drop has run is not allowed. + #[test] + fn ecdh_secret_key_erase_overwrites_the_key() { + let scalar = [0x5a; 32]; + let mut key = EcdhSecretKey::from_secret_bytes(&scalar).expect("valid scalar"); + assert_eq!(key.0.to_secret_bytes(), scalar); + + key.erase(); + + assert_ne!(key.0.to_secret_bytes(), scalar); + } + + /// The ECDH key refuses the same invalid scalars secp256k1 does. + #[test] + fn ecdh_secret_key_rejects_invalid_scalar() { + assert!(EcdhSecretKey::from_secret_bytes(&[0; 32]).is_err()); + assert!(EcdhSecretKey::from_secret_bytes(&[0xff; 32]).is_err()); + } + fn generate_test_shared_key() -> [u8; 32] { let mut shared_key = [0u8; 32]; rand::thread_rng().fill_bytes(&mut shared_key); diff --git a/src/backend_task/wallet/derive_identity_key_for_display.rs b/src/backend_task/wallet/derive_identity_key_for_display.rs index 52dd10322..777a286ef 100644 --- a/src/backend_task/wallet/derive_identity_key_for_display.rs +++ b/src/backend_task/wallet/derive_identity_key_for_display.rs @@ -28,9 +28,9 @@ impl AppContext { let network = self.network; let wif = self .with_identity_secret_key(identity_id, target.clone(), key_id, |secret_key| { - let private_key = - PrivateKey::from_byte_array(secret_key.as_secret_bytes(), network) - .map_err(|_| TaskError::IdentityKeyMalformed)?; + // Identity keys are compressed by convention. The clone erases + // itself when `private_key` drops. + let private_key = PrivateKey::new(secret_key.clone(), network); Ok(Secret::new(private_key.to_wif())) }) .await?; diff --git a/src/backend_task/wallet/mod.rs b/src/backend_task/wallet/mod.rs index 5558f454a..f81983fc3 100644 --- a/src/backend_task/wallet/mod.rs +++ b/src/backend_task/wallet/mod.rs @@ -14,12 +14,11 @@ mod withdraw_from_platform_address; use crate::backend_task::error::TaskError; use crate::context::AppContext; -use crate::model::erased_secret_key::ErasedSecretKey; use crate::model::qualified_identity::PrivateKeyTarget; use crate::model::wallet::WalletSeedHash; use dash_sdk::dpp::address_funds::PlatformAddress; use dash_sdk::dpp::balances::credits::Credits; -use dash_sdk::dpp::dashcore::ecdsa::EcdsaRecSignature; +use dash_sdk::dpp::dashcore::ecdsa::{EcdsaRecSignature, EcdsaSecretKey}; use dash_sdk::dpp::dashcore::hashes::Hash; use dash_sdk::dpp::dashcore::sign_message::{self, signed_msg_hash}; use dash_sdk::dpp::dashcore::{OutPoint, PrivateKey}; @@ -27,7 +26,6 @@ use dash_sdk::dpp::identity::core_script::CoreScript; use dash_sdk::dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0; use dash_sdk::dpp::identity::{KeyID, KeyType}; use dash_sdk::dpp::key_wallet::bip32::DerivationPath; -use dash_sdk::dpp::secp256k1::{Message, SecretKey}; use dash_sdk::platform::Identifier; use std::collections::BTreeMap; use std::sync::Arc; @@ -38,30 +36,30 @@ use std::sync::Arc; /// a verifier can recover the signer's public key from the signature alone. /// Shared by the wallet-key and identity-key message-signing tasks. /// +/// The recoverable signature is plain RFC 6979 (no low-R grinding), so the +/// envelope bytes match every earlier build. [`EcdsaSecretKey`] marks every +/// key compressed, so the envelope is rebuilt with the caller's flag. +/// /// # Errors /// /// [`TaskError::WalletMessageSigningFailed`] if the signature cannot be put /// in the envelope; the detail is logged. pub(crate) fn dash_signed_message( message: &str, - secret_key: &SecretKey, + secret_key: &EcdsaSecretKey, compressed: bool, ) -> Result { let message_hash = signed_msg_hash(message); - let digest = Message::from_digest(*message_hash.as_byte_array()); - let recoverable = dash_sdk::dpp::secp256k1::ecdsa::RecoverableSignature::sign_ecdsa_recoverable( - digest, secret_key, - ); - let (recovery_id, _) = recoverable.serialize_compact(); - let envelope = EcdsaRecSignature::from_parts( - recoverable.to_standard().into(), - recovery_id.to_u8(), - compressed, - ) - .map_err(|detail| { - tracing::warn!(error = %detail, "Sign-message envelope construction failed"); - TaskError::WalletMessageSigningFailed - })?; + let recoverable = secret_key.sign_recoverable(message_hash.as_byte_array()); + let envelope = recoverable + .signature() + .and_then(|signature| { + EcdsaRecSignature::from_parts(signature, recoverable.recovery_id()?, compressed) + }) + .map_err(|detail| { + tracing::warn!(error = %detail, "Sign-message envelope construction failed"); + TaskError::WalletMessageSigningFailed + })?; Ok(sign_message::to_base64(envelope)) } @@ -119,10 +117,10 @@ impl AppContext { } /// Resolve the vault-backed identity key at `(identity_id, target, - /// key_id)` through the JIT chokepoint and lend its `SecretKey` to `f`. - /// The raw key and its secp256k1 copy are wiped when the closure returns - /// — only `f`'s result crosses back to the caller. Shared by the - /// identity-key sign and display tasks. + /// key_id)` through the JIT chokepoint and lend it to `f` as an + /// [`EcdsaSecretKey`]. The raw key and the parsed key are both wiped when + /// the closure returns — only `f`'s result crosses back to the caller. + /// Shared by the identity-key sign and display tasks. /// /// The placement is the caller's word, and it addresses the vault label /// directly, so the bytes are matched against the public key the stored @@ -151,7 +149,7 @@ impl AppContext { identity_id: Identifier, target: PrivateKeyTarget, key_id: KeyID, - f: impl FnOnce(&SecretKey) -> Result, + f: impl FnOnce(&EcdsaSecretKey) -> Result, ) -> Result { let identity = self .get_local_qualified_identity(&identity_id)? @@ -201,7 +199,7 @@ impl AppContext { .ok_or(TaskError::IdentityKeyMissing)?; // Present-but-malformed key bytes are distinct from a // genuinely absent key and from a signing/derivation failure. - let secret_key = ErasedSecretKey::from_secret_bytes(key).map_err(|detail| { + let secret_key = EcdsaSecretKey::from_bytes(key).map_err(|detail| { tracing::warn!(error = %detail, "Identity-key secret construction failed"); TaskError::IdentityKeyMalformed })?; @@ -399,7 +397,7 @@ mod tests { use dash_sdk::dpp::identity::{Identity, Purpose, SecurityLevel}; use dash_sdk::dpp::key_wallet::bip32::ChildNumber; use dash_sdk::dpp::platform_value::BinaryData; - use dash_sdk::dpp::secp256k1::PublicKey; + use dash_sdk::dpp::secp256k1::{PublicKey, SecretKey}; use dash_sdk::dpp::version::PlatformVersion; use dash_sdk::platform::IdentityPublicKey; use std::sync::atomic::{AtomicBool, Ordering}; @@ -524,8 +522,10 @@ mod tests { /// hardcoded recovery header would fail ~50% of the time here. Both the /// wallet-key and identity-key signers call this one helper. fn assert_recovers(compressed: bool) { - let secret_key = SecretKey::from_secret_bytes([0x42u8; 32]).expect("valid secret"); - let expected_pubkey = PublicKey::from_secret_key(&secret_key); + let secret_key = EcdsaSecretKey::from_bytes(&[0x42u8; 32]).expect("valid secret"); + let expected_pubkey = PublicKey::from_secret_key( + &SecretKey::from_secret_bytes([0x42u8; 32]).expect("valid secret"), + ); let message = "Bilby was here"; let base64 = dash_signed_message(message, &secret_key, compressed).expect("signs"); @@ -540,16 +540,20 @@ mod tests { /// The envelope keeps the layout the previous encoder produced: a header /// byte `27 + recovery id` (`+ 4` when compressed), then the compact - /// `r || s` of the same deterministic signature. + /// `r || s` of the same deterministic signature. The reference signs with + /// the raw secp256k1 primitive, so this also proves the signature bytes + /// did not change when signing moved to `EcdsaSecretKey`. #[test] fn the_envelope_keeps_its_byte_layout() { use dash_sdk::dpp::secp256k1::Message; use dash_sdk::dpp::secp256k1::ecdsa::RecoverableSignature; let message = "Bilby was here"; - let secret_key = SecretKey::from_secret_bytes([0x42u8; 32]).expect("valid secret"); + let reference_key = SecretKey::from_secret_bytes([0x42u8; 32]).expect("valid secret"); + let secret_key = EcdsaSecretKey::from_bytes(&[0x42u8; 32]).expect("valid secret"); let digest = Message::from_digest(*signed_msg_hash(message).as_byte_array()); let (recovery_id, compact) = - RecoverableSignature::sign_ecdsa_recoverable(digest, &secret_key).serialize_compact(); + RecoverableSignature::sign_ecdsa_recoverable(digest, &reference_key) + .serialize_compact(); for compressed in [false, true] { let mut expected = [0u8; 65]; expected[0] = 27 + recovery_id.to_u8() + if compressed { 4 } else { 0 }; @@ -643,7 +647,7 @@ mod tests { let bytes = fixture .ctx - .with_identity_secret_key(identity_id, MAIN, 7, |key| Ok(key.to_secret_bytes())) + .with_identity_secret_key(identity_id, MAIN, 7, |key| Ok(*key.to_bytes())) .await .expect("a key that matches its record must resolve"); @@ -786,7 +790,7 @@ mod tests { let bytes = fixture .ctx - .with_identity_secret_key(identity_id, MAIN, 7, |key| Ok(key.to_secret_bytes())) + .with_identity_secret_key(identity_id, MAIN, 7, |key| Ok(*key.to_bytes())) .await .expect("the sibling placement's live bytes must be served"); diff --git a/src/backend_task/wallet/sign_message_with_key.rs b/src/backend_task/wallet/sign_message_with_key.rs index 82a2b2f62..2a3b10840 100644 --- a/src/backend_task/wallet/sign_message_with_key.rs +++ b/src/backend_task/wallet/sign_message_with_key.rs @@ -5,7 +5,6 @@ use crate::backend_task::BackendTaskSuccessResult; use crate::backend_task::error::TaskError; use crate::backend_task::wallet::dash_signed_message; use crate::context::AppContext; -use crate::model::erased_secret_key::ErasedSecretKey; use crate::model::wallet::WalletSeedHash; use dash_sdk::dpp::identity::KeyType; use dash_sdk::dpp::key_wallet::bip32::DerivationPath; @@ -40,12 +39,11 @@ impl AppContext { &derivation_path, TaskError::WalletMessageSigningFailed, |private_key| { - let secret_key = ErasedSecretKey::from_secret_bytes(&private_key.inner.to_bytes()) - .map_err(|detail| { - tracing::warn!(error = %detail, "Sign-message secret key construction failed"); - TaskError::WalletMessageSigningFailed - })?; - dash_signed_message(message.as_str(), &secret_key, private_key.compressed) + dash_signed_message( + message.as_str(), + &private_key.inner, + private_key.compressed, + ) }, ) .await?; diff --git a/src/model/dashpay_derivation/dip14.rs b/src/model/dashpay_derivation/dip14.rs index 837e2d9cf..69dbb9359 100644 --- a/src/model/dashpay_derivation/dip14.rs +++ b/src/model/dashpay_derivation/dip14.rs @@ -1,5 +1,5 @@ use dash_sdk::dpp::dashcore::Network; -use dash_sdk::dpp::dashcore::ecdsa::EcdsaPublicKey; +use dash_sdk::dpp::dashcore::ecdsa::{EcdsaError, EcdsaPublicKey, EcdsaSecretKey}; use dash_sdk::dpp::dashcore::hashes::hmac::{Hmac, HmacEngine}; use dash_sdk::dpp::dashcore::hashes::sha512; /// DIP-14 compliant 256-bit HD key derivation implementation @@ -11,7 +11,6 @@ use dash_sdk::dpp::key_wallet::bip32::{ChainCode, ExtendedPrivKey, ExtendedPubKe use dash_sdk::platform::Identifier; use super::DerivationError; -use crate::model::erased_secret_key::ErasedSecretKey; /// Perform DIP-14 compliant 256-bit child key derivation for private keys /// @@ -63,10 +62,8 @@ pub fn ckd_priv_256( // Parse I_L as a private key (refusing zero and values >= n) and add it // to the parent key. - let i_l: &[u8; 32] = i_l - .try_into() - .map_err(|_| dash_sdk::dpp::secp256k1::Error::InvalidSecretKey)?; - ErasedSecretKey::from_secret_bytes(i_l)?; + let i_l: &[u8; 32] = i_l.try_into().map_err(|_| EcdsaError::InvalidSecretKey)?; + EcdsaSecretKey::from_bytes(i_l)?; // k_i = parse_256(I_L) + k_par (mod n) let child_key = parent_key.private_key.add_tweak(i_l)?; diff --git a/src/model/dashpay_derivation/mod.rs b/src/model/dashpay_derivation/mod.rs index 24a2aaad5..5ed877885 100644 --- a/src/model/dashpay_derivation/mod.rs +++ b/src/model/dashpay_derivation/mod.rs @@ -22,10 +22,6 @@ pub enum DerivationError { #[error("The wallet keys could not be derived. The wallet data may be invalid.")] Bip32(#[from] dash_sdk::dpp::key_wallet::bip32::Error), - /// A secp256k1 operation on derived key material failed. - #[error("The wallet key material could not be processed. The wallet data may be invalid.")] - Secp256k1(#[from] dash_sdk::dpp::secp256k1::Error), - /// An ECDSA operation on derived key material failed. #[error("The wallet key material could not be processed. The wallet data may be invalid.")] Ecdsa(#[from] dash_sdk::dpp::dashcore::ecdsa::EcdsaError), diff --git a/src/model/erased_secret_key.rs b/src/model/erased_secret_key.rs deleted file mode 100644 index c9c0dce8b..000000000 --- a/src/model/erased_secret_key.rs +++ /dev/null @@ -1,69 +0,0 @@ -//! A secp256k1 secret key that erases itself when dropped. - -use dash_sdk::dpp::secp256k1; - -/// A secp256k1 secret key erased when dropped. -/// -/// `secp256k1::SecretKey` is `Copy` with no `Drop`, so every plain copy DET -/// builds from its self-zeroizing key material would otherwise outlive the -/// operation in freed memory. Wrapping the copy keeps the secp256k1 signing and -/// ECDH primitives (byte-identical output) while overwriting the key once the -/// operation finishes. -/// -/// The erase is best effort: it uses secp256k1's volatile `non_secure_erase`, -/// and the compiler may still have moved the bytes elsewhere before. Not -/// `Clone`/`Copy` on purpose, so the only copy is the one that gets erased. -pub(crate) struct ErasedSecretKey(secp256k1::SecretKey); - -impl ErasedSecretKey { - /// Build a secret key from a 32-byte scalar, refusing zero and values not - /// below the curve order. - pub(crate) fn from_secret_bytes(scalar: &[u8; 32]) -> Result { - secp256k1::SecretKey::from_secret_bytes(*scalar).map(Self) - } - - /// Overwrite the held key. Called on drop. - fn erase(&mut self) { - self.0.non_secure_erase(); - } -} - -impl std::ops::Deref for ErasedSecretKey { - type Target = secp256k1::SecretKey; - - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl Drop for ErasedSecretKey { - fn drop(&mut self) { - self.erase(); - } -} - -#[cfg(test)] -mod tests { - use super::*; - - /// The erase that `Drop` runs leaves none of the original key bytes - /// behind. Checked through `erase` itself, because reading a value after - /// its drop has run is not allowed. - #[test] - fn erase_overwrites_the_key() { - let scalar = [0x5a; 32]; - let mut key = ErasedSecretKey::from_secret_bytes(&scalar).expect("valid scalar"); - assert_eq!(key.to_secret_bytes(), scalar); - - key.erase(); - - assert_ne!(key.to_secret_bytes(), scalar); - } - - /// The wrapper refuses the same invalid scalars secp256k1 does. - #[test] - fn rejects_invalid_scalar() { - assert!(ErasedSecretKey::from_secret_bytes(&[0; 32]).is_err()); - assert!(ErasedSecretKey::from_secret_bytes(&[0xff; 32]).is_err()); - } -} diff --git a/src/model/mod.rs b/src/model/mod.rs index ee4825665..7648d0387 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -12,7 +12,6 @@ pub mod derived_identity_key; pub mod dpns; pub mod dpns_usernames; pub mod dpns_voting; -pub(crate) mod erased_secret_key; pub mod fee_estimation; pub mod grovestark_prover; pub mod identity_discovery; diff --git a/src/model/qualified_identity/mod.rs b/src/model/qualified_identity/mod.rs index 5622e7f58..27e908b85 100644 --- a/src/model/qualified_identity/mod.rs +++ b/src/model/qualified_identity/mod.rs @@ -442,13 +442,13 @@ impl Signer for QualifiedIdentity { // For ECDSA_HASH160, verify that the private key matches the public key hash on Platform // If there's a mismatch (due to incorrect stored derivation path), regenerate the correct path if identity_public_key.key_type() == KeyType::ECDSA_HASH160 { - use crate::model::erased_secret_key::ErasedSecretKey; use dash_sdk::dpp::dashcore::PublicKey; + use dash_sdk::dpp::dashcore::ecdsa::EcdsaSecretKey; use dash_sdk::dpp::dashcore::hashes::{Hash, ripemd160, sha256}; let platform_key_data = identity_public_key.data().as_slice(); - if let Ok(secret_key) = ErasedSecretKey::from_secret_bytes(&private_key) { + if let Ok(secret_key) = EcdsaSecretKey::from_bytes(&private_key) { let derived_pubkey = PublicKey::new(secret_key.public_key()); let pubkey_bytes = derived_pubkey.to_bytes(); let sha256_hash = sha256::Hash::hash(&pubkey_bytes); diff --git a/src/ui/identity/keys/key_info_screen.rs b/src/ui/identity/keys/key_info_screen.rs index 7a66c26a2..c8ccdceea 100644 --- a/src/ui/identity/keys/key_info_screen.rs +++ b/src/ui/identity/keys/key_info_screen.rs @@ -4,7 +4,6 @@ use crate::backend_task::identity::IdentityTask; use crate::backend_task::wallet::WalletTask; use crate::backend_task::{BackendTask, BackendTaskContext, BackendTaskSuccessResult}; use crate::context::AppContext; -use crate::model::erased_secret_key::ErasedSecretKey; use crate::model::identity_key_protection::validate_protection_password; use crate::model::legacy_recovery::RecoveryItem; use crate::model::qualified_identity::encrypted_key_storage::{ @@ -34,6 +33,7 @@ use crate::wallet_backend::IdentityKeyView; use crate::wallet_backend::secret_seam::SecretScheme; use dash_sdk::dashcore_rpc::dashcore::PrivateKey as RPCPrivateKey; use dash_sdk::dpp::dashcore::address::Payload; +use dash_sdk::dpp::dashcore::ecdsa::EcdsaSecretKey; use dash_sdk::dpp::dashcore::hashes::Hash; use dash_sdk::dpp::dashcore::{Address, PrivateKey, PubkeyHash, ScriptHash}; use dash_sdk::dpp::identity::KeyType; @@ -1302,7 +1302,7 @@ impl KeyInfoScreen { /// are compressed by convention, so a verifier can recover the signer's /// public key and address from the signature alone. fn sign_ecdsa_local(private_key_bytes: &[u8; 32], message: &str) -> Result { - let secret_key = ErasedSecretKey::from_secret_bytes(private_key_bytes) + let secret_key = EcdsaSecretKey::from_bytes(private_key_bytes) .expect("clear private key is a valid 32-byte secret"); crate::backend_task::wallet::dash_signed_message(message, &secret_key, true) } diff --git a/src/wallet_backend/det_signer.rs b/src/wallet_backend/det_signer.rs index 642fd7c6c..e33f13e75 100644 --- a/src/wallet_backend/det_signer.rs +++ b/src/wallet_backend/det_signer.rs @@ -35,13 +35,12 @@ use async_trait::async_trait; use dash_sdk::dpp::dashcore::Network; -use dash_sdk::dpp::dashcore::ecdsa::{EcdsaPublicKey, EcdsaSignature}; +use dash_sdk::dpp::dashcore::ecdsa::{EcdsaError, EcdsaPublicKey, EcdsaSecretKey, EcdsaSignature}; use dash_sdk::dpp::key_wallet::bip32::{DerivationPath, ExtendedPrivKey, ExtendedPubKey}; use dash_sdk::dpp::key_wallet::signer::{ExtendedPubKeySigner, Signer, SignerMethod}; -use dash_sdk::dpp::secp256k1::{self, Message, PublicKey, ecdsa}; +use dash_sdk::dpp::secp256k1::ecdsa; use zeroize::Zeroizing; -use crate::model::erased_secret_key::ErasedSecretKey; use crate::wallet_backend::secret_access::SecretPlaintext; /// Errors returned by [`DetSigner`]. Wired with `#[source]` so callers can @@ -52,22 +51,15 @@ pub enum DetSignerError { /// Derivation from the held seed failed. #[error("just-in-time signer key derivation failed")] Derive(#[source] dash_sdk::dpp::key_wallet::bip32::Error), - /// secp256k1 sign / digest construction failed. + /// The held single key is not a valid secp256k1 secret key. #[error("just-in-time signer secp256k1 operation failed")] - Sign(#[source] secp256k1::Error), + Sign(#[source] EcdsaError), /// The held secret is the wrong kind for the requested operation /// (e.g. a single-key plaintext asked for path-based HD signing). #[error("just-in-time signer received the wrong secret kind for this operation")] WrongSecretKind, } -/// Hash a 32-byte digest into an ECDSA-ready [`Message`], surfacing the -/// upstream digest-length error as a typed signer error rather than -/// panicking. -fn message_from_digest(digest: [u8; 32]) -> Result { - Ok(Message::from_digest(digest)) -} - /// JIT [`Signer`] backed by a **borrowed** held secret. /// /// Constructed from a [`SecretPlaintext`] borrow inside a @@ -122,31 +114,21 @@ impl<'a> DetSigner<'a> { } } - /// Derive the secp256k1 secret at `path` from the held HD seed. Errors - /// with [`DetSignerError::WrongSecretKind`] if the held secret is a - /// single key (no derivation tree). - /// - /// Signing stays on the secp256k1 primitive (plain RFC 6979 nonce), so - /// signatures are byte-identical to earlier builds; the newer - /// `EcdsaSecretKey::sign` grinds for a low R and would change them. - fn derive_secret(&self, path: &DerivationPath) -> Result { - let scalar = self.derive_xprv(path)?.private_key.to_bytes(); - ErasedSecretKey::from_secret_bytes(&scalar).map_err(DetSignerError::Sign) - } - /// Sign `msg` (a 32-byte digest) with the held **single key** directly, /// no derivation. Errors if the held secret is an HD seed. Used by the /// JIT single-key signing chokepoint /// ([`WalletBackend::sign_single_key`](super::WalletBackend::sign_single_key)). + /// + /// The signature is low-S with a low R ground as Dash Core grinds it (see + /// [`EcdsaSecretKey::sign`]); the key erases itself when it drops. pub(crate) fn sign_single_key_ecdsa( &self, msg: &[u8; 32], ) -> Result { match &self.secret { HeldSecret::SingleKey(key) => { - let sk = ErasedSecretKey::from_secret_bytes(key).map_err(DetSignerError::Sign)?; - let message = message_from_digest(*msg)?; - Ok(dash_sdk::dpp::secp256k1::ecdsa::sign(message, &sk)) + let secret = EcdsaSecretKey::from_bytes(key).map_err(DetSignerError::Sign)?; + Ok(secret.sign(msg).into()) } HeldSecret::HdSeed(_) => Err(DetSignerError::WrongSecretKind), } @@ -179,16 +161,16 @@ impl Signer for DetSigner<'_> { path: &DerivationPath, sighash: [u8; 32], ) -> Result<(EcdsaSignature, EcdsaPublicKey), Self::Error> { - let secret = self.derive_secret(path)?; - let msg = message_from_digest(sighash)?; - let signature = dash_sdk::dpp::secp256k1::ecdsa::sign(msg, &secret); - let public_key = PublicKey::from_secret_key(&secret); - Ok((signature.into(), public_key.into())) + // Sign with the derived key itself, which erases itself on drop, so no + // copy of the scalar is made. `EcdsaSecretKey::sign` is low-S and + // grinds a low R as Dash Core does. + let xprv = self.derive_xprv(path)?; + let secret = &xprv.private_key; + Ok((secret.sign(&sighash), secret.public_key())) } async fn public_key(&self, path: &DerivationPath) -> Result { - let secret = self.derive_secret(path)?; - Ok(PublicKey::from_secret_key(&secret).into()) + Ok(self.derive_xprv(path)?.private_key.public_key()) } } @@ -221,6 +203,7 @@ mod tests { use crate::wallet_backend::secret_prompt::test_support::{ScriptedAnswer, TestPrompt}; use crate::wallet_backend::single_key::open_secret_store; use crate::wallet_backend::wallet_seed_store::WalletSeedView; + use dash_sdk::dpp::secp256k1::{self, Message, PublicKey}; use std::sync::Arc; const SENTINEL_PASSPHRASE: &str = "correct-horse-battery-staple-SENTINEL"; @@ -311,8 +294,11 @@ mod tests { for network in [Network::Testnet, Network::Mainnet] { // Independent reference: derive the key directly from the seed at - // the path, then sign + recover the public key with the same - // secp256k1 primitive `DetSigner` uses internally. + // the path, then sign + recover the public key with the raw + // secp256k1 primitive. The signer signs through + // `EcdsaSecretKey::sign`, which grinds a low R as Dash Core does, + // so the reference is secp256k1's own low-R signer; plain RFC 6979 + // `ecdsa::sign` differs in about half of all signatures. let reference_sk = secp256k1::SecretKey::from_secret_bytes( *path .derive_priv_ecdsa_for_master_seed(&PARITY_SEED, network) @@ -322,10 +308,8 @@ mod tests { ) .expect("valid scalar"); let reference_msg = Message::from_digest(sighash); - let reference_sig = EcdsaSignature::from(dash_sdk::dpp::secp256k1::ecdsa::sign( - reference_msg, - &reference_sk, - )); + let reference_sig = + EcdsaSignature::from(secp256k1::ecdsa::sign_low_r(reference_msg, &reference_sk)); let reference_pk = EcdsaPublicKey::from(PublicKey::from_secret_key(&reference_sk)); // The JIT signer over the same seed, held open through the @@ -352,6 +336,56 @@ mod tests { } } + /// FUND-SAFETY: every HD signature the signer hands to a Core transaction + /// verifies against the key it reports, and is in the standard form Dash + /// Core relays: low S (a high S is non-standard) and a low R ground the + /// way Dash Core grinds it, so the DER encoding never exceeds 70 bytes. + /// R lands low half the time on its own, so a single digest proves + /// nothing; this signs many. + #[tokio::test] + async fn hd_signer_signatures_are_valid_and_standard() { + let path: DerivationPath = "m/44'/1'/0'/0/0".parse().unwrap(); + let held = Zeroizing::new(PARITY_SEED); + let signer = DetSigner::from_held(SecretPlaintext::HdSeed(&held), Network::Testnet); + + for i in 0..32u8 { + let sighash = [i; 32]; + let (signature, public_key) = signer.sign_ecdsa(&path, sighash).await.expect("sign"); + assert_signature_is_valid_and_standard(&sighash, signature, public_key, i); + } + } + + /// Check `signature` over `digest` against `public_key`, and that it has the + /// low-S, low-R form Dash Core relays. + fn assert_signature_is_valid_and_standard( + digest: &[u8; 32], + signature: EcdsaSignature, + public_key: EcdsaPublicKey, + case: u8, + ) { + let raw = ecdsa::Signature::from(signature); + let mut normalized = raw; + normalized.normalize_s(); + assert_eq!(normalized, raw, "high S for case {case}"); + assert!( + ecdsa::verify( + &raw, + Message::from_digest(*digest), + &PublicKey::from(public_key) + ) + .is_ok(), + "signature does not verify for case {case}" + ); + assert!( + signature.to_bytes()[0] < 0x80, + "R is not low for case {case}" + ); + assert!( + raw.serialize_der().len() <= 70, + "DER signature is longer than 70 bytes for case {case}" + ); + } + /// FUND-SAFETY PARITY: `DetSigner::extended_public_key` (the BIP-32 xpub /// export callers use to non-hardened-derive descendant payment addresses /// without a re-prompt) must reproduce, byte-for-byte, an independent @@ -464,8 +498,14 @@ mod tests { sa.with_secret_session(&scope, async |session| { let signer = DetSigner::from_held(session.plaintext(), Network::Testnet); - // Raw single-key sign succeeds. - signer.sign_single_key_ecdsa(&msg).expect("raw sign"); + // Raw single-key sign succeeds, with a valid, standard signature. + let signature = signer.sign_single_key_ecdsa(&msg).expect("raw sign"); + assert_signature_is_valid_and_standard( + &msg, + signature.into(), + PublicKey::from_secret_key(&sk).into(), + 0, + ); // Path-based HD surface rejects a single-key secret. let err = signer.sign_ecdsa(&path, msg).await.expect_err("wrong kind"); assert!(matches!(err, DetSignerError::WrongSecretKind)); From 100ce2929f097fa277f4ed19f924111cf89a3767 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Mon, 26 Oct 2026 13:00:47 +0000 Subject: [PATCH 26/27] refactor(security): implement Zeroize for the ECDH key and keep DIP-14's I_L in Zeroizing Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- src/backend_task/dashpay/encryption.rs | 20 ++++++++++++-------- src/model/dashpay_derivation/dip14.rs | 3 ++- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/backend_task/dashpay/encryption.rs b/src/backend_task/dashpay/encryption.rs index b03cf7a61..ec226f44c 100644 --- a/src/backend_task/dashpay/encryption.rs +++ b/src/backend_task/dashpay/encryption.rs @@ -6,7 +6,7 @@ use dash_sdk::dpp::identity::KeyType; use dash_sdk::dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0; use dash_sdk::dpp::secp256k1::{self, PublicKey}; use sha2::{Digest, Sha256}; -use zeroize::Zeroizing; +use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing}; /// A secp256k1 secret key erased when dropped, for the ECDH below. /// @@ -26,16 +26,20 @@ impl EcdhSecretKey { fn from_secret_bytes(scalar: &[u8; 32]) -> Result { secp256k1::SecretKey::from_secret_bytes(*scalar).map(Self) } +} - /// Overwrite the held key. Called on drop. - fn erase(&mut self) { +impl Zeroize for EcdhSecretKey { + /// Overwrite the held key through secp256k1's volatile erase. + fn zeroize(&mut self) { self.0.non_secure_erase(); } } +impl ZeroizeOnDrop for EcdhSecretKey {} + impl Drop for EcdhSecretKey { fn drop(&mut self) { - self.erase(); + self.zeroize(); } } @@ -325,16 +329,16 @@ mod tests { use bip39::rand::{self, RngCore}; use dash_sdk::dpp::secp256k1::{PublicKey, SecretKey}; - /// The erase that `Drop` runs leaves none of the original key bytes - /// behind. Checked through `erase` itself, because reading a value after + /// The zeroize that `Drop` runs leaves none of the original key bytes + /// behind. Checked through `zeroize` itself, because reading a value after /// its drop has run is not allowed. #[test] - fn ecdh_secret_key_erase_overwrites_the_key() { + fn ecdh_secret_key_zeroize_overwrites_the_key() { let scalar = [0x5a; 32]; let mut key = EcdhSecretKey::from_secret_bytes(&scalar).expect("valid scalar"); assert_eq!(key.0.to_secret_bytes(), scalar); - key.erase(); + key.zeroize(); assert_ne!(key.0.to_secret_bytes(), scalar); } diff --git a/src/model/dashpay_derivation/dip14.rs b/src/model/dashpay_derivation/dip14.rs index 69dbb9359..7c7c3a755 100644 --- a/src/model/dashpay_derivation/dip14.rs +++ b/src/model/dashpay_derivation/dip14.rs @@ -9,6 +9,7 @@ use dash_sdk::dpp::dashcore::hashes::sha512; use dash_sdk::dpp::dashcore::hashes::{Hash, HashEngine}; use dash_sdk::dpp::key_wallet::bip32::{ChainCode, ExtendedPrivKey, ExtendedPubKey, Fingerprint}; use dash_sdk::platform::Identifier; +use zeroize::Zeroizing; use super::DerivationError; @@ -55,7 +56,7 @@ pub fn ckd_priv_256( } let hmac_result = Hmac::::from_engine(hmac_engine); - let hmac_bytes = hmac_result.to_byte_array(); + let hmac_bytes = Zeroizing::new(hmac_result.to_byte_array()); // Split into I_L (first 32 bytes) and I_R (last 32 bytes) let (i_l, i_r) = hmac_bytes.split_at(32); From 72cae6904d7260066678beefaa2372dd87551ddc Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Mon, 26 Oct 2026 13:47:49 +0000 Subject: [PATCH 27/27] fix(security): erase the local SecretKey copy when building the ECDH key secp256k1::SecretKey is Copy, so wrapping it left the source copy unerased. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q --- src/backend_task/dashpay/encryption.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/backend_task/dashpay/encryption.rs b/src/backend_task/dashpay/encryption.rs index ec226f44c..56ec3e64d 100644 --- a/src/backend_task/dashpay/encryption.rs +++ b/src/backend_task/dashpay/encryption.rs @@ -24,7 +24,12 @@ impl EcdhSecretKey { /// Build a secret key from a 32-byte scalar, refusing zero and values not /// below the curve order. fn from_secret_bytes(scalar: &[u8; 32]) -> Result { - secp256k1::SecretKey::from_secret_bytes(*scalar).map(Self) + let mut source = secp256k1::SecretKey::from_secret_bytes(*scalar)?; + let key = Self(source); + // `SecretKey` is `Copy`, so wrapping it copies the scalar; erase the + // local copy as well so only the wrapped one remains. + source.non_secure_erase(); + Ok(key) } }