Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transcoders for pos txn types #28

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/backend-types/deso-types-custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,16 @@ export enum TransactionType {
AccessGroup = 'ACCESS_GROUP',
AccessGroupMembers = 'ACCESS_GROUP_MEMBERS',
NewMessage = 'NEW_MESSAGE',
RegisterAsValidator = 'REGISTER_AS_VALIDATOR',
UnregisterAsValidator = 'UNREGISTER_AS_VALIDATOR',
Stake = 'STAKE',
Unstake = 'UNSTAKE',
UnlockStake = 'UNLOCK_STAKE',
UnjailValidator = 'UNJAIL_VALIDATOR',
CoinLockup = 'COIN_LOCKUP',
UpdateCoinLockupParams = 'UPDATE_COIN_LOCKUP_PARAMS',
CoinLockupTransfer = 'COIN_LOCKUP_TRANSFER',
CoinUnlock = 'COIN_UNLOCK',
}

export interface IdentityDeriveParams {
Expand Down
74 changes: 74 additions & 0 deletions src/identity/transaction-transcoders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
Uvarint64,
VarBuffer,
instanceToType,
VarBufferArray,
} from './transcoders.js';
export class TransactionInput extends BinaryRecord {
@Transcode(FixedBuffer(32))
Expand Down Expand Up @@ -599,6 +600,67 @@ export class TransactionMetadataNewMessage extends BinaryRecord {
newMessageOperation = 0;
}

export class TransactionMetadataRegisterAsValidator extends BinaryRecord {
@Transcode(VarBufferArray)
domains: Buffer[] = [];

@Transcode(Boolean)
disableDelegatedStake = false;

@Transcode(Uvarint64)
delegatedStakeCommissionBasisPoints = 0;

// TODO: Technically this is a bls public key,
// but under the hood it's really just a byte array.
// The challenge is converting this into something human
// readable in the UI.
@Transcode(VarBuffer)
votingPublicKey: Buffer = Buffer.alloc(0);

// TODO: Technically this is a bls signature,
// but under the hood it's really just a byte array.
// The challenge is converting this into something human
// readable in the UI.
@Transcode(VarBuffer)
votingAuthorization: Buffer = Buffer.alloc(0);
}

export class TransactionMetadataUnregisterAsValidator extends BinaryRecord {}

export class TransactionMetadataStake extends BinaryRecord {
@Transcode(VarBuffer)
validatorPublicKey: Buffer = Buffer.alloc(0);

@Transcode(Uint8)
rewardMethod = 0;

// TODO: We may want a better way to handle uint256s.
@Transcode(Optional(VarBuffer))
stakeAmountNanos: Buffer = Buffer.alloc(0);
}

export class TransactionMetadataUnstake extends BinaryRecord {
@Transcode(VarBuffer)
validatorPublicKey: Buffer = Buffer.alloc(0);

// TODO: We may want a better way to handle uint256s.
@Transcode(Optional(VarBuffer))
unstakeAmountNanos: Buffer = Buffer.alloc(0);
}

export class TransactionMetadataUnlockStake extends BinaryRecord {
@Transcode(VarBuffer)
validatorPublicKey: Buffer = Buffer.alloc(0);

@Transcode(Uvarint64)
startEpochNumber = 0;

@Transcode(Uvarint64)
endEpochNumber = 0;
}

export class TransactionMetadataUnjailValidator extends BinaryRecord {}

export const TransactionTypeMetadataMap = {
1: TransactionMetadataBlockReward,
2: TransactionMetadataBasicTransfer,
Expand Down Expand Up @@ -632,6 +694,12 @@ export const TransactionTypeMetadataMap = {
31: TransactionMetadataAccessGroup,
32: TransactionMetadataAccessGroupMembers,
33: TransactionMetadataNewMessage,
34: TransactionMetadataRegisterAsValidator,
35: TransactionMetadataUnregisterAsValidator,
36: TransactionMetadataStake,
37: TransactionMetadataUnstake,
38: TransactionMetadataUnlockStake,
39: TransactionMetadataUnjailValidator,
};

export const TransactionTypeToStringMap: { [k: number]: string } = {
Expand Down Expand Up @@ -668,6 +736,12 @@ export const TransactionTypeToStringMap: { [k: number]: string } = {
31: TransactionType.AccessGroup,
32: TransactionType.AccessGroupMembers,
33: TransactionType.NewMessage,
34: TransactionType.RegisterAsValidator,
35: TransactionType.UnregisterAsValidator,
36: TransactionType.Stake,
37: TransactionType.Unstake,
38: TransactionType.UnlockStake,
39: TransactionType.UnjailValidator,
};

export class Transaction extends BinaryRecord {
Expand Down
27 changes: 27 additions & 0 deletions src/identity/transcoders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,33 @@ export const VarBuffer: Transcoder<Uint8Array> = {
},
write: (bytes) => concatUint8Arrays([uvarint64ToBuf(bytes.length), bytes]),
};

export const VarBufferArray: Transcoder<Uint8Array[]> = {
read: (bytes) => {
const countAndBuffer = bufToUvarint64(bytes);
const count = countAndBuffer[0];
let buffer = countAndBuffer[1];
const result = [];
for (let i = 0; i < count; i++) {
let size;
[size, buffer] = bufToUvarint64(buffer);
result.push(buffer.slice(0, size));
buffer = buffer.slice(size);
}

return [result, buffer];
},
write: (buffers) => {
const count = uvarint64ToBuf(buffers.length);
return concatUint8Arrays([
count,
...buffers.map((buffer) =>
concatUint8Arrays([uvarint64ToBuf(buffer.length), buffer])
),
]);
},
};

export const TransactionNonceTranscoder: Transcoder<TransactionNonce | null> = {
read: (bytes) => {
return TransactionNonce.fromBytes(bytes) as [TransactionNonce, Uint8Array];
Expand Down
Loading