-
Notifications
You must be signed in to change notification settings - Fork 808
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
Vasu-08
wants to merge
2
commits into
bcoin-org:master
Choose a base branch
from
Vasu-08:createmultisig_rpc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
|
@@ -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; | ||||||
} | ||||||
|
||||||
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); | ||||||
|
@@ -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); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||||||
}; | ||||||
} | ||||||
|
||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.