From 1731206ed9e79b02323ca4dd81a836d064521642 Mon Sep 17 00:00:00 2001 From: Ted Kalaw Date: Tue, 1 Sep 2020 16:24:19 -0700 Subject: [PATCH 1/8] Impelments trustSetToJSON --- src/XRP/serializer.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/XRP/serializer.ts b/src/XRP/serializer.ts index 01cf1d02..67c1a41b 100644 --- a/src/XRP/serializer.ts +++ b/src/XRP/serializer.ts @@ -71,6 +71,7 @@ import { EscrowCancel, EscrowCreate, EscrowFinish, + TrustSet, } from './generated/org/xrpl/rpc/v1/transaction_pb' import XrpUtils from './xrp-utils' @@ -176,6 +177,13 @@ export interface OfferCreateJSON { TakerPays: TakerPaysJSON } +export interface TrustSetJSON { + LimitAmount: LimitAmountJSON, + QualityIn?: QualityInJSON, + QualityOut?: QualityOutJSON, + TransactionType: 'TrustSet' +} + // Generic field representing an OR of all above fields. type TransactionDataJSON = | AccountDeleteJSON @@ -1535,6 +1543,41 @@ const serializer = { return this.currencyAmountToJSON(currencyAmount) }, + + /** + * Convert a TrustSet to a JSON representation. + * + * @param trustSet - The TrustSet to convert. + * @returns The TrustSet as JSON. + */ + trustSetToJSON(trustSet: TrustSet): TrustSetJSON | undefined { + const limitAmount = trustSet.getLimitAmount() + if (limitAmount === undefined) { + return undefined + } + + const limitAmountJson = this.limitAmountToJSON(limitAmount) + if (limitAmountJson === undefined) { + return undefined + } + + const json: TrustSetJSON = { + LimitAmount: limitAmountJson, + TransactionType: 'TrustSet', + } + + const qualityIn = trustSet.getQualityIn() + if (qualityIn !== undefined) { + json.QualityIn = this.qualityInToJSON(qualityIn) + } + + const qualityOut = trustSet.getQualityOut() + if (qualityOut !== undefined) { + json.QualityOut = this.qualityOutToJSON(qualityOut) + } + + return json + } } export default serializer From c9b1bc1e336148473659b8d7bb9bbf8942d1ef31 Mon Sep 17 00:00:00 2001 From: Ted Kalaw Date: Tue, 1 Sep 2020 16:34:42 -0700 Subject: [PATCH 2/8] Adds more tests --- src/XRP/serializer.ts | 26 +++++----- test/XRP/serializer.test.ts | 97 ++++++++++++++++++++++++++++++++++--- 2 files changed, 103 insertions(+), 20 deletions(-) diff --git a/src/XRP/serializer.ts b/src/XRP/serializer.ts index 67c1a41b..815afb9d 100644 --- a/src/XRP/serializer.ts +++ b/src/XRP/serializer.ts @@ -178,9 +178,9 @@ export interface OfferCreateJSON { } export interface TrustSetJSON { - LimitAmount: LimitAmountJSON, - QualityIn?: QualityInJSON, - QualityOut?: QualityOutJSON, + LimitAmount: LimitAmountJSON + QualityIn?: QualityInJSON + QualityOut?: QualityOutJSON TransactionType: 'TrustSet' } @@ -1423,7 +1423,7 @@ const serializer = { return signerWeight.getValue() }, - /** + /** * Convert a Channel to a JSON representation. * * @param channel - The Channel to convert. @@ -1433,7 +1433,7 @@ const serializer = { return Utils.toHex(channel.getValue_asU8()) }, - /** + /** * Convert a SignerQuorum to a JSON representation. * * @param signerQuorum - The SignerQuorum to convert. @@ -1442,7 +1442,7 @@ const serializer = { signerQuorumToJSON(signerQuorum: SignerQuorum): SignerQuorumJSON | undefined { return signerQuorum.getValue() }, - + /** * Convert an OfferCreate to a JSON representation. * @@ -1481,8 +1481,8 @@ const serializer = { return json }, - - /** + + /** * Convert a RegularKey to a JSON representation. * * @param regularKey - The RegularKey to convert. @@ -1506,7 +1506,7 @@ const serializer = { settleDelayToJSON(settleDelay: SettleDelay): SettleDelayJSON { return settleDelay.getValue() }, - + /** * Convert a PaymentChannelSignature to a JSON representation. * @@ -1518,7 +1518,7 @@ const serializer = { ): PaymentChannelSignatureJSON { return Utils.toHex(paymentChannelSignature.getValue_asU8()) }, - + /** * Convert a PublicKey to a JSON representation. * @@ -1528,7 +1528,7 @@ const serializer = { publicKeyToJSON(publicKey: PublicKey): PublicKeyJSON { return Utils.toHex(publicKey.getValue_asU8()) }, - + /** * Convert a Balance to a JSON representation. * @@ -1546,7 +1546,7 @@ const serializer = { /** * Convert a TrustSet to a JSON representation. - * + * * @param trustSet - The TrustSet to convert. * @returns The TrustSet as JSON. */ @@ -1577,7 +1577,7 @@ const serializer = { } return json - } + }, } export default serializer diff --git a/test/XRP/serializer.test.ts b/test/XRP/serializer.test.ts index 14fb9ecc..c1b91706 100644 --- a/test/XRP/serializer.test.ts +++ b/test/XRP/serializer.test.ts @@ -76,6 +76,7 @@ import { EscrowCreate, EscrowFinish, OfferCancel, + TrustSet, } from '../../src/XRP/generated/org/xrpl/rpc/v1/transaction_pb' import Serializer, { CheckCreateJSON, @@ -87,6 +88,7 @@ import Serializer, { TransactionJSON, OfferCreateJSON, PaymentJSON, + TrustSetJSON, } from '../../src/XRP/serializer' import XrpUtils from '../../src/XRP/xrp-utils' @@ -2243,7 +2245,7 @@ describe('serializer', function (): void { // THEN the output is the input encoded as hex. assert.equal(serialized, Utils.toHex(channelValue)) }) - + it('Serializes an OfferCreate with only mandatory fields', function (): void { // GIVEN a OfferCreate with mandatory fields set. const takerPays = new TakerPays() @@ -2325,7 +2327,7 @@ describe('serializer', function (): void { // THEN the result is undefined. assert.isUndefined(serialized) }) - + it('Serializes a PublicKey', function (): void { // GIVEN a PublicKey. const publicKeyValue = new Uint8Array([1, 2, 3, 4]) @@ -2339,7 +2341,7 @@ describe('serializer', function (): void { // THEN the output is the input encoded as hex. assert.equal(serialized, Utils.toHex(publicKeyValue)) }) - + it('Serializes a Balance', function (): void { // GIVEN a Balance. const currencyAmount = makeXrpCurrencyAmount('10') @@ -2364,7 +2366,7 @@ describe('serializer', function (): void { // THEN the result is undefined. assert.isUndefined(serialized) }) - + it('Converts a PathList', function (): void { // GIVEN a Path list with two paths. const path1Element1 = makePathElement( @@ -2588,7 +2590,7 @@ describe('serializer', function (): void { // THEN the result is as expected. assert.equal(serialized, settleDelayValue) }) - + it('Serializes a PaymentChannelSignature', function (): void { // GIVEN a PaymentChannelSignature. const paymentChannelSignatureValue = new Uint8Array([1, 2, 3, 4]) @@ -2604,7 +2606,7 @@ describe('serializer', function (): void { // THEN the output is the input encoded as hex. assert.equal(serialized, Utils.toHex(paymentChannelSignatureValue)) }) - + it('Serializes a Fulfillment', function (): void { // GIVEN a Fulfillment with some bytes. const fulfillmentBytes = new Uint8Array([0, 1, 2, 3]) @@ -2631,7 +2633,7 @@ describe('serializer', function (): void { // THEN the result is as expected. assert.equal(serialized, signerWeightValue) }) - + it('Serializes an EscrowFinish with required fields', function (): void { // GIVEN an EscrowFinish with required fields. const offerSequence = new OfferSequence() @@ -2761,4 +2763,85 @@ describe('serializer', function (): void { // THEN the result is undefined. assert.isUndefined(serialized) }) + + it('Serializes a TrustSet with required fields', function (): void { + // GIVEN a TrustSet with required fields. + const currencyAmount = makeXrpCurrencyAmount('10') + + const limitAmount = new LimitAmount() + limitAmount.setValue(currencyAmount) + + const trustSet = new TrustSet() + trustSet.setLimitAmount(limitAmount) + + // WHEN the TrustSet is serialized. + const serialized = Serializer.trustSetToJSON(trustSet) + + // THEN the result is as expected. + const expected: TrustSetJSON = { + LimitAmount: Serializer.limitAmountToJSON(limitAmount)!, + TransactionType: 'TrustSet', + } + + assert.deepEqual(serialized, expected) + }) + + it('Serializes a TrustSet with all fields', function (): void { + // GIVEN a TrustSet with all fields. + const currencyAmount = makeXrpCurrencyAmount('10') + + const limitAmount = new LimitAmount() + limitAmount.setValue(currencyAmount) + + const qualityInValue = 6 + const qualityIn = new QualityIn() + qualityIn.setValue(qualityInValue) + + const qualityOutValue = 7 + const qualityOut = new QualityOut() + qualityOut.setValue(qualityOutValue) + + const trustSet = new TrustSet() + trustSet.setLimitAmount(limitAmount) + trustSet.setQualityIn(qualityIn) + trustSet.setQualityOut(qualityOut) + + // WHEN the TrustSet is serialized. + const serialized = Serializer.trustSetToJSON(trustSet) + + // THEN the result is as expected. + const expected: TrustSetJSON = { + LimitAmount: Serializer.limitAmountToJSON(limitAmount)!, + QualityIn: Serializer.qualityInToJSON(qualityIn), + QualityOut: Serializer.qualityOutToJSON(qualityOut), + TransactionType: 'TrustSet', + } + + assert.deepEqual(serialized, expected) + }) + + it('Fails to serialize a TrustSet missing limitAmount', function (): void { + // GIVEN a TrustSet missing a limitAmount. + const trustSet = new TrustSet() + + // WHEN the TrustSet is serialized. + const serialized = Serializer.trustSetToJSON(trustSet) + + // THEN the result is undefined. + assert.isUndefined(serialized) + }) + + it('Fails to serialize a TrustSet with malformed limitAmount', function (): void { + // GIVEN a TrustSet with a malformed limitAmount. + const limitAmount = new LimitAmount() + + const trustSet = new TrustSet() + trustSet.setLimitAmount(limitAmount) + + // WHEN the TrustSet is serialized. + const serialized = Serializer.trustSetToJSON(trustSet) + + // THEN the result is undefined. + assert.isUndefined(serialized) + }) }) From db2fa9793f205f9b18aaa928b4a37c5920718e7a Mon Sep 17 00:00:00 2001 From: Keefer Taylor Date: Tue, 1 Sep 2020 16:39:17 -0700 Subject: [PATCH 3/8] Serialize signerentry --- src/XRP/serializer.ts | 47 +++++++++++++++++++++----- test/XRP/serializer.test.ts | 67 +++++++++++++++++++++++++++++++++---- 2 files changed, 99 insertions(+), 15 deletions(-) diff --git a/src/XRP/serializer.ts b/src/XRP/serializer.ts index 01cf1d02..041ed811 100644 --- a/src/XRP/serializer.ts +++ b/src/XRP/serializer.ts @@ -55,6 +55,7 @@ import { QualityIn, QualityOut, LimitAmount, + SignerEntry, } from './generated/org/xrpl/rpc/v1/common_pb' import { AccountSet, @@ -227,6 +228,11 @@ export type TransactionJSON = /** * Types for serialized sub-objects. */ +interface SignerEntryJSON { + Account: AccountJSON + SignerWeight: SignerWeightJSON +} + interface MemoJSON { Memo?: MemoDetailsJSON } @@ -1415,7 +1421,7 @@ const serializer = { return signerWeight.getValue() }, - /** + /** * Convert a Channel to a JSON representation. * * @param channel - The Channel to convert. @@ -1425,7 +1431,7 @@ const serializer = { return Utils.toHex(channel.getValue_asU8()) }, - /** + /** * Convert a SignerQuorum to a JSON representation. * * @param signerQuorum - The SignerQuorum to convert. @@ -1434,7 +1440,7 @@ const serializer = { signerQuorumToJSON(signerQuorum: SignerQuorum): SignerQuorumJSON | undefined { return signerQuorum.getValue() }, - + /** * Convert an OfferCreate to a JSON representation. * @@ -1473,8 +1479,8 @@ const serializer = { return json }, - - /** + + /** * Convert a RegularKey to a JSON representation. * * @param regularKey - The RegularKey to convert. @@ -1498,7 +1504,7 @@ const serializer = { settleDelayToJSON(settleDelay: SettleDelay): SettleDelayJSON { return settleDelay.getValue() }, - + /** * Convert a PaymentChannelSignature to a JSON representation. * @@ -1510,7 +1516,7 @@ const serializer = { ): PaymentChannelSignatureJSON { return Utils.toHex(paymentChannelSignature.getValue_asU8()) }, - + /** * Convert a PublicKey to a JSON representation. * @@ -1520,7 +1526,7 @@ const serializer = { publicKeyToJSON(publicKey: PublicKey): PublicKeyJSON { return Utils.toHex(publicKey.getValue_asU8()) }, - + /** * Convert a Balance to a JSON representation. * @@ -1535,6 +1541,31 @@ const serializer = { return this.currencyAmountToJSON(currencyAmount) }, + + /** + * Convert a SignerEntry to a JSON representation. + * + * @param signerEntry - The SignerEntry to convert. + * @returns The SignerEntry as JSON. + */ + signerEntryToJSON(signerEntry: SignerEntry): SignerEntryJSON | undefined { + const account = signerEntry.getAccount() + const signerWeight = signerEntry.getSignerWeight() + if (account === undefined || signerWeight === undefined) { + return undefined + } + + const accountJSON = this.accountToJSON(account) + const signerWeightJSON = this.signerWeightToJSON(signerWeight) + if (accountJSON === undefined || signerWeightJSON === undefined) { + return undefined + } + + return { + Account: accountJSON, + SignerWeight: signerWeightJSON, + } + }, } export default serializer diff --git a/test/XRP/serializer.test.ts b/test/XRP/serializer.test.ts index 14fb9ecc..564f43ba 100644 --- a/test/XRP/serializer.test.ts +++ b/test/XRP/serializer.test.ts @@ -60,6 +60,7 @@ import { QualityIn, QualityOut, LimitAmount, + SignerEntry, } from '../../src/XRP/generated/org/xrpl/rpc/v1/common_pb' import { Memo, @@ -2243,7 +2244,7 @@ describe('serializer', function (): void { // THEN the output is the input encoded as hex. assert.equal(serialized, Utils.toHex(channelValue)) }) - + it('Serializes an OfferCreate with only mandatory fields', function (): void { // GIVEN a OfferCreate with mandatory fields set. const takerPays = new TakerPays() @@ -2325,7 +2326,7 @@ describe('serializer', function (): void { // THEN the result is undefined. assert.isUndefined(serialized) }) - + it('Serializes a PublicKey', function (): void { // GIVEN a PublicKey. const publicKeyValue = new Uint8Array([1, 2, 3, 4]) @@ -2339,7 +2340,7 @@ describe('serializer', function (): void { // THEN the output is the input encoded as hex. assert.equal(serialized, Utils.toHex(publicKeyValue)) }) - + it('Serializes a Balance', function (): void { // GIVEN a Balance. const currencyAmount = makeXrpCurrencyAmount('10') @@ -2364,7 +2365,7 @@ describe('serializer', function (): void { // THEN the result is undefined. assert.isUndefined(serialized) }) - + it('Converts a PathList', function (): void { // GIVEN a Path list with two paths. const path1Element1 = makePathElement( @@ -2588,7 +2589,7 @@ describe('serializer', function (): void { // THEN the result is as expected. assert.equal(serialized, settleDelayValue) }) - + it('Serializes a PaymentChannelSignature', function (): void { // GIVEN a PaymentChannelSignature. const paymentChannelSignatureValue = new Uint8Array([1, 2, 3, 4]) @@ -2604,7 +2605,7 @@ describe('serializer', function (): void { // THEN the output is the input encoded as hex. assert.equal(serialized, Utils.toHex(paymentChannelSignatureValue)) }) - + it('Serializes a Fulfillment', function (): void { // GIVEN a Fulfillment with some bytes. const fulfillmentBytes = new Uint8Array([0, 1, 2, 3]) @@ -2631,7 +2632,7 @@ describe('serializer', function (): void { // THEN the result is as expected. assert.equal(serialized, signerWeightValue) }) - + it('Serializes an EscrowFinish with required fields', function (): void { // GIVEN an EscrowFinish with required fields. const offerSequence = new OfferSequence() @@ -2761,4 +2762,56 @@ describe('serializer', function (): void { // THEN the result is undefined. assert.isUndefined(serialized) }) + + it('Serializes a SignerEntry', function (): void { + // GIVEN a SignerEntry + const account = new Account() + account.setValue(testAccountAddress) + + const signerWeight = new SignerWeight() + signerWeight.setValue(1) + + const signerEntry = new SignerEntry() + signerEntry.setAccount(account) + signerEntry.setSignerWeight(signerWeight) + + // WHEN the SignerEntry is serialized. + const serialized = Serializer.signerEntryToJSON(signerEntry) + + // THEN the result is the expected form. + const expected = { + Account: Serializer.accountToJSON(account)!, + SignerWeight: Serializer.signerWeightToJSON(signerWeight)!, + } + assert.deepEqual(serialized, expected) + }) + + it('Fails to serialize a SignerEntry with malformed components', function (): void { + // GIVEN a SignerEntry with a malformed account + const account = new Account() + + const signerWeight = new SignerWeight() + signerWeight.setValue(1) + + const signerEntry = new SignerEntry() + signerEntry.setAccount(account) + signerEntry.setSignerWeight(signerWeight) + + // WHEN the SignerEntry is serialized. + const serialized = Serializer.signerEntryToJSON(signerEntry) + + // THEN the result is undefined + assert.isUndefined(serialized) + }) + + it('Fails to serialize a malformed SignerEntry', function (): void { + // GIVEN a malformed SignerEntry + const signerEntry = new SignerEntry() + + // WHEN the SignerEntry is serialized. + const serialized = Serializer.signerEntryToJSON(signerEntry) + + // THEN the result is undefined + assert.isUndefined(serialized) + }) }) From 85ec5d19a8e330565e827b8bf7c87c7844c36e64 Mon Sep 17 00:00:00 2001 From: Keefer Taylor Date: Tue, 1 Sep 2020 16:56:57 -0700 Subject: [PATCH 4/8] serialize lists of signer entries --- src/XRP/serializer.ts | 24 ++++++++++++++++ test/XRP/serializer.test.ts | 57 +++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/src/XRP/serializer.ts b/src/XRP/serializer.ts index 041ed811..e9343b93 100644 --- a/src/XRP/serializer.ts +++ b/src/XRP/serializer.ts @@ -1566,6 +1566,30 @@ const serializer = { SignerWeight: signerWeightJSON, } }, + + /** + * Convert a list of SignerEntry to a JSON representation. + * + * If any entry in the list fails conversion, this method will return undefined. + * + * @param signerEntryList - The list of `SignerEntry`s to convert. + * @returns A list of the same `SignerEntry`s as JSON objects. + */ + signerEntryListToJSON( + signerEntryList: SignerEntry[], + ): SignerEntryJSON[] | undefined { + const signerEntryListJSON: SignerEntryJSON[] = [] + for (const signerEntry of signerEntryList) { + const signerEntryJSON = this.signerEntryToJSON(signerEntry) + if (signerEntryJSON === undefined) { + return undefined + } + + signerEntryListJSON.push(signerEntryJSON) + } + + return signerEntryListJSON + }, } export default serializer diff --git a/test/XRP/serializer.test.ts b/test/XRP/serializer.test.ts index 564f43ba..c913524e 100644 --- a/test/XRP/serializer.test.ts +++ b/test/XRP/serializer.test.ts @@ -2814,4 +2814,61 @@ describe('serializer', function (): void { // THEN the result is undefined assert.isUndefined(serialized) }) + + it('Serializes a list of signer entries', function (): void { + // GIVEN a list of signer entries. + const account1 = new Account() + account1.setValue(makeAccountAddress('r1')) + + const signerWeight1 = new SignerWeight() + signerWeight1.setValue(1) + + const signerEntry1 = new SignerEntry() + signerEntry1.setAccount(account1) + signerEntry1.setSignerWeight(signerWeight1) + + const account2 = new Account() + account2.setValue(makeAccountAddress('r2')) + + const signerWeight2 = new SignerWeight() + signerWeight2.setValue(2) + + const signerEntry2 = new SignerEntry() + signerEntry2.setAccount(account2) + signerEntry2.setSignerWeight(signerWeight2) + + const signerEntryList = [signerEntry1, signerEntry2] + + // WHEN the list is serialized. + const serialized = Serializer.signerEntryListToJSON(signerEntryList) + + // THEN the result is the serialized versions of the list elements. + const expected = [ + Serializer.signerEntryToJSON(signerEntry1)!, + Serializer.signerEntryToJSON(signerEntry2)!, + ] + assert.deepEqual(serialized, expected) + }) + + it('Fails to serialize a list of signer entries where an entry is malformed', function (): void { + // GIVEN a list of signer entries with a malformed second entry.. + const account1 = new Account() + account1.setValue(makeAccountAddress('r1')) + + const signerWeight1 = new SignerWeight() + signerWeight1.setValue(1) + + const signerEntry1 = new SignerEntry() + signerEntry1.setAccount(account1) + signerEntry1.setSignerWeight(signerWeight1) + + const signerEntry2 = new SignerEntry() + const signerEntryList = [signerEntry1, signerEntry2] + + // WHEN the list is serialized. + const serialized = Serializer.signerEntryListToJSON(signerEntryList) + + // THEN the result is undefined. + assert.isUndefined(serialized) + }) }) From d7e2904c3ce27bf5aee4af891af78975d291787b Mon Sep 17 00:00:00 2001 From: Keefer Taylor Date: Tue, 1 Sep 2020 17:12:10 -0700 Subject: [PATCH 5/8] SignerListSet ser --- src/XRP/serializer.ts | 40 ++++++++++++++++++- test/XRP/serializer.test.ts | 78 ++++++++++++++++++++++++++++++++++++- 2 files changed, 116 insertions(+), 2 deletions(-) diff --git a/src/XRP/serializer.ts b/src/XRP/serializer.ts index e9343b93..6e7fef8d 100644 --- a/src/XRP/serializer.ts +++ b/src/XRP/serializer.ts @@ -72,6 +72,7 @@ import { EscrowCancel, EscrowCreate, EscrowFinish, + SignerListSet, } from './generated/org/xrpl/rpc/v1/transaction_pb' import XrpUtils from './xrp-utils' @@ -177,6 +178,12 @@ export interface OfferCreateJSON { TakerPays: TakerPaysJSON } +export interface SignerListSetJSON { + SignerQuorum: SignerQuorumJSON + SignerEntries: SignerEntryJSON[] + TransactionType: 'SignerListSet' +} + // Generic field representing an OR of all above fields. type TransactionDataJSON = | AccountDeleteJSON @@ -191,6 +198,7 @@ type TransactionDataJSON = | OfferCancelJSON | OfferCreateJSON | PaymentJSON + | SignerListSetJSON /** * Individual Transaction Types. @@ -207,6 +215,7 @@ type EscrowCancelTransactionJSON = BaseTransactionJSON & EscrowCancelJSON type EscrowCreateTransactionJSON = BaseTransactionJSON & EscrowCreateJSON type EscrowFinishTransactionJSON = BaseTransactionJSON & EscrowFinishJSON type PaymentTransactionJSON = BaseTransactionJSON & PaymentJSON +type SignerListSetTransactionJSON = BaseTransactionJSON & SignerListSetJSON /** * All Transactions. @@ -224,6 +233,7 @@ export type TransactionJSON = | OfferCancelTransactionJSON | OfferCreateTransactionJSON | PaymentTransactionJSON + | SignerListSetTransactionJSON /** * Types for serialized sub-objects. @@ -1437,7 +1447,7 @@ const serializer = { * @param signerQuorum - The SignerQuorum to convert. * @returns The SignerQuorum as JSON. */ - signerQuorumToJSON(signerQuorum: SignerQuorum): SignerQuorumJSON | undefined { + signerQuorumToJSON(signerQuorum: SignerQuorum): SignerQuorumJSON { return signerQuorum.getValue() }, @@ -1590,6 +1600,34 @@ const serializer = { return signerEntryListJSON }, + + /** + * Convert a SignerListSEt to a JSON representation. + * + * @param signerListSet - The SignerListSEt to convert. + * @returns The SignerListSEt as JSON. + */ + signerListSetToJSON( + signerListSet: SignerListSet, + ): SignerListSetJSON | undefined { + const signerQuorum = signerListSet.getSignerQuorum() + const signerEntryList = signerListSet.getSignerEntriesList() + if (signerQuorum === undefined) { + return undefined + } + + const signerQuorumJSON = this.signerQuorumToJSON(signerQuorum) + const signerEntriesJSON = this.signerEntryListToJSON(signerEntryList) + if (signerEntriesJSON === undefined) { + return undefined + } + + return { + SignerQuorum: signerQuorumJSON, + SignerEntries: signerEntriesJSON, + TransactionType: 'SignerListSet', + } + }, } export default serializer diff --git a/test/XRP/serializer.test.ts b/test/XRP/serializer.test.ts index c913524e..c8a37158 100644 --- a/test/XRP/serializer.test.ts +++ b/test/XRP/serializer.test.ts @@ -77,6 +77,7 @@ import { EscrowCreate, EscrowFinish, OfferCancel, + SignerListSet, } from '../../src/XRP/generated/org/xrpl/rpc/v1/transaction_pb' import Serializer, { CheckCreateJSON, @@ -88,6 +89,7 @@ import Serializer, { TransactionJSON, OfferCreateJSON, PaymentJSON, + SignerListSetJSON, } from '../../src/XRP/serializer' import XrpUtils from '../../src/XRP/xrp-utils' @@ -2851,7 +2853,7 @@ describe('serializer', function (): void { }) it('Fails to serialize a list of signer entries where an entry is malformed', function (): void { - // GIVEN a list of signer entries with a malformed second entry.. + // GIVEN a list of signer entries with a malformed second entry. const account1 = new Account() account1.setValue(makeAccountAddress('r1')) @@ -2871,4 +2873,78 @@ describe('serializer', function (): void { // THEN the result is undefined. assert.isUndefined(serialized) }) + + it('Serializes a SignerListSet', function (): void { + // GIVEN a SignerListSet + const signerQuorum = new SignerQuorum() + signerQuorum.setValue(1) + + const account1 = new Account() + account1.setValue(makeAccountAddress('r1')) + + const signerWeight1 = new SignerWeight() + signerWeight1.setValue(1) + + const signerEntry1 = new SignerEntry() + signerEntry1.setAccount(account1) + signerEntry1.setSignerWeight(signerWeight1) + + const account2 = new Account() + account2.setValue(makeAccountAddress('r2')) + + const signerWeight2 = new SignerWeight() + signerWeight2.setValue(2) + + const signerEntry2 = new SignerEntry() + signerEntry2.setAccount(account2) + signerEntry2.setSignerWeight(signerWeight2) + + const signerEntriesList = [signerEntry1, signerEntry2] + + const signerListSet = new SignerListSet() + signerListSet.setSignerQuorum(signerQuorum) + signerListSet.setSignerEntriesList(signerEntriesList) + + // WHEN it is serialized. + const serialized = Serializer.signerListSetToJSON(signerListSet) + + // THEN the result is the expected form. + const expected: SignerListSetJSON = { + SignerEntries: Serializer.signerEntryListToJSON(signerEntriesList)!, + SignerQuorum: Serializer.signerQuorumToJSON(signerQuorum)!, + TransactionType: 'SignerListSet', + } + assert.deepEqual(serialized, expected) + }) + + it('Fails to serialize a SignerListSet with malformed components', function (): void { + // GIVEN a SignerListSet with a malformed SignerEntriesList. + const signerQuorum = new SignerQuorum() + signerQuorum.setValue(1) + + const signerEntry = new SignerEntry() + + const signerEntriesList = [signerEntry] + + const signerListSet = new SignerListSet() + signerListSet.setSignerQuorum(signerQuorum) + signerListSet.setSignerEntriesList(signerEntriesList) + + // WHEN it is serialized. + const serialized = Serializer.signerListSetToJSON(signerListSet) + + // THEN the result is undefined. + assert.isUndefined(serialized) + }) + + it('Fails to serialize a malformed SignerListSet', function (): void { + // GIVEN a malformd SignerListSet. + const signerListSet = new SignerListSet() + + // WHEN it is serialized. + const serialized = Serializer.signerListSetToJSON(signerListSet) + + // THEN the result is undefined. + assert.isUndefined(serialized) + }) }) From b703de7d42c4b5cca098d7adb88870d06f82a3f3 Mon Sep 17 00:00:00 2001 From: Keefer Taylor Date: Wed, 2 Sep 2020 20:05:04 -0700 Subject: [PATCH 6/8] Finish Transaction Data --- src/XRP/serializer.ts | 111 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 106 insertions(+), 5 deletions(-) diff --git a/src/XRP/serializer.ts b/src/XRP/serializer.ts index f4297675..68a2559f 100644 --- a/src/XRP/serializer.ts +++ b/src/XRP/serializer.ts @@ -1928,6 +1928,13 @@ function getNormalizedAccount(transaction: Transaction): string | undefined { * * @throws An error if given a transaction that we do not know how to handle. */ +| PaymentJSON +| SignerListSetJSON +| PaymentChannelClaimJSON +| PaymentChannelCreateJSON +| PaymentChannelFundJSON +| SetRegularKeyJSON +// TODO(keefertaylor): There is no reason this should be separate from the serializer functionality. Move into `serializer` object. // eslint-disable-next-line max-statements -- No clear way to make this more succinct because gRPC is verbose function getAdditionalTransactionData( transaction: Transaction, @@ -1940,7 +1947,6 @@ function getAdditionalTransactionData( if (accountDelete === undefined) { return undefined } - return serializer.accountDeleteToJSON(accountDelete) } case Transaction.TransactionDataCase.ACCOUNT_SET: { @@ -1950,24 +1956,119 @@ function getAdditionalTransactionData( } return serializer.accountSetToJSON(accountSet) } + case Transaction.TransactionDataCase.CHECK_CANCEL: { + const checkCancel = transaction.getCheckCancel() + if (checkCancel === undefined) { + return undefined + } + return serializer.checkCancelToJSON(checkCancel) + } + case Transaction.TransactionDataCase.CHECK_CASH: { + const checkCash = transaction.getCheckCash() + if (checkCash === undefined) { + return undefined + } + return serializer.checkCashToJSON(checkCash) + } + case Transaction.TransactionDataCase.CHECK_CREATE: { + const checkCreate = transaction.getCheckCreate() + if (checkCreate === undefined) { + return undefined + } + return serializer.checkCreateToJSON(checkCreate) + } case Transaction.TransactionDataCase.DEPOSIT_PREAUTH: { const depositPreauth = transaction.getDepositPreauth() if (depositPreauth === undefined) { return undefined } - return serializer.depositPreauthToJSON(depositPreauth) } + case Transaction.TransactionDataCase.ESCROW_CANCEL: { + const escrowCancel = transaction.getEscrowCancel() + if (escrowCancel === undefined) { + return undefined + } + return serializer.escrowCancelToJSON(escrowCancel) + } + case Transaction.TransactionDataCase.ESCROW_CREATE: { + const escrowCreate = transaction.getEscrowCreate() + if (escrowCreate === undefined) { + return undefined + } + return serializer.escrowCreateToJSON(escrowCreate) + } + case Transaction.TransactionDataCase.ESCROW_FINISH: { + const escrowFinish = transaction.getEscrowFinish() + if (escrowFinish === undefined) { + return undefined + } + return serializer.escrowFinishToJSON(escrowFinish) + } + case Transaction.TransactionDataCase.OFFER_CANCEL: { + const offerCancel = transaction.getOfferCancel() + if (offerCancel === undefined) { + return undefined + } + return serializer.offerCancelToJSON(offerCancel) + } + case Transaction.TransactionDataCase.OFFER_CREATE: { + const offerCreate = transaction.getOfferCreate() + if (offerCreate === undefined) { + return undefined + } + return serializer.offerCreateToJSON(offerCreate) + } case Transaction.TransactionDataCase.PAYMENT: { const payment = transaction.getPayment() if (payment === undefined) { return undefined } - return serializer.paymentToJSON(payment) } - + case Transaction.TransactionDataCase.PAYMENT_CHANNEL_CLAIM: { + const paymentChannelClaim = transaction.getPaymentChannelClaim() + if (paymentChannelClaim === undefined) { + return undefined + } + return serializer.paymentChannelClaimToJSON(paymentChannelClaim) + } + case Transaction.TransactionDataCase.PAYMENT_CHANNEL_CREATE: { + const paymentChannelCreate = transaction.getPaymentChannelCreate() + if (paymentChannelCreate === undefined) { + return undefined + } + return serializer.paymentChannelCreateToJSON(paymentChannelCreate) + } + case Transaction.TransactionDataCase.PAYMENT_CHANNEL_FUND: { + const paymentChannelFund = transaction.getPaymentChannelFund() + if (paymentChannelFund === undefined) { + return undefined + } + return serializer.paymentChannelFundToJSON(paymentChannelFund) + } + case Transaction.TransactionDataCase.SET_REGULAR_KEY: { + const setRegularKey = transaction.getSetRegularKey() + if (setRegularKey === undefined) { + return undefined + } + return serializer.setRegularKeyToJSON(setRegularKey) + } + case Transaction.TransactionDataCase.SIGNER_LIST_SET: { + const signerListSet = transaction.getSignerListSet() + if (signerListSet === undefined) { + return undefined + } + return serializer.signerListSetToJSON(signerListSet) + } + case Transaction.TransactionDataCase.TRUST_SET: { + const trustSet = transaction.getTrustSet() + if (trustSet === undefined) { + return undefined + } + return serializer.trustSetToJSON(trustSet) + } default: - throw new Error('Unexpected transactionDataCase') + throw new Error('Unexpected TransactionDataCase') } } From 4acf5c20b19cd3c6cb9f2fde5283d12094d806a4 Mon Sep 17 00:00:00 2001 From: Keefer Taylor Date: Wed, 2 Sep 2020 20:30:22 -0700 Subject: [PATCH 7/8] more work --- src/XRP/serializer.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/XRP/serializer.ts b/src/XRP/serializer.ts index 68a2559f..07559e70 100644 --- a/src/XRP/serializer.ts +++ b/src/XRP/serializer.ts @@ -1928,12 +1928,6 @@ function getNormalizedAccount(transaction: Transaction): string | undefined { * * @throws An error if given a transaction that we do not know how to handle. */ -| PaymentJSON -| SignerListSetJSON -| PaymentChannelClaimJSON -| PaymentChannelCreateJSON -| PaymentChannelFundJSON -| SetRegularKeyJSON // TODO(keefertaylor): There is no reason this should be separate from the serializer functionality. Move into `serializer` object. // eslint-disable-next-line max-statements -- No clear way to make this more succinct because gRPC is verbose function getAdditionalTransactionData( From 248343c1f68731d72004f1251dc9114ef7b37147 Mon Sep 17 00:00:00 2001 From: Keefer Taylor Date: Thu, 3 Sep 2020 16:51:31 -0700 Subject: [PATCH 8/8] fix build --- src/XRP/serializer.ts | 3 + test/XRP/serializer.test.ts | 160 ------------------------------------ 2 files changed, 3 insertions(+), 160 deletions(-) diff --git a/src/XRP/serializer.ts b/src/XRP/serializer.ts index 13da4398..2e798110 100644 --- a/src/XRP/serializer.ts +++ b/src/XRP/serializer.ts @@ -251,6 +251,7 @@ type TransactionDataJSON = | PaymentChannelCreateJSON | PaymentChannelFundJSON | SetRegularKeyJSON + | TrustSetJSON /** * Individual Transaction Types. @@ -275,6 +276,7 @@ type PaymentChannelCreateTransactionJSON = BaseTransactionJSON & type PaymentChannelFundTransactionJSON = BaseTransactionJSON & PaymentChannelFundJSON type SetRegularKeyTransactionJSON = BaseTransactionJSON & SetRegularKeyJSON +type TrustSetTransactionJSON = BaseTransactionJSON & TrustSetJSON /** * All Transactions. @@ -297,6 +299,7 @@ export type TransactionJSON = | PaymentChannelClaimTransactionJSON | PaymentChannelFundTransactionJSON | SetRegularKeyTransactionJSON + | TrustSetTransactionJSON /** * Types for serialized sub-objects. diff --git a/test/XRP/serializer.test.ts b/test/XRP/serializer.test.ts index d995d6ba..07df69b3 100644 --- a/test/XRP/serializer.test.ts +++ b/test/XRP/serializer.test.ts @@ -3079,166 +3079,6 @@ describe('serializer', function (): void { assert.deepEqual(serialized, expected) }) - it('Fails to serialize a SignerEntry with malformed components', function (): void { - // GIVEN a SignerEntry with a malformed account - const account = new Account() - - const signerWeight = new SignerWeight() - signerWeight.setValue(1) - - const signerEntry = new SignerEntry() - signerEntry.setAccount(account) - signerEntry.setSignerWeight(signerWeight) - - // WHEN the SignerEntry is serialized. - const serialized = Serializer.signerEntryToJSON(signerEntry) - - // THEN the result is undefined - assert.isUndefined(serialized) - }) - - it('Fails to serialize a malformed SignerEntry', function (): void { - // GIVEN a malformed SignerEntry - const signerEntry = new SignerEntry() - - // WHEN the SignerEntry is serialized. - const serialized = Serializer.signerEntryToJSON(signerEntry) - - // THEN the result is undefined - assert.isUndefined(serialized) - }) - - it('Serializes a list of signer entries', function (): void { - // GIVEN a list of signer entries. - const account1 = new Account() - account1.setValue(makeAccountAddress('r1')) - - const signerWeight1 = new SignerWeight() - signerWeight1.setValue(1) - - const signerEntry1 = new SignerEntry() - signerEntry1.setAccount(account1) - signerEntry1.setSignerWeight(signerWeight1) - - const account2 = new Account() - account2.setValue(makeAccountAddress('r2')) - - const signerWeight2 = new SignerWeight() - signerWeight2.setValue(2) - - const signerEntry2 = new SignerEntry() - signerEntry2.setAccount(account2) - signerEntry2.setSignerWeight(signerWeight2) - - const signerEntryList = [signerEntry1, signerEntry2] - - // WHEN the list is serialized. - const serialized = Serializer.signerEntryListToJSON(signerEntryList) - - // THEN the result is the serialized versions of the list elements. - const expected = [ - Serializer.signerEntryToJSON(signerEntry1)!, - Serializer.signerEntryToJSON(signerEntry2)!, - ] - assert.deepEqual(serialized, expected) - }) - - it('Fails to serialize a list of signer entries where an entry is malformed', function (): void { - // GIVEN a list of signer entries with a malformed second entry. - const account1 = new Account() - account1.setValue(makeAccountAddress('r1')) - - const signerWeight1 = new SignerWeight() - signerWeight1.setValue(1) - - const signerEntry1 = new SignerEntry() - signerEntry1.setAccount(account1) - signerEntry1.setSignerWeight(signerWeight1) - - const signerEntry2 = new SignerEntry() - const signerEntryList = [signerEntry1, signerEntry2] - - // WHEN the list is serialized. - const serialized = Serializer.signerEntryListToJSON(signerEntryList) - - // THEN the result is undefined. - assert.isUndefined(serialized) - }) - - it('Serializes a SignerListSet', function (): void { - // GIVEN a SignerListSet - const signerQuorum = new SignerQuorum() - signerQuorum.setValue(1) - - const account1 = new Account() - account1.setValue(makeAccountAddress('r1')) - - const signerWeight1 = new SignerWeight() - signerWeight1.setValue(1) - - const signerEntry1 = new SignerEntry() - signerEntry1.setAccount(account1) - signerEntry1.setSignerWeight(signerWeight1) - - const account2 = new Account() - account2.setValue(makeAccountAddress('r2')) - - const signerWeight2 = new SignerWeight() - signerWeight2.setValue(2) - - const signerEntry2 = new SignerEntry() - signerEntry2.setAccount(account2) - signerEntry2.setSignerWeight(signerWeight2) - - const signerEntriesList = [signerEntry1, signerEntry2] - - const signerListSet = new SignerListSet() - signerListSet.setSignerQuorum(signerQuorum) - signerListSet.setSignerEntriesList(signerEntriesList) - - // WHEN it is serialized. - const serialized = Serializer.signerListSetToJSON(signerListSet) - - // THEN the result is the expected form. - const expected: SignerListSetJSON = { - SignerEntries: Serializer.signerEntryListToJSON(signerEntriesList)!, - SignerQuorum: Serializer.signerQuorumToJSON(signerQuorum)!, - TransactionType: 'SignerListSet', - } - assert.deepEqual(serialized, expected) - }) - - it('Fails to serialize a SignerListSet with malformed components', function (): void { - // GIVEN a SignerListSet with a malformed SignerEntriesList. - const signerQuorum = new SignerQuorum() - signerQuorum.setValue(1) - - const signerEntry = new SignerEntry() - - const signerEntriesList = [signerEntry] - - const signerListSet = new SignerListSet() - signerListSet.setSignerQuorum(signerQuorum) - signerListSet.setSignerEntriesList(signerEntriesList) - - // WHEN it is serialized. - const serialized = Serializer.signerListSetToJSON(signerListSet) - - // THEN the result is undefined. - assert.isUndefined(serialized) - }) - - it('Fails to serialize a malformed SignerListSet', function (): void { - // GIVEN a malformd SignerListSet. - const signerListSet = new SignerListSet() - - // WHEN it is serialized. - const serialized = Serializer.signerListSetToJSON(signerListSet) - - // THEN the result is undefined. - assert.isUndefined(serialized) - }) - it('Serializes a PaymentChannelCreate with mandatory fields', function (): void { // GIVEN a PaymentChannelCreate with only mandatory fields set. const amount = new Amount()