Skip to content

Commit

Permalink
WIP: getCollateral for NamiWallet
Browse files Browse the repository at this point in the history
  • Loading branch information
ngua committed Jan 17, 2022
1 parent dc50edf commit a0e959d
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 8 deletions.
26 changes: 24 additions & 2 deletions src/Lib/Serialization.js
Expand Up @@ -7,8 +7,30 @@ const serLib = require("@emurgo/cardano-serialization-lib-nodejs");
exports._hexToBytes = (hex) => Buffer.from(hex, "hex");

// _addrFromBytes :: Buffer -> Address
exports._addrFromBytes = (buf) => {
const addr = serLib.Address.from_bytes(buf);
exports._addrFromBytes = (buf) => mkAddress(serLib.Address.from_bytes(buf));

// _utxoFromBytes :: Buffer -> TransactionUnspentOutput
exports._utxoFromBytes = (buf) => {
const utxo = serLib.TransactionUnspentOutput.from_bytes(buf);
return {
input: {
transaction_id: mkTransactionId(utxo.input()),
index: utxo.input().index(),
},
output: {
address: mkAddress(utxo.address()),
// TODO
value: undefined,
// TODO
data_hash: undefined,
},
};
};

const mkTransactionId = (input) =>
Buffer.from(input.transaction_id().to_bytes()).toString("hex");

const mkAddress = (addr) => {
const baseAddr = serLib.BaseAddress.from_address(addr);
return {
AddrType: {
Expand Down
4 changes: 3 additions & 1 deletion src/Lib/Serialization.purs
Expand Up @@ -5,7 +5,7 @@ module Lib.Serialization
, _hexToBytes
) where

import Types.Transaction (Address)
import Types.Transaction (Address, TransactionUnspentOutput)

newtype Hex
= Hex String
Expand All @@ -15,3 +15,5 @@ foreign import data Buffer :: Type
foreign import _hexToBytes :: Hex -> Buffer

foreign import _addrFromBytes :: Buffer -> Address

foreign import _utxoFromBytes :: Buffer -> TransactionUnspentOutput
5 changes: 5 additions & 0 deletions src/Types/Transaction.purs
Expand Up @@ -121,6 +121,11 @@ newtype BaseAddress = BaseAddress

newtype Credential = Credential String

newtype TransactionUnspentOutput = TransactionUnspentOutput
{ input :: TransactionInput
, output :: TransactionOutput
}

-- Addresspub struct Address(AddrType);
-- AddrType
-- enum AddrType {
Expand Down
11 changes: 11 additions & 0 deletions src/Types/Wallet.js
Expand Up @@ -15,3 +15,14 @@ exports._enableNami = (nami) => () => nami.enable();
// _getUsedAddress :: NamiConnection -> Effect (Promise Hex)
exports._getUsedAddress = (nami) => () =>
nami.getUsedAddresses().then((addrs) => addrs[0]);

// _getCollateral ::
// NamiConnection ->
// (forall a. a -> Maybe a) ->
// (forall a. Maybe a) ->
// Effect (Promise Hex)
exports._getCollateral = (nami) => (just) => (nothing) => () =>
nami.getCollateral().then((utxos) => {
const collateral = utxos[0];
return collateral ? just(collateral) : nothing;
});
30 changes: 25 additions & 5 deletions src/Types/Wallet.purs
Expand Up @@ -18,11 +18,11 @@ import Effect.Class (liftEffect)
import Effect.Exception (error)
import Effect.Ref as Ref
import Lib.Serialization (Hex, _addrFromBytes, _hexToBytes)
import Types.Transaction (Address(..), BaseAddress(..), Credential(..))
import Types.Transaction (Address(..), BaseAddress(..), Credential(..), TransactionUnspentOutput(..))

-- At the moment, we only support Nami's wallet. In the future we will expand
-- this with more constructors to represent out-of-browser wallets (e.g. WBE)
newtype Wallet w
data Wallet w
= Nami (NamiWallet w)

-------------------------------------------------------------------------------
Expand All @@ -41,10 +41,14 @@ type NamiWallet w
, enable :: w -> Aff w
-- ^ Request that the user grant access to their nami wallet
, getWalletAddress :: w -> Aff Address
-- ^ Get the address associated with the wallet (Nami does not support
-- multiple addresses)
, getCollateral :: w -> Aff (Maybe TransactionUnspentOutput)
-- ^ Get the collateral UTxO associated with the Nami wallet
}

-- Make a `Wallet NamiConnection` with a reference to a `window.cardano` object
-- from Nami
-- Make a `Wallet NamiConnection` with a reference to a `window.cardano.nami`
-- object
mkNamiWallet :: Aff (Wallet NamiConnection)
mkNamiWallet =
liftEffect
Expand All @@ -53,7 +57,13 @@ mkNamiWallet =
Ref.new
=<< maybe throwConnError pure
=<< _mkNamiConnection Just Nothing
pure $ Nami { connection, enable, getWalletAddress }
pure
$ Nami
{ connection
, enable
, getWalletAddress
, getCollateral
}
where
enable :: NamiConnection -> Aff NamiConnection
enable = Promise.toAffE <<< _enableNami
Expand All @@ -64,6 +74,9 @@ mkNamiWallet =
<<< _hexToBytes
<$> Promise.toAffE (_getUsedAddress nami)

getCollateral :: NamiConnection -> Aff (Maybe TransactionUnspentOutput)
getCollateral nami = error "TODO"

throwConnError :: Effect NamiConnection
throwConnError =
throwError
Expand Down Expand Up @@ -92,6 +105,7 @@ mockNamiWallet =
, stake: Credential mempty
}
}
, getCollateral: const $ pure Nothing
}

foreign import data NamiConnection :: Type
Expand All @@ -105,3 +119,9 @@ foreign import _mkNamiConnection ::
foreign import _enableNami :: NamiConnection -> Effect (Promise NamiConnection)

foreign import _getUsedAddress :: NamiConnection -> Effect (Promise Hex)

foreign import _getCollateral ::
NamiConnection ->
(forall a. a -> Maybe a) ->
(forall a. Maybe a) ->
Effect (Promise Hex)

0 comments on commit a0e959d

Please sign in to comment.