From a3aecba1d03e40cc670a41301f7365ff782af96b Mon Sep 17 00:00:00 2001 From: Michael Vines Date: Wed, 26 Sep 2018 19:54:59 -0700 Subject: [PATCH] Improve rpc result validation --- web3.js/src/connection.js | 93 +++++++++++++-------------------- web3.js/test/connection.test.js | 4 +- 2 files changed, 39 insertions(+), 58 deletions(-) diff --git a/web3.js/src/connection.js b/web3.js/src/connection.js index cb9515901..14338ba1b 100644 --- a/web3.js/src/connection.js +++ b/web3.js/src/connection.js @@ -58,95 +58,76 @@ const GetBalanceRpcResult = struct({ }); +/** + * @private + */ +function jsonRpcResult(resultDescription: any) { + const jsonRpcVersion = struct.literal('2.0'); + return struct.union([ + struct({ + jsonrpc: jsonRpcVersion, + id: 'string', + error: 'any' + }), + struct({ + jsonrpc: jsonRpcVersion, + id: 'string', + error: 'null?', + result: resultDescription, + }), + ]); +} + + /** * Expected JSON RPC response for the "getAccountInfo" message */ -const GetAccountInfoRpcResult = struct({ - jsonrpc: struct.literal('2.0'), - id: 'string', - error: 'any?', - result: struct.optional({ - program_id: 'array', - tokens: 'number', - userdata: 'array', - }), +const GetAccountInfoRpcResult = jsonRpcResult({ + program_id: 'array', + tokens: 'number', + userdata: 'array', }); /** * Expected JSON RPC response for the "confirmTransaction" message */ -const ConfirmTransactionRpcResult = struct({ - jsonrpc: struct.literal('2.0'), - id: 'string', - error: 'any?', - result: 'boolean?', -}); +const ConfirmTransactionRpcResult = jsonRpcResult('boolean'); /** * Expected JSON RPC response for the "getSignatureStatus" message */ -const GetSignatureStatusRpcResult = struct({ - jsonrpc: struct.literal('2.0'), - id: 'string', - error: 'any?', - result: struct.optional(struct.enum([ - 'Confirmed', - 'SignatureNotFound', - 'ProgramRuntimeError', - 'GenericFailure', - ])), -}); +const GetSignatureStatusRpcResult = jsonRpcResult(struct.enum([ + 'Confirmed', + 'SignatureNotFound', + 'ProgramRuntimeError', + 'GenericFailure', +])); /** * Expected JSON RPC response for the "getTransactionCount" message */ -const GetTransactionCountRpcResult = struct({ - jsonrpc: struct.literal('2.0'), - id: 'string', - error: 'any?', - result: 'number?', -}); +const GetTransactionCountRpcResult = jsonRpcResult('number'); /** * Expected JSON RPC response for the "getLastId" message */ -const GetLastId = struct({ - jsonrpc: struct.literal('2.0'), - id: 'string', - error: 'any?', - result: 'string?', -}); +const GetLastId = jsonRpcResult('string'); /** * Expected JSON RPC response for the "getFinality" message */ -const GetFinalityRpcResult = struct({ - jsonrpc: struct.literal('2.0'), - id: 'string', - error: 'any?', - result: 'number?', -}); +const GetFinalityRpcResult = jsonRpcResult('number'); /** * Expected JSON RPC response for the "requestAirdrop" message */ -const RequestAirdropRpcResult = struct({ - jsonrpc: struct.literal('2.0'), - id: 'string', - error: 'any?', - result: 'string?', -}); +const RequestAirdropRpcResult = jsonRpcResult('string'); /** * Expected JSON RPC response for the "sendTransaction" message */ -const SendTokensRpcResult = struct({ - jsonrpc: struct.literal('2.0'), - id: 'string', - error: 'any?', - result: 'string?', -}); +const SendTokensRpcResult = jsonRpcResult('string'); /** * Information describing an account diff --git a/web3.js/test/connection.test.js b/web3.js/test/connection.test.js index 7480aa183..42c4295fd 100644 --- a/web3.js/test/connection.test.js +++ b/web3.js/test/connection.test.js @@ -5,9 +5,9 @@ import {Connection} from '../src/connection'; import {SystemProgram} from '../src/system-program'; import {mockRpc} from './__mocks__/node-fetch'; -let url = 'http://testnet.solana.com:8899'; +let url = 'http://localhost:8899'; +//url = 'http://testnet.solana.com:8899'; //url = 'http://master.testnet.solana.com:8899'; -//url = 'http://localhost:8899'; const errorMessage = 'Invalid request'; const errorResponse = {