Signing announced aggregate-bonded transactions
This guide will show you how to cosign aggregate bonded transactions that require your account’s cosignature.
Prerequisites
- Finish creating an escrow with aggregate bonded transaction guide.
- Have received an aggregate bounded transaction.
- XPX-Chain-SDK.
- A text editor or IDE.
- Have an account with
XPX
.
Getting into some code
You have announced an aggregate bonded transaction, but all required cosigners have not signed it yet.
- Create a function to cosign any aggregate bonded transactions.
func cosignAggregateBoundedTransaction(signedAggregateBoundedTransaction *sdk.SignedTransaction, account *sdk.Account) *sdk.CosignatureSignedTransaction {
cosignatureTransaction := sdk.NewCosignatureTransactionFromHash(signedAggregateBoundedTransaction.Hash)
signedCosignatureTransaction , err := account.SignCosignatureTransaction(cosignatureTransaction)
if err != nil {
panic(err)
}
return signedCosignatureTransaction
}
const cosignAggregateBondedTransaction = (signedAggregateBondedTransaction: AggregateTransaction, account: Account): CosignatureSignedTransaction => {
const cosignatureTransaction = CosignatureTransaction.create(signedAggregateBondedTransaction);
return account.signCosignatureTransaction(cosignatureTransaction);
};
const cosignAggregateBondedTransaction = (signedAggregateBondedTransaction, account) => {
const cosignatureTransaction = CosignatureTransaction.create(signedAggregateBondedTransaction);
return account.signCosignatureTransaction(cosignatureTransaction);
};
- Fetch all aggregate bonded transactions pending to be signed by your account.
Note:
To fetch aggregate bonded transactions that must be signed by multisig cosignatories, refer to the multisig public key instead. See how to get multisig accounts where an account is cosignatory.
For each transaction, check if you have not already signed it. Cosign each pending transaction using the previously created function.
Announce
CosignatureSignedTransaction
to the network.
_, err = client.Transaction.AnnounceAggregateBoundedCosignature(context.Background(), cosignatureSignedTransaction)
if err != nil {
panic(err)
}
const privateKey = process.env.PRIVATE_KEY as string;
const account = Account.createFromPrivateKey(privateKey, NetworkType.TEST_NET);
const nodeUrl = 'http://bctestnet1.brimstone.xpxsirius.io:3000';
const transactionHttp = new TransactionHttp(nodeUrl);
const signedCosignatureTransaction = cosignAggregateBondedTransaction(signedAggregateBoundedTransaction, account);
transactionHttp.announceAggregateBondedCosignature(signedCosignatureTransaction);
// or we can use get the aggregateBondedTransaction from blockchain, cosign and annouce it directly
const accountHttp = new AccountHttp(nodeUrl);
accountHttp
.aggregateBondedTransactions(account.publicAccount)
.pipe(
mergeMap((_) => _),
filter((_)=> _.transactionInfo.hash === signedAggregateBoundedTransaction.hash ),
filter((_) => !_.signedByAccount(account.publicAccount)),
map(signedAggregateBoundedTransaction => cosignAggregateBondedTransaction(signedAggregateBoundedTransaction, account)),
mergeMap(cosignatureSignedTransaction => transactionHttp.announceAggregateBondedCosignature(cosignatureSignedTransaction))
)
.subscribe(announcedTransaction => console.log(announcedTransaction),
err => console.error(err));
const privateKey = process.env.PRIVATE_KEY;
const account = Account.createFromPrivateKey(privateKey, NetworkType.TEST_NET);
const nodeUrl = 'http://bctestnet1.brimstone.xpxsirius.io:3000';
const transactionHttp = new TransactionHttp(nodeUrl);
const signedCosignatureTransaction = cosignAggregateBondedTransaction(signedAggregateBoundedTransaction, account);
transactionHttp.announceAggregateBondedCosignature(signedCosignatureTransaction);
// or we can use get the aggregateBondedTransaction from blockchain, cosign and annouce it directly
const accountHttp = new AccountHttp(nodeUrl);
accountHttp
.aggregateBondedTransactions(account.publicAccount)
.pipe(
mergeMap((_) => _),
filter((_)=> _.transactionInfo.hash === signedAggregateBoundedTransaction.hash ),
filter((_) => !_.signedByAccount(account.publicAccount)),
map(signedAggregateBoundedTransaction => cosignAggregateBondedTransaction(signedAggregateBoundedTransaction, account)),
mergeMap(cosignatureSignedTransaction => transactionHttp.announceAggregateBondedCosignature(cosignatureSignedTransaction))
)
.subscribe(announcedTransaction => console.log(announcedTransaction),
err => console.error(err));
// Replace with a private key
final String privateKey = "<privateKey>";
final Account account = Account.createFromPrivateKey(privateKey, NetworkType.TEST_NET);
final String nodeUrl = 'http://bctestnet1.brimstone.xpxsirius.io:3000';
final AccountHttp accountHttp = new AccountHttp(nodeUrl);
final TransactionHttp transactionHttp = new TransactionHttp(nodeUrl);
accountHttp.aggregateBondedTransactions(account.getPublicAccount())
.flatMapIterable(tx -> tx) // Transform transaction array to single transactions to process them
.filter(tx -> tx.getHash() == signedAggregateBoundedTransaction.getHash() )
.filter(tx -> !tx.signedByAccount(account.getPublicAccount()))
.map(tx -> {
final CosignatureTransaction cosignatureTransaction = CosignatureTransaction.create(tx);
final CosignatureSignedTransaction cosignatureSignedTransaction = account.signCosignatureTransaction(cosignatureTransaction);
return transactionHttp.announceAggregateBondedCosignature(cosignatureSignedTransaction).toFuture().get();
})
.toFuture()
.get();