mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-16 10:25:51 +00:00
- Convert tests to typescript - Update type definitions causing errors in tests - `makeParser` to accept a `Buffer` in addition to `string` - `SerializedType` constructor allows not passing in a byte array - `Comparable` is now a generic type so that it allows `compareTo` methods to take more that the type itself. Example: `Uint64.compareTo` can accept `number` - Update tests to use jasmine compatible functions - Switching from `test` to `it`. - Updated test checking if coretypes all implement SerializedType - Import fixtures directly instead of using `loadFixture` utility - Remove importing of `buffer/` explicitly. It was throwing off type checking in tests. Buffer is going away in a future PR anyway. - Fixed `npm run clean` not clearing `.tsbuildinfo` files for keypairs - Remove unused account-tx-transactions.db. It was likely used in the past to test historical ledgers.
182 lines
5.0 KiB
TypeScript
182 lines
5.0 KiB
TypeScript
import { encode, decode } from '../src'
|
|
import fixtures from './fixtures/x-codec-fixtures.json'
|
|
|
|
let json_x1 = {
|
|
OwnerCount: 0,
|
|
Account: 'XVXdn5wEVm5G4UhEHWDPqjvdeH361P7BsapL4m2D2XnPSwT',
|
|
PreviousTxnLgrSeq: 7,
|
|
LedgerEntryType: 'AccountRoot',
|
|
PreviousTxnID:
|
|
'DF530FB14C5304852F20080B0A8EEF3A6BDD044F41F4EBBD68B8B321145FE4FF',
|
|
Flags: 0,
|
|
Sequence: 1,
|
|
Balance: '10000000000',
|
|
}
|
|
|
|
let json_r1 = {
|
|
OwnerCount: 0,
|
|
Account: 'rLs1MzkFWCxTbuAHgjeTZK4fcCDDnf2KRv',
|
|
PreviousTxnLgrSeq: 7,
|
|
LedgerEntryType: 'AccountRoot',
|
|
PreviousTxnID:
|
|
'DF530FB14C5304852F20080B0A8EEF3A6BDD044F41F4EBBD68B8B321145FE4FF',
|
|
Flags: 0,
|
|
Sequence: 1,
|
|
Balance: '10000000000',
|
|
SourceTag: 12345,
|
|
}
|
|
|
|
let json_null_x = {
|
|
OwnerCount: 0,
|
|
Account: 'rLs1MzkFWCxTbuAHgjeTZK4fcCDDnf2KRv',
|
|
Destination: 'rLs1MzkFWCxTbuAHgjeTZK4fcCDDnf2KRv',
|
|
Issuer: 'XVXdn5wEVm5G4UhEHWDPqjvdeH361P4GETfNyyXGaoqBj71',
|
|
PreviousTxnLgrSeq: 7,
|
|
LedgerEntryType: 'AccountRoot',
|
|
PreviousTxnID:
|
|
'DF530FB14C5304852F20080B0A8EEF3A6BDD044F41F4EBBD68B8B321145FE4FF',
|
|
Flags: 0,
|
|
Sequence: 1,
|
|
Balance: '10000000000',
|
|
}
|
|
|
|
let json_invalid_x = {
|
|
OwnerCount: 0,
|
|
Account: 'rLs1MzkFWCxTbuAHgjeTZK4fcCDDnf2KRv',
|
|
Destination: 'rLs1MzkFWCxTbuAHgjeTZK4fcCDDnf2KRv',
|
|
Issuer: 'XVXdn5wEVm5g4UhEHWDPqjvdeH361P4GETfNyyXGaoqBj71',
|
|
PreviousTxnLgrSeq: 7,
|
|
LedgerEntryType: 'AccountRoot',
|
|
PreviousTxnID:
|
|
'DF530FB14C5304852F20080B0A8EEF3A6BDD044F41F4EBBD68B8B321145FE4FF',
|
|
Flags: 0,
|
|
Sequence: 1,
|
|
Balance: '10000000000',
|
|
}
|
|
|
|
let json_null_r = {
|
|
OwnerCount: 0,
|
|
Account: 'rLs1MzkFWCxTbuAHgjeTZK4fcCDDnf2KRv',
|
|
Destination: 'rLs1MzkFWCxTbuAHgjeTZK4fcCDDnf2KRv',
|
|
Issuer: 'rLs1MzkFWCxTbuAHgjeTZK4fcCDDnf2KRv',
|
|
PreviousTxnLgrSeq: 7,
|
|
LedgerEntryType: 'AccountRoot',
|
|
PreviousTxnID:
|
|
'DF530FB14C5304852F20080B0A8EEF3A6BDD044F41F4EBBD68B8B321145FE4FF',
|
|
Flags: 0,
|
|
Sequence: 1,
|
|
Balance: '10000000000',
|
|
}
|
|
|
|
let invalid_json_issuer_tagged = {
|
|
OwnerCount: 0,
|
|
Account: 'rLs1MzkFWCxTbuAHgjeTZK4fcCDDnf2KRv',
|
|
Destination: 'rLs1MzkFWCxTbuAHgjeTZK4fcCDDnf2KRv',
|
|
Issuer: 'XVXdn5wEVm5G4UhEHWDPqjvdeH361P7BsapL4m2D2XnPSwT',
|
|
PreviousTxnLgrSeq: 7,
|
|
LedgerEntryType: 'AccountRoot',
|
|
PreviousTxnID:
|
|
'DF530FB14C5304852F20080B0A8EEF3A6BDD044F41F4EBBD68B8B321145FE4FF',
|
|
Flags: 0,
|
|
Sequence: 1,
|
|
Balance: '10000000000',
|
|
}
|
|
|
|
let invalid_json_x_and_tagged = {
|
|
OwnerCount: 0,
|
|
Account: 'XVXdn5wEVm5G4UhEHWDPqjvdeH361P7BsapL4m2D2XnPSwT',
|
|
PreviousTxnLgrSeq: 7,
|
|
LedgerEntryType: 'AccountRoot',
|
|
PreviousTxnID:
|
|
'DF530FB14C5304852F20080B0A8EEF3A6BDD044F41F4EBBD68B8B321145FE4FF',
|
|
Flags: 0,
|
|
Sequence: 1,
|
|
Balance: '10000000000',
|
|
SourceTag: 12345,
|
|
}
|
|
|
|
let json_issued_x = {
|
|
TakerPays: {
|
|
currency: 'USD',
|
|
issuer: 'X7WZKEeNVS2p9Tire9DtNFkzWBZbFtJHWxDjN9fCrBGqVA4',
|
|
value: '7072.8',
|
|
},
|
|
}
|
|
|
|
let json_issued_r = {
|
|
TakerPays: {
|
|
currency: 'USD',
|
|
issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B',
|
|
value: '7072.8',
|
|
},
|
|
}
|
|
|
|
let json_issued_with_tag = {
|
|
TakerPays: {
|
|
currency: 'USD',
|
|
issuer: 'X7WZKEeNVS2p9Tire9DtNFkzWBZbFtSiS2eDBib7svZXuc2',
|
|
value: '7072.8',
|
|
},
|
|
}
|
|
|
|
describe('X-Address Account is equivalent to a classic address w/ SourceTag', () => {
|
|
let encoded_x = encode(json_x1)
|
|
let encoded_r = encode(json_r1)
|
|
it('Can encode with x-Address', () => {
|
|
expect(encoded_x).toEqual(encoded_r)
|
|
})
|
|
|
|
it('decoded X-address is object w/ source and tag', () => {
|
|
let decoded_x = decode(encoded_x)
|
|
expect(decoded_x).toEqual(json_r1)
|
|
})
|
|
|
|
it('Encoding issuer X-Address w/ undefined destination tag', () => {
|
|
expect(encode(json_null_x)).toEqual(encode(json_null_r))
|
|
})
|
|
|
|
it('Throws when X-Address is invalid', () => {
|
|
expect(() => encode(json_invalid_x)).toThrow(new Error('checksum_invalid'))
|
|
})
|
|
|
|
it('Encodes issued currency w/ x-address', () => {
|
|
expect(encode(json_issued_x)).toEqual(encode(json_issued_r))
|
|
})
|
|
})
|
|
|
|
describe('Invalid X-Address behavior', () => {
|
|
it('X-Address with tag throws value for invalid field', () => {
|
|
expect(() => encode(invalid_json_issuer_tagged)).toThrow(
|
|
new Error('Issuer cannot have an associated tag'),
|
|
)
|
|
})
|
|
|
|
it('Throws when Account has both X-Addr and Destination Tag', () => {
|
|
expect(() => encode(invalid_json_x_and_tagged)).toThrow(
|
|
new Error('Cannot have Account X-Address and SourceTag'),
|
|
)
|
|
})
|
|
|
|
it('Throws when issued currency has tag', () => {
|
|
expect(() => encode(json_issued_with_tag)).toThrow(
|
|
new Error('Only allowed to have tag on Account or Destination'),
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('ripple-binary-codec x-address test', function () {
|
|
function makeSuite(name: string, entries: typeof fixtures.transactions) {
|
|
describe(name, function () {
|
|
entries.forEach((t, testN) => {
|
|
it(`${name}[${testN}] encodes X-address json equivalent to classic address json`, () => {
|
|
expect(encode(t.rjson)).toEqual(encode(t.xjson))
|
|
})
|
|
it(`${name}[${testN}] decodes X-address json equivalent to classic address json`, () => {
|
|
expect(decode(encode(t.xjson))).toEqual(t.rjson)
|
|
})
|
|
})
|
|
})
|
|
}
|
|
makeSuite('transactions', fixtures.transactions)
|
|
})
|