Skip to content

Commit

Permalink
Update sigma-rust at ergo connector example
Browse files Browse the repository at this point in the history
  • Loading branch information
arobsn committed Sep 26, 2021
1 parent 09c72a1 commit d88600a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
37 changes: 30 additions & 7 deletions packages/yoroi-ergo-connector/example-ergo/index.js
@@ -1,4 +1,5 @@
import * as wasm from "ergo-lib-wasm-browser";
import JSONBigInt from 'json-bigint';

function initDapp() {
ergo_request_read_access().then(function(access_granted) {
Expand Down Expand Up @@ -62,7 +63,7 @@ function initDapp() {
const filteredUtxos = [];
for (const utxo of utxos) {
try {
await wasm.ErgoBox.from_json(JSON.stringify(utxo));
await wasm.ErgoBox.from_json(JSONBigInt.stringify(utxo));
filteredUtxos.push(utxo);
} catch (e) {
console.error('[getUtxos] UTxO failed parsing:', utxo, e);
Expand Down Expand Up @@ -122,31 +123,31 @@ function initDapp() {
// random tx we sent via the connector before - not referenced in any smart contract right now
//dataInputs.add(new wasm.DataInput(wasm.BoxId.from_str("0f0e4c71ccfbe7e749591ef2a906607b415deadee8c23a8d822517c4cd55374e")));
txBuilder.set_data_inputs(dataInputs);
const tx = txBuilder.build().to_json();
console.log(`tx: ${JSON.stringify(tx)}`);
const tx = parseJson(txBuilder.build().to_json());
console.log(`tx: ${JSONBigInt.stringify(tx)}`);
console.log(`original id: ${tx.id}`);
// sigma-rust doesn't support most compilation so manually insert it here
// this is HEIGHT > 1337 but in hex and without the checksum/etc for the address of the contract
//tx.outputs[0].ergoTree = "100104f214d191a37300";
// and this is a register-using one
//tx.outputs[0].ergoTree = "1002040004f2c001d193e4c6b2a573000004047301";
// and we rebuild it using
const correctTx = wasm.UnsignedTransaction.from_json(JSON.stringify(tx)).to_json();
console.log(`correct tx: ${JSON.stringify(correctTx)}`);
const correctTx = parseJson(wasm.UnsignedTransaction.from_json(JSONBigInt.stringify(tx)).to_json());
console.log(`correct tx: ${JSONBigInt.stringify(correctTx)}`);
console.log(`new id: ${correctTx.id}`);
// we must use the exact order chosen as after 0.4.3 in sigma-rust
// this can change and might not use all the utxos as the coin selection
// might choose a more optimal amount
correctTx.inputs = correctTx.inputs.map(box => {
console.log(`box: ${JSON.stringify(box)}`);
console.log(`box: ${JSONBigInt.stringify(box)}`);
const fullBoxInfo = utxos.find(utxo => utxo.boxId === box.boxId);
return {
...fullBoxInfo,
extension: {}
};
});
status.innerText = "Awaiting transaction signing";
console.log(`${JSON.stringify(correctTx)}`);
console.log(`${JSONBigInt.stringify(correctTx)}`);

async function signTx(txToBeSigned) {
try {
Expand Down Expand Up @@ -211,6 +212,28 @@ function initDapp() {
});
}

function parseJson(str) {
let json = JSONBigInt.parse(str);
return {
id: json.id,
inputs: json.inputs,
dataInputs: json.dataInputs,
outputs: json.outputs.map(output => ({
boxId: output.boxId,
value: output.value.toString(),
ergoTree: output.ergoTree,
assets: output.assets.map(asset => ({
tokenId: asset.tokenId,
amount: asset.amount.toString(),
})),
additionalRegisters: output.additionalRegisters,
creationHeight: output.creationHeight,
transactionId: output.transactionId,
index: output.index
})),
};
}

if (typeof ergo_request_read_access === "undefined") {
alert("ergo not found");
} else {
Expand Down
21 changes: 18 additions & 3 deletions packages/yoroi-ergo-connector/example-ergo/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/yoroi-ergo-connector/example-ergo/package.json
Expand Up @@ -11,7 +11,8 @@
"start": "webpack-dev-server"
},
"devDependencies": {
"ergo-lib-wasm-browser": "0.13.3",
"ergo-lib-wasm-browser": "0.14.0-alpha-a3940fc",
"json-bigint": "^1.0.0",
"webpack": "^4.29.3",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.5",
Expand Down

0 comments on commit d88600a

Please sign in to comment.