more matching to rippled

This commit is contained in:
Mayukha Vadari
2022-06-01 17:30:00 -04:00
parent 15b2522bfa
commit b3056890c8
6 changed files with 58 additions and 190 deletions

View File

@@ -1359,6 +1359,16 @@
"type": "STObject" "type": "STObject"
} }
], ],
[
"XChainProofSig",
{
"nth": 25,
"isVLEncoded": false,
"isSerialized": true,
"isSigningField": true,
"type": "STObject"
}
],
[ [
"ArrayEndMarker", "ArrayEndMarker",
{ {

View File

@@ -14,7 +14,6 @@ import { Hash256 } from './hash-256'
import { IssuedCurrency } from './issued-currency' import { IssuedCurrency } from './issued-currency'
import { PathSet } from './path-set' import { PathSet } from './path-set'
import { Sidechain } from './sidechain' import { Sidechain } from './sidechain'
import { Signature } from './signature'
import { STArray } from './st-array' import { STArray } from './st-array'
import { STObject } from './st-object' import { STObject } from './st-object'
import { UInt16 } from './uint-16' import { UInt16 } from './uint-16'
@@ -35,7 +34,6 @@ const coreTypes = {
IssuedCurrency, IssuedCurrency,
PathSet, PathSet,
Sidechain, Sidechain,
Signature,
STArray, STArray,
STObject, STObject,
UInt8, UInt8,

View File

@@ -1,130 +0,0 @@
import { BinaryParser } from '../serdes/binary-parser'
import { JsonObject, SerializedType } from './serialized-type'
import { Buffer } from 'buffer/'
import { Blob } from './blob'
import { decodeAccountPublic, encodeAccountPublic } from 'ripple-address-codec'
/**
* Interface for JSON objects that represent amounts
*/
interface SignatureObject extends JsonObject {
signature: string
signing_key: string
}
/**
* Type guard for AmountObject
*/
function isSignatureObject(arg): arg is SignatureObject {
const keys = Object.keys(arg).sort()
return (
keys.length === 2 && keys[0] === 'signature' && keys[1] === 'signing_key'
)
}
function encodeVariableLength(length: number): Buffer {
const lenBytes = Buffer.alloc(3)
if (length <= 192) {
lenBytes[0] = length
return lenBytes.slice(0, 1)
} else if (length <= 12480) {
length -= 193
lenBytes[0] = 193 + (length >>> 8)
lenBytes[1] = length & 0xff
return lenBytes.slice(0, 2)
} else if (length <= 918744) {
length -= 12481
lenBytes[0] = 241 + (length >>> 16)
lenBytes[1] = (length >> 8) & 0xff
lenBytes[2] = length & 0xff
return lenBytes.slice(0, 3)
}
throw new Error('Overflow error')
}
/**
* Class for serializing/Deserializing Amounts
*/
class Signature extends SerializedType {
static readonly ZERO_SIGNATURE: Signature = new Signature(
Buffer.concat([Buffer.alloc(1), Buffer.from([33]), Buffer.alloc(33)]),
)
constructor(bytes: Buffer) {
super(bytes ?? Signature.ZERO_SIGNATURE.bytes)
}
/**
* Construct an amount from an IOU or string amount
*
* @param value An Amount, object representing an IOU, or a string
* representing an integer amount
* @returns An Amount object
*/
static from<T extends Signature | SignatureObject>(value: T): Signature {
if (value instanceof Signature) {
return value
}
if (isSignatureObject(value)) {
const signature = Blob.from(value.signature).toBytes()
const signing_key = new Blob(
Buffer.from(decodeAccountPublic(value.signing_key)),
).toBytes()
return new Signature(
Buffer.concat([
encodeVariableLength(signature.length),
signature,
encodeVariableLength(signing_key.length),
signing_key,
]),
)
}
throw new Error('Invalid type to construct a Signature')
}
/**
* Read an amount from a BinaryParser
*
* @param parser BinaryParser to read the Amount from
* @returns An Amount object
*/
static fromParser(parser: BinaryParser): Signature {
const bytes: Array<Buffer> = []
const signatureLength = parser.readVariableLengthLength()
bytes.push(encodeVariableLength(signatureLength))
bytes.push(Blob.fromParser(parser, signatureLength).toBytes())
const signingKeyLength = parser.readVariableLengthLength()
bytes.push(encodeVariableLength(signingKeyLength))
bytes.push(Blob.fromParser(parser, signingKeyLength).toBytes())
return new Signature(Buffer.concat(bytes))
}
/**
* Get the JSON representation of this Amount
*
* @returns the JSON interpretation of this.bytes
*/
toJSON(): SignatureObject {
const parser = new BinaryParser(this.toString())
const signatureLength = parser.readVariableLengthLength()
const signature = Blob.fromParser(
parser,
signatureLength,
).toJSON() as string
const signingKeyLength = parser.readVariableLengthLength()
const signingKey = Blob.fromParser(parser, signingKeyLength)
return {
signature,
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- this is fine, they're both different bytes
signing_key: encodeAccountPublic(signingKey.toBytes() as any),
}
}
}
export { Signature, SignatureObject }

View File

@@ -3,15 +3,18 @@ import { BinaryParser } from '../serdes/binary-parser'
import { JsonObject, SerializedType } from './serialized-type' import { JsonObject, SerializedType } from './serialized-type'
import { Buffer } from 'buffer/' import { Buffer } from 'buffer/'
import { Sidechain, SidechainObject } from './sidechain' import { Sidechain, SidechainObject } from './sidechain'
import { Signature, SignatureObject } from './signature'
import { Amount } from './amount' import { Amount } from './amount'
import { UInt8 } from './uint-8' import { UInt8 } from './uint-8'
import { UInt32 } from './uint-32' import { UInt32 } from './uint-32'
import { STArray } from './st-array'
/** /**
* Constants for separating Paths in a PathSet * Interface for JSON objects that represent amounts
*/ */
const ARRAY_END_BYTE = 0xf1 interface SignatureObject extends JsonObject {
signature: string
signing_key: string
}
/** /**
* Interface for JSON objects that represent amounts * Interface for JSON objects that represent amounts
@@ -45,11 +48,10 @@ function isProofObject(arg): arg is XChainClaimProofObject {
class XChainClaimProof extends SerializedType { class XChainClaimProof extends SerializedType {
static readonly ZERO_PROOF: XChainClaimProof = new XChainClaimProof( static readonly ZERO_PROOF: XChainClaimProof = new XChainClaimProof(
Buffer.concat([ Buffer.concat([
Amount.defaultAmount.toBytes(),
Sidechain.ZERO_SIDECHAIN.toBytes(), Sidechain.ZERO_SIDECHAIN.toBytes(),
Buffer.from([ARRAY_END_BYTE]), Amount.defaultAmount.toBytes(),
UInt8.defaultUInt8.toBytes(),
UInt32.defaultUInt32.toBytes(), UInt32.defaultUInt32.toBytes(),
UInt8.defaultUInt8.toBytes(),
]), ]),
) )
@@ -72,24 +74,20 @@ class XChainClaimProof extends SerializedType {
} }
if (isProofObject(value)) { if (isProofObject(value)) {
const amount = Amount.from(value.amount).toBytes()
const sidechain = Sidechain.from(value.sidechain).toBytes() const sidechain = Sidechain.from(value.sidechain).toBytes()
const signatures: Array<Buffer> = [] const amount = Amount.from(value.amount).toBytes()
value.signatures.forEach((signature: SignatureObject) => { const xchain_seq = UInt32.from(value.xchain_seq).toBytes()
signatures.push(Signature.from(signature).toBytes())
})
signatures.push(Buffer.from([ARRAY_END_BYTE]))
const was_src_chain_send = UInt8.from( const was_src_chain_send = UInt8.from(
Number(value.was_src_chain_send), Number(value.was_src_chain_send),
).toBytes() ).toBytes()
const xchain_seq = UInt32.from(value.xchain_seq).toBytes() const signatures = STArray.from(value.signatures).toBytes()
return new XChainClaimProof( return new XChainClaimProof(
Buffer.concat([ Buffer.concat([
amount,
sidechain, sidechain,
...signatures, amount,
was_src_chain_send,
xchain_seq, xchain_seq,
was_src_chain_send,
signatures,
]), ]),
) )
} }
@@ -106,18 +104,11 @@ class XChainClaimProof extends SerializedType {
static fromParser(parser: BinaryParser): XChainClaimProof { static fromParser(parser: BinaryParser): XChainClaimProof {
const bytes: Array<Buffer> = [] const bytes: Array<Buffer> = []
bytes.push(Amount.fromParser(parser).toBytes())
bytes.push(Sidechain.fromParser(parser).toBytes()) bytes.push(Sidechain.fromParser(parser).toBytes())
while (!parser.end()) { bytes.push(Amount.fromParser(parser).toBytes())
bytes.push(Signature.fromParser(parser).toBytes())
if (parser.peek() === ARRAY_END_BYTE) {
bytes.push(parser.read(1))
break
}
}
bytes.push(parser.read(UInt8.width))
bytes.push(parser.read(UInt32.width)) bytes.push(parser.read(UInt32.width))
bytes.push(parser.read(UInt8.width))
bytes.push(STArray.fromParser(parser).toBytes())
return new XChainClaimProof(Buffer.concat(bytes)) return new XChainClaimProof(Buffer.concat(bytes))
} }
@@ -129,25 +120,16 @@ class XChainClaimProof extends SerializedType {
*/ */
toJSON(): XChainClaimProofObject { toJSON(): XChainClaimProofObject {
const parser = new BinaryParser(this.toString()) const parser = new BinaryParser(this.toString())
const amount = Amount.fromParser(parser).toJSON()
const sidechain = Sidechain.fromParser(parser).toJSON() const sidechain = Sidechain.fromParser(parser).toJSON()
const signatures: SignatureObject[] = [] const amount = Amount.fromParser(parser).toJSON()
while (!parser.end()) {
if (parser.peek() === ARRAY_END_BYTE) {
parser.skip(1)
break
}
signatures.push(Signature.fromParser(parser).toJSON())
}
const was_src_chain_send = UInt8.fromParser(parser).toJSON()
const xchain_seq = UInt32.fromParser(parser).toJSON() const xchain_seq = UInt32.fromParser(parser).toJSON()
const was_src_chain_send = UInt8.fromParser(parser).toJSON()
const signatures = STArray.fromParser(parser).toJSON()
return { return {
amount: amount as string, amount: amount as string,
sidechain, sidechain,
signatures, signatures: signatures as SignatureObject[],
was_src_chain_send: Boolean(was_src_chain_send), was_src_chain_send: Boolean(was_src_chain_send),
xchain_seq: xchain_seq as number, xchain_seq: xchain_seq as number,
} }

View File

@@ -1,9 +1,11 @@
{ {
"binary": "12002122800000008114621D345F8F094A085132431C69C89EC05D212CC28314CA64525733C3BEED910CFE2AE280D3C078DABB4B0119400000003B9ACA00C48CAD01682D7A86296EF14523074D4852C02EA90000000000000000000000000000000000000000CC86E58C9B58D4CF71CB8C1B41F21BB290CE13D400000000000000000000000000000000000000004730450221008CC9842A6855A37131FE7FB978675DCF329AC5CD7C881FAF6D13CDC23363059F02203A10475640C2C09541A55098109BB3326D3F49E2304710736A4E3C2773539B012103ADB44CA8E56F78A0096825E5667C450ABD5C24C34E027BC1AAF7E5BD114CB5B5473045022100B93B51FDBE42634828EEF7F2E5FA950557283648C5D39196ED1B08F8B394959102201F162F43B41D0D9316BF3B7AFA3BC9CA0EC1FCECF6EE8C94F58616BEA2D31F2C2102A14E886B3C3579FBAE3139F29728B903E6F4295AEE92160C8480695524D66A154630440220254038C5D6106246AEBB2A94E60CE79102321FDFD0468AA40C5E7F2DF1C205FE02204CEE5B3BFFDDCB67AE593348E8D8551E82770415BC9581EF0EF20DD000DD550C2102F7390DCF3352060847B81666EBAC79D52DEA2443BDF58439F75397C45334E2DC473045022100AA28592882A3B7C769B32564EDF9F816179D42D8C0E3988567F816E9A5453C6002202BA57DC085B9EB8427FBD58E05B3617183AC216EC7066F03FA4896D8B449490E2102498BD8CD9CA6A4BA567A2ECFA163F118AFD30511CBBA71429C2EC2F74D760592473045022100B9D649491723810B705282B66EAD6075235A1954BEEE054FF68066FADE11DA1C02205D977AE7C6706224A5A3AA75A53E13CA3DE9D67084405CD9BB474554EEE5C0702103219642288DEE8A3AA8FEA1F7DAE9ED4D9A9F0EADA1E2DE3DB56DD9598D9AD817F10100000001", "binary": "1200212280000000240000000168400000000000000A73210330E7FC9D56BB25D6893BA3F317AE5BCF33B3291BD63DB32654A313222F7FD0207446304402203CEE125C7EEA970DFC4DEFE89D79666C109EDD8B33FC006DBD8F2CCFC8CFAD4B02200E4210829E31079D623B8C5E6A634611688F50C9D41FF16341407B443B9605178114B5F762798A53D543A014CAF8B297CFF8F2F937E88314CA64525733C3BEED910CFE2AE280D3C078DABB4B0119C48CAD01682D7A86296EF14523074D4852C02EA90000000000000000000000000000000000000000CC86E58C9B58D4CF71CB8C1B41F21BB290CE13D40000000000000000000000000000000000000000400000003B9ACA000000000101E019712103ADB44CA8E56F78A0096825E5667C450ABD5C24C34E027BC1AAF7E5BD114CB5B5764730450221008CC9842A6855A37131FE7FB978675DCF329AC5CD7C881FAF6D13CDC23363059F02203A10475640C2C09541A55098109BB3326D3F49E2304710736A4E3C2773539B01E1E019712102A14E886B3C3579FBAE3139F29728B903E6F4295AEE92160C8480695524D66A1576473045022100B93B51FDBE42634828EEF7F2E5FA950557283648C5D39196ED1B08F8B394959102201F162F43B41D0D9316BF3B7AFA3BC9CA0EC1FCECF6EE8C94F58616BEA2D31F2CE1E019712102F7390DCF3352060847B81666EBAC79D52DEA2443BDF58439F75397C45334E2DC764630440220254038C5D6106246AEBB2A94E60CE79102321FDFD0468AA40C5E7F2DF1C205FE02204CEE5B3BFFDDCB67AE593348E8D8551E82770415BC9581EF0EF20DD000DD550CE1E019712102498BD8CD9CA6A4BA567A2ECFA163F118AFD30511CBBA71429C2EC2F74D76059276473045022100AA28592882A3B7C769B32564EDF9F816179D42D8C0E3988567F816E9A5453C6002202BA57DC085B9EB8427FBD58E05B3617183AC216EC7066F03FA4896D8B449490EE1E019712103219642288DEE8A3AA8FEA1F7DAE9ED4D9A9F0EADA1E2DE3DB56DD9598D9AD81776473045022100B9D649491723810B705282B66EAD6075235A1954BEEE054FF68066FADE11DA1C02205D977AE7C6706224A5A3AA75A53E13CA3DE9D67084405CD9BB474554EEE5C070E1",
"tx": { "tx": {
"Account" : "r9A8UyNpW3X46FUc6P7JZqgn6WgAPjBwPg", "Account" : "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh",
"Destination" : "rKT9gDkaedAosiHyHZTjyZs2HvXpzuiGmC", "Destination" : "rKT9gDkaedAosiHyHZTjyZs2HvXpzuiGmC",
"Fee": "10",
"Flags" : 2147483648, "Flags" : 2147483648,
"Sequence": 1,
"TransactionType" : "XChainClaim", "TransactionType" : "XChainClaim",
"XChainClaimProof" : "XChainClaimProof" :
{ {
@@ -17,34 +19,41 @@
}, },
"signatures" : "signatures" :
[ [
{ {
"signature" : "30450221008CC9842A6855A37131FE7FB978675DCF329AC5CD7C881FAF6D13CDC23363059F02203A10475640C2C09541A55098109BB3326D3F49E2304710736A4E3C2773539B01", "XChainProofSig": {
"signing_key" : "aBRDkDojKThV2dNbjvxQDwxtCLkgpd16bVFNCRhGMQKrG1VjGQmi" "Signature" : "30450221008CC9842A6855A37131FE7FB978675DCF329AC5CD7C881FAF6D13CDC23363059F02203A10475640C2C09541A55098109BB3326D3F49E2304710736A4E3C2773539B01",
"PublicKey" : "03ADB44CA8E56F78A0096825E5667C450ABD5C24C34E027BC1AAF7E5BD114CB5B5"
}
}, },
{ {
"signature" : "3045022100B93B51FDBE42634828EEF7F2E5FA950557283648C5D39196ED1B08F8B394959102201F162F43B41D0D9316BF3B7AFA3BC9CA0EC1FCECF6EE8C94F58616BEA2D31F2C", "XChainProofSig": {
"signing_key" : "aBPBY4NyQAGwBHLEYchMNtsGPLmsCDeqXs3dvvTuZffYxgH6pb5Y" "Signature" : "3045022100B93B51FDBE42634828EEF7F2E5FA950557283648C5D39196ED1B08F8B394959102201F162F43B41D0D9316BF3B7AFA3BC9CA0EC1FCECF6EE8C94F58616BEA2D31F2C",
"PublicKey" : "02A14E886B3C3579FBAE3139F29728B903E6F4295AEE92160C8480695524D66A15"
}
}, },
{ {
"signature" : "30440220254038C5D6106246AEBB2A94E60CE79102321FDFD0468AA40C5E7F2DF1C205FE02204CEE5B3BFFDDCB67AE593348E8D8551E82770415BC9581EF0EF20DD000DD550C", "XChainProofSig": {
"signing_key" : "aBPq4y6k28jQNzcNMESzFPhwt1gdNTQCzBMD6iSAW8rEqvuX2fi6" "Signature" : "30440220254038C5D6106246AEBB2A94E60CE79102321FDFD0468AA40C5E7F2DF1C205FE02204CEE5B3BFFDDCB67AE593348E8D8551E82770415BC9581EF0EF20DD000DD550C",
"PublicKey" : "02F7390DCF3352060847B81666EBAC79D52DEA2443BDF58439F75397C45334E2DC"
}
}, },
{ {
"signature" : "3045022100AA28592882A3B7C769B32564EDF9F816179D42D8C0E3988567F816E9A5453C6002202BA57DC085B9EB8427FBD58E05B3617183AC216EC7066F03FA4896D8B449490E", "XChainProofSig": {
"signing_key" : "aB4WteEVakaAcnKq6VDxVA812ehqgr25XpQnycM96RjHUrrh76MH" "Signature" : "3045022100AA28592882A3B7C769B32564EDF9F816179D42D8C0E3988567F816E9A5453C6002202BA57DC085B9EB8427FBD58E05B3617183AC216EC7066F03FA4896D8B449490E",
"PublicKey" : "02498BD8CD9CA6A4BA567A2ECFA163F118AFD30511CBBA71429C2EC2F74D760592"
}
}, },
{ {
"signature" : "3045022100B9D649491723810B705282B66EAD6075235A1954BEEE054FF68066FADE11DA1C02205D977AE7C6706224A5A3AA75A53E13CA3DE9D67084405CD9BB474554EEE5C070", "XChainProofSig": {
"signing_key" : "aBQwsfTh1zikjFDLpkihw6Ug3iyeeHP93P3rZ8pwTDJe7veiRMSH" "Signature" : "3045022100B9D649491723810B705282B66EAD6075235A1954BEEE054FF68066FADE11DA1C02205D977AE7C6706224A5A3AA75A53E13CA3DE9D67084405CD9BB474554EEE5C070",
"PublicKey" : "03219642288DEE8A3AA8FEA1F7DAE9ED4D9A9F0EADA1E2DE3DB56DD9598D9AD817"
}
} }
], ],
"was_src_chain_send" : true, "was_src_chain_send" : true,
"xchain_seq" : 1 "xchain_seq" : 1
} },
"SigningPubKey": "0330E7FC9D56BB25D6893BA3F317AE5BCF33B3291BD63DB32654A313222F7FD020",
"TxnSignature": "304402203CEE125C7EEA970DFC4DEFE89D79666C109EDD8B33FC006DBD8F2CCFC8CFAD4B02200E4210829E31079D623B8C5E6A634611688F50C9D41FF16341407B443B960517"
} }
} }

View File

@@ -21,7 +21,6 @@ describe('SerializedType interfaces', () => {
}) })
test(`${name}.from(json).toJSON() == json`, () => { test(`${name}.from(json).toJSON() == json`, () => {
const newJSON = new Value().toJSON() const newJSON = new Value().toJSON()
console.log(newJSON)
expect(Value.from(newJSON).toJSON()).toEqual(newJSON) expect(Value.from(newJSON).toJSON()).toEqual(newJSON)
}) })
describe(`${name} supports all methods of the SerializedType mixin`, () => { describe(`${name} supports all methods of the SerializedType mixin`, () => {