-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
307 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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,6 @@ | ||
# Rest Client Flow | ||
|
||
This example is a Work In Progress. It shows how to use the `@marlowe.io/runtime-rest-client` package. At the moment it is only used for internal testing. | ||
|
||
[ ] - [Issue 104](https://github.com/input-output-hk/marlowe-ts-sdk/issues/104): Complete this readme. | ||
[ ] - [Issue 112](https://github.com/input-output-hk/marlowe-ts-sdk/issues/112): Improve the example usability. |
This file contains 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,41 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>CIP45 test Flow</title> | ||
<!-- This import map tells the browser what version of the SDK to use, the route is rewritten by the | ||
local web server to point to the correct version --> | ||
<script src="/importmap"></script> | ||
</head> | ||
<body> | ||
<h1>CIP45 test Flow</h1> | ||
<div> | ||
<h2>Setup Runtime</h2> | ||
|
||
<label for="runtimeUrl">URL to a Marlowe Runtime instance:</label> | ||
<input | ||
id="runtimeUrl" | ||
type="text" | ||
autocomplete="on" | ||
placeholder="http://localhost:32952" | ||
/> | ||
</div> | ||
<div id="qr-code"></div> | ||
<div id="connected-wallet" style="display: none"> | ||
<h2>Connected Wallet</h2> | ||
<div id="connected-wallet-address"></div> | ||
<div id="connected-wallet-balance"></div> | ||
<button id="create-contract">Create contract</button> | ||
<button id="wallet-flow">Wallet flow</button> | ||
<button id="disconnect-wallet">Disconnect Wallet</button> | ||
</div> | ||
<span id="connection-id"></span> | ||
<h2>Console</h2> | ||
<input id="clear-console" type="button" value="Clear console" /> | ||
<div id="console"></div> | ||
|
||
<script src="https://fabianbormann.github.io/cardano-peer-connect/latest/index.js"></script> | ||
<script type="module" src="./index.js"></script> | ||
</body> | ||
</html> |
This file contains 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,170 @@ | ||
import { mkRestClient } from "@marlowe.io/runtime-rest-client"; | ||
import { MarloweJSON } from "@marlowe.io/adapter/codec"; | ||
import { clearConsole, log, logJSON } from "../js/poc-helpers.js"; | ||
import { mkPeerConnectAdapter } from "@marlowe.io/wallet/peer-connect"; | ||
import { mkRuntimeLifecycle } from "@marlowe.io/runtime-lifecycle"; | ||
|
||
import * as H from "../js/poc-helpers.js"; | ||
|
||
window.restClient = null; | ||
function getRestClient() { | ||
if (window.restClient === null) { | ||
const runtimeURL = H.getRuntimeUrl(); | ||
window.restClient = mkRestClient(runtimeURL); | ||
} | ||
return window.restClient; | ||
} | ||
const runtimeUrlInput = document.getElementById("runtimeUrl"); | ||
runtimeUrlInput.addEventListener("change", () => { | ||
window.restClient = null; | ||
}); | ||
|
||
const clearConsoleButton = document.getElementById("clear-console"); | ||
clearConsoleButton.addEventListener("click", clearConsole); | ||
|
||
H.setupLocalStorageRuntimeUrl(); | ||
|
||
const adapter = mkPeerConnectAdapter(); | ||
adapter.onDeleteWallet(async (walletId) => { | ||
updateDisconnectedWalletStatus(); | ||
}); | ||
adapter.onNewWallet(async (walletId, wallet) => { | ||
updateConnectedWalletStatus(wallet); | ||
}); | ||
window.adapter = adapter; | ||
|
||
// Give your app some basic information that will be displayed to the client wallet when he is connecting to your DApp. | ||
const dAppInfo = { | ||
name: "Cip45 test", | ||
url: "http://localhost:1337/examples", | ||
}; | ||
// Define a function that will be called when the client tries to connect to your DApp. | ||
const verifyConnection = (walletInfo, callback) => { | ||
logJSON("walletInfo", walletInfo); | ||
callback( | ||
// | ||
window.confirm( | ||
`Do you want to connect to wallet ${walletInfo.name} (${walletInfo.address})?` | ||
) | ||
); | ||
}; | ||
|
||
function updateDisconnectedWalletStatus() { | ||
document.getElementById("connected-wallet").style.display = "none"; | ||
document.getElementById("qr-code").style.display = "block"; | ||
} | ||
|
||
async function updateConnectedWalletStatus(wallet) { | ||
const address = await wallet.getChangeAddress(); | ||
const balance = await wallet.getLovelaces(); | ||
const addressElm = document.getElementById("connected-wallet-address"); | ||
const balanceElm = document.getElementById("connected-wallet-balance"); | ||
addressElm.textContent = address; | ||
balanceElm.textContent = balance; | ||
|
||
document.getElementById("connected-wallet").style.display = "block"; | ||
document.getElementById("qr-code").style.display = "none"; | ||
} | ||
|
||
const dAppConnect = new CardanoPeerConnect.DAppPeerConnect({ | ||
dAppInfo: dAppInfo, | ||
verifyConnection: verifyConnection, | ||
onApiInject: adapter.adaptApiInject, | ||
onApiEject: adapter.adaptApiEject, | ||
}); | ||
window.dAppConnect = dAppConnect; | ||
|
||
document | ||
.getElementById("disconnect-wallet") | ||
.addEventListener("click", async () => { | ||
// NOTE: dAppConnect doesn't have a disconnect method, and this is currently | ||
// not working. It's not clear how to disconnect from the wallet. | ||
// https://github.com/fabianbormann/cardano-peer-connect/issues/57 | ||
logJSON("adapter", adapter); | ||
logJSON("dAppConnect", dAppConnect); | ||
const walletInfo = { version: 1, name: "a", icon: "bubu" }; | ||
dAppConnect.meerkat.api.disconnect( | ||
// dAppConnect.dAppInfo.address, | ||
dAppConnect.connectedWallet, | ||
walletInfo, | ||
(connectStatus) => { | ||
console.log(connectStatus); | ||
debugger; | ||
} | ||
); | ||
}); | ||
|
||
document | ||
.getElementById("create-contract") | ||
.addEventListener("click", async () => { | ||
const wallet = adapter.getWallet(); | ||
const runtime = mkRuntimeLifecycle({ | ||
runtimeURL: H.getRuntimeUrl(), | ||
wallet, | ||
}); | ||
|
||
const [contractId, txId] = await runtime.contracts.createContract({ | ||
contract: "close", | ||
tags: { | ||
"cip-45": "true", | ||
}, | ||
}); | ||
|
||
log(`contractId: ${contractId}`); | ||
log(`waiting for txId ${txId}`); | ||
await wallet.waitConfirmation(txId); | ||
log("transaction confirmed"); | ||
}); | ||
|
||
document.getElementById("wallet-flow").addEventListener("click", async () => { | ||
const wallet = adapter.getWallet(); | ||
log(`<h2>CIP-45 Wallet Extension</h2>`); | ||
log(""); | ||
log("Reading Wallet information..."); | ||
log(""); | ||
|
||
const isMainnet = await wallet.isMainnet(); | ||
log(`* <b>Network</b>: ${isMainnet ? "Mainnet" : "Testnet"}`); | ||
log(""); | ||
|
||
const lovelaces = await wallet.getLovelaces(); | ||
log("- <b>Lovelaces</b>: " + lovelaces); | ||
const tokensResult = await wallet.getTokens(); | ||
log(""); | ||
|
||
log(`- <b>Tokens</b>: (${tokensResult.length} tokens)`); | ||
tokensResult.map((token) => { | ||
const tokenName = | ||
token.assetId.assetName == "" ? "lovelaces" : token.assetId.assetName; | ||
log(` <i>${tokenName}</i> - ${token.quantity}`); | ||
}); | ||
log(""); | ||
|
||
const changeAddress = await wallet.getChangeAddress(); | ||
log("- <b>Change Address</b>: " + changeAddress); | ||
log(""); | ||
|
||
const usedAddresses = await wallet.getUsedAddresses(); | ||
log(`- <b>Used Addresses</b>: (${usedAddresses.length} addresses)`); | ||
usedAddresses.map((usedAddress) => | ||
log(" - " + usedAddress) | ||
); | ||
log(""); | ||
|
||
const collaterals = await wallet.getCollaterals(); | ||
log(`- <b>Collaterals</b>: (${collaterals.length} collaterals)`); | ||
collaterals.map((collateral) => log(" - " + collateral)); | ||
log(""); | ||
|
||
const utxos = await wallet.getUTxOs(); | ||
log(`- <b>UTxOs</b>: (${utxos.length} utxos)`); | ||
utxos.map((utxo) => log(" - " + utxo)); | ||
log(""); | ||
log("Wallet flow done 🎉"); | ||
}); | ||
// This is the code (identifier) that the client needs to enter into the wallet to connect to your dapp | ||
const clientConnectCode = dAppConnect.getAddress(); | ||
document.getElementById("connection-id").innerText = clientConnectCode; | ||
|
||
// Create and insert a QR code on your DApp, so the user can scan it easily in their app | ||
dAppConnect.generateQRCode(document.getElementById("qr-code")); |
This file contains 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
This file contains 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
This file contains 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
This file contains 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,44 @@ | ||
import { WalletAPI } from "../api.js"; | ||
import * as Browser from "../browser/index.js"; | ||
|
||
type WalletHandler = (walletId: string, wallet: WalletAPI) => void; | ||
export const mkPeerConnectAdapter = () => { | ||
let wallet: WalletAPI | undefined; | ||
let newWalletHandler: WalletHandler = () => {}; | ||
let deleteWalletHandler: WalletHandler = () => {}; | ||
return { | ||
adaptApiEject(walletName: string, walletId: string) { | ||
if (wallet) { | ||
deleteWalletHandler(walletId, wallet); | ||
} | ||
wallet = undefined; | ||
}, | ||
async adaptApiInject(walletName: string, walletId: string) { | ||
const di = { | ||
extension: await window.cardano[walletName.toLowerCase()].enable(), | ||
}; | ||
const peerWallet = { | ||
waitConfirmation: Browser.waitConfirmation(di), | ||
signTx: Browser.signTx(di), | ||
getChangeAddress: Browser.getChangeAddress(di), | ||
getUsedAddresses: Browser.getUsedAddresses(di), | ||
getCollaterals: Browser.getCollaterals(di), | ||
getUTxOs: Browser.getUTxOs(di), | ||
isMainnet: Browser.isMainnet(di), | ||
getTokens: Browser.getTokens(di), | ||
getLovelaces: Browser.getLovelaces(di), | ||
}; | ||
wallet = peerWallet; | ||
newWalletHandler(walletId, peerWallet); | ||
}, | ||
onNewWallet(handler: WalletHandler) { | ||
newWalletHandler = handler; | ||
}, | ||
onDeleteWallet(handler: WalletHandler) { | ||
deleteWalletHandler = handler; | ||
}, | ||
getWallet() { | ||
return wallet; | ||
}, | ||
}; | ||
}; |