Skip to content

Commit

Permalink
Merge branch 'master' into feat/get-raw-transaction-multi
Browse files Browse the repository at this point in the history
  • Loading branch information
shumkov committed May 24, 2024
2 parents 65fc026 + d210ce7 commit fa79bb4
Show file tree
Hide file tree
Showing 4 changed files with 502 additions and 379 deletions.
44 changes: 32 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,22 @@ function rpc(request, callback) {
}
function innerRpc(request, callback) {
const self = this;
const path = request.path;
delete request.path;
request = JSON.stringify(request);
const auth = Buffer.from(`${self.user}:${self.pass}`).toString('base64');

const options = {
host: self.host,
path: '/',
path,
method: 'POST',
port: self.port,
rejectUnauthorized: self.rejectUnauthorized,
agent: self.disableAgent ? false : undefined,
};
};

if (self.timeout) {
options.timeout = self.timeout;
if (self.timeout) {
options.timeout = self.timeout;
};

if (self.httpOptions) {
Expand Down Expand Up @@ -149,6 +151,7 @@ if (self.timeout) {

req.on('timeout', () => {
const err = new Error(`Timeout Error: ${options.timeout}ms exceeded`);
called = true;
callback(err);
});

Expand Down Expand Up @@ -179,6 +182,7 @@ RpcClient.callspec = {
clearBanned: '',
createMultiSig: 'int str',
createRawTransaction: 'str str int',
createWallet: 'str bool bool str bool bool',
debug: 'str',
decodeRawTransaction: 'str',
decodeScript: 'str',
Expand Down Expand Up @@ -232,6 +236,7 @@ RpcClient.callspec = {
getRawMemPool: 'bool',
getRawChangeAddress: '',
getRawTransaction: 'str int',
sendRawTransactionMulti: 'obj int',
getReceivedByAccount: 'str int',
getReceivedByAddress: 'str int',
getSpentInfo: 'obj',
Expand Down Expand Up @@ -275,16 +280,16 @@ RpcClient.callspec = {
resendWalletTransactions: '',
sendFrom: 'str str float int str str',
sendMany: 'str obj int str str bool bool',
sendRawTransaction: 'str bool bool',
sendRawTransactionMulti: 'obj bool',
sendRawTransaction: 'str float bool',
sendToAddress: 'str float str str',
sentinelPing: 'str',
setAccount: '',
setBan: 'str str int bool',
setGenerate: 'bool int',
setTxFee: 'float',
setMockTime: 'int',
spork: 'str int',
spork: 'str',
sporkupdate: 'str int',
signMessage: 'str str',
signRawTransaction: 'str str str str',
stop: '',
Expand All @@ -311,29 +316,44 @@ const slice = function (arr, start, end) {
function generateRPCMethods(constructor, apiCalls, rpc) {
function createRPCMethod(methodName, argMap) {
return function () {
let limit = arguments.length - 1;
let path = '/';
let slicedArguments = slice(arguments);

const length = slicedArguments.length;

// The last optional parameter of requested method is a wallet name. We don't want to pass it to core,
// that's why we remove it. And since the latest parameter here is a callback, we use length - 2,
// instead of length - 1
if (length > 0 && typeof slicedArguments[length - 2] === 'object' && slicedArguments[length - 2].wallet) {
path = '/wallet/' + slicedArguments[length - 2].wallet;
slicedArguments.splice(length - 2, 1);
}

let limit = slicedArguments.length - 1;

if (this.batchedCalls) {
limit = arguments.length;
limit = slicedArguments.length;
}

for (let i = 0; i < limit; i++) {
if (argMap[i]) {
arguments[i] = argMap[i](arguments[i]);
slicedArguments[i] = argMap[i](slicedArguments[i]);
}
}

if (this.batchedCalls) {
this.batchedCalls.push({
path,
jsonrpc: '2.0',
method: methodName,
params: slice(arguments),
params: slice(slicedArguments),
id: getRandomId(),
});
} else {
rpc.call(this, {
path,
method: methodName,
params: slice(arguments, 0, arguments.length - 1),
params: slice(slicedArguments, 0, slicedArguments.length - 1),
id: getRandomId(),
}, arguments[arguments.length - 1]);
}
Expand Down
Loading

0 comments on commit fa79bb4

Please sign in to comment.