Skip to content

Commit

Permalink
descriptor: move descriptor module to lib directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Vasu-08 committed Jun 3, 2023
1 parent c0099d3 commit 1c28e9d
Show file tree
Hide file tree
Showing 20 changed files with 151 additions and 51 deletions.
4 changes: 2 additions & 2 deletions lib/wallet/descriptor/common.js → lib/descriptor/common.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*!
* common.js - common functions for descriptor
* Copyright (c) 2015-2016, Christopher Jeffrey (MIT License).
* Copyright (c) 2023, the bcoin developers (MIT License).
* https://github.com/bcoin-org/bcoin
*/

'use strict';

const assert = require('bsert');
const {types} = require('../../script/common');
const {types} = require('../script/common');
const BN = require('bcrypto/lib/bn.js');
const common = exports;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*!
* descriptor.js - descriptor object for bcoin
* Copyright (c) 2015-2016, Christopher Jeffrey (MIT License).
* Copyright (c) 2023, the bcoin developers (MIT License).
* https://github.com/bcoin-org/bcoin
*/

Expand All @@ -11,7 +11,8 @@ const {stringType, addChecksum} = require('./common');

/**
* Descriptor
* Base class for descriptors
* Base class for descriptors.
* Represents an output script
* @property {String} name
* @property {PubkeyProvider[]} pubkeys
* @property {Descriptor[]} subdescriptors
Expand Down
9 changes: 9 additions & 0 deletions lib/descriptor/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*!
* descriptor/index.js - descriptor parser for bcoin
* Copyright (c) 2023, the bcoin developers (MIT License).
* https://github.com/bcoin-org/bcoin
*/

'use strict';

module.exports = require('./parser');
40 changes: 23 additions & 17 deletions lib/wallet/descriptor/parser.js → lib/descriptor/parser.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
/*!
* parser.js - descriptor parser for bcoin
* Copyright (c) 2023, the bcoin developers (MIT License).
* https://github.com/bcoin-org/bcoin
*/

'use strict';

const assert = require('bsert');
const HD = require('../../hd/hd');
const {HARDENED} = require('../../hd/common');
const HD = require('../hd/hd');
const {HARDENED} = require('../hd/common');
// const Descriptor = require('./descriptor');
const KeyRing = require('../../primitives/keyring');
const PKDescriptor = require('./pkdescriptor');
const PKHDescriptor = require('./pkhdescriptor');
const WPKHDescriptor = require('./wpkhdescriptor');
const SHDescriptor = require('./shdescriptor');
const WSHDescriptor = require('./wshdescriptor');
const ComboDescriptor = require('./combodescriptor');
const AddressDescriptor = require('./addressdescriptor');
const MultisigDescriptor = require('./multisigdescriptor');
const RawDescriptor = require('./rawdescriptor');
const Network = require('../../protocol/network');
const Address = require ('../../primitives/address');
const Script = require('../../script/script');
const {isCompressedEncoding} = require('../../script/common');
const KeyRing = require('../primitives/keyring');
const PKDescriptor = require('./type/pk');
const PKHDescriptor = require('./type/pkh');
const WPKHDescriptor = require('./type/wpkh');
const SHDescriptor = require('./type/sh');
const WSHDescriptor = require('./type/wsh');
const ComboDescriptor = require('./type/combo');
const AddressDescriptor = require('./type/addr');
const MultisigDescriptor = require('./type/multisig');
const RawDescriptor = require('./type/raw');
const Network = require('../protocol/network');
const Address = require ('../primitives/address');
const Script = require('../script/script');
const {isCompressedEncoding} = require('../script/common');
const {
MAX_SCRIPT_PUSH,
MAX_MULTISIG_PUBKEYS
} = require('../../protocol/consensus');
} = require('../protocol/consensus');

const {
ConstPubkeyProvider,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*!
* pubkeyprovider.js - parsed public key object for descriptor in bcoin
* Copyright (c) 2015-2016, Christopher Jeffrey (MIT License).
* Copyright (c) 2023, the bcoin developers (MIT License).
* https://github.com/bcoin-org/bcoin
*/

'use strict';

const HD = require('../../hd');
const {HARDENED} = require('../../hd/common');
const HD = require('../hd');
const {HARDENED} = require('../hd/common');
const hash160 = require('bcrypto/lib/hash160');
const {deriveType} = require('./common');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
/*!
* addr.js - address descriptor object for bcoin
* Copyright (c) 2023, the bcoin developers (MIT License).
* https://github.com/bcoin-org/bcoin
*/

'use strict';

const Descriptor = require('./descriptor');
const Descriptor = require('../descriptor');

class AddressDescriptor extends Descriptor {
constructor(options) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
/*!
* combo.js - combo descriptor object for bcoin
* Copyright (c) 2023, the bcoin developers (MIT License).
* https://github.com/bcoin-org/bcoin
*/

'use strict';

const Descriptor = require('./descriptor');
const Descriptor = require('../descriptor');

class ComboDescriptor extends Descriptor {
constructor(options) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
/*!
* multisig.js - multisig descriptor object for bcoin
* Copyright (c) 2023, the bcoin developers (MIT License).
* https://github.com/bcoin-org/bcoin
*/

'use strict';

const Descriptor = require('./descriptor');
const Descriptor = require('../descriptor');

class MultisigDescriptor extends Descriptor {
constructor(options) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/*!
* pk.js - public key descriptor object for bcoin
* Copyright (c) 2023, the bcoin developers (MIT License).
* https://github.com/bcoin-org/bcoin
*/

'use strict';

const Descriptor = require('./descriptor');
const {outputTypes} = require('./common');
const Descriptor = require('../descriptor');
const {outputTypes} = require('../common');

class PKDescriptor extends Descriptor {
constructor(options) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
/*!
* pkh.js - public key hash descriptor object for bcoin
* Copyright (c) 2023, the bcoin developers (MIT License).
* https://github.com/bcoin-org/bcoin
*/

'use strict';
const Descriptor = require('./descriptor');
const {outputTypes} = require('./common');

const Descriptor = require('../descriptor');
const {outputTypes} = require('../common');

class PKHDescriptor extends Descriptor {
constructor(options) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
/*!
* raw.js - raw descriptor object for bcoin
* Copyright (c) 2023, the bcoin developers (MIT License).
* https://github.com/bcoin-org/bcoin
*/

'use strict';

const Descriptor = require('./descriptor');
const Descriptor = require('../descriptor');
const Address = require('../../primitives/address');

class RawDescriptor extends Descriptor {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
/*!
* sh.js - script hash descriptor object for bcoin
* Copyright (c) 2023, the bcoin developers (MIT License).
* https://github.com/bcoin-org/bcoin
*/

'use strict';
const Descriptor = require('./descriptor');

const Descriptor = require('../descriptor');

class SHDescriptor extends Descriptor {
constructor(options) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
/*!
* wpkh.js - witness script hash descriptor object for bcoin
* Copyright (c) 2023, the bcoin developers (MIT License).
* https://github.com/bcoin-org/bcoin
*/

'use strict';
const Descriptor = require('./descriptor');
const outputTypes = require('./common');

const Descriptor = require('../descriptor');
const outputTypes = require('../common');

class WPKHDescriptor extends Descriptor {
constructor(options) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
/*!
* wsh.js - witness script hash descriptor object for bcoin
* Copyright (c) 2023, the bcoin developers (MIT License).
* https://github.com/bcoin-org/bcoin
*/

'use strict';

const Descriptor = require('./descriptor');
const Descriptor = require('../descriptor');

class WSHDescriptor extends Descriptor {
constructor(options) {
Expand Down
7 changes: 6 additions & 1 deletion lib/hd/keyorigin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
/* eslint-disable max-len */
/*!
* keyorigin.js - hd key path object for bcoin
* Copyright (c) 2023, the bcoin developers (MIT License).
* https://github.com/bcoin-org/bcoin
*/

'use strict';

const assert = require('assert');
Expand Down
3 changes: 0 additions & 3 deletions lib/wallet/descriptor/index.js

This file was deleted.

15 changes: 8 additions & 7 deletions lib/wallet/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
'use strict';

const assert = require('bsert');
const { format } = require('util');
const {format} = require('util');
const bweb = require('bweb');
const { Lock } = require('bmutex');
const {Lock} = require('bmutex');
const fs = require('bfile');
const Validator = require('bval');
const { BufferMap, BufferSet } = require('buffer-map');
const {BufferMap, BufferSet} = require('buffer-map');
const util = require('../utils/util');
const messageUtil = require('../utils/message');
const Amount = require('../btc/amount');
Expand All @@ -27,10 +27,11 @@ const TX = require('../primitives/tx');
const consensus = require('../protocol/consensus');
const pkg = require('../pkg');
const common = require('./common');
const { BlockMeta } = require('./records');
const {BlockMeta} = require('./records');
const RPCBase = bweb.RPC;
const RPCError = bweb.RPCError;
const Descriptor = require('./descriptor');
const DescriptorParser = require('../descriptor/parser');
const {createChecksum} = require('../descriptor/common');

/*
* Constants
Expand Down Expand Up @@ -479,7 +480,7 @@ class RPC extends RPCBase {

const result = {
descriptor: descriptor.toString(),
checksum: Descriptor.addChecksum(desc),
checksum: createChecksum(desc),
isrange: descriptor.isRange(),
issolvable: descriptor.isSolvable(),
hasprivatekeys: descriptor.hasPrivateKeys()
Expand Down Expand Up @@ -1758,7 +1759,7 @@ function parseAddress(raw, network) {

function parseDescriptor(raw, network) {
try {
return Descriptor.fromString(raw, network);
return DescriptorParser.fromString(raw, network);
} catch (e) {
throw new RPCError(errs.INVALID_DESCRIPTOR,
`Invalid descriptor: ${e.message}`
Expand Down
28 changes: 26 additions & 2 deletions test/descriptor-test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
/* eslint-disable quotes */
/* eslint-disable no-unused-vars */

'use strict';

const assert = require('bsert');
const DescriptorParser = require('../lib/wallet/descriptor/parser');
const {createChecksum} = require('../lib/wallet/descriptor/common');
const DescriptorParser = require('../lib/descriptor/parser');
const {createChecksum} = require('../lib/descriptor/common');

const testcases = [
{
"input": `multi(1,xprvA2JDeKCSNNZky6uBCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334/*,L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1)`,
"descriptor":`multi(1,xpub6FHa3pjLCk84BayeJxFW2SP4XRrFd1JYnxeLeU8EqN3vDfZmbqBqaGJAyiLjTAwm6ZLRQUMv1ZACTj37sR62cfN7fe5JnJ7dh8zL4fiyLHV/*,03a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd)#ds4nc7vf`,
"isrange":true,
"checksum":"aefpzjr7",
"issolvable":true,
"hasprivatekeys":true,
"network": "main"
},
{
"input":`tr(xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc/0/*`,
"network": "main",
"error": "tr(xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc/0/* is not a valid descriptor function"
},
{
"input": `combo(xprvA2JDeKCSNNZky6uBCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334/*)#f68555z5`,
"descriptor": `combo(xpub6FHa3pjLCk84BayeJxFW2SP4XRrFd1JYnxeLeU8EqN3vDfZmbqBqaGJAyiLjTAwm6ZLRQUMv1ZACTj37sR62cfN7fe5JnJ7dh8zL4fiyLHV/*)#3are946j`,
"checksum": "f68555z5",
"isrange": true,
"issolvable": true,
"hasprivatekeys": true,
"network": "main"
},
{
"input": `addr(2NBFNJTktNa7GZusGbDbGKRZTxdK9VVez3n)`,
"descriptor": "addr(2NBFNJTktNa7GZusGbDbGKRZTxdK9VVez3n)#l98qe9z6",
Expand Down
2 changes: 1 addition & 1 deletion test/hd-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function base58Equal(a, b) {
assert.bufferEqual(base58.decode(a), base58.decode(b));
}

describe('HD', function () {
describe('HD', function() {
it('should create a pbkdf2 seed', () => {
const seed = pbkdf2.derive(sha512,
Buffer.from(vectors.phrase),
Expand Down
2 changes: 1 addition & 1 deletion test/keyring-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const uncompressed = KeyRing.fromSecret(
const compressed = KeyRing.fromSecret(
'L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1', 'main');

describe('KeyRing', function () {
describe('KeyRing', function() {
it('should get uncompressed public key', () => {
assert.strictEqual(
'04a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd5b'
Expand Down

0 comments on commit 1c28e9d

Please # to comment.