2023-04-13 23:35:15 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2023-05-04 14:23:05 +00:00
|
|
|
import C.Zcash (RawData(..), UnifiedFullViewingKey(..), rustWrapperIsUA)
|
2023-04-13 23:35:15 +00:00
|
|
|
import qualified Data.ByteString as BS
|
2023-04-27 14:50:07 +00:00
|
|
|
import qualified Data.Text.Encoding as E
|
2023-04-13 23:35:15 +00:00
|
|
|
import Data.Word
|
|
|
|
import Test.Hspec
|
|
|
|
import Zcash
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
|
|
|
hspec $ do
|
|
|
|
describe "F4Jumble" $ do
|
|
|
|
it "jumble a string" $ do
|
|
|
|
let input =
|
|
|
|
[ 0x5d
|
|
|
|
, 0x7a
|
|
|
|
, 0x8f
|
|
|
|
, 0x73
|
|
|
|
, 0x9a
|
|
|
|
, 0x2d
|
|
|
|
, 0x9e
|
|
|
|
, 0x94
|
|
|
|
, 0x5b
|
|
|
|
, 0x0c
|
|
|
|
, 0xe1
|
|
|
|
, 0x52
|
|
|
|
, 0xa8
|
|
|
|
, 0x04
|
|
|
|
, 0x9e
|
|
|
|
, 0x29
|
|
|
|
, 0x4c
|
|
|
|
, 0x4d
|
|
|
|
, 0x6e
|
|
|
|
, 0x66
|
|
|
|
, 0xb1
|
|
|
|
, 0x64
|
|
|
|
, 0x93
|
|
|
|
, 0x9d
|
|
|
|
, 0xaf
|
|
|
|
, 0xfa
|
|
|
|
, 0x2e
|
|
|
|
, 0xf6
|
|
|
|
, 0xee
|
|
|
|
, 0x69
|
|
|
|
, 0x21
|
|
|
|
, 0x48
|
|
|
|
, 0x1c
|
|
|
|
, 0xdd
|
|
|
|
, 0x86
|
|
|
|
, 0xb3
|
|
|
|
, 0xcc
|
|
|
|
, 0x43
|
|
|
|
, 0x18
|
|
|
|
, 0xd9
|
|
|
|
, 0x61
|
|
|
|
, 0x4f
|
|
|
|
, 0xc8
|
|
|
|
, 0x20
|
|
|
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
|
, 0x9c
|
|
|
|
, 0xcd
|
|
|
|
, 0x88
|
|
|
|
, 0xee
|
|
|
|
, 0x27
|
|
|
|
, 0x36
|
|
|
|
, 0x8c
|
|
|
|
, 0xd0
|
|
|
|
, 0xc0
|
|
|
|
, 0x92
|
|
|
|
, 0x1a
|
|
|
|
, 0x04
|
|
|
|
, 0x44
|
|
|
|
, 0xc8
|
|
|
|
, 0xe5
|
|
|
|
, 0x85
|
|
|
|
, 0x8d
|
|
|
|
, 0x22
|
|
|
|
] :: [Word8]
|
|
|
|
f4UnJumble (BS.pack out) `shouldBe` BS.pack input
|
2023-04-13 23:35:15 +00:00
|
|
|
describe "Unified address" $ do
|
|
|
|
it "succeeds with correct UA" $ do
|
|
|
|
let ua =
|
|
|
|
"u1salpdyefywvsg2dlmxg9589yznh0h9v6qjr478k80amtkqkws5pr408lxt2953dpprvu06mahxt99cv65fgsm7sw8hlchplfg5pl89ur"
|
|
|
|
isValidUnifiedAddress ua `shouldBe` True
|
|
|
|
it "fails with incorrect UA" $ do
|
|
|
|
let ua =
|
|
|
|
"u1salpdyefbreakingtheaddressh0h9v6qjr478k80amtkqkws5pr408lxt2953dpprvu06mahxt99cv65fgsm7sw8hlchplfg5pl89ur"
|
|
|
|
isValidUnifiedAddress ua `shouldBe` False
|
2023-05-04 14:23:05 +00:00
|
|
|
describe "Decode UVK from YWallet" $ do
|
|
|
|
let uvk =
|
|
|
|
"uview1u833rp8yykd7h4druwht6xp6k8krle45fx8hqsw6vzw63n24atxpcatws82z092kryazuu6d7rayyut8m36wm4wpjy2z8r9hj48fx5pf49gw4sjrq8503qpz3vqj5hg0vg9vsqeasg5qjuyh94uyfm7v76udqcm2m0wfc25hcyqswcn56xxduq3xkgxkr0l73cjy88fdvf90eq5fda9g6x7yv7d0uckpevxg6540wc76xrc4axxvlt03ptaa2a0rektglmdy68656f3uzcdgqqyu0t7wk5cvwghyyvgqc0rp3vgu5ye4nd236ml57rjh083a2755qemf6dk6pw0qrnfm7246s8eg2hhzkzpf9h73chhng7xhmyem2sjh8rs2m9nhfcslsgenm"
|
|
|
|
let res = decodeUfvk uvk
|
|
|
|
it "is mainnet" $ do maybe 0 net res `shouldBe` 1
|
|
|
|
it "has Orchard key" $ do BS.length (maybe "" o_key res) `shouldBe` 96
|
|
|
|
it "has Sapling key" $ do BS.length (maybe "" s_key res) `shouldBe` 128
|
|
|
|
it "does not have Transparent key" $ do
|
|
|
|
BS.length (maybe "" t_key res) `shouldBe` 1
|
|
|
|
describe "Decode bad UVK" $ do
|
|
|
|
it "should fail" $ do
|
|
|
|
let fakeUvk =
|
|
|
|
"uview1u83changinga987bundchofch4ract3r5x8hqsw6vzw63n24atxpcatws82z092kryazuu6d7rayyut8m36wm4wpjy2z8r9hj48fx5pf49gw4sjrq8503qpz3vqj5hg0vg9vsqeasg5qjuyh94uyfm7v76udqcm2m0wfc25hcyqswcn56xxduq3xkgxkr0l73cjy88fdvf90eq5fda9g6x7yv7d0uckpevxg6540wc76xrc4axxvlt03ptaa2a0rektglmdy68656f3uzcdgqqyu0t7wk5cvwghyyvgqc0rp3vgu5ye4nd236ml57rjh083a2755qemf6dk6pw0qrnfm7246s8eg2hhzkzpf9h73chhng7xhmyem2sjh8rs2m9nhfcslsgenm"
|
|
|
|
decodeUfvk fakeUvk `shouldBe` Nothing
|