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

Add descriptor support to createmultisig rpc #1171

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/descriptor/abstractdescriptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class AbstractDescriptor {
* @returns {Script[]}
*/

generateScripts(pos) {
generateScripts(pos = 0) {
const pubkeys = [];
const subscripts = [];

Expand Down Expand Up @@ -274,7 +274,7 @@ class AbstractDescriptor {
* @returns {Address[]}
*/

getAddresses(pos) {
getAddresses(pos = 0) {
const scripts = this.generateScripts(pos);
const addresses = [];

Expand Down
11 changes: 11 additions & 0 deletions lib/descriptor/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ common.scriptContext = {
P2WSH: 'P2WSH'
};

/**
* output types for descriptor
* @const {Object}
*/

common.outputTypes = {
LEGACY: 'legacy',
BECH32: 'bech32',
P2SH_SEGWIT: 'p2sh-segwit'
};

/**
* Allowed characters in descriptor expressions for checksum to work.
* @see https://github.com/bitcoin/bips/blob/master/bip-0380.mediawiki#character-set
Expand Down
66 changes: 54 additions & 12 deletions lib/node/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ const TX = require('../primitives/tx');
const consensus = require('../protocol/consensus');
const pkg = require('../pkg');
const {filters} = require('../blockstore/common');
const {createChecksum} = require('../descriptor/common');
const MultisigDescriptor = require('../descriptor/type/multisig');
const SHDescriptor = require('../descriptor/type/sh');
const WSHDescriptor = require('../descriptor/type/wsh');
const {parse} = require('../descriptor/parser');
const RPCBase = bweb.RPC;
const RPCError = bweb.RPCError;
const {
createChecksum, outputTypes, scriptContext
} = require('../descriptor/common');

/*
* Constants
Expand Down Expand Up @@ -2050,20 +2055,32 @@ class RPC extends RPCBase {
*/

async createMultisig(args, help) {
if (help || args.length < 2 || args.length > 2) {
if (help || args.length < 2 || args.length > 3) {
throw new RPCError(errs.MISC_ERROR,
'createmultisig nrequired ["key",...]');
'createmultisig nrequired ["key",...] (address_type)');
}

const valid = new Validator(args);
const keys = valid.array(1, []);
let outputType = valid.str(2);

if (outputType === null) {
outputType = outputTypes.LEGACY;
}
Comment on lines +2065 to +2069
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let outputType = valid.str(2);
if (outputType === null) {
outputType = outputTypes.LEGACY;
}
const outputType = valid.str(2) ?? outputTypes.LEGACY;


assert(
Object.values(outputTypes).includes(outputType),
`Unknown address type '${outputType}'.`
);

const m = valid.u32(0, 0);
const n = keys.length;

if (m < 1 || n < m || n > 16)
if (m < 1 || n < m || n > 20)
throw new RPCError(errs.INVALID_PARAMETER, 'Invalid m and n values.');

const items = new Validator(keys);
let keyString = '';

for (let i = 0; i < keys.length; i++) {
const key = items.buf(i);
Expand All @@ -2074,21 +2091,46 @@ class RPC extends RPCBase {
if (!secp256k1.publicKeyVerify(key))
throw new RPCError(errs.INVALID_ADDRESS_OR_KEY, 'Invalid key.');

keyString += items.str(i) + ',';
keys[i] = key;
}

const script = Script.fromMultisig(m, n, keys, true, false);
keyString = keyString.slice(0, -1);

if (script.getSize() > consensus.MAX_SCRIPT_PUSH) {
throw new RPCError(errs.VERIFY_ERROR,
'Redeem script exceeds size limit.');
}
const isWitness = outputType !== outputTypes.LEGACY;
const script = Script.fromMultisig(m, n, keys, false, isWitness);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const script = Script.fromMultisig(m, n, keys, false, isWitness);
const redeemScript = Script.fromMultisig(m, n, keys, false, isWitness);


const addr = script.getAddress();
const {P2SH, P2WSH} = scriptContext;
const context = outputType === outputTypes.LEGACY ? P2SH : P2WSH;

const subdesc = MultisigDescriptor.fromString(
`multi(${m},${keyString})`, this.network, context
);
Comment on lines +2106 to +2108
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this, but can't think of a better alternative atm


let descriptor;

const options = {
subdescriptors: [subdesc],
network: this.network
};

switch (outputType) {
case outputTypes.LEGACY:
descriptor = SHDescriptor.fromOptions(options);
break;
case outputTypes.BECH32:
descriptor = WSHDescriptor.fromOptions(options);
break;
case outputTypes.P2SH_SEGWIT: {
options.subdescriptors = [WSHDescriptor.fromOptions(options)];
descriptor = SHDescriptor.fromOptions(options);
}
}

return {
address: addr.toString(this.network),
redeemScript: script.toJSON()
address: descriptor.getAddresses()[0],
redeemScript: script.toJSON(),
descriptor: descriptor.toString()
};
}

Expand Down
Loading