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 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 79718bdf..95bcbe9a 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 c132bf7f..15ec38e9 100644 --- a/packages/xrpl/src/sugar/index.ts +++ b/packages/xrpl/src/sugar/index.ts @@ -7,6 +7,8 @@ export { default as getLedgerIndex } from './getLedgerIndex' export { default as getOrderbook } from './getOrderbook' export { getFeeXrp, getFeeEstimateXrp } from './getFeeXrp' +export { default as getNetworkID } from './getNetworkID' + export * from './submit' export * from './utils'