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 a5deee1274.

* update autofill

* update getNetworkID

* remove set network func

* fixup!

remove autoset network id

remove my mistake

fixup!
This commit is contained in:
Denis Angell
2023-03-28 20:27:38 +00:00
committed by GitHub
parent 62a39c69ce
commit 4447a7af09
7 changed files with 59 additions and 1 deletions

View File

@@ -321,6 +321,16 @@
"type": "UInt16" "type": "UInt16"
} }
], ],
[
"NetworkID",
{
"nth": 1,
"isVLEncoded": false,
"isSerialized": true,
"isSigningField": true,
"type": "UInt32"
}
],
[ [
"Flags", "Flags",
{ {
@@ -2176,6 +2186,9 @@
"telCAN_NOT_QUEUE_BLOCKED": -389, "telCAN_NOT_QUEUE_BLOCKED": -389,
"telCAN_NOT_QUEUE_FEE": -388, "telCAN_NOT_QUEUE_FEE": -388,
"telCAN_NOT_QUEUE_FULL": -387, "telCAN_NOT_QUEUE_FULL": -387,
"telWRONG_NETWORK": -386,
"telREQUIRES_NETWORK_ID": -385,
"telNETWORK_ID_MAKES_TX_NON_CANONICAL": -384,
"temMALFORMED": -299, "temMALFORMED": -299,
"temBAD_AMOUNT": -298, "temBAD_AMOUNT": -298,

View File

@@ -101,6 +101,7 @@ import {
getXrpBalance, getXrpBalance,
submit, submit,
submitAndWait, submitAndWait,
getNetworkID,
} from '../sugar' } from '../sugar'
import fundWallet from '../Wallet/fundWallet' import fundWallet from '../Wallet/fundWallet'
@@ -200,6 +201,13 @@ class Client extends EventEmitter {
*/ */
public readonly maxFeeXRP: string 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. * 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.feeCushion = options.feeCushion ?? DEFAULT_FEE_CUSHION
this.maxFeeXRP = options.maxFeeXRP ?? DEFAULT_MAX_FEE_XRP this.maxFeeXRP = options.maxFeeXRP ?? DEFAULT_MAX_FEE_XRP
this.networkID = 1
this.connection = new Connection(server, options) this.connection = new Connection(server, options)
@@ -634,6 +643,11 @@ class Client extends EventEmitter {
*/ */
public getLedgerIndex = getLedgerIndex public getLedgerIndex = getLedgerIndex
/**
* @category Abstraction
*/
public getNetworkID = getNetworkID
/** /**
* @category Faucet * @category Faucet
*/ */

View File

@@ -136,6 +136,10 @@ export interface ServerInfoResponse extends BaseResponse {
* overall network's load factor. * overall network's load factor.
*/ */
load_factor?: number load_factor?: number
/**
* The network id of the server.
*/
network_id?: number
/** /**
* Current multiplier to the transaction cost based on * Current multiplier to the transaction cost based on
* load to this server. * load to this server.

View File

@@ -159,6 +159,10 @@ export interface BaseTransaction {
* account it says it is from. * account it says it is from.
*/ */
TxnSignature?: string TxnSignature?: string
/**
* The network id of the transaction.
*/
NetworkID?: number
} }
/** /**
@@ -252,6 +256,9 @@ export function validateBaseTransaction(common: Record<string, unknown>): void {
) { ) {
throw new ValidationError('BaseTransaction: invalid TxnSignature') throw new ValidationError('BaseTransaction: invalid TxnSignature')
} }
if (common.NetworkID !== undefined && typeof common.NetworkID !== 'number') {
throw new ValidationError('BaseTransaction: invalid NetworkID')
}
} }
/** /**

View File

@@ -12,6 +12,7 @@ import getFeeXrp from './getFeeXrp'
// Expire unconfirmed transactions after 20 ledger versions, approximately 1 minute, by default // Expire unconfirmed transactions after 20 ledger versions, approximately 1 minute, by default
const LEDGER_OFFSET = 20 const LEDGER_OFFSET = 20
const RESTRICTED_NETWORKS = 1024
interface ClassicAccountAndTag { interface ClassicAccountAndTag {
classicAccount: string classicAccount: string
tag: number | false | undefined tag: number | false | undefined
@@ -39,8 +40,10 @@ async function autofill<T extends Transaction>(
setValidAddresses(tx) setValidAddresses(tx)
setTransactionFlagsToNumber(tx) setTransactionFlagsToNumber(tx)
const promises: Array<Promise<void>> = [] const promises: Array<Promise<void>> = []
if (this.networkID > RESTRICTED_NETWORKS && tx.NetworkID == null) {
tx.NetworkID = this.networkID
}
if (tx.Sequence == null) { if (tx.Sequence == null) {
promises.push(setNextValidSequenceNumber(this, tx)) promises.push(setNextValidSequenceNumber(this, tx))
} }

View File

@@ -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<number> {
const response = await this.request({
command: 'server_info',
})
return response.result.info.network_id ?? 1
}

View File

@@ -6,6 +6,8 @@ export { default as getLedgerIndex } from './getLedgerIndex'
export { default as getOrderbook } from './getOrderbook' export { default as getOrderbook } from './getOrderbook'
export { default as getNetworkID } from './getNetworkID'
export * from './submit' export * from './submit'
export * from './utils' export * from './utils'