| @@ 276-316 (lines=41) @@ | ||
| 273 | * @param path |
|
| 274 | * @returns {{address: *, redeemScript: *}} |
|
| 275 | */ |
|
| 276 | WalletSweeper.prototype.createAddress = function(path) { |
|
| 277 | //ensure a public path is used |
|
| 278 | path = path.replace("m", "M"); |
|
| 279 | var keyIndex = path.split("/")[1].replace("'", ""); |
|
| 280 | var scriptType = parseInt(path.split("/")[2]); |
|
| 281 | ||
| 282 | //derive the primary pub key directly from the primary priv key |
|
| 283 | var primaryPubKey = walletSDK.deriveByPath(this.primaryPrivateKey, path, "m"); |
|
| 284 | //derive the backup pub key directly from the backup priv key (unharden path) |
|
| 285 | var backupPubKey = walletSDK.deriveByPath(this.backupPublicKey, path.replace("'", ""), "M"); |
|
| 286 | //derive a pub key for this path from the blocktrail pub key |
|
| 287 | var blocktrailPubKey = walletSDK.deriveByPath(this.getBlocktrailPublicKey(path), path, "M/" + keyIndex + "'"); |
|
| 288 | ||
| 289 | //sort the keys and generate a multisig redeem script and address |
|
| 290 | var multisigKeys = walletSDK.sortMultiSigKeys([ |
|
| 291 | primaryPubKey.keyPair.getPublicKeyBuffer(), |
|
| 292 | backupPubKey.keyPair.getPublicKeyBuffer(), |
|
| 293 | blocktrailPubKey.keyPair.getPublicKeyBuffer() |
|
| 294 | ]); |
|
| 295 | ||
| 296 | var multisig = bitcoin.script.multisig.output.encode(2, multisigKeys); |
|
| 297 | var redeemScript, witnessScript; |
|
| 298 | if (this.network !== "bitcoincash" && scriptType === walletSDK.CHAIN_BTC_SEGWIT) { |
|
| 299 | witnessScript = multisig; |
|
| 300 | redeemScript = bitcoin.script.witnessScriptHash.output.encode(bitcoin.crypto.sha256(witnessScript)); |
|
| 301 | } else { |
|
| 302 | witnessScript = null; |
|
| 303 | redeemScript = multisig; |
|
| 304 | } |
|
| 305 | var scriptHash = bitcoin.crypto.hash160(redeemScript); |
|
| 306 | var scriptPubKey = bitcoin.script.scriptHash.output.encode(scriptHash); |
|
| 307 | ||
| 308 | var network = this.network; |
|
| 309 | if (typeof this.network !== "undefined") { |
|
| 310 | network = this.network; |
|
| 311 | } |
|
| 312 | var address = bitcoin.address.fromOutputScript(scriptPubKey, network); |
|
| 313 | ||
| 314 | //@todo return as buffers |
|
| 315 | return {address: address.toString(), redeem: redeemScript, witness: witnessScript}; |
|
| 316 | }; |
|
| 317 | ||
| 318 | /** |
|
| 319 | * create a batch of multisig addresses |
|
| @@ 548-586 (lines=39) @@ | ||
| 545 | ||
| 546 | /** |
|
| 547 | * get redeemscript for specified path |
|
| 548 | * |
|
| 549 | * @param path |
|
| 550 | * @returns {bitcoin.Script} |
|
| 551 | */ |
|
| 552 | Wallet.prototype.getRedeemScriptByPath = function(path) { |
|
| 553 | return this.getWalletScriptByPath(path).redeemScript; |
|
| 554 | }; |
|
| 555 | ||
| 556 | Wallet.prototype.getWalletScriptByPath = function(path) { |
|
| 557 | var self = this; |
|
| 558 | ||
| 559 | // get derived primary key |
|
| 560 | var derivedPrimaryPublicKey = self.getPrimaryPublicKey(path); |
|
| 561 | // get derived blocktrail key |
|
| 562 | var derivedBlocktrailPublicKey = self.getBlocktrailPublicKey(path); |
|
| 563 | // derive the backup key |
|
| 564 | var derivedBackupPublicKey = Wallet.deriveByPath(self.backupPublicKey, path.replace("'", ""), "M"); |
|
| 565 | ||
| 566 | // sort the pubkeys |
|
| 567 | var pubKeys = Wallet.sortMultiSigKeys([ |
|
| 568 | derivedPrimaryPublicKey.keyPair.getPublicKeyBuffer(), |
|
| 569 | derivedBackupPublicKey.keyPair.getPublicKeyBuffer(), |
|
| 570 | derivedBlocktrailPublicKey.keyPair.getPublicKeyBuffer() |
|
| 571 | ]); |
|
| 572 | ||
| 573 | var multisig = bitcoin.script.multisig.output.encode(2, pubKeys); |
|
| 574 | var scriptType = parseInt(path.split("/")[2]); |
|
| 575 | ||
| 576 | var ws, rs; |
|
| 577 | if (this.network !== "bitcoincash" && scriptType === Wallet.CHAIN_BTC_SEGWIT) { |
|
| 578 | ws = multisig; |
|
| 579 | rs = bitcoin.script.witnessScriptHash.output.encode(bitcoin.crypto.sha256(ws)); |
|
| 580 | } else { |
|
| 581 | ws = null; |
|
| 582 | rs = multisig; |
|
| 583 | } |
|
| 584 | ||
| 585 | var spk = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(rs)); |
|
| 586 | var addr = bitcoin.address.fromOutputScript(spk, this.network); |
|
| 587 | ||
| 588 | return { |
|
| 589 | witnessScript: ws, |
|