Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: support for core 18.2 #57

Merged
merged 30 commits into from
Dec 30, 2022
29 changes: 23 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ 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,
Expand Down Expand Up @@ -313,29 +315,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
29 changes: 29 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,4 +553,33 @@ describe('RpcClient', function() {
done();
})
});

it('should call wallet method and receive the response', (done) => {
var client = new RpcClient({
user: 'user',
pass: 'pass',
host: 'localhost',
port: 8332,
rejectUnauthorized: true,
disableAgent: true
});

var requestStub = sinon.stub(client.protocol, 'request').callsFake(function(options, callback){
var res = new FakeResponse();
var req = new FakeRequest();
setImmediate(function(){
res.emit('data', '{}');
res.emit('end');
});
callback(res);
return req;
});

client.getBalance('n28S35tqEMbt6vNad7A5K3mZ7vdn8dZ86X', 6, { wallet: 'default' }, function(error, parsedBuf) {
requestStub.restore();
should.not.exist(error);
should.exist(parsedBuf);
done();
});
});
})