mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-27 07:35:52 +00:00
rename Bridge -> XChainBridge in binary codec, fix tests
This commit is contained in:
@@ -7,7 +7,7 @@ import { Buffer } from 'buffer/'
|
||||
* BinaryParser is used to compute fields and values from a HexString
|
||||
*/
|
||||
class BinaryParser {
|
||||
private bytes: Buffer
|
||||
public bytes: Buffer
|
||||
|
||||
/**
|
||||
* Initialize bytes to a hex string
|
||||
|
||||
@@ -1,109 +0,0 @@
|
||||
import { BinaryParser } from '../serdes/binary-parser'
|
||||
|
||||
import { AccountID } from './account-id'
|
||||
import { JsonObject, SerializedType } from './serialized-type'
|
||||
import { Buffer } from 'buffer/'
|
||||
import { IssuedCurrency, IssuedCurrencyObject } from './issued-currency'
|
||||
|
||||
/**
|
||||
* Interface for JSON objects that represent bridges
|
||||
*/
|
||||
interface BridgeObject extends JsonObject {
|
||||
dst_chain_door: string
|
||||
dst_chain_issue: IssuedCurrencyObject | string
|
||||
src_chain_door: string
|
||||
src_chain_issue: IssuedCurrencyObject | string
|
||||
}
|
||||
|
||||
/**
|
||||
* Type guard for BridgeObject
|
||||
*/
|
||||
function isBridgeObject(arg): arg is BridgeObject {
|
||||
const keys = Object.keys(arg).sort()
|
||||
return (
|
||||
keys.length === 4 &&
|
||||
keys[0] === 'dst_chain_door' &&
|
||||
keys[1] === 'dst_chain_issue' &&
|
||||
keys[2] === 'src_chain_door' &&
|
||||
keys[3] === 'src_chain_issue'
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Class for serializing/deserializing Bridges
|
||||
*/
|
||||
class Bridge extends SerializedType {
|
||||
static readonly ZERO_Bridge: Bridge = new Bridge(Buffer.alloc(80))
|
||||
|
||||
static readonly TYPE_ORDER: { name: string; type: typeof SerializedType }[] =
|
||||
[
|
||||
{ name: 'src_chain_door', type: AccountID },
|
||||
{ name: 'src_chain_issue', type: IssuedCurrency },
|
||||
{ name: 'dst_chain_door', type: AccountID },
|
||||
{ name: 'dst_chain_issue', type: IssuedCurrency },
|
||||
]
|
||||
|
||||
constructor(bytes: Buffer) {
|
||||
super(bytes ?? Bridge.ZERO_Bridge.bytes)
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a bridge from a JSON
|
||||
*
|
||||
* @param value Bridge or JSON to parse into a Bridge
|
||||
* @returns A Bridge object
|
||||
*/
|
||||
static from<T extends Bridge | BridgeObject>(value: T): Bridge {
|
||||
if (value instanceof Bridge) {
|
||||
return value
|
||||
}
|
||||
|
||||
if (isBridgeObject(value)) {
|
||||
const bytes: Array<Buffer> = []
|
||||
this.TYPE_ORDER.forEach((item) => {
|
||||
const { name, type } = item
|
||||
const object = type.from(value[name])
|
||||
bytes.push(object.toBytes())
|
||||
})
|
||||
return new Bridge(Buffer.concat(bytes))
|
||||
}
|
||||
|
||||
throw new Error('Invalid type to construct a Bridge')
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a Bridge from a BinaryParser
|
||||
*
|
||||
* @param parser BinaryParser to read the Bridge from
|
||||
* @returns A Bridge object
|
||||
*/
|
||||
static fromParser(parser: BinaryParser): Bridge {
|
||||
const bytes: Array<Buffer> = []
|
||||
|
||||
this.TYPE_ORDER.forEach((item) => {
|
||||
const { type } = item
|
||||
const object = type.fromParser(parser)
|
||||
bytes.push(object.toBytes())
|
||||
})
|
||||
|
||||
return new Bridge(Buffer.concat(bytes))
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the JSON representation of this Bridge
|
||||
*
|
||||
* @returns the JSON interpretation of this.bytes
|
||||
*/
|
||||
toJSON(): BridgeObject {
|
||||
const parser = new BinaryParser(this.toString())
|
||||
const json = {}
|
||||
Bridge.TYPE_ORDER.forEach((item) => {
|
||||
const { name, type } = item
|
||||
const object = type.fromParser(parser).toJSON()
|
||||
json[name] = object
|
||||
})
|
||||
return json as BridgeObject
|
||||
}
|
||||
}
|
||||
|
||||
export { Bridge, BridgeObject }
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
import { AccountID } from './account-id'
|
||||
import { Amount } from './amount'
|
||||
import { Blob } from './blob'
|
||||
import { Bridge } from './bridge'
|
||||
import { XChainBridge } from './xchain-bridge'
|
||||
import { Currency } from './currency'
|
||||
import { Hash128 } from './hash-128'
|
||||
import { Hash160 } from './hash-160'
|
||||
@@ -26,7 +26,7 @@ const coreTypes = {
|
||||
AccountID,
|
||||
Amount,
|
||||
Blob,
|
||||
Bridge,
|
||||
XChainBridge,
|
||||
Currency,
|
||||
Hash128,
|
||||
Hash160,
|
||||
|
||||
128
packages/ripple-binary-codec/src/types/xchain-bridge.ts
Normal file
128
packages/ripple-binary-codec/src/types/xchain-bridge.ts
Normal file
@@ -0,0 +1,128 @@
|
||||
import { BinaryParser } from '../serdes/binary-parser'
|
||||
|
||||
import { AccountID } from './account-id'
|
||||
import { JsonObject, SerializedType } from './serialized-type'
|
||||
import { Buffer } from 'buffer/'
|
||||
import { IssuedCurrency, IssuedCurrencyObject } from './issued-currency'
|
||||
|
||||
/**
|
||||
* Interface for JSON objects that represent cross-chain bridges
|
||||
*/
|
||||
interface XChainBridgeObject extends JsonObject {
|
||||
dst_chain_door: string
|
||||
dst_chain_issue: IssuedCurrencyObject | string
|
||||
src_chain_door: string
|
||||
src_chain_issue: IssuedCurrencyObject | string
|
||||
}
|
||||
|
||||
/**
|
||||
* Type guard for XChainBridgeObject
|
||||
*/
|
||||
function isXChainBridgeObject(arg): arg is XChainBridgeObject {
|
||||
const keys = Object.keys(arg).sort()
|
||||
return (
|
||||
keys.length === 4 &&
|
||||
keys[0] === 'IssuingChainDoor' &&
|
||||
keys[1] === 'IssuingChainIssue' &&
|
||||
keys[2] === 'LockingChainDoor' &&
|
||||
keys[3] === 'LockingChainIssue'
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Class for serializing/deserializing XChainBridges
|
||||
*/
|
||||
class XChainBridge extends SerializedType {
|
||||
static readonly ZERO_XCHAIN_BRIDGE: XChainBridge = new XChainBridge(
|
||||
Buffer.concat([
|
||||
Buffer.from([0x14]),
|
||||
Buffer.alloc(40),
|
||||
Buffer.from([0x14]),
|
||||
Buffer.alloc(40),
|
||||
]),
|
||||
)
|
||||
|
||||
static readonly TYPE_ORDER: { name: string; type: typeof SerializedType }[] =
|
||||
[
|
||||
{ name: 'LockingChainDoor', type: AccountID },
|
||||
{ name: 'LockingChainIssue', type: IssuedCurrency },
|
||||
{ name: 'IssuingChainDoor', type: AccountID },
|
||||
{ name: 'IssuingChainIssue', type: IssuedCurrency },
|
||||
]
|
||||
|
||||
constructor(bytes: Buffer) {
|
||||
super(bytes ?? XChainBridge.ZERO_XCHAIN_BRIDGE.bytes)
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a cross-chain bridge from a JSON
|
||||
*
|
||||
* @param value XChainBridge or JSON to parse into a XChainBridge
|
||||
* @returns A XChainBridge object
|
||||
*/
|
||||
static from<T extends XChainBridge | XChainBridgeObject>(
|
||||
value: T,
|
||||
): XChainBridge {
|
||||
if (value instanceof XChainBridge) {
|
||||
return value
|
||||
}
|
||||
|
||||
if (isXChainBridgeObject(value)) {
|
||||
const bytes: Array<Buffer> = []
|
||||
this.TYPE_ORDER.forEach((item) => {
|
||||
const { name, type } = item
|
||||
if (type === AccountID) {
|
||||
bytes.push(Buffer.from([0x14]))
|
||||
}
|
||||
const object = type.from(value[name])
|
||||
bytes.push(object.toBytes())
|
||||
})
|
||||
return new XChainBridge(Buffer.concat(bytes))
|
||||
}
|
||||
|
||||
throw new Error('Invalid type to construct a XChainBridge')
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a XChainBridge from a BinaryParser
|
||||
*
|
||||
* @param parser BinaryParser to read the XChainBridge from
|
||||
* @returns A XChainBridge object
|
||||
*/
|
||||
static fromParser(parser: BinaryParser): XChainBridge {
|
||||
const bytes: Array<Buffer> = []
|
||||
|
||||
this.TYPE_ORDER.forEach((item) => {
|
||||
const { type } = item
|
||||
if (type === AccountID) {
|
||||
parser.skip(1)
|
||||
bytes.push(Buffer.from([0x14]))
|
||||
}
|
||||
const object = type.fromParser(parser)
|
||||
bytes.push(object.toBytes())
|
||||
})
|
||||
|
||||
return new XChainBridge(Buffer.concat(bytes))
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the JSON representation of this XChainBridge
|
||||
*
|
||||
* @returns the JSON interpretation of this.bytes
|
||||
*/
|
||||
toJSON(): XChainBridgeObject {
|
||||
const parser = new BinaryParser(this.toString())
|
||||
const json = {}
|
||||
XChainBridge.TYPE_ORDER.forEach((item) => {
|
||||
const { name, type } = item
|
||||
if (type === AccountID) {
|
||||
parser.skip(1)
|
||||
}
|
||||
const object = type.fromParser(parser).toJSON()
|
||||
json[name] = object
|
||||
})
|
||||
return json as XChainBridgeObject
|
||||
}
|
||||
}
|
||||
|
||||
export { XChainBridge, XChainBridgeObject }
|
||||
@@ -4451,34 +4451,34 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"binary": "12001E2280000000240000000168400000000000000A601540000000000003E86016400000000000271073210330E7FC9D56BB25D6893BA3F317AE5BCF33B3291BD63DB32654A313222F7FD020744630440220432A94FC7F759730DD66CC7FB0044E563E6C0DF28C3CDF673CB1D2DF03862FA5022020F70FCF14FF24080E34FA5CA0F555C83A520FC4730F5EDA82523FB0E145E2DA8114AF80285F637EE4AF3C20378F9DFB12511ACB8D270118AF80285F637EE4AF3C20378F9DFB12511ACB8D270000000000000000000000000000000000000000550FC62003E785DC231A1058A05E56E3F09CF4E60000000000000000000000000000000000000000",
|
||||
"binary": "12001E2200000000240000000168400000000000000A601540000000000003E86016400000000000271073210330E7FC9D56BB25D6893BA3F317AE5BCF33B3291BD63DB32654A313222F7FD02074463044022044798D0E4310C7E79B2929308E765F0C29B2D2A1DB4FDF0D7A62DAB39A4874F402207DC871A0E0049078ECB98EAD1E0B78DAD17F4E3E6DF4AB2E5F6DAC9451A5630A8114AF80285F637EE4AF3C20378F9DFB12511ACB8D27011B14AF80285F637EE4AF3C20378F9DFB12511ACB8D27000000000000000000000000000000000000000014550FC62003E785DC231A1058A05E56E3F09CF4E60000000000000000000000000000000000000000",
|
||||
"json": {
|
||||
"Account": "rGzx83BVoqTYbGn7tiVAnFw7cbxjin13jL",
|
||||
"Bridge": {
|
||||
"src_chain_door": "rGzx83BVoqTYbGn7tiVAnFw7cbxjin13jL",
|
||||
"src_chain_issue": "XRP",
|
||||
"dst_chain_door": "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV",
|
||||
"dst_chain_issue": "XRP"
|
||||
"XChainBridge": {
|
||||
"LockingChainDoor": "rGzx83BVoqTYbGn7tiVAnFw7cbxjin13jL",
|
||||
"LockingChainIssue": "XRP",
|
||||
"IssuingChainDoor": "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV",
|
||||
"IssuingChainIssue": "XRP"
|
||||
},
|
||||
"Fee": "10",
|
||||
"Flags": 2147483648,
|
||||
"Flags": 0,
|
||||
"MinAccountCreateAmount": "10000",
|
||||
"Sequence": 1,
|
||||
"SignatureReward": "1000",
|
||||
"SigningPubKey": "0330E7FC9D56BB25D6893BA3F317AE5BCF33B3291BD63DB32654A313222F7FD020",
|
||||
"TransactionType": "XChainCreateBridge",
|
||||
"TxnSignature": "30440220432A94FC7F759730DD66CC7FB0044E563E6C0DF28C3CDF673CB1D2DF03862FA5022020F70FCF14FF24080E34FA5CA0F555C83A520FC4730F5EDA82523FB0E145E2DA"
|
||||
"TxnSignature": "3044022044798D0E4310C7E79B2929308E765F0C29B2D2A1DB4FDF0D7A62DAB39A4874F402207DC871A0E0049078ECB98EAD1E0B78DAD17F4E3E6DF4AB2E5F6DAC9451A5630A"
|
||||
}
|
||||
},
|
||||
{
|
||||
"binary": "12001F2280000000240000000168400000000000000A6015400000000000271073210330E7FC9D56BB25D6893BA3F317AE5BCF33B3291BD63DB32654A313222F7FD02074463044022065BB8ABE6E34ADB25C1350F7AF34B1DF01CE87A5ACE1610454EE6B2ED70A0D3302201A4CC9664A191E70F98E921E569E7D3D2EF7FC28353085BC8CBA5296425C287E8114550FC62003E785DC231A1058A05E56E3F09CF4E6801214AF80285F637EE4AF3C20378F9DFB12511ACB8D270118AF80285F637EE4AF3C20378F9DFB12511ACB8D270000000000000000000000000000000000000000550FC62003E785DC231A1058A05E56E3F09CF4E60000000000000000000000000000000000000000",
|
||||
"binary": "12001F2280000000240000000168400000000000000A6015400000000000271073210330E7FC9D56BB25D6893BA3F317AE5BCF33B3291BD63DB32654A313222F7FD0207446304402203858DCC266B53EDFDF16359AF483323BF4DF7176CD0EDCCC253EF5556C9D131B0220755FE28874AA927C9B41D4C7D35D88BCA4B38B5B8E2F87D21B738F087F32B3988114550FC62003E785DC231A1058A05E56E3F09CF4E6801214AF80285F637EE4AF3C20378F9DFB12511ACB8D27011B14AF80285F637EE4AF3C20378F9DFB12511ACB8D27000000000000000000000000000000000000000014550FC62003E785DC231A1058A05E56E3F09CF4E60000000000000000000000000000000000000000",
|
||||
"json": {
|
||||
"Account": "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV",
|
||||
"Bridge": {
|
||||
"src_chain_door": "rGzx83BVoqTYbGn7tiVAnFw7cbxjin13jL",
|
||||
"src_chain_issue": "XRP",
|
||||
"dst_chain_door": "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV",
|
||||
"dst_chain_issue": "XRP"
|
||||
"XChainBridge": {
|
||||
"LockingChainDoor": "rGzx83BVoqTYbGn7tiVAnFw7cbxjin13jL",
|
||||
"LockingChainIssue": "XRP",
|
||||
"IssuingChainDoor": "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV",
|
||||
"IssuingChainIssue": "XRP"
|
||||
},
|
||||
"Fee": "10",
|
||||
"Flags": 2147483648,
|
||||
@@ -4487,39 +4487,39 @@
|
||||
"SignatureReward": "10000",
|
||||
"SigningPubKey": "0330E7FC9D56BB25D6893BA3F317AE5BCF33B3291BD63DB32654A313222F7FD020",
|
||||
"TransactionType": "XChainCreateClaimID",
|
||||
"TxnSignature": "3044022065BB8ABE6E34ADB25C1350F7AF34B1DF01CE87A5ACE1610454EE6B2ED70A0D3302201A4CC9664A191E70F98E921E569E7D3D2EF7FC28353085BC8CBA5296425C287E"
|
||||
"TxnSignature": "304402203858DCC266B53EDFDF16359AF483323BF4DF7176CD0EDCCC253EF5556C9D131B0220755FE28874AA927C9B41D4C7D35D88BCA4B38B5B8E2F87D21B738F087F32B398"
|
||||
}
|
||||
},
|
||||
{
|
||||
"binary": "120020228000000024000000013014000000000000000161400000000000271068400000000000000A73210330E7FC9D56BB25D6893BA3F317AE5BCF33B3291BD63DB32654A313222F7FD0207446304402204C1E4F103CB24B10925F34DF2A90AD3008078A0A725A1F96A59652E34EF5071B02200F567E95FF140F12A35CDBD2324F8BE769BF57DE5D2F92367A4C8AAB7C5F34548114550FC62003E785DC231A1058A05E56E3F09CF4E60118AF80285F637EE4AF3C20378F9DFB12511ACB8D270000000000000000000000000000000000000000550FC62003E785DC231A1058A05E56E3F09CF4E60000000000000000000000000000000000000000",
|
||||
"binary": "120020228000000024000000013014000000000000000161400000000000271068400000000000000A73210330E7FC9D56BB25D6893BA3F317AE5BCF33B3291BD63DB32654A313222F7FD0207446304402201B39894271B8512FE9C6E146587F695BC0E2AF19EC47570054FAA8B95A2CBC0C02201A9A63FEACD2416BC6E49EA45143927C474CC2CC85AA135A46A183423E76615A8114550FC62003E785DC231A1058A05E56E3F09CF4E6011B14AF80285F637EE4AF3C20378F9DFB12511ACB8D27000000000000000000000000000000000000000014550FC62003E785DC231A1058A05E56E3F09CF4E60000000000000000000000000000000000000000",
|
||||
"json": {
|
||||
"Account": "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV",
|
||||
"Amount": "10000",
|
||||
"Bridge": {
|
||||
"src_chain_door": "rGzx83BVoqTYbGn7tiVAnFw7cbxjin13jL",
|
||||
"src_chain_issue": "XRP",
|
||||
"dst_chain_door": "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV",
|
||||
"dst_chain_issue": "XRP"
|
||||
"XChainBridge": {
|
||||
"LockingChainDoor": "rGzx83BVoqTYbGn7tiVAnFw7cbxjin13jL",
|
||||
"LockingChainIssue": "XRP",
|
||||
"IssuingChainDoor": "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV",
|
||||
"IssuingChainIssue": "XRP"
|
||||
},
|
||||
"Fee": "10",
|
||||
"Flags": 2147483648,
|
||||
"Sequence": 1,
|
||||
"SigningPubKey": "0330E7FC9D56BB25D6893BA3F317AE5BCF33B3291BD63DB32654A313222F7FD020",
|
||||
"TransactionType": "XChainCommit",
|
||||
"TxnSignature": "304402204C1E4F103CB24B10925F34DF2A90AD3008078A0A725A1F96A59652E34EF5071B02200F567E95FF140F12A35CDBD2324F8BE769BF57DE5D2F92367A4C8AAB7C5F3454",
|
||||
"TxnSignature": "304402201B39894271B8512FE9C6E146587F695BC0E2AF19EC47570054FAA8B95A2CBC0C02201A9A63FEACD2416BC6E49EA45143927C474CC2CC85AA135A46A183423E76615A",
|
||||
"XChainClaimID": "0000000000000001"
|
||||
}
|
||||
},
|
||||
{
|
||||
"binary": "120021228000000024000000013014000000000000000161400000000000271068400000000000000A73210330E7FC9D56BB25D6893BA3F317AE5BCF33B3291BD63DB32654A313222F7FD02074473045022100D4AFE8A4ACED40969B3E06EEC1D3C94FB9B6702537CC56FB180BA2F74E9CCB5C02203B30C46AB83B665BD3B4FF6AB3CBF33C138640CB7CA76888BB7280B114FC94458114550FC62003E785DC231A1058A05E56E3F09CF4E68314550FC62003E785DC231A1058A05E56E3F09CF4E60118AF80285F637EE4AF3C20378F9DFB12511ACB8D270000000000000000000000000000000000000000550FC62003E785DC231A1058A05E56E3F09CF4E60000000000000000000000000000000000000000",
|
||||
"binary": "120021228000000024000000013014000000000000000161400000000000271068400000000000000A73210330E7FC9D56BB25D6893BA3F317AE5BCF33B3291BD63DB32654A313222F7FD02074473045022100CA14E5FB5F30FC698119ED7E7D8F291E48A21693190A01AD89E140035476EE9D02206A642A396246C726739195D5D2CCC0635E1A67EDB84DEFBD54D42F7809F45F8B8114550FC62003E785DC231A1058A05E56E3F09CF4E68314550FC62003E785DC231A1058A05E56E3F09CF4E6011B14AF80285F637EE4AF3C20378F9DFB12511ACB8D27000000000000000000000000000000000000000014550FC62003E785DC231A1058A05E56E3F09CF4E60000000000000000000000000000000000000000",
|
||||
"json": {
|
||||
"Account": "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV",
|
||||
"Amount": "10000",
|
||||
"Bridge": {
|
||||
"src_chain_door": "rGzx83BVoqTYbGn7tiVAnFw7cbxjin13jL",
|
||||
"src_chain_issue": "XRP",
|
||||
"dst_chain_door": "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV",
|
||||
"dst_chain_issue": "XRP"
|
||||
"XChainBridge": {
|
||||
"LockingChainDoor": "rGzx83BVoqTYbGn7tiVAnFw7cbxjin13jL",
|
||||
"LockingChainIssue": "XRP",
|
||||
"IssuingChainDoor": "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV",
|
||||
"IssuingChainIssue": "XRP"
|
||||
},
|
||||
"Destination": "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV",
|
||||
"Fee": "10",
|
||||
@@ -4527,7 +4527,7 @@
|
||||
"Sequence": 1,
|
||||
"SigningPubKey": "0330E7FC9D56BB25D6893BA3F317AE5BCF33B3291BD63DB32654A313222F7FD020",
|
||||
"TransactionType": "XChainClaim",
|
||||
"TxnSignature": "3045022100D4AFE8A4ACED40969B3E06EEC1D3C94FB9B6702537CC56FB180BA2F74E9CCB5C02203B30C46AB83B665BD3B4FF6AB3CBF33C138640CB7CA76888BB7280B114FC9445",
|
||||
"TxnSignature": "3045022100CA14E5FB5F30FC698119ED7E7D8F291E48A21693190A01AD89E140035476EE9D02206A642A396246C726739195D5D2CCC0635E1A67EDB84DEFBD54D42F7809F45F8B",
|
||||
"XChainClaimID": "0000000000000001"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user