This repository has been archived by the owner on Mar 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Ed25519Addresses.hs
197 lines (181 loc) · 7.3 KB
/
Ed25519Addresses.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
module Golden.Ed25519Addresses
( spec
) where
import Universum hiding (pretty)
import Crypto.Encoding.BIP39 (ConsistentEntropy, EntropySize)
import qualified Data.Aeson as Aeson
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import Test.Hspec (Spec, describe, it, shouldBe)
import Cardano.Mnemonic (Mnemonic, mkMnemonic)
import Cardano.Wallet.Kernel.Ed25519Bip44 (ChangeChain (..),
deriveAddressKeyPair, genEncryptedSecretKey,
isInternalChange)
import Pos.Core (Address)
import Pos.Core.Common (IsBootstrapEraAddr (..), decodeTextAddress,
makePubKeyAddress)
import Pos.Core.NetworkMagic (NetworkMagic (..))
data TestVector = forall n. KnownNat n => TestVector
{ _address
:: Address -- | Corresponding Cardano Address
, _accountIx
:: Word32 -- | Account index, hardened
, _changeChain
:: ChangeChain -- | Whether this is a change address or not
, _addressIx
:: Word32 -- | Address index, non-hardened
, _mnemonic
:: Mnemonic n -- | @n@ mnemonic words
, _mnemonicPassphrase
:: ByteString -- | A optional passphrase for mnemonic protection.
}
spec :: Spec
spec = do
describe "Ed25519 Addresses - Golden Tests" $ forM_ tests
$ \vec@(TestVector addr accIx change addrIx mw pw) -> it (titleize vec) $ do
let rootPassphrase = mempty
let esk = genEncryptedSecretKey (mw, pw) rootPassphrase
let pubKey = fst <$> deriveAddressKeyPair rootPassphrase esk accIx change addrIx
let addr' = makePubKeyAddress NetworkMainOrStage (IsBootstrapEraAddr True) <$> pubKey
addr' `shouldBe` (Just addr)
where
tests :: [TestVector]
tests =
[ TestVector
{ _address = unsafeMkAddress "Ae2tdPwUPEZ1DYmhvpJWtVkMUbypPVkCVjQLNJeKRRG4LJ64dqTSRpWqzLH"
, _accountIx = 0x80000000
, _changeChain = ExternalChain
, _addressIx = 0x00000000
, _mnemonic = unsafeMkMnemonic @24
[ "process", "sauce", "ahead"
, "chase", "away", "ability"
, "odor", "excuse", "immune"
, "local", "climb", "wrestle"
, "vanish", "annual", "mimic"
, "square", "light", "corn"
, "dance", "fun", "april"
, "rail", "view", "certain"
]
, _mnemonicPassphrase = "Cardano the cardano that cardano!"
}
, TestVector
{ _address = unsafeMkAddress "Ae2tdPwUPEZ7ZyqyuDKkCnjrRjTY1vMJ8353gD7XWrUYufpVwEPhKwseVvf"
, _accountIx = 0x80000000
, _changeChain = ExternalChain
, _addressIx = 0x0000000E
, _mnemonic = unsafeMkMnemonic @24
[ "process", "sauce", "ahead"
, "chase", "away", "ability"
, "odor", "excuse", "immune"
, "local", "climb", "wrestle"
, "vanish", "annual", "mimic"
, "square", "light", "corn"
, "dance", "fun", "april"
, "rail", "view", "certain"
]
, _mnemonicPassphrase = "Cardano the cardano that cardano!"
}
, TestVector
{ _address = unsafeMkAddress "Ae2tdPwUPEZLSqQN7XNJRMJ6yHWdfFLaQgPPYgyJKrJnCVnRtbfw6EHRv1D"
, _accountIx = 0x8000000E
, _changeChain = InternalChain
, _addressIx = 0x0000002A
, _mnemonic = unsafeMkMnemonic @24
[ "process", "sauce", "ahead"
, "chase", "away", "ability"
, "odor", "excuse", "immune"
, "local", "climb", "wrestle"
, "vanish", "annual", "mimic"
, "square", "light", "corn"
, "dance", "fun", "april"
, "rail", "view", "certain"
]
, _mnemonicPassphrase = "Cardano the cardano that cardano!"
}
, TestVector
{ _address = unsafeMkAddress "Ae2tdPwUPEZGQVrA6qKreDzdtYxcWMMrpTFYCpFcuJfhJBEfoeiuW4MtaXZ"
, _accountIx = 0x80000000
, _changeChain = ExternalChain
, _addressIx = 0x00000000
, _mnemonic = unsafeMkMnemonic @15
[ "cruise", "legend", "nasty"
, "impose", "desk", "motion"
, "pistol", "rely", "camp"
, "journey", "drill", "spirit"
, "among", "basic", "frequent"
]
, _mnemonicPassphrase = mempty
}
, TestVector
{ _address = unsafeMkAddress "Ae2tdPwUPEZDLWQQEBR1UW7HeXJVaqUnuw8DUFu52TDWCJbxbkCyQYyxckP"
, _accountIx = 0x80000000
, _changeChain = ExternalChain
, _addressIx = 0x0000000E
, _mnemonic = unsafeMkMnemonic @15
[ "cruise", "legend", "nasty"
, "impose", "desk", "motion"
, "pistol", "rely", "camp"
, "journey", "drill", "spirit"
, "among", "basic", "frequent"
]
, _mnemonicPassphrase = mempty
}
, TestVector
{ _address = unsafeMkAddress "Ae2tdPwUPEZFRbyhz3cpfC2CumGzNkFBN2L42rcUc2yjQpEkxDbkPodpMAi"
, _accountIx = 0x8000000E
, _changeChain = InternalChain
, _addressIx = 0x0000002A
, _mnemonic = unsafeMkMnemonic @15
[ "cruise", "legend", "nasty"
, "impose", "desk", "motion"
, "pistol", "rely", "camp"
, "journey", "drill", "spirit"
, "among", "basic", "frequent"
]
, _mnemonicPassphrase = mempty
}
]
--
-- Internals
--
class UnsafeMkMnemonic mw where
unsafeMkMnemonic
:: (ConsistentEntropy n mw csz, EntropySize mw ~ n)
=> [Text]
-> Mnemonic mw
unsafeMkMnemonic ws =
case mkMnemonic ws of
Left e ->
error $
"Golden.Ed25519Addresses: failed to create mnemonic for the \
\test vector using the given words: " <> show ws
<> "\n" <> show e
Right m -> m
instance UnsafeMkMnemonic 24
instance UnsafeMkMnemonic 15
unsafeMkAddress
:: Text
-> Address
unsafeMkAddress txt =
case decodeTextAddress txt of
Left e ->
error $
"Golden.Ed25519Addresses: failed to create address for the \
\test vector using the given base58 text: " <> show txt
<> "\n" <> show e
Right a -> a
titleize
:: TestVector
-> String
titleize (TestVector _ accIx change addrIx (mw :: Mnemonic n) pw) =
mwS <> " (n = " <> nS <> ") " <> pwS <> " " <> accIxS <> "/" <> changeS <> "/" <> addrIxS
where
ellipse n bs = BS.take n bs <> "..." <> BS.drop (BS.length bs - n) bs
nS = show $ natVal $ Proxy @n
mwS = T.unpack $ T.decodeUtf8 $ ellipse 26 (BL.toStrict $ Aeson.encode mw)
pwS = if null pw then "no passphrase " else "with passphrase"
accIxS = show (accIx - 0x80000000) <> "'"
changeS = if isInternalChange change then "1" else "0"
addrIxS = show addrIx