Skip to content

Commit

Permalink
Make SignStakingResult an array to allow signing and returning multip…
Browse files Browse the repository at this point in the history
…le transactions

This is used for type = UPDATE_STAKER, which should also reactivate all stake.

In a future Albatross update, this functionality will be added to the update_staker transaction type, so again only one tx is required to be signed and returned.
  • Loading branch information
sisou committed Aug 10, 2023
1 parent ca6e3cd commit 479c45c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
4 changes: 2 additions & 2 deletions client/src/PublicRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,10 +512,10 @@ export type ListLegacyResult = LegacyKeyInfoObject[];
export type SignTransactionResult = SignatureResult & {
serializedTx: Uint8Array,
};
export type SignStakingResult = SignatureResult & {
export type SignStakingResult = Array<SignatureResult & {
data: Uint8Array,
serializedTx: Uint8Array,
};
}>;
export type SimpleResult = { success: boolean };
export type SignedBitcoinTransaction = {
transactionHash: string,
Expand Down
41 changes: 26 additions & 15 deletions src/request/sign-staking/SignStaking.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,70 +155,81 @@ class SignStaking {
const privateKey = Albatross.PrivateKey.unserialize(powPrivateKey.serialize());
const keyPair = Albatross.KeyPair.derive(privateKey);

/** @type {Albatross.Transaction} */
let tx;
/** @type {Albatross.Transaction[]} */
const txs = [];

switch (request.type) {
case SignStakingApi.IncomingStakingType.CREATE_STAKER:
tx = Albatross.TransactionBuilder.newCreateStaker(
txs.push(Albatross.TransactionBuilder.newCreateStaker(
keyPair.toAddress(),
Albatross.Address.fromString(/** @type {Nimiq.Address} */ (request.delegation).toHex()),
BigInt(request.transaction.value),
BigInt(request.transaction.fee),
request.transaction.validityStartHeight,
request.transaction.networkId,
);
));
break;
case SignStakingApi.IncomingStakingType.ADD_STAKE:
tx = Albatross.TransactionBuilder.newStake(
txs.push(Albatross.TransactionBuilder.newStake(
keyPair.toAddress(),
keyPair.toAddress(),
BigInt(request.transaction.value),
BigInt(request.transaction.fee),
request.transaction.validityStartHeight,
request.transaction.networkId,
);
));
break;
case SignStakingApi.IncomingStakingType.UPDATE_STAKER:
tx = Albatross.TransactionBuilder.newUpdateStaker(
txs.push(Albatross.TransactionBuilder.newUpdateStaker(
keyPair.toAddress(),
Albatross.Address.fromString(/** @type {Nimiq.Address} */ (request.delegation).toHex()),
BigInt(request.transaction.fee),
request.transaction.validityStartHeight,
request.transaction.networkId,
);
));
if (request.newInactiveBalance !== undefined) {
txs.push(Albatross.TransactionBuilder.newSetInactiveStake(
keyPair.toAddress(),
BigInt(request.newInactiveBalance),
BigInt(request.transaction.fee),
request.transaction.validityStartHeight,
request.transaction.networkId,
));
}
break;
case SignStakingApi.IncomingStakingType.SET_INACTIVE_STAKE:
tx = Albatross.TransactionBuilder.newSetInactiveStake(
txs.push(Albatross.TransactionBuilder.newSetInactiveStake(
keyPair.toAddress(),
BigInt(request.newInactiveBalance),
BigInt(request.transaction.fee),
request.transaction.validityStartHeight,
request.transaction.networkId,
);
));
break;
case SignStakingApi.IncomingStakingType.UNSTAKE:
tx = Albatross.TransactionBuilder.newUnstake(
txs.push(Albatross.TransactionBuilder.newUnstake(
keyPair.toAddress(),
BigInt(request.transaction.value),
BigInt(request.transaction.fee),
request.transaction.validityStartHeight,
request.transaction.networkId,
);
));
break;
default:
throw new Errors.KeyguardError('Unreachable');
}

tx = tx.sign(keyPair);
await Promise.all(txs.map(async (tx, i, arr) => {
arr[i] = await tx.sign(keyPair);
}));

/** @type {KeyguardRequest.SignStakingResult} */
const result = {
const result = txs.map((tx) => ({
publicKey: keyPair.publicKey.serialize(),
signature: tx.proof.subarray(tx.proof.length - 64),
data: request.transaction.data,
serializedTx: tx.serialize(),
};
}));
resolve(result);
}

Expand Down
5 changes: 5 additions & 0 deletions src/request/sign-staking/SignStakingApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class SignStakingApi extends TopLevelApi {
case SignStakingApi.IncomingStakingType.CREATE_STAKER:
case SignStakingApi.IncomingStakingType.UPDATE_STAKER: {
parsedRequest.delegation = this.parseAddress(request.delegation, 'delegation');
parsedRequest.newInactiveBalance = this.parseNonNegativeFiniteNumber(
request.newInactiveBalance,
true,
'newInactiveBalance',
);
const data = new Nimiq.SerialBuffer(
1 // Data type
+ 1 // Option<> indicator
Expand Down

0 comments on commit 479c45c

Please sign in to comment.