-
Notifications
You must be signed in to change notification settings - Fork 8
Aggregate complex example
Chun Lam edited this page Feb 21, 2024
·
3 revisions
import { Network } from '../utils';
import { Account, NetworkType, MosaicId, NamespaceId, UInt64, Mosaic, AggregateTransaction,
Deadline, TransferTransaction, PlainMessage, AggregateTransactionCosignature,
HashLockTransaction, CosignatureTransaction, CosignatureSignedTransaction
} from 'tsjs-xpx-chain-sdk';
const NETWORK_TYPE = NetworkType.MIJIN_TEST;
const network = new Network('http://localhost:3000');
const seed = Account.createFromPrivateKey('28FCECEA252231D2C86E1BCF7DD541552BDBBEFBB09324758B3AC199B4AA7B78', NETWORK_TYPE, 1);
const alice = Account.createFromPrivateKey('0123456789012345678901234567890123456789012345678901234567890124', NETWORK_TYPE, 1);
const bob = Account.createFromPrivateKey('ABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCD', NETWORK_TYPE, 1);
const hundredXpx = new Mosaic(new NamespaceId("prx.xpx"), UInt64.fromUint(100 * 1000000));
const tenXpx = new Mosaic(new NamespaceId("prx.xpx"), UInt64.fromUint(10 * 1000000));
const seedToAlice = TransferTransaction.create(
Deadline.create(),
alice.address,
[hundredXpx],
PlainMessage.create('Good luck!'),
NETWORK_TYPE
);
const seedToBob = TransferTransaction.create(
Deadline.create(),
bob.address,
[hundredXpx],
PlainMessage.create('Good luck!'),
NETWORK_TYPE
);
const aliceToBob = TransferTransaction.create(
Deadline.create(),
bob.address,
[],
PlainMessage.create('hello from alice to bob'),
NETWORK_TYPE
);
const bobToAlice = TransferTransaction.create(
Deadline.create(),
alice.address,
[],
PlainMessage.create('hello from bob to alice'),
NETWORK_TYPE
);
const aggregateComplete = AggregateTransaction.createCompleteV1(
Deadline.create(),
[
seedToAlice.toAggregateV1(seed.publicAccount),
seedToBob.toAggregateV1(seed.publicAccount)
],
NETWORK_TYPE,
[]
)
network.networkProperties.then(networkProperties => {
// only one account is required to sign the aggregated transaction
const signedAggregateComplete = seed.signTransactionWithCosignatoriesV1(aggregateComplete, [], networkProperties.generationHash);
network.announceAndWaitForConfirmation(signedAggregateComplete).then(_ => {
console.log('Alice and Bob seeded.');
const aggregateBonded = AggregateTransaction.createBondedV1(
Deadline.create(),
[
aliceToBob.toAggregateV1(alice.publicAccount),
bobToAlice.toAggregateV1(bob.publicAccount)
],
NETWORK_TYPE,
[]
);
// both alice and bob are required to sign the aggregate transaction;
/* alice - signs the aggregateBonded and announces it
- has to broadcast hashlock transaction first, which serves as an anti-spam feature; the funds are
a) either returned to the alice if the aggregate bonded is signed by all cosigners and confirmed
b) or not returned to the alice and collected by harvesters if the hashlock expires
bob - listens for partially signed transactions where bob is required as a cosigner, then cosigns it and announces
the cosignature
*/
const signedAggregateBonded = alice.signTransactionWithCosignatoriesV1(aggregateBonded, [], networkProperties.generationHash);
const hashLock = HashLockTransaction.create(
Deadline.create(),
tenXpx,
UInt64.fromUint(100), // number of block before this HashLockTransaction expires
signedAggregateBonded,
NETWORK_TYPE
);
const signedHashLock = alice.preV2Sign(hashLock, networkProperties.generationHash);
network.announceAndWaitForConfirmation(signedHashLock).then(tx => {
console.log('Alice announced the hash lock transaction.');
// setup Bob; this code usually runs on another client/machine
const bobSub = network.api.listener.aggregateBondedAdded(bob.address).subscribe(aggregateTx => {
if (! aggregateTx.signedByAccount(bob.publicAccount)) {
bobSub.unsubscribe();
const cosignatureSignedTx = bob.preV2SignCosignatureTransaction(CosignatureTransaction.create(aggregateTx));
network.api.transaction.announceAggregateBondedCosignature(cosignatureSignedTx).subscribe(resp => {
console.log('Bob announced the signed cosignature.');
},
error => {
console.error(error);
},
() => {
// console.log('Announced.');
});
}
});
// this gets confirmed only after Bob successfully announces the signed cosignature
network.announceAndWaitForConfirmation(signedAggregateBonded, true).then(tx => {
console.log('Alice and Bob greeted each other in single aggregate bonded transaction.');
console.log(tx);
});
});
});
});