const MNEMONICS = "bottom stone xxxx xxxx xxx xxxx tourist finish xxx trend xxx xxxxxx";
let settings = Wallet.BlockchainSettings.mainnet();
let entropy = Wallet.Entropy.from_english_mnemonics(MNEMONICS);
let wallet = Wallet.Bip44RootPrivateKey.recover(entropy);
let account = wallet.bip44_account(Wallet.AccountIndex.new(0 | 0x80000000));
let account_public = account.public();
let chain_prv = account.bip44_chain(false);
let key_prv = chain_prv.address_key(Wallet.AddressKeyIndex.new(0));
// console.log('address public ' + key_prv.to_hex());
let chain_pub = account_public.bip44_chain(false);
let key_pub = chain_pub.address_key(Wallet.AddressKeyIndex.new(0));
// console.log('address public ' + key_pub.to_hex());
let address = key_pub.bootstrap_era_address(settings);
console.log("Address m/bip44/ada/'0/0/0", address.to_base58());
// Building a transaction
let transaction_builder = new Wallet.TransactionBuilder();
let trx = await axios.get('https://cardanoexplorer.com/api/addresses/summary/Ae2tdPwUPEZKfa3EfkEhj9K1jyxUhkuxYaG1SAVFyD3KzrfLBgwaRV11zc1');
// console.log('Inputs:', trx.data.Right.caTxList[0].ctbInputs,'\nOutputs:',trx.data.Right.caTxList[0].ctbOutputs, '\nInputs Total:', trx.data.Right.caTxList[0].ctbInputSum.getCoin/1000000);
console.log(trx.data.Right.caTxList.length)
var trxResult = [];
for (let i = 0; i < trx.data.Right.caTxList.length; i++) {
// console.log(trx.data.Right.caTxList[i].ctbId);
trxResult.push(trx.data.Right.caTxList[i].ctbId);
}
// console.log(trxResult);
const inputs = [
// { pointer: { id: inputResult[2], index: 2 }, value: 1 },
// { pointer: { id: inputResult[1], index: 1 }, value: 1 },
{ pointer: { id: trxResult[0], index: 0 }, value: 5 }
];
// console.log(inputs);
const outputs = [
// TODO: you can test the balance by changing the value here.
{ address: "Ae2tdPwUPEZCNHvZbqWhKmeHPxzvZxrnwJZghsm16Vjrzd8DriLn9jCarpr", value: "1000000" }
];
for (let index = 0; index < inputs.length; index++) {
const pointer = Wallet.TxoPointer.from_json(inputs[index].pointer);
const value = Wallet.Coin.from(inputs[index].value, 0);
transaction_builder.add_input(pointer, value);
}
console.log("all inputs set...", transaction_builder.get_input_total().to_str());
for (let index = 0; index < outputs.length; index++) {
const txout = Wallet.TxOut.from_json(outputs[index]);
transaction_builder.add_output(txout);
}
console.log("all outputs set...", transaction_builder.get_output_total().to_str());
// verify the balance and the fees:
const fee_algorithm = Wallet.LinearFeeAlgorithm.default();
const balance = transaction_builder.get_balance(fee_algorithm);
if (balance.is_negative()) {
console.error("not enough inputs, ", balance.value().to_str());
throw Error("Not enough inputs");
} else {
if (balance.is_zero()) {
console.info("Perfect balance no dust", balance.value().to_str());
} else {
console.warn("Loosing some coins in extra fees: ", balance.value().to_str());
}
}
let transaction = transaction_builder.make_transaction();
// console.log("unsigned transaction built", transaction);
let transaction_finalizer = new Wallet.TransactionFinalized(transaction);
// console.log("transaction finalizer built", transaction_finalizer);
for (let index = 0; index < inputs.length; index++) {
const witness = Wallet.Witness.new_extended_key(
settings,
key_prv,
transaction_finalizer.id()
);
transaction_finalizer.add_witness(witness);
console.log("signature ", index, "added");
}
// at this stage the transaction is ready to be sent
const signed_transaction = transaction_finalizer.finalize();
console.log("ready to send transaction: ", signed_transaction.to_hex());
const result = signed_transaction.to_json()
console.log(result.tx);
// console.log(result.witness);
Address m/bip44/ada/'0/0/0 Ae2tdPwUPEZKfa3EfkEhj9K1jyxUhkuxYaG1SAVFyD3KzrfLBgwaRV11zc1
all inputs set... 5.000000
all outputs set... 1.000000
Loosing some coins in extra fees: 3.834159
signature 0 added
ready to send transaction: 82839f8200d818582482582050a89fc4d468c5b25022faa0d01b33ad6551c3f71fee10fe5dd5fcd860a6831900ff9f8282d818582183581c9bf3a0807b668107a9cac1e50e10ada427b7e68835de69f46ad104efa0001adcb4a5531a000f4240ffa0818200d8185885825840d9f10d0ad1e60ca23de9918b7e1cd6b27d3efe564166b428dda5203a076aa83f08a74cb34468ae6b256c71e295d773d79bbd3d6f5b78044652dfb2768744a3bb5840d739b94a8951e95e0524866c44e88991898f16bfd733d23590f7f177f11c23b3097f0993128c3a1273d5864eb7a62b4cc9ff3a33ce0791d66dcada7dd5bda409
{ inputs:
[ { id:
'50a89fc4d468c5b25022faa0d01b33ad6551c3f71fee10fe5dd5fcd860a68319',
index: 0 } ],
outputs:
[ { address:
'Ae2tdPwUPEZCNHvZbqWhKmeHPxzvZxrnwJZghsm16Vjrzd8DriLn9jCarpr',
value: 1000000 } ] }