Skip to content

Commit

Permalink
Merge pull request #109 from virtualeconomy/feat/add-getUnit-in-sysCtrt
Browse files Browse the repository at this point in the history
feat: add getUnit in sysCtrt
  • Loading branch information
Meiyunoda authored Feb 5, 2024
2 parents e30c215 + e8fd3c2 commit 0f8a723
Showing 1 changed file with 34 additions and 25 deletions.
59 changes: 34 additions & 25 deletions src/contract/sys_ctrt.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import * as acnt from '../account.js';
import * as md from '../model.js';
import * as tx from '../tx_req.js';
import * as de from '../data_entry.js';
import base58 from "bs58";
import { Buffer } from "buffer";
import base58 from 'bs58';
import { Buffer } from 'buffer';
import { blake2b } from 'blakejs';
import * as msacnt from '../multisign_account.js';

Expand Down Expand Up @@ -81,6 +81,14 @@ export class SysCtrt extends ctrt.BaseTokCtrt {
return md.VSYS.UNIT;
}

/**
* getUnit returns the unit of tokens defined in System Contract(VSYS coins)
* @returns {number} The unit.
*/
async getUnit() {
return md.VSYS.UNIT;
}

/**
* send sends VSYS coins to another account.
* @param {acnt.Account | msacnt.MultiSignAccount} by - The action taker.
Expand Down Expand Up @@ -224,7 +232,7 @@ export class SysCtrt extends ctrt.BaseTokCtrt {
* getInt64Bits converts Big Integer into 8-byte array
* @param {BigInt} x - integer to be converted
* @returns {Buffer} 8 Byte array
*/
*/
getInt64Bits(x) {
const bytes = Buffer.alloc(8);
bytes.writeBigInt64BE(x);
Expand All @@ -235,7 +243,7 @@ export class SysCtrt extends ctrt.BaseTokCtrt {
* getInt16Bits converts Short Integer into 2-byte array
* @param {number} x - integer to be converted
* @returns {Buffer} 2 Byte array
*/
*/
getInt16Bits(x) {
const bytes = Buffer.alloc(2);
bytes.writeInt16BE(x);
Expand All @@ -252,29 +260,30 @@ export class SysCtrt extends ctrt.BaseTokCtrt {
* @param {string} attachment - encoded attachment from transaction info
* @returns {string} generated transaction ID
*/
generateTxID(
timestamp,
amount,
fee,
feeScale,
recipient,
attachment
) {
const timestampBytes = this.getInt64Bits(BigInt(timestamp.toString()))
const amountBytes = this.getInt64Bits(BigInt(amount.toString()))
const feeBytes = this.getInt64Bits(BigInt(fee.toString()))
const feeScaleBytes = this.getInt16Bits(feeScale)
const recipientBytesArr = base58.decode(recipient)
generateTxID(timestamp, amount, fee, feeScale, recipient, attachment) {
const timestampBytes = this.getInt64Bits(BigInt(timestamp.toString()));
const amountBytes = this.getInt64Bits(BigInt(amount.toString()));
const feeBytes = this.getInt64Bits(BigInt(fee.toString()));
const feeScaleBytes = this.getInt16Bits(feeScale);
const recipientBytesArr = base58.decode(recipient);

const attachmentBytes = base58.decode(attachment);
const lenBytes = this.getInt16Bits(attachmentBytes.length);

const attachmentBytes = base58.decode(attachment)
const lenBytes = this.getInt16Bits(attachmentBytes.length)

const toSign = Buffer.concat([Uint8Array.from([2]), timestampBytes, amountBytes, feeBytes, feeScaleBytes, recipientBytesArr, lenBytes, attachmentBytes])
const toSign = Buffer.concat([
Uint8Array.from([2]),
timestampBytes,
amountBytes,
feeBytes,
feeScaleBytes,
recipientBytesArr,
lenBytes,
attachmentBytes,
]);

const txIDHashed = blake2b(toSign, undefined, 32)
const txIDStr = base58.encode(txIDHashed)
const txIDHashed = blake2b(toSign, undefined, 32);
const txIDStr = base58.encode(txIDHashed);

return txIDStr
return txIDStr;
}

}

0 comments on commit 0f8a723

Please sign in to comment.