From 62a39c69ce5a0aba042bf69507131a6cbe4327b9 Mon Sep 17 00:00:00 2001 From: Caleb Kniffen Date: Mon, 27 Mar 2023 17:32:54 -0500 Subject: [PATCH 1/2] chore: bump codeql-analysis (#2246) --- .github/workflows/codeql-analysis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index cdc02e90..83db958b 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -39,7 +39,7 @@ jobs: # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v1 + uses: github/codeql-action/init@v2 with: languages: ${{ matrix.language }} # If you wish to specify custom queries, you can do so here or in a config file. @@ -50,7 +50,7 @@ jobs: # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). # If this step fails, then you should remove it and run the build manually (see below) - name: Autobuild - uses: github/codeql-action/autobuild@v1 + uses: github/codeql-action/autobuild@v2 # â„šī¸ Command-line programs to run using the OS shell. # 📚 https://git.io/JvXDl @@ -64,4 +64,4 @@ jobs: # make release - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v1 + uses: github/codeql-action/analyze@v2 From 4447a7af091dfd0b626ccd2a7a641eff04e71371 Mon Sep 17 00:00:00 2001 From: Denis Angell Date: Tue, 28 Mar 2023 20:27:38 +0000 Subject: [PATCH 2/2] NetworkID (#11) * update definitions * add network id to base transaction * update server info * update base tx * add network id to autofill * update tests and fixtures * network id option 3 fixup add network id to server info fixture fixup! Revert "update tests and fixtures" This reverts commit a5deee1274e7bc066d573d3a30fdb9cf555e0954. * update autofill * update getNetworkID * remove set network func * fixup! remove autoset network id remove my mistake fixup! --- .../src/enums/definitions.json | 13 +++++++++++++ packages/xrpl/src/client/index.ts | 14 ++++++++++++++ packages/xrpl/src/models/methods/serverInfo.ts | 4 ++++ packages/xrpl/src/models/transactions/common.ts | 7 +++++++ packages/xrpl/src/sugar/autofill.ts | 5 ++++- packages/xrpl/src/sugar/getNetworkID.ts | 15 +++++++++++++++ packages/xrpl/src/sugar/index.ts | 2 ++ 7 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 packages/xrpl/src/sugar/getNetworkID.ts diff --git a/packages/ripple-binary-codec/src/enums/definitions.json b/packages/ripple-binary-codec/src/enums/definitions.json index 317e1feb..a59de992 100644 --- a/packages/ripple-binary-codec/src/enums/definitions.json +++ b/packages/ripple-binary-codec/src/enums/definitions.json @@ -321,6 +321,16 @@ "type": "UInt16" } ], + [ + "NetworkID", + { + "nth": 1, + "isVLEncoded": false, + "isSerialized": true, + "isSigningField": true, + "type": "UInt32" + } + ], [ "Flags", { @@ -2176,6 +2186,9 @@ "telCAN_NOT_QUEUE_BLOCKED": -389, "telCAN_NOT_QUEUE_FEE": -388, "telCAN_NOT_QUEUE_FULL": -387, + "telWRONG_NETWORK": -386, + "telREQUIRES_NETWORK_ID": -385, + "telNETWORK_ID_MAKES_TX_NON_CANONICAL": -384, "temMALFORMED": -299, "temBAD_AMOUNT": -298, diff --git a/packages/xrpl/src/client/index.ts b/packages/xrpl/src/client/index.ts index 0f812000..6543b9d3 100644 --- a/packages/xrpl/src/client/index.ts +++ b/packages/xrpl/src/client/index.ts @@ -101,6 +101,7 @@ import { getXrpBalance, submit, submitAndWait, + getNetworkID, } from '../sugar' import fundWallet from '../Wallet/fundWallet' @@ -200,6 +201,13 @@ class Client extends EventEmitter { */ public readonly maxFeeXRP: string + /** + * Network ID of the server this sdk is connected to + * + * @category Fee + */ + public networkID: number + /** * Creates a new Client with a websocket connection to a rippled server. * @@ -218,6 +226,7 @@ class Client extends EventEmitter { this.feeCushion = options.feeCushion ?? DEFAULT_FEE_CUSHION this.maxFeeXRP = options.maxFeeXRP ?? DEFAULT_MAX_FEE_XRP + this.networkID = 1 this.connection = new Connection(server, options) @@ -634,6 +643,11 @@ class Client extends EventEmitter { */ public getLedgerIndex = getLedgerIndex + /** + * @category Abstraction + */ + public getNetworkID = getNetworkID + /** * @category Faucet */ diff --git a/packages/xrpl/src/models/methods/serverInfo.ts b/packages/xrpl/src/models/methods/serverInfo.ts index bfcfcbbd..5bd3dd2b 100644 --- a/packages/xrpl/src/models/methods/serverInfo.ts +++ b/packages/xrpl/src/models/methods/serverInfo.ts @@ -136,6 +136,10 @@ export interface ServerInfoResponse extends BaseResponse { * overall network's load factor. */ load_factor?: number + /** + * The network id of the server. + */ + network_id?: number /** * Current multiplier to the transaction cost based on * load to this server. diff --git a/packages/xrpl/src/models/transactions/common.ts b/packages/xrpl/src/models/transactions/common.ts index 498a2d74..8bceabe6 100644 --- a/packages/xrpl/src/models/transactions/common.ts +++ b/packages/xrpl/src/models/transactions/common.ts @@ -159,6 +159,10 @@ export interface BaseTransaction { * account it says it is from. */ TxnSignature?: string + /** + * The network id of the transaction. + */ + NetworkID?: number } /** @@ -252,6 +256,9 @@ export function validateBaseTransaction(common: Record): void { ) { throw new ValidationError('BaseTransaction: invalid TxnSignature') } + if (common.NetworkID !== undefined && typeof common.NetworkID !== 'number') { + throw new ValidationError('BaseTransaction: invalid NetworkID') + } } /** diff --git a/packages/xrpl/src/sugar/autofill.ts b/packages/xrpl/src/sugar/autofill.ts index 05d50a96..c7a0d75f 100644 --- a/packages/xrpl/src/sugar/autofill.ts +++ b/packages/xrpl/src/sugar/autofill.ts @@ -12,6 +12,7 @@ import getFeeXrp from './getFeeXrp' // Expire unconfirmed transactions after 20 ledger versions, approximately 1 minute, by default const LEDGER_OFFSET = 20 +const RESTRICTED_NETWORKS = 1024 interface ClassicAccountAndTag { classicAccount: string tag: number | false | undefined @@ -39,8 +40,10 @@ async function autofill( setValidAddresses(tx) setTransactionFlagsToNumber(tx) - const promises: Array> = [] + if (this.networkID > RESTRICTED_NETWORKS && tx.NetworkID == null) { + tx.NetworkID = this.networkID + } if (tx.Sequence == null) { promises.push(setNextValidSequenceNumber(this, tx)) } diff --git a/packages/xrpl/src/sugar/getNetworkID.ts b/packages/xrpl/src/sugar/getNetworkID.ts new file mode 100644 index 00000000..d7c3489f --- /dev/null +++ b/packages/xrpl/src/sugar/getNetworkID.ts @@ -0,0 +1,15 @@ +import type { Client } from '..' +// import { XrplError } from '../errors' + +/** + * Returns the network ID of the rippled server. + * + * @param this - The Client used to connect to the ledger. + * @returns The network id. + */ +export default async function getNetworkID(this: Client): Promise { + const response = await this.request({ + command: 'server_info', + }) + return response.result.info.network_id ?? 1 +} diff --git a/packages/xrpl/src/sugar/index.ts b/packages/xrpl/src/sugar/index.ts index 991c1533..33668485 100644 --- a/packages/xrpl/src/sugar/index.ts +++ b/packages/xrpl/src/sugar/index.ts @@ -6,6 +6,8 @@ export { default as getLedgerIndex } from './getLedgerIndex' export { default as getOrderbook } from './getOrderbook' +export { default as getNetworkID } from './getNetworkID' + export * from './submit' export * from './utils'