-
Notifications
You must be signed in to change notification settings - Fork 29
cardano-wasm demo: multi-wallet management #1270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| project: cardano-wasm | ||
|
|
||
| pr: 1270 | ||
|
|
||
| kind: | ||
| - feature | ||
|
|
||
| description: | | ||
| Demo: multi-wallet management (generate/restore, aliases, network switch with address re-derivation). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| module Format exposing (ada, adaToLovelace, amountError, lovelaceToAda, orDefault, shorten) | ||
|
|
||
| {-| String formatting and parsing: ada ↔ lovelace, abbreviation, small text helpers. | ||
| -} | ||
|
|
||
|
|
||
| {-| Parse a user-typed ADA amount ("1.5" → 1500000 lovelace). | ||
| -} | ||
| adaToLovelace : String -> Maybe Int | ||
| adaToLovelace s = | ||
| String.toFloat (String.trim s) | ||
| |> Maybe.map (\f -> round (f * 1.0e6)) | ||
|
|
||
|
|
||
| {-| True when a typed amount is non-empty but not a valid positive number (e.g. "1,5"). | ||
| -} | ||
| amountError : String -> Bool | ||
| amountError s = | ||
| if String.trim s == "" then | ||
| False | ||
|
|
||
| else | ||
| case adaToLovelace s of | ||
| Just n -> | ||
| n <= 0 | ||
|
|
||
| Nothing -> | ||
| True | ||
|
|
||
|
|
||
| lovelaceToAda : Int -> String | ||
| lovelaceToAda l = | ||
| let | ||
| sign = | ||
| if l < 0 then | ||
| "-" | ||
|
|
||
| else | ||
| "" | ||
|
|
||
| a = | ||
| abs l | ||
|
|
||
| whole = | ||
| a // 1000000 | ||
|
|
||
| frac = | ||
| String.padLeft 6 '0' (String.fromInt (modBy 1000000 a)) |> stripTrailingZeros | ||
| in | ||
| sign | ||
| ++ String.fromInt whole | ||
| ++ (if frac == "" then | ||
| "" | ||
|
|
||
| else | ||
| "." ++ frac | ||
| ) | ||
|
|
||
|
|
||
| ada : Int -> String | ||
| ada l = | ||
| lovelaceToAda l ++ " ₳" | ||
|
|
||
|
|
||
| stripTrailingZeros : String -> String | ||
| stripTrailingZeros s = | ||
| String.foldr | ||
| (\c ( acc, trimming ) -> | ||
| if trimming && c == '0' then | ||
| ( acc, True ) | ||
|
|
||
| else | ||
| ( String.cons c acc, False ) | ||
| ) | ||
| ( "", True ) | ||
| s | ||
| |> Tuple.first | ||
|
|
||
|
|
||
| {-| Abbreviate long identifiers (addresses, keys, hashes) for display. | ||
| -} | ||
| shorten : String -> String | ||
| shorten s = | ||
| if String.length s > 22 then | ||
| String.left 12 s ++ "…" ++ String.right 6 s | ||
|
|
||
| else | ||
| s | ||
|
|
||
|
|
||
| orDefault : String -> String -> String | ||
| orDefault d s = | ||
| if String.trim s == "" then | ||
| d | ||
|
|
||
| else | ||
| s | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,42 @@ | ||
| module Main exposing (main) | ||
|
|
||
| {-| Placeholder page: proves the demo build pipeline and that the cardano-wasm | ||
| engine loads in the browser (web/ports.js only starts this application after | ||
| `initialise()` has succeeded). The wallet application itself follows. | ||
| {-| Entry point — wires The Elm Architecture together. The interesting code lives in: | ||
|
|
||
| - Types — every data type, the Model and the Msg | ||
| - State — initial state, derived queries, small updaters | ||
| - Update — the controller (one branch per Msg) | ||
| - View — the whole UI | ||
| - Wasm — the cardano-wasm boundary (port commands + decoders) | ||
| - Net / Format — static tables and pure utilities | ||
| - Ports — the raw port declarations (JS side: web/ports.js) | ||
|
|
||
| -} | ||
|
|
||
| import Html exposing (Html, div, h3, p, text) | ||
| import Html.Attributes exposing (class, style) | ||
| import Browser | ||
| import Ports | ||
| import State exposing (init) | ||
| import Types exposing (..) | ||
| import Update exposing (update) | ||
| import View exposing (view) | ||
| import Wasm | ||
|
|
||
|
|
||
| main : Html msg | ||
| main = | ||
| div [ class "grid" ] | ||
| [ div [ class "col" ] [] | ||
| , div [ class "col" ] | ||
| [ div [ class "card", style "margin-top" "40px" ] | ||
| [ h3 [] [ text "cardano-wasm wallet demo" ] | ||
| , p [] [ text "✓ the cardano-wasm engine loaded successfully in your browser." ] | ||
| , p [ class "muted small" ] [ text "The wallet application will appear here as it is built up in subsequent changes." ] | ||
| ] | ||
| ] | ||
| , div [ class "col" ] [] | ||
| {-| Each incoming port is decoded by Wasm and dispatched as a Msg. | ||
| -} | ||
| subscriptions : Model -> Sub Msg | ||
| subscriptions _ = | ||
| Sub.batch | ||
| [ Ports.wasmWalletGenerated (Wasm.decodeResult Wasm.genDecoder >> GotGeneratedWallet) | ||
| , Ports.wasmWalletRestored (Wasm.decodeResult Wasm.genDecoder >> GotRestoredWallet) | ||
| , Ports.wasmAddressesDerived (Wasm.decodeResult Wasm.addrsDecoder >> GotDerivedAddresses) | ||
| ] | ||
|
|
||
|
|
||
| main : Program () Model Msg | ||
| main = | ||
| Browser.element | ||
| { init = init | ||
| , update = update | ||
| , view = view | ||
| , subscriptions = subscriptions | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| module Net exposing | ||
| ( blockfrostBase | ||
| , cliFlag | ||
| , explorerTx | ||
| , faucetUrl | ||
| , netMagic | ||
| , netName | ||
| , netTag | ||
| ) | ||
|
|
||
| {-| Static tables for the three networks. Pure data — no logic lives here. | ||
| -} | ||
|
|
||
| import Types exposing (..) | ||
|
|
||
|
|
||
|
|
||
| -- NETWORKS | ||
|
|
||
|
|
||
| netName : Network -> String | ||
| netName n = | ||
| case n of | ||
| Mainnet -> | ||
| "Mainnet" | ||
|
|
||
| Preprod -> | ||
| "Preprod" | ||
|
|
||
| Preview -> | ||
| "Preview" | ||
|
|
||
|
|
||
| {-| Lowercase identifier used on the JS side of the ports. | ||
| -} | ||
| netTag : Network -> String | ||
| netTag n = | ||
| case n of | ||
| Mainnet -> | ||
| "mainnet" | ||
|
|
||
| Preprod -> | ||
| "preprod" | ||
|
|
||
| Preview -> | ||
| "preview" | ||
|
|
||
|
|
||
| netMagic : Network -> String | ||
| netMagic n = | ||
| case n of | ||
| Mainnet -> | ||
| "764824073" | ||
|
|
||
| Preprod -> | ||
| "1" | ||
|
|
||
| Preview -> | ||
| "2" | ||
|
|
||
|
|
||
| blockfrostBase : Network -> String | ||
| blockfrostBase n = | ||
| case n of | ||
| Mainnet -> | ||
| "https://cardano-mainnet.blockfrost.io/api/v0" | ||
|
|
||
| Preprod -> | ||
| "https://cardano-preprod.blockfrost.io/api/v0" | ||
|
|
||
| Preview -> | ||
| "https://cardano-preview.blockfrost.io/api/v0" | ||
|
|
||
|
|
||
| explorerTx : Network -> String | ||
| explorerTx n = | ||
| case n of | ||
| Mainnet -> | ||
| "https://cardanoscan.io/" | ||
|
|
||
| Preprod -> | ||
| "https://preprod.cardanoscan.io/" | ||
|
|
||
| Preview -> | ||
| "https://preview.cardanoscan.io/" | ||
|
|
||
|
|
||
| faucetUrl : Network -> Maybe String | ||
| faucetUrl n = | ||
| case n of | ||
| Mainnet -> | ||
| Nothing | ||
|
|
||
| _ -> | ||
| Just "https://docs.cardano.org/cardano-testnets/tools/faucet/" | ||
|
|
||
|
|
||
| cliFlag : Network -> String | ||
| cliFlag n = | ||
| case n of | ||
| Mainnet -> | ||
| "--mainnet" | ||
|
|
||
| Preprod -> | ||
| "--testnet-magic 1" | ||
|
|
||
| Preview -> | ||
| "--testnet-magic 2" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| port module Ports exposing (..) | ||
|
|
||
| {-| The raw port declarations — the only holes in the wall between Elm and | ||
| JavaScript. The JS side lives in web/ports.js. Payloads are untyped JSON; | ||
| Wasm.elm encodes the requests and decodes the replies. | ||
| -} | ||
|
|
||
| import Json.Decode as D | ||
| import Json.Encode as E | ||
|
|
||
|
|
||
|
|
||
| -- PORTS (out → cardano-wasm / clipboard) | ||
|
|
||
|
|
||
| port wasmGenerateWallet : E.Value -> Cmd msg | ||
|
|
||
|
|
||
| port wasmRestoreWallet : E.Value -> Cmd msg | ||
|
|
||
|
|
||
| port wasmDeriveAddresses : E.Value -> Cmd msg | ||
|
|
||
|
|
||
| port clipboardWrite : String -> Cmd msg | ||
|
|
||
|
|
||
|
|
||
| -- PORTS (in ← cardano-wasm) | ||
|
|
||
|
|
||
| port wasmWalletGenerated : (D.Value -> msg) -> Sub msg | ||
|
|
||
|
|
||
| port wasmWalletRestored : (D.Value -> msg) -> Sub msg | ||
|
|
||
|
|
||
| port wasmAddressesDerived : (D.Value -> msg) -> Sub msg |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.