Skip to content
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

Specify Tx receiver when creating a Tx from the dapp-example & other small fixes #3101

Merged
merged 4 commits into from
Jan 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 45 additions & 36 deletions packages/yoroi-connector/example-cardano/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,32 @@ import { getTtl } from "./utils";
import { Bech32Prefix } from "../../yoroi-extension/app/config/stringConfig";
import { bytesToHex, hexToBytes } from "./coreUtils";

const cardanoAccessBtnRow = document.querySelector("#request-button-row");
const cardanoAuthCheck = document.querySelector("#check-identification");
const cardanoAccessBtn = document.querySelector("#request-access");
const connectionStatus = document.querySelector("#connection-status");
const walletPlateSpan = document.querySelector("#wallet-plate");
const walletIconSpan = document.querySelector("#wallet-icon");
const getUnUsedAddresses = document.querySelector("#get-unused-addresses");
const getUsedAddresses = document.querySelector("#get-used-addresses");
const getChangeAddress = document.querySelector("#get-change-address");
const getRewardAddresses = document.querySelector("#get-reward-addresses");
const getAccountBalance = document.querySelector("#get-balance");
const isEnabledBtn = document.querySelector("#is-enabled");
const getUtxos = document.querySelector("#get-utxos");
const submitTx = document.querySelector("#submit-tx");
const signTx = document.querySelector("#sign-tx");
const showUtxos = document.querySelector("#show-utxos");
const getCollateralUtxos = document.querySelector("#get-collateral-utxos");
const signData = document.querySelector("#sign-data");
const alertEl = document.querySelector("#alert");
const spinner = document.querySelector("#spinner");
const utxosContainer = document.querySelector("#utxos");
const getNFTs = document.getElementById("nfts");
const getNetworkId = document.getElementById("get-network-id");
const get = (selector) => document.querySelector(selector);
const getAll = (selector) => document.querySelectorAll(selector);

const cardanoAccessBtnRow = get("#request-button-row");
const cardanoAuthCheck = get("#check-identification");
const cardanoAccessBtn = get("#request-access");
const connectionStatus = get("#connection-status");
const walletPlateSpan = get("#wallet-plate");
const walletIconSpan = get("#wallet-icon");
const getUnUsedAddresses = get("#get-unused-addresses");
const getUsedAddresses = get("#get-used-addresses");
const getChangeAddress = get("#get-change-address");
const getRewardAddresses = get("#get-reward-addresses");
const getAccountBalance = get("#get-balance");
const isEnabledBtn = get("#is-enabled");
const getUtxos = get("#get-utxos");
const submitTx = get("#submit-tx");
const signTx = get("#sign-tx");
const showUtxos = get("#show-utxos");
const getCollateralUtxos = get("#get-collateral-utxos");
const signData = get("#sign-data");
const alertEl = get("#alert");
const spinner = get("#spinner");
const utxosContainer = get("#utxos");
const getNFTs = get("#nfts");
const getNetworkId = get("#get-network-id");

let accessGranted = false;
let cardanoApi;
Expand Down Expand Up @@ -587,12 +590,12 @@ showUtxos.addEventListener("click", () => {

function alertError(text) {
toggleSpinner("hide");
alertEl.className = "alert alert-danger overflow-scroll";
alertEl.className = "alert alert-danger overflow-auto";
alertEl.innerHTML = text;
}

function alertSuccess(text) {
alertEl.className = "alert alert-success overflow-scroll";
alertEl.className = "alert alert-success overflow-auto";
alertEl.innerHTML = text;
}

Expand Down Expand Up @@ -639,17 +642,24 @@ function renderUtxo() {
let utxosHTML = "";
for (let idx in utxos) {
const utxo = utxos[idx];
const amountInADA = Number(utxo.amount) / 1000000;
const numOfAssets = utxo.assets.length;

utxosHTML += `
<li id='${idx}' class="utxo-item list-group-item d-flex justify-content-between align-items-center ${
selectedUtxoIdx == idx && "bg-primary text-white"
}" style='cursor: pointer;'>
<p id='${idx}' class='mb-0'>${utxo.utxo_id.slice(0, 50)}</p>
<span class="badge bg-primary rounded-pill">${utxo.amount}</span>
<p id='${idx}' class='mb-0'>${utxo.utxo_id.slice(0, 25)}</p>
<div>
${numOfAssets ? `<span class="badge bg-primary rounded-pill">${utxo.assets.length} Assets</span>` : ''}
<span class="badge bg-primary rounded-pill">${amountInADA} ADA</span>
</div>
</li>
`;
}

utxosHTML += `
<input class="w-100 mt-3 p-1" placeholder="Receiver addresss..." type="text" id="create-tx-receiver" />
<button id="create-tx" class="btn btn-light mt-3 w-100">[Experimental] Create Tx</button>
`;
utxosContainer.innerHTML = utxosHTML;
Expand All @@ -661,14 +671,12 @@ function renderUtxo() {
"mb-5"
);
// Add select utxo handler for each list item
document.querySelectorAll(".utxo-item").forEach((el) => {
getAll(".utxo-item").forEach((el) => {
el.addEventListener("click", selectUtxo);
});

// Add event handler for create tx button
document
.querySelector("#create-tx")
.addEventListener("click", createTxHandler);
get("#create-tx").addEventListener("click", createTxHandler);
}

function createTxHandler(e) {
Expand All @@ -691,7 +699,7 @@ function createTxHandler(e) {

const selectedUtxo = utxos[selectedUtxoIdx];
if (!selectedUtxo) {
alertError("Failed to select a random utxo from the available list!");
alertError("No utxo selected");
return;
}

Expand Down Expand Up @@ -741,9 +749,10 @@ function createTxHandler(e) {
expectedPolicyId,
});

let receiver = get('#create-tx-receiver').value || selectedUtxo.receiver;
const outputHex = bytesToHex(
CardanoWasm.TransactionOutput.new(
CardanoWasm.Address.from_bech32(selectedUtxo.receiver),
CardanoWasm.Address.from_bech32(receiver),
CardanoWasm.Value.new(CardanoWasm.BigNum.from_str("1000000"))
).to_bytes()
);
Expand All @@ -752,7 +761,7 @@ function createTxHandler(e) {
const includeOutputs = [];
const includeTargets = [];

let targetAddress = selectedUtxo.receiver;
let targetAddress = receiver;
let targetDataHash = null;

/****** FLAGS ******/
Expand Down Expand Up @@ -923,7 +932,7 @@ function createTxHandler(e) {
console.log("[createTx] Including asset:", asset);
txReq.includeTargets.push({
// do not specify value, the connector will use minimum value
address: selectedUtxo.receiver,
address: receiver,
assets: {
[asset.assetId]: "1",
},
Expand Down Expand Up @@ -1005,7 +1014,7 @@ signData.addEventListener("click", () => {
address = addressToCbor(address);
}

const payload = document.querySelector("#sign-data-payload").value;
const payload = get("#sign-data-payload").value;
let payloadHex;
if (payload.startsWith("0x")) {
payloadHex = Buffer.from(payload.replace("^0x", ""), "hex").toString("hex");
Expand Down