From a7740583f732d1f305d1815abaa169058e544e3c Mon Sep 17 00:00:00 2001 From: Lazy Nina <> Date: Fri, 5 Jan 2024 15:17:51 -0500 Subject: [PATCH] Add transcoders for pos txn types --- src/backend-types/deso-types-custom.ts | 10 ++++ src/identity/transaction-transcoders.ts | 74 +++++++++++++++++++++++++ src/identity/transcoders.ts | 27 +++++++++ 3 files changed, 111 insertions(+) diff --git a/src/backend-types/deso-types-custom.ts b/src/backend-types/deso-types-custom.ts index 32e4366..bd1204b 100644 --- a/src/backend-types/deso-types-custom.ts +++ b/src/backend-types/deso-types-custom.ts @@ -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 { diff --git a/src/identity/transaction-transcoders.ts b/src/identity/transaction-transcoders.ts index 7141081..ba8b0b0 100644 --- a/src/identity/transaction-transcoders.ts +++ b/src/identity/transaction-transcoders.ts @@ -21,6 +21,7 @@ import { Uvarint64, VarBuffer, instanceToType, + VarBufferArray, } from './transcoders.js'; export class TransactionInput extends BinaryRecord { @Transcode(FixedBuffer(32)) @@ -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, @@ -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 } = { @@ -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 { diff --git a/src/identity/transcoders.ts b/src/identity/transcoders.ts index 032aa5f..038caf7 100644 --- a/src/identity/transcoders.ts +++ b/src/identity/transcoders.ts @@ -95,6 +95,33 @@ export const VarBuffer: Transcoder = { }, write: (bytes) => concatUint8Arrays([uvarint64ToBuf(bytes.length), bytes]), }; + +export const VarBufferArray: Transcoder = { + 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 = { read: (bytes) => { return TransactionNonce.fromBytes(bytes) as [TransactionNonce, Uint8Array];