Created
October 29, 2023 13:18
-
-
Save satran004/7344e26153529dcc2df989d25fd6c1b5 to your computer and use it in GitHub Desktop.
Spend from a multisig address
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
import com.bloxbean.cardano.client.account.Account; | |
import com.bloxbean.cardano.client.address.AddressProvider; | |
import com.bloxbean.cardano.client.api.model.Amount; | |
import com.bloxbean.cardano.client.backend.api.BackendService; | |
import com.bloxbean.cardano.client.backend.blockfrost.common.Constants; | |
import com.bloxbean.cardano.client.backend.blockfrost.service.BFBackendService; | |
import com.bloxbean.cardano.client.cip.cip20.MessageMetadata; | |
import com.bloxbean.cardano.client.common.model.Networks; | |
import com.bloxbean.cardano.client.crypto.VerificationKey; | |
import com.bloxbean.cardano.client.example.Constant; | |
import com.bloxbean.cardano.client.function.helper.SignerProviders; | |
import com.bloxbean.cardano.client.quicktx.QuickTxBuilder; | |
import com.bloxbean.cardano.client.quicktx.Tx; | |
import com.bloxbean.cardano.client.transaction.spec.script.NativeScript; | |
import com.bloxbean.cardano.client.transaction.spec.script.ScriptAll; | |
import com.bloxbean.cardano.client.transaction.spec.script.ScriptPubkey; | |
import org.junit.jupiter.api.Test; | |
public class MultisigAddressExample extends QuickTxBaseTest { | |
private BackendService backendService = new BFBackendService(Constants.BLOCKFROST_PREPROD_URL, Constant.BF_PROJECT_KEY); | |
String sender1Mnemonic = "<sender1 mnemonic>"; | |
Account sender1 = new Account(Networks.testnet(), sender1Mnemonic); | |
String sender1Addr = sender1.baseAddress(); | |
String sender2Mnemonic = "<sender2 menmonic>"; | |
Account sender2 = new Account(Networks.testnet(), sender2Mnemonic); | |
String sender2Addr = sender2.baseAddress(); | |
//Get a native script (type=all) from two accounts | |
public NativeScript getMultisigScript(Account sender1, Account sender2) throws Exception { | |
//sender1 - Account object1 | |
var pubKeyBytes1 = sender1.publicKeyBytes(); | |
var vkey1 = VerificationKey.create(pubKeyBytes1); | |
//sender2 - Account object2 | |
var pubKeyBytes2 = sender2.publicKeyBytes(); | |
var vkey2 = VerificationKey.create(pubKeyBytes2); | |
var scriptPubkey1 = ScriptPubkey.create(vkey1); | |
var scriptPubkey2 = ScriptPubkey.create(vkey2); | |
//Create ScriptAll native script | |
var scriptAll = new ScriptAll(); | |
scriptAll.addScript(scriptPubkey1); | |
scriptAll.addScript(scriptPubkey2); | |
return scriptAll; | |
} | |
//Get a multi-sig address from a native script | |
public String getMultisigAddress(NativeScript script) { | |
var multiSigAddr = AddressProvider.getEntAddress(script, Networks.testnet()).toBech32(); | |
return multiSigAddr; | |
} | |
//https://preprod.cardanoscan.io/transaction/9bc0b73918052f7cabd49f4ceb7348bb9cef4f7170e50807b2af2f37c067f287 | |
//Pay to multi-sig address | |
@Test | |
public void payToMultiSigAddress() throws Exception { | |
var multisigScript = getMultisigScript(sender1, sender2); | |
var multiSigAddr = getMultisigAddress(multisigScript); | |
//Pay to multi-sig address | |
var payTx = new Tx() | |
.payToAddress(multiSigAddr, Amount.ada(50)) | |
.attachMetadata(MessageMetadata.create().add("Transfer to multi-sig address")) | |
.from(sender1Addr); | |
var result = new QuickTxBuilder(backendService) | |
.compose(payTx) | |
.withSigner(SignerProviders.signerFrom(sender1)) | |
.completeAndWait(System.out::println); | |
} | |
//https://preprod.cardanoscan.io/transaction/92189ff220d576af8f68eea45d7543a8092cfa78c8224c5d3553ae07340e9ad6 | |
@Test | |
public void spendFromMultisigAddress() throws Exception { | |
var multisigScript = getMultisigScript(sender1, sender2); | |
var multiSigAddr = getMultisigAddress(multisigScript); | |
var spendTx = new Tx() | |
.payToAddress(receiver1Addr, Amount.ada(2)) | |
.attachMetadata(MessageMetadata.create().add("Spend from multi-sig address - signed by both sender1 & sender2")) | |
.from(multiSigAddr); | |
var result = new QuickTxBuilder(backendService) | |
.compose(spendTx) | |
.withSigner(SignerProviders.signerFrom(sender1)) | |
.withSigner(SignerProviders.signerFrom(sender2)) | |
.preBalanceTx((txBuilderContext, transaction) -> { | |
transaction.getWitnessSet().getNativeScripts().add(multisigScript); | |
}) | |
.completeAndWait(System.out::println); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment