mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-21 04:35:49 +00:00
refactor: define typescript type for AccountSet transaction (#1515)
* define typescript type for AccountSet transaction
This commit is contained in:
committed by
Mayukha Vadari
parent
930d214107
commit
72f34d9388
69
src/models/transactions/accountSet.ts
Normal file
69
src/models/transactions/accountSet.ts
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
import { ValidationError } from "../../common/errors"
|
||||||
|
import { BaseTransaction, verifyBaseTransaction } from "./common"
|
||||||
|
|
||||||
|
enum AccountSetFlagEnum {
|
||||||
|
asfRequireDest = 1,
|
||||||
|
asfRequireAuth = 2,
|
||||||
|
asfDisallowXRP = 3,
|
||||||
|
asfDisableMaster = 4,
|
||||||
|
asfAccountTxnID = 5,
|
||||||
|
asfNoFreeze = 6,
|
||||||
|
asfGlobalFreeze = 7,
|
||||||
|
asfDefaultRipple = 8,
|
||||||
|
asfDepositAuth = 9,
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface AccountSet extends BaseTransaction {
|
||||||
|
TransactionType: "AccountSet"
|
||||||
|
ClearFlag?: number
|
||||||
|
Domain?: string
|
||||||
|
EmailHash?: string
|
||||||
|
MessageKey?: string
|
||||||
|
SetFlag?: AccountSetFlagEnum
|
||||||
|
TransferRate?: number
|
||||||
|
TickSize?: number
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verify the form and type of an AccountSet at runtime.
|
||||||
|
*
|
||||||
|
* @param tx - An AccountSet Transaction
|
||||||
|
* @returns - Void.
|
||||||
|
* @throws - When the AccountSet is Malformed.
|
||||||
|
*/
|
||||||
|
export function verifyAccountSet(tx: AccountSet): void {
|
||||||
|
verifyBaseTransaction(tx)
|
||||||
|
|
||||||
|
if (tx.ClearFlag !== undefined){
|
||||||
|
if (typeof tx.ClearFlag !== 'number')
|
||||||
|
throw new ValidationError("AccountSet: invalid ClearFlag")
|
||||||
|
if (!Object.values(AccountSetFlagEnum).includes(tx.ClearFlag))
|
||||||
|
throw new ValidationError("AccountSet: invalid ClearFlag")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tx.Domain !== undefined && typeof tx.Domain !== 'string')
|
||||||
|
throw new ValidationError("AccountSet: invalid Domain")
|
||||||
|
|
||||||
|
if (tx.EmailHash !== undefined && typeof tx.EmailHash !== 'string')
|
||||||
|
throw new ValidationError("AccountSet: invalid EmailHash")
|
||||||
|
|
||||||
|
if (tx.MessageKey !== undefined && typeof tx.MessageKey !== 'string')
|
||||||
|
throw new ValidationError("AccountSet: invalid MessageKey")
|
||||||
|
|
||||||
|
if (tx.SetFlag !== undefined){
|
||||||
|
if (typeof tx.SetFlag !== 'number')
|
||||||
|
throw new ValidationError("AccountSet: invalid SetFlag")
|
||||||
|
if (!Object.values(AccountSetFlagEnum).includes(tx.SetFlag))
|
||||||
|
throw new ValidationError("AccountSet: invalid SetFlag")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tx.TransferRate !== undefined && typeof tx.TransferRate !== 'number')
|
||||||
|
throw new ValidationError("AccountSet: invalid TransferRate")
|
||||||
|
|
||||||
|
if (tx.TickSize !== undefined){
|
||||||
|
if (typeof tx.TickSize !== 'number')
|
||||||
|
throw new ValidationError("AccountSet: invalid TickSize")
|
||||||
|
if (tx.TickSize !== 0 && (3 > tx.TickSize || tx.TickSize > 15))
|
||||||
|
throw new ValidationError("AccountSet: invalid TickSize")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
export * from './transaction'
|
export * from './transaction'
|
||||||
export * from './offerCreate'
|
export * from './offerCreate'
|
||||||
export * from './offerCancel'
|
export * from './accountSet'
|
||||||
export * from './checkCreate'
|
export * from './checkCreate'
|
||||||
export * from './checkCash'
|
export * from './checkCash'
|
||||||
export * from './checkCancel'
|
export * from './checkCancel'
|
||||||
export * from './signerListSet'
|
export * from './signerListSet'
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
import { ValidationError } from "../../common/errors"
|
|
||||||
import { BaseTransaction, verifyBaseTransaction } from "./common"
|
|
||||||
|
|
||||||
export interface OfferCancel extends BaseTransaction {
|
|
||||||
TransactionType: "OfferCancel"
|
|
||||||
OfferSequence: number
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Verify the form and type of an OfferCancel at runtime.
|
|
||||||
*
|
|
||||||
* @param tx - An OfferCancel Transaction
|
|
||||||
* @returns - Void.
|
|
||||||
* @throws - When the OfferCancel is Malformed.
|
|
||||||
*/
|
|
||||||
export function verifyOfferCancel(tx: OfferCancel): void {
|
|
||||||
verifyBaseTransaction(tx)
|
|
||||||
|
|
||||||
if (tx.OfferSequence === undefined)
|
|
||||||
throw new ValidationError("OfferCancel: missing field OfferSequence")
|
|
||||||
|
|
||||||
if (typeof tx.OfferSequence !== 'number')
|
|
||||||
throw new ValidationError("OfferCancel: invalid OfferSequence")
|
|
||||||
}
|
|
||||||
@@ -1,25 +1,21 @@
|
|||||||
import Metadata from "../common/metadata"
|
import Metadata from "../common/metadata";
|
||||||
import { OfferCreate } from "./offerCreate"
|
import { OfferCreate } from "./offerCreate";
|
||||||
import { OfferCancel } from "./offerCancel"
|
import { CheckCash } from "./checkCash";
|
||||||
import { CheckCash } from "./checkCash"
|
import { CheckCancel } from "./checkCancel";
|
||||||
import { CheckCancel } from "./checkCancel"
|
import { CheckCreate } from "./checkCreate";
|
||||||
import { CheckCreate } from "./checkCreate"
|
import { SignerListSet } from "./signerListSet";
|
||||||
import { SignerListSet } from "./signerListSet"
|
import { AccountSet } from "./accountSet";
|
||||||
|
|
||||||
export type Transaction =
|
export type Transaction =
|
||||||
// AccountSet
|
AccountSet
|
||||||
// | AccountDelete
|
// | AccountDelete
|
||||||
// | CheckCancel
|
| CheckCancel
|
||||||
// | CheckCash
|
|
||||||
// | CheckCreate
|
|
||||||
CheckCancel
|
|
||||||
| CheckCash
|
| CheckCash
|
||||||
| CheckCreate
|
| CheckCreate
|
||||||
// | DepositPreauth
|
// | DepositPreauth
|
||||||
// | EscrowCancel
|
// | EscrowCancel
|
||||||
// | EscrowCreate
|
// | EscrowCreate
|
||||||
// | EscrowFinish
|
// | EscrowFinish
|
||||||
| OfferCancel
|
|
||||||
// | OfferCancel
|
// | OfferCancel
|
||||||
| OfferCreate
|
| OfferCreate
|
||||||
// | PaymentTransaction
|
// | PaymentTransaction
|
||||||
|
|||||||
110
test/models/accountSet.ts
Normal file
110
test/models/accountSet.ts
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
import { ValidationError } from 'xrpl-local/common/errors'
|
||||||
|
import { verifyAccountSet } from './../../src/models/transactions/accountSet'
|
||||||
|
import { assert } from 'chai'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AccountSet Transaction Verification Testing
|
||||||
|
*
|
||||||
|
* Providing runtime verification testing for each specific transaction type
|
||||||
|
*/
|
||||||
|
describe('AccountSet Transaction Verification', function () {
|
||||||
|
|
||||||
|
let account
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
account = {
|
||||||
|
TransactionType : "AccountSet",
|
||||||
|
Account : "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn",
|
||||||
|
Fee : "12",
|
||||||
|
Sequence : 5,
|
||||||
|
Domain : "6578616D706C652E636F6D",
|
||||||
|
SetFlag : 5,
|
||||||
|
MessageKey : "03AB40A0490F9B7ED8DF29D246BF2D6269820A0EE7742ACDD457BEA7C7D0931EDB"
|
||||||
|
} as any
|
||||||
|
})
|
||||||
|
|
||||||
|
it (`verifies valid AccountSet`, () => {
|
||||||
|
assert.doesNotThrow(() => verifyAccountSet(account))
|
||||||
|
})
|
||||||
|
|
||||||
|
it (`throws w/ invalid SetFlag (out of range)`, () => {
|
||||||
|
account.SetFlag = 12
|
||||||
|
|
||||||
|
assert.throws(
|
||||||
|
() => verifyAccountSet(account),
|
||||||
|
ValidationError,
|
||||||
|
"AccountSet: invalid SetFlag"
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it (`throws w/ invalid SetFlag (incorrect type)`, () => {
|
||||||
|
account.SetFlag = 'abc'
|
||||||
|
|
||||||
|
assert.throws(
|
||||||
|
() => verifyAccountSet(account),
|
||||||
|
ValidationError,
|
||||||
|
"AccountSet: invalid SetFlag"
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it (`throws w/ invalid ClearFlag`, () => {
|
||||||
|
account.ClearFlag = 12
|
||||||
|
|
||||||
|
assert.throws(
|
||||||
|
() => verifyAccountSet(account),
|
||||||
|
ValidationError,
|
||||||
|
"AccountSet: invalid ClearFlag"
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it (`throws w/ invalid Domain`, () => {
|
||||||
|
account.Domain = 6578616
|
||||||
|
|
||||||
|
assert.throws(
|
||||||
|
() => verifyAccountSet(account),
|
||||||
|
ValidationError,
|
||||||
|
"AccountSet: invalid Domain"
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it (`throws w/ invalid EmailHash`, () => {
|
||||||
|
account.EmailHash = 657861645678909876543456789876543
|
||||||
|
|
||||||
|
assert.throws(
|
||||||
|
() => verifyAccountSet(account),
|
||||||
|
ValidationError,
|
||||||
|
"AccountSet: invalid EmailHash"
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it (`throws w/ invalid MessageKey`, () => {
|
||||||
|
account.MessageKey = 65786165678908765456789567890678
|
||||||
|
|
||||||
|
assert.throws(
|
||||||
|
() => verifyAccountSet(account),
|
||||||
|
ValidationError,
|
||||||
|
"AccountSet: invalid MessageKey"
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it (`throws w/ invalid TransferRate`, () => {
|
||||||
|
account.TransferRate = "1000000001"
|
||||||
|
|
||||||
|
assert.throws(
|
||||||
|
() => verifyAccountSet(account),
|
||||||
|
ValidationError,
|
||||||
|
"AccountSet: invalid TransferRate"
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it (`throws w/ invalid TickSize`, () => {
|
||||||
|
account.TickSize = 20
|
||||||
|
|
||||||
|
assert.throws(
|
||||||
|
() => verifyAccountSet(account),
|
||||||
|
ValidationError,
|
||||||
|
"AccountSet: invalid TickSize"
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
@@ -7,7 +7,7 @@ import { assert } from 'chai'
|
|||||||
*
|
*
|
||||||
* Providing runtime verification testing for each specific transaction type
|
* Providing runtime verification testing for each specific transaction type
|
||||||
*/
|
*/
|
||||||
describe('CheckCancel Transaction Verification', function () {
|
describe('Transaction Verification', function () {
|
||||||
|
|
||||||
it (`verifies valid CheckCancel`, () => {
|
it (`verifies valid CheckCancel`, () => {
|
||||||
const validCheckCancel = {
|
const validCheckCancel = {
|
||||||
|
|||||||
@@ -1,53 +0,0 @@
|
|||||||
import { ValidationError } from 'xrpl-local/common/errors'
|
|
||||||
import { verifyOfferCancel } from './../../src/models/transactions/offerCancel'
|
|
||||||
import { assert } from 'chai'
|
|
||||||
|
|
||||||
/**
|
|
||||||
* OfferCancel Transaction Verification Testing
|
|
||||||
*
|
|
||||||
* Providing runtime verification testing for each specific transaction type
|
|
||||||
*/
|
|
||||||
describe('OfferCancel Transaction Verification', function () {
|
|
||||||
let offer
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
offer = {
|
|
||||||
Account: "rnKiczmiQkZFiDES8THYyLA2pQohC5C6EF",
|
|
||||||
Fee: "10",
|
|
||||||
LastLedgerSequence: 65477334,
|
|
||||||
OfferSequence: 60797528,
|
|
||||||
Sequence: 60797535,
|
|
||||||
SigningPubKey: "0361BFD43D1EEA54B77CC152887312949EBF052997FBFFCDAF6F2653164B55B21...",
|
|
||||||
TransactionType: "OfferCancel",
|
|
||||||
TxnSignature: "30450221008C43BDCFC68B4793857CA47DF454C07E5B45D3F80E8E6785CAB9292...",
|
|
||||||
date: "2021-08-06T21:04:11Z"
|
|
||||||
} as any
|
|
||||||
})
|
|
||||||
|
|
||||||
it (`verifies valid OfferCancel`, () => {
|
|
||||||
assert.doesNotThrow(() => verifyOfferCancel(offer))
|
|
||||||
})
|
|
||||||
|
|
||||||
it (`verifies valid OfferCancel with flags`, () => {
|
|
||||||
offer.Flags = 2147483648
|
|
||||||
assert.doesNotThrow(() => verifyOfferCancel(offer))
|
|
||||||
})
|
|
||||||
|
|
||||||
it (`throws w/ invalid OfferSequence`, () => {
|
|
||||||
offer.OfferSequence = '99'
|
|
||||||
assert.throws(
|
|
||||||
() => verifyOfferCancel(offer),
|
|
||||||
ValidationError,
|
|
||||||
"OfferCancel: invalid OfferSequence"
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it (`throws w/ missing OfferSequence`, () => {
|
|
||||||
delete offer.OfferSequence
|
|
||||||
assert.throws(
|
|
||||||
() => verifyOfferCancel(offer),
|
|
||||||
ValidationError,
|
|
||||||
"OfferCancel: missing field OfferSequence"
|
|
||||||
)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -8,9 +8,10 @@ import { assert } from 'chai'
|
|||||||
* Providing runtime verification testing for each specific transaction type
|
* Providing runtime verification testing for each specific transaction type
|
||||||
*/
|
*/
|
||||||
describe('SignerListSet Transaction Verification', function () {
|
describe('SignerListSet Transaction Verification', function () {
|
||||||
|
let SignerListSetTx
|
||||||
it (`verifies valid SignerListSet`, () => {
|
|
||||||
const validSignerListSet = {
|
beforeEach(() => {
|
||||||
|
SignerListSetTx = {
|
||||||
Flags: 0,
|
Flags: 0,
|
||||||
TransactionType: "SignerListSet",
|
TransactionType: "SignerListSet",
|
||||||
Account: "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn",
|
Account: "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn",
|
||||||
@@ -37,72 +38,38 @@ describe('SignerListSet Transaction Verification', function () {
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
} as any
|
} as any
|
||||||
|
})
|
||||||
assert.doesNotThrow(() => verifySignerListSet(validSignerListSet))
|
|
||||||
|
it (`verifies valid SignerListSet`, () => {
|
||||||
|
assert.doesNotThrow(() => verifySignerListSet(SignerListSetTx))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
it (`throws w/ missing SignerQuorum`, () => {
|
it (`throws w/ missing SignerQuorum`, () => {
|
||||||
const invalidSignerQuorum = {
|
SignerListSetTx.SignerQuorum = undefined
|
||||||
Flags: 0,
|
|
||||||
TransactionType: "SignerListSet",
|
|
||||||
Account: "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn",
|
|
||||||
SignerEntries: [
|
|
||||||
{
|
|
||||||
SignerEntry: {
|
|
||||||
Account: "rsA2LpzuawewSBQXkiju3YQTMzW13pAAdW",
|
|
||||||
SignerWeight: 2
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
SignerEntry: {
|
|
||||||
Account: "rUpy3eEg8rqjqfUoLeBnZkscbKbFsKXC3v",
|
|
||||||
SignerWeight: 1
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
SignerEntry: {
|
|
||||||
Account: "raKEEVSGnKSD9Zyvxu4z6Pqpm4ABH8FS6n",
|
|
||||||
SignerWeight: 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
} as any
|
|
||||||
|
|
||||||
assert.throws(
|
assert.throws(
|
||||||
() => verifySignerListSet(invalidSignerQuorum),
|
() => verifySignerListSet(SignerListSetTx),
|
||||||
ValidationError,
|
ValidationError,
|
||||||
"SignerListSet: missing field SignerQuorum"
|
"SignerListSet: missing field SignerQuorum"
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it (`throws w/ empty SignerEntries`, () => {
|
it (`throws w/ empty SignerEntries`, () => {
|
||||||
const emptySignerEntries = {
|
SignerListSetTx.SignerEntries = []
|
||||||
Flags: 0,
|
|
||||||
TransactionType: "SignerListSet",
|
|
||||||
Account: "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn",
|
|
||||||
SignerQuorum: 3,
|
|
||||||
SignerEntries: []
|
|
||||||
} as any
|
|
||||||
|
|
||||||
assert.throws(
|
assert.throws(
|
||||||
() => verifySignerListSet(emptySignerEntries),
|
() => verifySignerListSet(SignerListSetTx),
|
||||||
ValidationError,
|
ValidationError,
|
||||||
"SignerListSet: need atleast 1 member in SignerEntries"
|
"SignerListSet: need atleast 1 member in SignerEntries"
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it (`throws w/ invalid SignerEntries`, () => {
|
it (`throws w/ invalid SignerEntries`, () => {
|
||||||
const invalidSignerEntries = {
|
SignerListSetTx.SignerEntries = "khgfgyhujk"
|
||||||
Flags: 0,
|
|
||||||
TransactionType: "SignerListSet",
|
|
||||||
Account: "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn",
|
|
||||||
SignerQuorum: 3,
|
|
||||||
SignerEntries: "khgfgyhujk"
|
|
||||||
} as any
|
|
||||||
|
|
||||||
assert.throws(
|
assert.throws(
|
||||||
() => verifySignerListSet(invalidSignerEntries),
|
() => verifySignerListSet(SignerListSetTx),
|
||||||
ValidationError,
|
ValidationError,
|
||||||
"SignerListSet: invalid SignerEntries"
|
"SignerListSet: invalid SignerEntries"
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user