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

rename bip32 to HDWallet and bip39 to Mnemonic #258

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ goals:
fromString, toJSON, fromJSON, toBuffer, fromBuffer, toHex, fromHex methods.

4. All standard features of the blockchain are implemented (or will be) and
saved in lib/. All BIPs are correctly implemented and, where appropriate, saved
as bip-xx.js in lib/ (since that is their standard name). In order to allow
rapid development, Yours Bitcoin includes non-standard and experimental
features. Any non-standard features (such as colored coins or stealth
addresses) are labeled as such in index.js as well as in comments.
saved in lib/. All BIPs are correctly implemented and saved
under their functional names to make them easy to find when using the lib.
In order to allow rapid development, Yours Bitcoin includes non-standard
and experimental features. Any non-standard features (such as colored coins
or stealth addresses) are labeled as such in index.js as well as in comments.

5. Expose everything, including dependencies. This makes it possible to develop
apps that require fine-grained control over the basics, such as big numbers and
Expand Down
6 changes: 3 additions & 3 deletions entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ export { version }
// Main bitcoin library - bitcoin protocols, standards, cryptography, and
// utilities.
export * from './lib/address'
export * from './lib/bip-32'
export * from './lib/bip-39'
export * from './lib/bip-39-words'
export * from './lib/hd-wallet'
export * from './lib/mnemonic'
export * from './lib/mnemonic/bip-39-words'
export * from './lib/bn'
export * from './lib/br'
export * from './lib/bsm'
Expand Down
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ bsv.version = require('./package').version
// Main bitcoin library - bitcoin protocols, standards, cryptography, and
// utilities.
bsv.Address = require('./lib/address')
bsv.Bip32 = require('./lib/bip-32')
bsv.Bip39 = require('./lib/bip-39')
bsv.Bn = require('./lib/bn')
bsv.Br = require('./lib/br')
bsv.Bsm = require('./lib/bsm')
Expand Down Expand Up @@ -61,6 +59,10 @@ bsv.Workers = require('./lib/workers')
bsv.WorkersResult = require('./lib/workers-result')
bsv.cmp = require('./lib/cmp')

// BIPs, accessible by their functional names
bsv.HDWallet = require('./lib/hd-wallet')
bsv.Mnemonic = require('./lib/mnemonic')

// Encryption tools. Some bitcoin standards use Aes encryption, so that's why
// these are available.
bsv.Ach = require('./lib/ach')
Expand Down
8 changes: 4 additions & 4 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Constants.Mainnet = {
pubKeyHash: 0x00,
payToScriptHash: 0x05
},
Bip32: {
HDWallet: {
pubKey: 0x0488b21e,
privKey: 0x0488ade4
},
Expand Down Expand Up @@ -49,7 +49,7 @@ Constants.Testnet = Object.assign({}, Constants.Mainnet, {
pubKeyHash: 0x6f,
payToScriptHash: 0xc4
},
Bip32: {
HDWallet: {
pubKey: 0x043587cf,
privKey: 0x04358394
},
Expand All @@ -72,7 +72,7 @@ Constants.Regtest = Object.assign({}, Constants.Mainnet, {
pubKeyHash: 0x6f,
payToScriptHash: 0xc4
},
Bip32: {
HDWallet: {
pubKey: 0x043587cf,
privKey: 0x04358394
},
Expand All @@ -95,7 +95,7 @@ Constants.STN = Object.assign({}, Constants.Mainnet, {
pubKeyHash: 0x6f,
payToScriptHash: 0xc4
},
Bip32: {
HDWallet: {
pubKey: 0x043587cf,
privKey: 0x04358394
},
Expand Down
74 changes: 37 additions & 37 deletions lib/bip-32.js → lib/hd-wallet.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* Bip32: HD Wallets
* HDWallet: Bip 32 HD Wallets
* =================
*
* Bip32 is hierarchical deterministic wallets. The standard way to use this is:
* const bip32 = new Bip32().fromRandom()
* const bip32 = new Bip32().fromSeed(buf)
* const bip32 = new Bip32().fromString(string)
* const xprv = bip32.toString()
* const xpub = bip32.toPublic().toString()
* const hdwallet = new HDWallet().fromRandom()
* const hdwallet = new HDWallet().fromSeed(buf)
* const hdwallet = new HDWallet().fromString(string)
* const xprv = hdwallet.toString()
* const xpub = hdwallet.toPublic().toString()
*
* This code was originally copied from here:
*
Expand All @@ -29,8 +29,8 @@ import { Random } from './random'
import { Struct } from './struct'
import { Workers } from './workers'

class Bip32 extends Struct {
constructor (
class HDWallet extends Struct {
constructor(
versionBytesNum,
depth,
parentFingerPrint,
Expand All @@ -50,12 +50,12 @@ class Bip32 extends Struct {
privKey,
pubKey
})
constants = constants || Constants.Default.Bip32
constants = constants || Constants.Default.HDWallet
this.Constants = constants
this.PrivKey = PrivKey
}

fromRandom () {
fromRandom() {
this.versionBytesNum = this.Constants.privKey
this.depth = 0x00
this.parentFingerPrint = Buffer.from([0, 0, 0, 0])
Expand All @@ -66,19 +66,19 @@ class Bip32 extends Struct {
return this
}

static fromRandom () {
static fromRandom() {
return new this().fromRandom()
}

fromString (str) {
fromString(str) {
return this.fromBuffer(Base58Check.decode(str))
}

/**
* Use workers to convert a bip32 string into a bip32 object without
* blocking.
*/
async asyncFromString (str) {
async asyncFromString(str) {
const args = [str]
const workersResult = await Workers.asyncObjectMethod(
this,
Expand All @@ -88,7 +88,7 @@ class Bip32 extends Struct {
return this.fromFastBuffer(workersResult.resbuf)
}

fromSeed (bytes) {
fromSeed(bytes) {
if (!Buffer.isBuffer(bytes)) {
throw new Error('bytes must be a buffer')
}
Expand All @@ -111,22 +111,22 @@ class Bip32 extends Struct {
return this
}

static fromSeed (bytes) {
static fromSeed(bytes) {
return new this().fromSeed(bytes)
}

async asyncFromSeed (bytes) {
async asyncFromSeed(bytes) {
const workersResult = await Workers.asyncObjectMethod(this, 'fromSeed', [
bytes
])
return this.fromFastBuffer(workersResult.resbuf)
}

static asyncFromSeed (bytes) {
static asyncFromSeed(bytes) {
return new this().asyncFromSeed(bytes)
}

fromBuffer (buf) {
fromBuffer(buf) {
// Both pub and private extended keys are 78 buf
if (buf.length !== 78) {
throw new Error('incorrect bip32 data length')
Expand Down Expand Up @@ -167,7 +167,7 @@ class Bip32 extends Struct {
* .fromFastBuffer transmit the public key in uncompressed form, we want it
* to be set to compressed when stored in memory.
*/
fromFastBuffer (buf) {
fromFastBuffer(buf) {
if (buf.length === 0) {
return this
}
Expand Down Expand Up @@ -201,7 +201,7 @@ class Bip32 extends Struct {
return this
}

derive (path) {
derive(path) {
const e = path.split('/')

if (path === 'm') {
Expand Down Expand Up @@ -237,14 +237,14 @@ class Bip32 extends Struct {
return bip32
}

async asyncDerive (path) {
async asyncDerive(path) {
const workersResult = await Workers.asyncObjectMethod(this, 'derive', [
path
])
return new this.constructor().fromFastBuffer(workersResult.resbuf)
}

deriveChild (i) {
deriveChild(i) {
if (typeof i !== 'number') {
throw new Error('i must be a number')
}
Expand Down Expand Up @@ -318,14 +318,14 @@ class Bip32 extends Struct {
return ret
}

toPublic () {
toPublic() {
const bip32 = new this.constructor().fromObject(this)
bip32.versionBytesNum = this.Constants.pubKey
bip32.privKey = undefined
return bip32
}

toBuffer () {
toBuffer() {
const isPrivate = this.versionBytesNum === this.Constants.privKey
const isPublic = this.versionBytesNum === this.Constants.pubKey
if (isPrivate) {
Expand Down Expand Up @@ -365,7 +365,7 @@ class Bip32 extends Struct {
* fancy, slow point multiplication to derive the y value of the public key.
* Thus, although .toFastBuffer is not any faster, .fromFastBuffer is faster.
*/
toFastBuffer () {
toFastBuffer() {
if (!this.versionBytesNum) {
return Buffer.alloc(0)
}
Expand Down Expand Up @@ -395,34 +395,34 @@ class Bip32 extends Struct {
}
}

toString () {
toString() {
return Base58Check.encode(this.toBuffer())
}

/**
* Use workers to convert a bip32 object into a bip32 string without
* blocking.
*/
async asyncToString () {
async asyncToString() {
const workersResult = await Workers.asyncObjectMethod(this, 'toString', [])
return JSON.parse(workersResult.resbuf.toString())
}

toJSON () {
toJSON() {
return this.toFastHex()
}

fromJSON (json) {
fromJSON(json) {
return this.fromFastHex(json)
}

isPrivate () {
isPrivate() {
return this.versionBytesNum === this.Constants.privKey
}
}

Bip32.Mainnet = class extends Bip32 {
constructor (
HDWallet.Mainnet = class extends HDWallet {
constructor(
versionBytesNum,
depth,
parentFingerPrint,
Expand All @@ -439,14 +439,14 @@ Bip32.Mainnet = class extends Bip32 {
chainCode,
privKey,
pubKey,
Constants.Mainnet.Bip32,
Constants.Mainnet.HDWallet,
PrivKeyClass.Mainnet
)
}
}

Bip32.Testnet = class extends Bip32 {
constructor (
HDWallet.Testnet = class extends HDWallet {
constructor(
versionBytesNum,
depth,
parentFingerPrint,
Expand All @@ -463,10 +463,10 @@ Bip32.Testnet = class extends Bip32 {
chainCode,
privKey,
pubKey,
Constants.Testnet.Bip32,
Constants.Testnet.HDWallet,
PrivKeyClass.Testnet
)
}
}

export { Bip32 }
export { HDWallet }
5 changes: 0 additions & 5 deletions lib/mainnet-bip-32.js

This file was deleted.

5 changes: 5 additions & 0 deletions lib/mainnet-hd-wallet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { HDWallet } from './hd-wallet'

const MainnetHDWallet = HDWallet.Mainnet

export { MainnetHDWallet }
Loading