Skip to content
Open
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
35 changes: 33 additions & 2 deletions packages/xahau/src/models/common/xahau.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import { Amount } from '.'

/**
* Enum representing values for Hook Flags for SetHook Transaction.
*
* @category Transaction Flags
*/
export enum HookFlags {
/**
*/
hsfOverride = 0x00000001,
/**
*/
hsfNSDelete = 0x0000002,
/**
*/
hsfCollect = 0x00000004,
}

export interface HookFlagsInterface {
hsfOverride?: boolean
hsfNSDelete?: boolean
hsfCollect?: boolean
}

export interface AmountEntry {
AmountEntry: { Amount: Amount }
}
Expand Down Expand Up @@ -58,7 +81,7 @@ export interface Hook {
/**
* The flags that are set on the hook.
*/
Flags?: number
Flags?: number | HookFlagsInterface
/**
* The transactions that triggers the hook. Represented as a 256Hash
*/
Expand Down Expand Up @@ -94,6 +117,14 @@ export interface EmitDetails {
sfEmitCallback?: string
}

export enum MintURITokenFlags {
tfBurnable = 0x00000001,
}

export interface MintURITokenFlagsInterface {
tfBurnable?: boolean
}

/**
* The object that describes the uritoken in MintURIToken.
*/
Expand All @@ -109,5 +140,5 @@ export interface MintURIToken {
/**
* The flags that are set on the uritoken.
*/
Flags?: number
Flags?: number | MintURITokenFlagsInterface
}
2 changes: 1 addition & 1 deletion packages/xahau/src/models/transactions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export {
export { PaymentChannelCreate } from './paymentChannelCreate'
export { PaymentChannelFund } from './paymentChannelFund'
export { Remit } from './remit'
export { SetHookFlagsInterface, SetHookFlags, SetHook } from './setHook'
export { SetHook } from './setHook'
export { SetFee, SetFeePreAmendment, SetFeePostAmendment } from './setFee'
export { SetRegularKey } from './setRegularKey'
export {
Expand Down
27 changes: 1 addition & 26 deletions packages/xahau/src/models/transactions/setHook.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,7 @@
import { ValidationError } from '../../errors'
import { Hook } from '../common/xahau'

import { BaseTransaction, GlobalFlags, validateBaseTransaction } from './common'

/**
* Enum representing values for Set Hook Transaction Flags.
*
* @category Transaction Flags
*/
export enum SetHookFlags {
/**
*/
hsfOverride = 0x00000001,
/**
*/
hsfNSDelete = 0x0000002,
/**
*/
hsfCollect = 0x00000004,
}

export interface SetHookFlagsInterface extends GlobalFlags {
hsfOverride?: boolean
hsfNSDelete?: boolean
hsfCollect?: boolean
}
import { BaseTransaction, validateBaseTransaction } from './common'

/**
*
Expand All @@ -37,8 +14,6 @@ export interface SetHook extends BaseTransaction {
*
*/
Hooks: Hook[]

Flags?: number | SetHookFlagsInterface
}

const MAX_HOOKS = 10
Expand Down
20 changes: 19 additions & 1 deletion packages/xahau/src/models/utils/flags.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-param-reassign -- param reassign is safe */
/* eslint-disable no-bitwise -- flags require bitwise operations */
import { ValidationError } from '../../errors'
import { Hook } from '../common/xahau'
import { Hook, HookFlags, MintURITokenFlags } from '../common/xahau'
import {
AccountRootFlagsInterface,
AccountRootFlags,
Expand Down Expand Up @@ -60,6 +60,23 @@ const txToFlag = {
* @param tx - A transaction to set its flags to its numeric representation.
*/
export function setTransactionFlagsToNumber(tx: Transaction): void {
if (tx.TransactionType === 'SetHook' && Array.isArray(tx.Hooks)) {
tx.Hooks.forEach((hook: Hook) => {
if (typeof hook.Hook.Flags === 'object') {
hook.Hook.Flags = convertFlagsToNumber(hook.Hook.Flags, HookFlags)
}
})
}

if (tx.TransactionType === 'Remit') {
if (tx.MintURIToken != null && typeof tx.MintURIToken.Flags === 'object') {
tx.MintURIToken.Flags = convertFlagsToNumber(
tx.MintURIToken.Flags,
MintURITokenFlags,
)
}
}

if (tx.Flags == null) {
tx.Flags = 0
return
Expand All @@ -68,6 +85,7 @@ export function setTransactionFlagsToNumber(tx: Transaction): void {
return
}


if (tx.TransactionType === 'SetHook') {
tx.Flags = convertFlagsToNumber(tx.Flags, SetHookFlags)
tx.Hooks.forEach((hook: Hook) => {
Expand Down
3 changes: 3 additions & 0 deletions packages/xahau/test/integration/transactions/remit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ describe('Remit', function () {
Destination: wallet2.classicAddress,
MintURIToken: {
URI: stringToHex('https://example.com'),
Flags: {
tfBurnable: true,
},
},
}

Expand Down
6 changes: 4 additions & 2 deletions packages/xahau/test/models/setHook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { validateSetHook } from '../../src/models/transactions/setHook'
* Providing runtime verification testing for each specific transaction type.
*/
describe('SetHook', function () {
let setHookTx
let setHookTx: any

beforeEach(function () {
setHookTx = {
Expand All @@ -24,7 +24,9 @@ describe('SetHook', function () {
'0061736D01000000011C0460057F7F7F7F7F017E60037F7F7E017E60027F7F017F60017F017E02230303656E76057472616365000003656E7606616363657074000103656E76025F670002030201030503010002062B077F0141B088040B7F004180080B7F0041A6080B7F004180080B7F0041B088040B7F0041000B7F0041010B07080104686F6F6B00030AC4800001C0800001017F230041106B220124002001200036020C41920841134180084112410010001A410022002000420010011A41012200200010021A200141106A240042000B0B2C01004180080B254163636570742E633A2043616C6C65642E00224163636570742E633A2043616C6C65642E22',
HookOn:
'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBFFFF7',
Flags: 1,
Flags: {
hsfOverride: true,
},
HookApiVersion: 0,
HookNamespace:
'4FF9961269BF7630D32E15276569C94470174A5DA79FA567C0F62251AA9A36B9',
Expand Down
46 changes: 46 additions & 0 deletions packages/xahau/test/models/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import {
PaymentFlags,
TrustSet,
TrustSetFlags,
Remit,
convertStringToHex,
SetHook,
} from '../../src'
import { HookFlags, MintURITokenFlags } from '../../src/models/common/xahau'
import { AccountRootFlags } from '../../src/models/ledger'
import { isFlagEnabled } from '../../src/models/utils'
import {
Expand Down Expand Up @@ -141,6 +145,48 @@ describe('Models Utils', function () {
assert.strictEqual(tx.Flags, expected)
})

it('sets MintURITokenFlags in Remit Transaction to its numeric value', function () {
const tx: Remit = {
TransactionType: 'Remit',
Account: 'rUn84CUYbNjRoTQ6mSW7BVJPSVJNLb1QLo',
Destination: 'rcXY84C4g14iFp6taFXjjQGVeHqSCh9RX',
MintURIToken: {
URI: convertStringToHex('https://xahau.network'),
Flags: {
tfBurnable: true,
},
},
}

setTransactionFlagsToNumber(tx)
const expected = MintURITokenFlags.tfBurnable
assert.strictEqual(tx.MintURIToken?.Flags, expected)
})

it('sets HookFlags in SetHook Transaction to its numeric value', function () {
const tx: SetHook = {
TransactionType: 'SetHook',
Account: 'rUn84CUYbNjRoTQ6mSW7BVJPSVJNLb1QLo',
Hooks: [
{
Hook: {
// invalid flags but for testing purposes
Flags: {
hsfCollect: true,
hsfNSDelete: true,
hsfOverride: true,
},
},
},
],
}

setTransactionFlagsToNumber(tx)
const expected =
HookFlags.hsfCollect | HookFlags.hsfNSDelete | HookFlags.hsfOverride
assert.strictEqual(tx.Hooks[0].Hook.Flags, expected)
})

it('sets other transaction types flags to its numeric value', function () {
const tx: DepositPreauth = {
TransactionType: 'DepositPreauth',
Expand Down
Loading