Skip to content

Commit

Permalink
make byron witnesses test work
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Sep 15, 2020
1 parent d109410 commit 1e42340
Showing 1 changed file with 95 additions and 26 deletions.
Expand Up @@ -54,6 +54,8 @@ import Data.Aeson
( (.=) )
import Data.ByteArray.Encoding
( Base (Base16, Base64), convertFromBase, convertToBase )
import Data.ByteString.Base58
( bitcoinAlphabet, decodeBase58 )
import Data.Either.Extra
( eitherToMaybe )
import Data.Function
Expand Down Expand Up @@ -990,7 +992,6 @@ spec = do
(Link.getTransactionFee @'Shelley wSrc) Default payload2
let (Quantity feeMin1) = getFromResponse #estimatedMin r6

--let feeMin1 = 142400
-- (b) the change adddress of mnemonic15 wallet
--- $ cat root-src.prv \
--- > | cardano-address key child 1852H/1815H/0H/1/0 \
Expand Down Expand Up @@ -1092,14 +1093,18 @@ spec = do
--- | xpub1alff7qkyshud2kstclddrnf582zags6hfc4wl5qpxn7gx6r65kuk8sf74u9645am65csyhkelztnpe4e3p6tkw8a24s99xpwsg9s60s36nlzc \
--- --path 0H/14H --network-tag 764824073
--- --> DdzFFzCqrhsfRTAFKYtEMjB1vy5fTth3QEitVbMBuk5r9Um6Uf2bWYj8cSYfbad9MLKokM2Y5FhybrJqCgUDVPNPkgG6oa33VLQ6jugc
-- 6. change address
--- $ cat root-src.prv | cardano-address key child --legacy 0H/15H \
--- | cardano-address key public | cardano-address address bootstrap \
--- | xpub1alff7qkyshud2kstclddrnf582zags6hfc4wl5qpxn7gx6r65kuk8sf74u9645am65csyhkelztnpe4e3p6tkw8a24s99xpwsg9s60s36nlzc \
--- --path 0H/15H --network-tag 764824073
--- --> DdzFFzCqrht3xS8ySv9t4MVM1euWvcC6WbzPZKkr6WqE7zcW8nSS9A3eGvcrMKvANGF6auayauFQjazrQtC7T8hx9CvXKq4U3qS2ApAC
let payload1 = Json [json|
{ "passphrase": #{fixturePassphrase}
, "address_index": 2147483662
}|]
r1 <- request @(ApiAddress n) ctx (Link.postRandomAddress wByron) Default payload1
verify r1
[ expectResponseCode @IO HTTP.status201
]
expectResponseCode @IO HTTP.status201 r1
let destination = getFromResponse #id r1
let amt = (10_000_000 :: Natural)
let payload2 = Json [json|{
Expand All @@ -1112,9 +1117,18 @@ spec = do
}],
"passphrase": #{fixturePassphrase}
}|]
tx <- request @(ApiTransaction n) ctx
r2 <- request @(ApiTransaction n) ctx
(Link.createTransaction @'Byron wFaucet) Default payload2
expectResponseCode HTTP.status202 tx
expectResponseCode HTTP.status202 r2

let (Hash txid) = getApiT $ getFromResponse #id r2
let txix = case getFromResponse #outputs r2 of
[(AddressAmount _ (Quantity out1)), (AddressAmount _ (Quantity out2))]
| out1 == amt -> 0
| out2 == amt -> 1
| otherwise -> error "this should not happen"
_ -> error "this should not happen"

eventually "wByron received money" $ do
r' <- request @ApiByronWallet ctx
(Link.getWallet @'Byron wByron) Default Empty
Expand All @@ -1126,18 +1140,31 @@ spec = do
[ "broken", "pass", "shrug", "pause", "crush"
, "caught", "honey", "lonely", "dose", "rabbit"
, "olympic", "honey", "hair", "panther", "stage"] :: [Text]

-- 1. recovery-phrase-dest.prv
-- --> broken pass shrug pause crush caught honey lonely dose rabbit olympic honey hair panther stage
-- 2. corresponding root key:
--- $ cat recovery-phrase-dest.prv | cardano-address key from-recovery-phrase Shelley > root-dest.prv
-- --> xprv1cqnvuzmz796zgfmuuu8fq9rdufqsv0ar0sr9x0nee932snhw7f9w7qfyxz525c2f8d5uzyv38hru6c5ad8hz38eltrseu8s3ela226745sge3xtgsfjkhyzrz0dkjelz8hksgjtk73fzkrp4jgjns0569q9pz6js
-- 3. staking private key:
--- $ cat root-dest.prv | cardano-address key child 1852H/1815H/0H/2/0 > stake-dest.prv
-- --> xprv1pzgx4cds2qu6qcstnqx8srnfn99xu6356nktr4npdpjwse0w7f9y77kec8vc7sl8zvqh43lzq6e9rgudgh2rz87aupyx0ncrwfsnzredmntfg0u2v9v3dar75vsw0eyxphmz70fsx99l8r4586weg77xyuu8t0sg
-- 4. delegation address (index 1 and for network tag 1):
--- $ cat root-dest.prv \
--- | cardano-address key child 1852H/1815H/0H/0/1 \
--- | cardano-address key public \
--- | cardano-address address payment --network-tag 1 \
--- | cardano-address address delegation $(cat stake-dest.prv | cardano-address key public)
--- --> addr1q8zrclx9djykejekxdr2yec20c0vn9gl2clykc9ktsegm9k89d93kzpx8gf9y35hfmgkx68avmaucem9lzg0lucpex8qxqqqy8
let walletPostData = Json [json| {
"name": "empty Shelley wallet",
"mnemonic_sentence": #{shelleyMnemonics},
"passphrase": #{fixturePassphrase}
} |]
r2 <- request @ApiWallet ctx (Link.postWallet @'Shelley) Default walletPostData
expectResponseCode @IO HTTP.status201 r2
r3 <- request @ApiWallet ctx (Link.postWallet @'Shelley) Default walletPostData
expectResponseCode @IO HTTP.status201 r3
let wShelley = getFromResponse Prelude.id r3

let wShelley = getFromResponse Prelude.id r2
addrs <- listAddresses @n ctx wShelley

let amt1 = (1_000_000 :: Natural)
let destination1 = (addrs !! 1) ^. #id
let payload3 = Json [json|{
Expand All @@ -1149,25 +1176,36 @@ spec = do
}
}]
}|]
let addrChange =
"DdzFFzCqrht3xS8ySv9t4MVM1euWvcC6WbzPZKkr6WqE7zcW8nSS9A3eGvcrMK\
\vANGF6auayauFQjazrQtC7T8hx9CvXKq4U3qS2ApAC"
let addrOut =
"addr1q8zrclx9djykejekxdr2yec20c0vn9gl2clykc9ktsegm9k89d93kzpx8\
\gf9y35hfmgkx68avmaucem9lzg0lucpex8qxqqqy8"
let addrInp =
"DdzFFzCqrhsfRTAFKYtEMjB1vy5fTth3QEitVbMBuk5r9Um6Uf2bWYj8cSYfba\
\d9MLKokM2Y5FhybrJqCgUDVPNPkgG6oa33VLQ6jugc"
let wit =
"c323bec83ccf2e39ee42d499acd5ee7ade10e822270b4479f8e98c8c8e8abf\
\0842dc7de2c0e69e52982743c73a75ea06ef87c80b94d380c83a9a99060488\
\438f06c552df3d16013201a44025f79b7eae1cd697b3fb173f31110b97935c\
\6ba600"

rFeeEst <- request @ApiFee ctx
(Link.getTransactionFee @'Byron wByron) Default payload3
verify rFeeEst
[ expectSuccess
, expectResponseCode HTTP.status202
]

expectResponseCode @IO HTTP.status202 rFeeEst
let (Quantity feeEstMin) = getFromResponse #estimatedMin rFeeEst
let (Quantity feeEstMax) = getFromResponse #estimatedMax rFeeEst
let outChange = amt - feeEstMin - amt1

r <- postTx ctx
(wByron, Link.createTransaction @'Byron, fixturePassphrase)
wShelley
amt1
verify r
[ expectSuccess
, expectResponseCode HTTP.status202
]
let (Right blob) = constructTxByronWitsFromCardanoTransactions
(fromIntegral feeEstMin) txid txix outChange addrChange amt1 addrOut addrInp wit
let baseOk = Base64
let encodedSignedTx = T.decodeUtf8 $ convertToBase baseOk blob
let payloadExt = NonJson . BL.fromStrict . toRawBytes baseOk
let headers = Headers [ ("Content-Type", "application/octet-stream") ]
r4 <- request
@ApiTxId ctx Link.postExternalTransaction headers (payloadExt encodedSignedTx)
expectResponseCode HTTP.status202 r4

eventually "wByron and wShelley balances are as expected" $ do
r' <- request @ApiWallet ctx
Expand All @@ -1180,7 +1218,7 @@ spec = do
(Link.getWallet @'Byron wByron) Default Empty
expectField
(#balance . #available)
(`shouldBe` Quantity (amt - feeEstMax - amt1)) r''
(`shouldBe` Quantity (amt - feeEstMin - amt1)) r''

describe "TRANS_ESTIMATE_08 - Bad payload" $ do
let matrix =
Expand Down Expand Up @@ -2385,6 +2423,13 @@ spec = do
unsafeMkOutput n str =
fromJust $ CardanoTransactions.mkOutput n (unsafeBech32 str)

unsafeMkOutput'
:: Natural
-> Text
-> CardanoTransactions.Output CardanoTransactions.Shelley
unsafeMkOutput' n str =
fromJust $ CardanoTransactions.mkOutput n (unsafeBase58 str)

fromBech32 :: Text -> Maybe BS.ByteString
fromBech32 txt = do
(_, dp) <- either (const Nothing) Just (Bech32.decodeLenient txt)
Expand All @@ -2407,6 +2452,21 @@ spec = do
unsafeMkShelleySignKey str =
fromJust $ CardanoTransactions.mkShelleySignKey (unsafeB16 str)

fromBase58 :: Text -> Maybe BS.ByteString
fromBase58 = decodeBase58 bitcoinAlphabet . T.encodeUtf8

unsafeBase58 :: Text -> BS.ByteString
unsafeBase58 = fromMaybe (error msg) . fromBase58
where msg = "unable to decode base58 string."

unsafeMkByronSignKey
:: Text
-> Text
-> CardanoTransactions.SignKey CardanoTransactions.Shelley
unsafeMkByronSignKey str1 str2 =
let (Just addr) = fromBase58 str1
in fromJust $ CardanoTransactions.mkByronSignKey addr (unsafeB16 str2)

constructTxFromCardanoTransactions fee txid txix out1 addr1 out2 addr2 wit =
CardanoTransactions.empty (initTx fee)
& CardanoTransactions.addInput (unsafeMkInput txix txid)
Expand All @@ -2426,3 +2486,12 @@ spec = do
& CardanoTransactions.signWith (unsafeMkShelleySignKey wit1)
& CardanoTransactions.signWith (unsafeMkShelleySignKey wit2)
& CardanoTransactions.serialize

constructTxByronWitsFromCardanoTransactions fee txid txix out1 addr1 out2 addr2 addr3 wit =
CardanoTransactions.empty (initTx fee)
& CardanoTransactions.addInput (unsafeMkInput txix txid)
& CardanoTransactions.addOutput (unsafeMkOutput' out1 addr1)
& CardanoTransactions.addOutput (unsafeMkOutput out2 addr2)
& CardanoTransactions.lock
& CardanoTransactions.signWith (unsafeMkByronSignKey addr3 wit)
& CardanoTransactions.serialize

0 comments on commit 1e42340

Please sign in to comment.