Skip to content

Commit 7b5cd90

Browse files
authored
Merge pull request #777 from IntersectMBO/jordan/add-Key-instance-for-extended-stake-pool-key
Add Key instance for StakePoolExtendedKey
2 parents 75633d3 + d4e75ea commit 7b5cd90

File tree

3 files changed

+147
-7
lines changed

3 files changed

+147
-7
lines changed

cardano-api/src/Cardano/Api/Internal/DeserialiseAnyOf.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ data SomeAddressVerificationKey
236236
| AVrfVerificationKey (VerificationKey VrfKey)
237237
| AStakeVerificationKey (VerificationKey StakeKey)
238238
| AStakeExtendedVerificationKey (VerificationKey StakeExtendedKey)
239+
| AStakePoolVerificationKey (VerificationKey StakePoolKey)
240+
| AStakePoolExtendedVerificationKey (VerificationKey StakePoolExtendedKey)
239241
| ADRepVerificationKey (VerificationKey DRepKey)
240242
| ADRepExtendedVerificationKey (VerificationKey DRepExtendedKey)
241243
| ACommitteeColdVerificationKey (VerificationKey CommitteeColdKey)
@@ -264,6 +266,8 @@ renderSomeAddressVerificationKey =
264266
AKesVerificationKey vk -> serialiseToBech32 vk
265267
AVrfVerificationKey vk -> serialiseToBech32 vk
266268
AStakeVerificationKey vk -> serialiseToBech32 vk
269+
AStakePoolVerificationKey vk -> serialiseToBech32 vk
270+
AStakePoolExtendedVerificationKey vk -> serialiseToBech32 vk
267271
AStakeExtendedVerificationKey vk -> serialiseToBech32 vk
268272
ADRepVerificationKey vk -> serialiseToBech32 vk
269273
ADRepExtendedVerificationKey vk -> serialiseToBech32 vk
@@ -287,6 +291,8 @@ mapSomeAddressVerificationKey f = \case
287291
AGenesisExtendedVerificationKey vk -> f vk
288292
AVrfVerificationKey vk -> f vk
289293
AStakeVerificationKey vk -> f vk
294+
AStakePoolVerificationKey vk -> f vk
295+
AStakePoolExtendedVerificationKey vk -> f vk
290296
AStakeExtendedVerificationKey vk -> f vk
291297
ADRepVerificationKey vk -> f vk
292298
ADRepExtendedVerificationKey vk -> f vk
@@ -340,6 +346,8 @@ deserialiseAnyVerificationKeyBech32 =
340346
, FromSomeType (AsVerificationKey AsVrfKey) AVrfVerificationKey
341347
, FromSomeType (AsVerificationKey AsStakeKey) AStakeVerificationKey
342348
, FromSomeType (AsVerificationKey AsStakeExtendedKey) AStakeExtendedVerificationKey
349+
, FromSomeType (AsVerificationKey AsStakePoolKey) AStakePoolVerificationKey
350+
, FromSomeType (AsVerificationKey AsStakePoolExtendedKey) AStakePoolExtendedVerificationKey
343351
]
344352

345353
deserialiseAnyVerificationKeyTextEnvelope
@@ -362,6 +370,8 @@ deserialiseAnyVerificationKeyTextEnvelope bs =
362370
, FromSomeType (AsVerificationKey AsPaymentExtendedKey) APaymentExtendedVerificationKey
363371
, FromSomeType (AsVerificationKey AsStakeKey) AStakeVerificationKey
364372
, FromSomeType (AsVerificationKey AsStakeExtendedKey) AStakeExtendedVerificationKey
373+
, FromSomeType (AsVerificationKey AsStakePoolKey) AStakePoolVerificationKey
374+
, FromSomeType (AsVerificationKey AsStakePoolExtendedKey) AStakePoolExtendedVerificationKey
365375
, FromSomeType (AsVerificationKey AsGenesisUTxOKey) AGenesisUTxOVerificationKey
366376
, FromSomeType (AsVerificationKey AsGenesisExtendedKey) AGenesisExtendedVerificationKey
367377
]

cardano-api/src/Cardano/Api/Internal/Keys/Shelley.hs

Lines changed: 136 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module Cardano.Api.Internal.Keys.Shelley
2424
, PaymentExtendedKey
2525
, StakeKey
2626
, StakeExtendedKey
27+
, StakePoolExtendedKey
2728
, StakePoolKey
2829
, GenesisKey
2930
, GenesisExtendedKey
@@ -231,7 +232,7 @@ instance Key PaymentExtendedKey where
231232
getVerificationKey (PaymentExtendedSigningKey sk) =
232233
PaymentExtendedVerificationKey (Crypto.HD.toXPub sk)
233234

234-
-- \| We use the hash of the normal non-extended pub key so that it is
235+
-- We use the hash of the normal non-extended pub key so that it is
235236
-- consistent with the one used in addresses and signatures.
236237
verificationKeyHash
237238
:: VerificationKey PaymentExtendedKey
@@ -482,7 +483,7 @@ instance Key StakeExtendedKey where
482483
getVerificationKey (StakeExtendedSigningKey sk) =
483484
StakeExtendedVerificationKey (Crypto.HD.toXPub sk)
484485

485-
-- \| We use the hash of the normal non-extended pub key so that it is
486+
-- We use the hash of the normal non-extended pub key so that it is
486487
-- consistent with the one used in addresses and signatures.
487488
verificationKeyHash
488489
:: VerificationKey StakeExtendedKey
@@ -934,7 +935,7 @@ instance Key CommitteeColdExtendedKey where
934935
getVerificationKey (CommitteeColdExtendedSigningKey sk) =
935936
CommitteeColdExtendedVerificationKey (Crypto.HD.toXPub sk)
936937

937-
-- \| We use the hash of the normal non-extended pub key so that it is
938+
-- We use the hash of the normal non-extended pub key so that it is
938939
-- consistent with the one used in addresses and signatures.
939940
verificationKeyHash
940941
:: VerificationKey CommitteeColdExtendedKey
@@ -1069,7 +1070,7 @@ instance Key CommitteeHotExtendedKey where
10691070
getVerificationKey (CommitteeHotExtendedSigningKey sk) =
10701071
CommitteeHotExtendedVerificationKey (Crypto.HD.toXPub sk)
10711072

1072-
-- \| We use the hash of the normal non-extended pub key so that it is
1073+
-- We use the hash of the normal non-extended pub key so that it is
10731074
-- consistent with the one used in addresses and signatures.
10741075
verificationKeyHash
10751076
:: VerificationKey CommitteeHotExtendedKey
@@ -1217,7 +1218,7 @@ instance Key GenesisExtendedKey where
12171218
getVerificationKey (GenesisExtendedSigningKey sk) =
12181219
GenesisExtendedVerificationKey (Crypto.HD.toXPub sk)
12191220

1220-
-- \| We use the hash of the normal non-extended pub key so that it is
1221+
-- We use the hash of the normal non-extended pub key so that it is
12211222
-- consistent with the one used in addresses and signatures.
12221223
verificationKeyHash
12231224
:: VerificationKey GenesisExtendedKey
@@ -1460,7 +1461,7 @@ instance Key GenesisDelegateExtendedKey where
14601461
getVerificationKey (GenesisDelegateExtendedSigningKey sk) =
14611462
GenesisDelegateExtendedVerificationKey (Crypto.HD.toXPub sk)
14621463

1463-
-- \| We use the hash of the normal non-extended pub key so that it is
1464+
-- We use the hash of the normal non-extended pub key so that it is
14641465
-- consistent with the one used in addresses and signatures.
14651466
verificationKeyHash
14661467
:: VerificationKey GenesisDelegateExtendedKey
@@ -1777,6 +1778,134 @@ instance HasTextEnvelope (SigningKey StakePoolKey) where
17771778
proxy :: Proxy (Shelley.DSIGN StandardCrypto)
17781779
proxy = Proxy
17791780

1781+
---
1782+
--- Stake pool extended keys
1783+
---
1784+
1785+
data StakePoolExtendedKey
1786+
1787+
instance HasTypeProxy StakePoolExtendedKey where
1788+
data AsType StakePoolExtendedKey = AsStakePoolExtendedKey
1789+
proxyToAsType _ = AsStakePoolExtendedKey
1790+
1791+
instance Key StakePoolExtendedKey where
1792+
newtype VerificationKey StakePoolExtendedKey
1793+
= StakePoolExtendedVerificationKey Crypto.HD.XPub
1794+
deriving stock Eq
1795+
deriving (Show, IsString) via UsingRawBytesHex (VerificationKey StakePoolExtendedKey)
1796+
deriving anyclass SerialiseAsCBOR
1797+
1798+
newtype SigningKey StakePoolExtendedKey
1799+
= StakePoolExtendedSigningKey Crypto.HD.XPrv
1800+
deriving (Show, IsString) via UsingRawBytesHex (SigningKey StakePoolExtendedKey)
1801+
deriving anyclass SerialiseAsCBOR
1802+
1803+
deterministicSigningKey
1804+
:: AsType StakePoolExtendedKey
1805+
-> Crypto.Seed
1806+
-> SigningKey StakePoolExtendedKey
1807+
deterministicSigningKey AsStakePoolExtendedKey seed =
1808+
StakePoolExtendedSigningKey
1809+
(Crypto.HD.generate seedbs BS.empty)
1810+
where
1811+
(seedbs, _) = Crypto.getBytesFromSeedT 32 seed
1812+
1813+
deterministicSigningKeySeedSize :: AsType StakePoolExtendedKey -> Word
1814+
deterministicSigningKeySeedSize AsStakePoolExtendedKey = 32
1815+
1816+
getVerificationKey
1817+
:: SigningKey StakePoolExtendedKey
1818+
-> VerificationKey StakePoolExtendedKey
1819+
getVerificationKey (StakePoolExtendedSigningKey sk) =
1820+
StakePoolExtendedVerificationKey (Crypto.HD.toXPub sk)
1821+
1822+
-- We use the hash of the normal non-extended pub key so that it is
1823+
-- consistent with the one used in addresses and signatures.
1824+
verificationKeyHash
1825+
:: VerificationKey StakePoolExtendedKey
1826+
-> Hash StakePoolExtendedKey
1827+
verificationKeyHash (StakePoolExtendedVerificationKey vk) =
1828+
StakePoolExtendedKeyHash
1829+
. Shelley.KeyHash
1830+
. Crypto.castHash
1831+
$ Crypto.hashWith Crypto.HD.xpubPublicKey vk
1832+
1833+
instance ToCBOR (VerificationKey StakePoolExtendedKey) where
1834+
toCBOR (StakePoolExtendedVerificationKey xpub) =
1835+
toCBOR (Crypto.HD.unXPub xpub)
1836+
1837+
instance FromCBOR (VerificationKey StakePoolExtendedKey) where
1838+
fromCBOR = do
1839+
bs <- fromCBOR
1840+
either
1841+
fail
1842+
(return . StakePoolExtendedVerificationKey)
1843+
(Crypto.HD.xpub (bs :: ByteString))
1844+
1845+
instance ToCBOR (SigningKey StakePoolExtendedKey) where
1846+
toCBOR (StakePoolExtendedSigningKey xprv) =
1847+
toCBOR (Crypto.HD.unXPrv xprv)
1848+
1849+
instance FromCBOR (SigningKey StakePoolExtendedKey) where
1850+
fromCBOR = do
1851+
bs <- fromCBOR
1852+
either
1853+
fail
1854+
(return . StakePoolExtendedSigningKey)
1855+
(Crypto.HD.xprv (bs :: ByteString))
1856+
1857+
instance SerialiseAsRawBytes (VerificationKey StakePoolExtendedKey) where
1858+
serialiseToRawBytes (StakePoolExtendedVerificationKey xpub) =
1859+
Crypto.HD.unXPub xpub
1860+
1861+
deserialiseFromRawBytes (AsVerificationKey AsStakePoolExtendedKey) bs =
1862+
first
1863+
( \msg ->
1864+
SerialiseAsRawBytesError
1865+
("Unable to deserialise VerificationKey StakePoolExtendedKey: " ++ msg)
1866+
)
1867+
$ StakePoolExtendedVerificationKey <$> Crypto.HD.xpub bs
1868+
1869+
instance SerialiseAsRawBytes (SigningKey StakePoolExtendedKey) where
1870+
serialiseToRawBytes (StakePoolExtendedSigningKey xprv) =
1871+
Crypto.HD.unXPrv xprv
1872+
1873+
deserialiseFromRawBytes (AsSigningKey AsStakePoolExtendedKey) bs =
1874+
first
1875+
( \msg ->
1876+
SerialiseAsRawBytesError
1877+
("Unable to deserialise SigningKey StakePoolExtendedKey: " ++ msg)
1878+
)
1879+
$ StakePoolExtendedSigningKey <$> Crypto.HD.xprv bs
1880+
1881+
newtype instance Hash StakePoolExtendedKey
1882+
= StakePoolExtendedKeyHash
1883+
{unStakePoolExtendedKeyHash :: Shelley.KeyHash Shelley.StakePool StandardCrypto}
1884+
deriving stock (Eq, Ord, Show)
1885+
1886+
instance SerialiseAsRawBytes (Hash StakePoolExtendedKey) where
1887+
serialiseToRawBytes (StakePoolExtendedKeyHash (Shelley.KeyHash vkh)) =
1888+
Crypto.hashToBytes vkh
1889+
1890+
deserialiseFromRawBytes (AsHash AsStakePoolExtendedKey) bs =
1891+
maybeToRight
1892+
(SerialiseAsRawBytesError "Unable to deserialise Hash StakePoolExtendedKey")
1893+
(StakePoolExtendedKeyHash . Shelley.KeyHash <$> Crypto.hashFromBytes bs)
1894+
1895+
instance HasTextEnvelope (VerificationKey StakePoolExtendedKey) where
1896+
textEnvelopeType _ = "StakePoolExtendedVerificationKey_ed25519_bip32"
1897+
1898+
instance HasTextEnvelope (SigningKey StakePoolExtendedKey) where
1899+
textEnvelopeType _ = "StakePoolExtendedSigningKey_ed25519_bip32"
1900+
1901+
instance SerialiseAsBech32 (VerificationKey StakePoolExtendedKey) where
1902+
bech32PrefixFor _ = "pool_xvk"
1903+
bech32PrefixesPermitted _ = ["pool_xvk"]
1904+
1905+
instance SerialiseAsBech32 (SigningKey StakePoolExtendedKey) where
1906+
bech32PrefixFor _ = "pool_xsk"
1907+
bech32PrefixesPermitted _ = ["pool_xsk"]
1908+
17801909
--
17811910
-- DRep keys
17821911
--
@@ -1941,7 +2070,7 @@ instance Key DRepExtendedKey where
19412070
getVerificationKey (DRepExtendedSigningKey sk) =
19422071
DRepExtendedVerificationKey (Crypto.HD.toXPub sk)
19432072

1944-
-- \| We use the hash of the normal non-extended pub key so that it is
2073+
-- We use the hash of the normal non-extended pub key so that it is
19452074
-- consistent with the one used in addresses and signatures.
19462075
verificationKeyHash
19472076
:: VerificationKey DRepExtendedKey

cardano-api/src/Cardano/Api/Shelley.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ module Cardano.Api.Shelley
203203
, DRepMetadataReference (DRepMetadataReference)
204204

205205
-- ** Stake pool operator's keys
206+
, StakePoolExtendedKey
206207
, StakePoolKey
207208
, PoolId
208209

0 commit comments

Comments
 (0)