Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : Tests/QyvoraCoreTests/HDTests.swift8// Module : Tests9// Repository : https://git.spboucher.ai/qyvora10// Project Website : Not specified11// Created : 2026-12-0412// Last Modified : 2026-12-0413//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Testing42import QyvoraMath43import QyvoraSymmetric44import QyvoraSecp256k145@testable import QyvoraCore4647@Suite struct HDTests {48 let about12 = Array(repeating: "abandon", count: 11) + ["about"]49 let art24 = Array(repeating: "abandon", count: 23) + ["art"]5051 @Test func bip39Vectors() throws {52 #expect(try BIP39.mnemonic(entropy: Bytes(repeating: 0, count: 16)) == about12)53 #expect(try BIP39.mnemonic(entropy: Bytes(repeating: 0, count: 32)) == art24)54 #expect(try BIP39.entropy(mnemonic: art24) == Bytes(repeating: 0, count: 32))55 #expect(try BIP39.seed(mnemonic: about12, passphrase: "TREZOR").hex == "c55257c360c07c72029aebc1b53c05ed0362ada38ead3e3e9efa3708e53495531f09a6987599d18264c1e1c92f2cf141630c7a3c4ab7c81b2f001698e7463b04")56 #expect(try BIP39.seed(mnemonic: art24, passphrase: "TREZOR").hex == "bda85446c68413707090a52022edd26a1c9462295029f2e60cd7c4f2bbd3097170af7a4d73245cafa9c3cca8d561a7c3de6f5d4a10be8ed2a5e608d68f92fcc8")57 let ff = try BIP39.mnemonic(entropy: Bytes(repeating: 0xff, count: 32))58 #expect(ff.last == "vote" && ff.first == "zoo")59 }6061 @Test func bip39Rejections() throws {62 var bad = art2463 bad[23] = "about"64 #expect(throws: BIP39.Error.invalidChecksum) { try BIP39.entropy(mnemonic: bad) }65 bad[3] = "qyvora"66 #expect(throws: BIP39.Error.unknownWord(position: 3)) { try BIP39.entropy(mnemonic: bad) }67 #expect(throws: BIP39.Error.invalidWordCount) { try BIP39.entropy(mnemonic: Array(art24.prefix(23))) }68 #expect(BIP39.words(from: " Abandon\tABOUT \n art ") == ["abandon", "about", "art"])69 }7071 @Test func bip39GenerateRoundTrip() throws {72 for _ in 0..<20 {73 let w = try BIP39.generate()74 #expect(w.count == 24)75 #expect(BIP39.isValid(w))76 #expect(try BIP39.mnemonic(entropy: BIP39.entropy(mnemonic: w)) == w)77 }78 }7980 @Test func bip32Vector1() throws {81 let m = try ExtendedKey.master(seed: Bytes(hex: "000102030405060708090a0b0c0d0e0f")!)82 #expect(try m.serialized(.xprv) == "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi")83 #expect(try m.serialized(.xpub) == "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8")84 let c = try m.derive(DerivationPath("m/0'")!)85 #expect(try c.serialized(.xpub) == "xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw")86 let deep = try m.derive(DerivationPath("m/0'/1/2'/2/1000000000")!)87 #expect(try deep.serialized(.xpub) == "xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy")88 // public derivation of a normal child equals the neutered private derivation89 let parent = try m.derive(DerivationPath("m/0'/1/2'")!)90 #expect(try parent.neutered.derive(.normal(2)).publicKey == parent.derive(.normal(2)).publicKey)91 #expect(throws: ExtendedKey.Error.hardenedFromPublic) { try parent.neutered.derive(.hardened(0)) }92 let parsed = try ExtendedKey.parse(try deep.serialized(.xprv))93 #expect(parsed.version == .xprv && parsed.key.privateKey == deep.privateKey && parsed.key.chainCode == deep.chainCode)94 }9596 @Test func bip84Vector() throws {97 // BIP-84 test vector98 let m = try ExtendedKey.master(seed: try BIP39.seed(mnemonic: about12))99 let account = try m.derive(StandardPaths.bip84Account(.mainnet))100 #expect(try account.serialized(.zpub) == "zpub6rFR7y4Q2AijBEqTUquhVz398htDFrtymD9xYYfG1m4wAcvPhXNfE3EfH1r1ADqtfSdVCToUG868RvUUkgDKf31mGDtKsAYz2oz2AGutZYs")101 let first = try account.derive(.normal(0)).derive(.normal(0))102 #expect(first.publicKey.hex == "0330d54fd0dd420a6e5f8d3624f5f3482cae350f79d5f0753bf5beef9c2d91af3c")103 #expect(SegWitAddress.encode(hrp: "bc", version: 0, program: Hash160.hash(first.publicKey)) == "bc1qcr8te4kr609gcawutmrza0j4xv80jy8z306fyu")104 let change = try account.derive(.normal(1)).derive(.normal(0))105 #expect(SegWitAddress.encode(hrp: "bc", version: 0, program: Hash160.hash(change.publicKey)) == "bc1q8c6fshw2dlwun7ekn9qwf37cu2rn755upcp6el")106 }107108 @Test func pathsAndAmounts() {109 #expect(DerivationPath("m/84'/1h/0'/0/7")?.description == "m/84'/1'/0'/0/7")110 #expect(DerivationPath("m/84'/x") == nil)111 #expect(DerivationPath("m//1") == nil)112 #expect(DerivationPath("m/2147483648") == nil)113 #expect(BaseUnits.parse("1.5", decimals: 18)?.description == "1500000000000000000")114 #expect(BaseUnits.parse("0.000000000000000001", decimals: 18) == UInt256(1))115 #expect(BaseUnits.parse("1.0000000000000000001", decimals: 18) == nil)116 #expect(BaseUnits.parse("1,5", decimals: 18) == nil)117 #expect(BaseUnits.parse("-1", decimals: 8) == nil)118 #expect(BaseUnits.format(UInt256(decimal: "1500000000000000000")!, decimals: 18) == "1.5")119 #expect(BaseUnits.format(UInt256(123), decimals: 8) == "0.00000123")120 #expect(BaseUnits.format(UInt256(199_999_999), decimals: 8, maxFractionDigits: 2) == "1.99")121 #expect(UInt256(decimal: "115792089237316195423570985008687907853269984665640564039457584007913129639935") == UInt256.max)122 #expect(UInt256(decimal: "115792089237316195423570985008687907853269984665640564039457584007913129639936") == nil)123 #expect(UInt256(hex: "0xde0b6b3a7640000")?.description == "1000000000000000000")124 #expect(UInt256(1_000_000).hexQuantity == "0xf4240" && UInt256.zero.hexQuantity == "0x0")125 #expect(UInt256.max.addingReportingOverflow(UInt256(1)).1)126 let (p, o) = UInt256(decimal: "340282366920938463463374607431768211456")!.multipliedReportingOverflow(by: UInt256(decimal: "340282366920938463463374607431768211455")!)127 #expect(!o && p.description == "115792089237316195423570985008687907852929702298719625575994209400481361428480")128 }129}130131// The test of `MainnetPolicy.configure` (both conditions are required) lives in the wallet tests, in the suite that has132// the process-wide policy to itself: QyvoraWalletTests.MainnetPolicyTests.bothConditionsAreRequired. Here it switched133// mainnet on for an instant while wallet tests of the same process were running.134