|
| 1 | +# Delegating to Always Abstain |
| 2 | + |
| 3 | +This example can be used to construct a CBOR offline for vote delegation to always abstain on testnet. |
| 4 | + |
| 5 | +code taken from (here)[https://github.com/VladislavKudrin/csl_delegate_to_abstain] |
| 6 | + |
| 7 | +```javascript |
| 8 | +import { |
| 9 | + LinearFee, |
| 10 | + BigNum, |
| 11 | + TransactionBuilderConfigBuilder, |
| 12 | + TransactionBuilder, |
| 13 | + Bip32PrivateKey, |
| 14 | + BaseAddress, |
| 15 | + NetworkInfo, |
| 16 | + Credential, |
| 17 | + DRep, |
| 18 | + VoteDelegation, |
| 19 | + Certificates, |
| 20 | + Certificate, |
| 21 | + FixedTransaction, |
| 22 | + TxInputsBuilder, |
| 23 | + TransactionInput, |
| 24 | + TransactionHash, |
| 25 | + Value, |
| 26 | + ExUnitPrices, |
| 27 | + UnitInterval, |
| 28 | +} from "@emurgo/cardano-serialization-lib-nodejs"; |
| 29 | +import { mnemonicToEntropy } from "bip39"; |
| 30 | + |
| 31 | +const MNEMONIC = "your mnemonic"; |
| 32 | +const INPUT_HASH = "UTXO hash"; |
| 33 | +const INPUT_INDEX = "UTXO index"; |
| 34 | +const INPUT_AMOUNT = "UTXO amount" //lovelace |
| 35 | + |
| 36 | +function harden(num: number): number { |
| 37 | + return 0x80000000 + num; |
| 38 | +} |
| 39 | + |
| 40 | +function main(): void { |
| 41 | + // derive the keys from mnemonic |
| 42 | + const entropy = mnemonicToEntropy(MNEMONIC!); |
| 43 | + const rootKey = Bip32PrivateKey.from_bip39_entropy( |
| 44 | + Buffer.from(entropy, "hex"), |
| 45 | + Buffer.from("") |
| 46 | + ); |
| 47 | + |
| 48 | + const accountKey = rootKey |
| 49 | + .derive(harden(1852)) |
| 50 | + .derive(harden(1815)) |
| 51 | + .derive(harden(0)); |
| 52 | + const stakePrivKey = accountKey.derive(2).derive(0); |
| 53 | + const utxoPrivKey = accountKey.derive(0).derive(0); |
| 54 | + |
| 55 | + // get payment address |
| 56 | + const addr = BaseAddress.new( |
| 57 | + NetworkInfo.testnet_preprod().network_id(), |
| 58 | + Credential.from_keyhash(utxoPrivKey.to_public().to_raw_key().hash()), |
| 59 | + Credential.from_keyhash(stakePrivKey.to_public().to_raw_key().hash()) |
| 60 | + ); |
| 61 | + |
| 62 | + // instantiate the tx builder with the Cardano protocol parameters |
| 63 | + const linearFee = LinearFee.new( |
| 64 | + BigNum.from_str("44"), |
| 65 | + BigNum.from_str("155381") |
| 66 | + ); |
| 67 | + |
| 68 | + // these parameters is used as an example, you should use the latest actual protocol params from cardano-node, cardano-cli or from 3rd party API providers |
| 69 | + const txBuilderCfg = TransactionBuilderConfigBuilder.new() |
| 70 | + .fee_algo(linearFee) |
| 71 | + .pool_deposit(BigNum.from_str("500000000")) |
| 72 | + .key_deposit(BigNum.from_str("2000000")) |
| 73 | + .max_value_size(5000) |
| 74 | + .max_tx_size(16384) |
| 75 | + .coins_per_utxo_byte(BigNum.from_str("4310")) |
| 76 | + .ex_unit_prices(ExUnitPrices.new( |
| 77 | + UnitInterval.new( |
| 78 | + BigNum.from_str("577"), |
| 79 | + BigNum.from_str("10000") |
| 80 | + ), |
| 81 | + UnitInterval.new( |
| 82 | + BigNum.from_str("721"), |
| 83 | + BigNum.from_str("10000000") |
| 84 | + ) |
| 85 | + )) |
| 86 | + .build(); |
| 87 | + .build(); |
| 88 | + |
| 89 | + const txBuilder = TransactionBuilder.new(txBuilderCfg); |
| 90 | + |
| 91 | + // create new "Always Abstain" DRep and Vote Delegation |
| 92 | + const drep = DRep.new_always_abstain(); |
| 93 | + const voteDelegation = VoteDelegation.new( |
| 94 | + Credential.from_keyhash(stakePrivKey.to_public().to_raw_key().hash()), |
| 95 | + drep |
| 96 | + ); |
| 97 | + |
| 98 | + // add vote delegation certificate to the txBuilder |
| 99 | + const certs = Certificates.new(); |
| 100 | + certs.add(Certificate.new_vote_delegation(voteDelegation)); |
| 101 | + txBuilder.set_certs(certs); |
| 102 | + |
| 103 | + // add input for paying fees and add change |
| 104 | + const txInputsBuilder = TxInputsBuilder.new(); |
| 105 | + txInputsBuilder.add_regular_input( |
| 106 | + addr.to_address(), |
| 107 | + TransactionInput.new(TransactionHash.from_hex(INPUT_HASH!), Number(INPUT_INDEX!)), |
| 108 | + Value.new(BigNum.from_str(INPUT_AMOUNT!)) |
| 109 | + ); |
| 110 | + txBuilder.set_inputs(txInputsBuilder); |
| 111 | + txBuilder.add_change_if_needed(addr.to_address()); |
| 112 | + |
| 113 | + |
| 114 | + const txBody = txBuilder.build(); |
| 115 | + |
| 116 | + //Use FixedTransaction for each time when you need to sign a transaction, especially if you recieved it from a third party |
| 117 | + const transaction = FixedTransaction.new_from_body_bytes(txBody.to_bytes()); |
| 118 | + |
| 119 | + // sign the tx with stake and payment keys |
| 120 | + transaction.sign_and_add_vkey_signature(stakePrivKey.to_raw_key()); |
| 121 | + transaction.sign_and_add_vkey_signature(utxoPrivKey.to_raw_key()); |
| 122 | + |
| 123 | + // CBOR |
| 124 | + console.log(transaction.to_hex()); |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | +``` |
0 commit comments