Skip to content

Commit

Permalink
Add support for lockup txn construction in deso-js
Browse files Browse the repository at this point in the history
  • Loading branch information
lazynina committed Jan 29, 2024
1 parent 0ac4de9 commit 635e34b
Show file tree
Hide file tree
Showing 4 changed files with 419 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/identity/crypto-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ export const uint64ToBufBigEndian = (uint: number) => {
return new Uint8Array(result.reverse());
};

export const varint64ToBuf = (int: number) => {
let ux = BigInt(int) << BigInt(1);
if (int < 0) {
ux = ~ux;
}
return uvarint64ToBuf(Number(ux));
};

export const bufToVarint64 = (buffer: Uint8Array): [number, Uint8Array] => {
const [ux, n] = bufToUvarint64(buffer);
let x = BigInt(ux) >> BigInt(1);
if (ux & 1) {
x = ~x;
}
return [Number(x), n];
};

interface Base58CheckOptions {
network: Network;
}
Expand Down
60 changes: 60 additions & 0 deletions src/identity/transaction-transcoders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
instanceToType,
VarBufferArray,
BoolOptional,
Varint64,
} from './transcoders.js';
export class TransactionInput extends BinaryRecord {
@Transcode(FixedBuffer(32))
Expand Down Expand Up @@ -662,6 +663,61 @@ export class TransactionMetadataUnlockStake extends BinaryRecord {

export class TransactionMetadataUnjailValidator extends BinaryRecord {}

export class TransactionMetadataCoinLockup extends BinaryRecord {
@Transcode(VarBuffer)
profilePublicKey: Uint8Array = new Uint8Array(0);

@Transcode(VarBuffer)
recipientPublicKey: Uint8Array = new Uint8Array(0);

@Transcode(Varint64)
unlockTimestampNanoSecs = 0;

@Transcode(Varint64)
vestingEndTimestampNanoSecs = 0;

// TODO: We may want a better way to handle uint256s.
@Transcode(BoolOptional(VarBuffer))
lockupAmountBaseUnits: Uint8Array = new Uint8Array(0);
}

export class TransactionMetadataUpdateCoinLockupParams extends BinaryRecord {
@Transcode(Varint64)
lockupYieldDurationNanoSecs = 0;

@Transcode(Uvarint64)
lockupYieldAPYBasisPoints = 0;

@Transcode(Boolean)
removeYieldCurvePoint = false;

@Transcode(Boolean)
newLockupTransferRestrictions = false;

@Transcode(Uint8)
lockupTransferRestrictionStatus = 0;
}

export class TransactionMetadataCoinLockupTransfer extends BinaryRecord {
@Transcode(VarBuffer)
recipientPublicKey: Uint8Array = new Uint8Array(0);

@Transcode(VarBuffer)
profilePublicKey: Uint8Array = new Uint8Array(0);

@Transcode(Varint64)
unlockTimestampNanoSecs = 0;

// TODO: We may want a better way to handle uint256s.
@Transcode(BoolOptional(VarBuffer))
lockedCoinsToTransferBaseUnits: Uint8Array = new Uint8Array(0);
}

export class TransactionMetadataCoinUnlock extends BinaryRecord {
@Transcode(VarBuffer)
profilePublicKey: Uint8Array = new Uint8Array(0);
}

export const TransactionTypeMetadataMap = {
1: TransactionMetadataBlockReward,
2: TransactionMetadataBasicTransfer,
Expand Down Expand Up @@ -743,6 +799,10 @@ export const TransactionTypeToStringMap: { [k: number]: string } = {
37: TransactionType.Unstake,
38: TransactionType.UnlockStake,
39: TransactionType.UnjailValidator,
40: TransactionType.CoinLockup,
41: TransactionType.UpdateCoinLockupParams,
42: TransactionType.CoinLockupTransfer,
43: TransactionType.CoinUnlock,
};

export class Transaction extends BinaryRecord {
Expand Down
7 changes: 7 additions & 0 deletions src/identity/transcoders.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'reflect-metadata';
import {
bufToUvarint64,
bufToVarint64,
concatUint8Arrays,
uvarint64ToBuf,
varint64ToBuf,
} from './crypto-utils.js';
import { TransactionNonce } from './transaction-transcoders.js';
export class BinaryRecord {
Expand Down Expand Up @@ -69,6 +71,11 @@ export const Uvarint64: Transcoder<number> = {
write: (uint) => uvarint64ToBuf(uint),
};

export const Varint64: Transcoder<number> = {
read: (bytes) => bufToVarint64(bytes),
write: (int) => varint64ToBuf(int),
};

export const Boolean: Transcoder<boolean> = {
read: (bytes) => [bytes.at(0) != 0, bytes.slice(1)],
write: (bool) => {
Expand Down
Loading

0 comments on commit 635e34b

Please sign in to comment.