Skip to content

Commit

Permalink
Script.number.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Cardona committed May 22, 2019
1 parent 131091a commit b589a61
Show file tree
Hide file tree
Showing 20 changed files with 656 additions and 475 deletions.
82 changes: 41 additions & 41 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
/// <reference path="./lib/interfaces/vendors.d.ts"/>

export * from "./lib/Address";
export * from "./lib/BITBOX";
export * from "./lib/BitcoinCash";
export * from "./lib/Block";
export * from "./lib/Blockchain";
export * from "./lib/Control";
export * from "./lib/Crypto";
export * from "./lib/ECPair";
export * from "./lib/Generating";
export * from "./lib/HDNode";
export * from "./lib/Mining";
export * from "./lib/Mnemonic";
export * from "./lib/Price";
export * from "./lib/RawTransactions";
export * from "./lib/Schnorr";
export * from "./lib/Script";
export * from "./lib/Socket";
export * from "./lib/Transaction";
export * from "./lib/TransactionBuilder";
export * from "./lib/Util";
export * from "./lib/Address"
export * from "./lib/BITBOX"
export * from "./lib/BitcoinCash"
export * from "./lib/Block"
export * from "./lib/Blockchain"
export * from "./lib/Control"
export * from "./lib/Crypto"
export * from "./lib/ECPair"
export * from "./lib/Generating"
export * from "./lib/HDNode"
export * from "./lib/Mining"
export * from "./lib/Mnemonic"
export * from "./lib/Price"
export * from "./lib/RawTransactions"
export * from "./lib/Schnorr"
export * from "./lib/Script"
export * from "./lib/Socket"
export * from "./lib/Transaction"
export * from "./lib/TransactionBuilder"
export * from "./lib/Util"

export interface BchInfo {
hashGenesisBlock: string;
port: number;
portRpc: number;
hashGenesisBlock: string
port: number
portRpc: number
protocol: {
magic: number;
};
seedsDns: string[];
magic: number
}
seedsDns: string[]
versions: {
bip32: {
private: number;
public: number;
};
bip44: number;
private: number;
public: number;
scripthash: number;
messagePrefix: string;
};
name: string;
per1: number;
unit: string;
testnet: boolean;
toBitcoinJS: any;
toBitcore: any;
private: number
public: number
}
bip44: number
private: number
public: number
scripthash: number
messagePrefix: string
}
name: string
per1: number
unit: string
testnet: boolean
toBitcoinJS: any
toBitcore: any
}
29 changes: 21 additions & 8 deletions lib/Address.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
// imports
import axios, { AxiosResponse } from "axios"
import { AddressDetailsResult, AddressUtxoResult, AddressUnconfirmedResult } from "bitcoin-com-rest";
import {
AddressDetailsResult,
AddressUnconfirmedResult,
AddressUtxoResult
} from "bitcoin-com-rest"
import * as bcl from "bitcoincashjs-lib"
import { BchInfo } from ".."
import { resturl } from "./BITBOX"
import { BchInfo } from "..";

// consts
// TODO: port require statements to impprt
const Bitcoin = require("bitcoincashjs-lib")
const cashaddr = require("cashaddrjs")
Expand Down Expand Up @@ -203,7 +210,10 @@ export class Address {
else bitcoincash = coininfo.bitcoincash.test

const bitcoincashBitcoinJSLib: any = bitcoincash.toBitcoinJS()
const HDNode: bcl.HDNode = Bitcoin.HDNode.fromBase58(xpub, bitcoincashBitcoinJSLib)
const HDNode: bcl.HDNode = Bitcoin.HDNode.fromBase58(
xpub,
bitcoincashBitcoinJSLib
)
const address: bcl.HDNode = HDNode.derivePath(path)
return this.toCashAddress(address.getAddress())
}
Expand All @@ -222,7 +232,10 @@ export class Address {
return this.toCashAddress(address.getAddress())
}

public fromOutputScript(scriptPubKey: Buffer, network: string = "mainnet"): string {
public fromOutputScript(
scriptPubKey: Buffer,
network: string = "mainnet"
): string {
let netParam: any
if (network !== "bitcoincash" && network !== "mainnet")
netParam = Bitcoin.networks.testnet
Expand Down Expand Up @@ -369,19 +382,19 @@ export class Address {
private _decode(address: string): Decoded {
try {
return this._decodeLegacyAddress(address)
} catch (error) { }
} catch (error) {}

try {
return this._decodeCashAddress(address)
} catch (error) { }
} catch (error) {}

throw new Error(`Unsupported address format : ${address}`)
}

private _decodeHash160(address: string): DecodedHash160 {
try {
return this._decodeAddressFromHash160(address)
} catch (error) { }
} catch (error) {}

throw new Error(`Unsupported address format : ${address}`)
}
Expand Down Expand Up @@ -449,7 +462,7 @@ export class Address {
const decoded: Decoded = cashaddr.decode(`${prefixes[i]}:${address}`)
decoded.format = "cashaddr"
return decoded
} catch (error) { }
} catch (error) {}
}

throw new Error(`Invalid format : ${address}`)
Expand Down
28 changes: 14 additions & 14 deletions lib/BITBOX.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
/// <reference path="./interfaces/vendors.d.ts"/>

// import interfaces
import { IConfig } from "./interfaces/BITBOXInterfaces"

// local deps
// imports
import { Address } from "./Address"
import { BitcoinCash } from "./BitcoinCash"
import { Crypto } from "./Crypto"
import { Util } from "./Util"
import { Block } from "./Block"
import { Blockchain } from "./Blockchain"
import { Control } from "./Control"
import { Crypto } from "./Crypto"
import { ECPair } from "./ECPair"
import { Generating } from "./Generating"
import { HDNode } from "./HDNode"
import { IConfig } from "./interfaces/BITBOXInterfaces"
import { Mining } from "./Mining"
import { RawTransactions } from "./RawTransactions"
import { Mnemonic } from "./Mnemonic"
import { Address } from "./Address"
import { HDNode } from "./HDNode"
import { Transaction } from "./Transaction"
import { TransactionBuilder } from "./TransactionBuilder"
import { ECPair } from "./ECPair"
import { Script } from "./Script"
import { Price } from "./Price"
import { Socket } from "./Socket"
import { RawTransactions } from "./RawTransactions"
import { Schnorr } from "./Schnorr"
import { Script } from "./Script"
import { Socket } from "./Socket"
import { Transaction } from "./Transaction"
import { TransactionBuilder } from "./TransactionBuilder"
import { Util } from "./Util"
// import { Wallet } from "./Wallet"

// consts
export const resturl = "https://rest.bitcoin.com/v2/"

export class BITBOX {
public restURL: string
public Address: Address
Expand Down
17 changes: 12 additions & 5 deletions lib/BitcoinCash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ export class BitcoinCash {
return satoshis / 100
}

public signMessageWithPrivKey(privateKeyWIF: string, message: string): string {
public signMessageWithPrivKey(
privateKeyWIF: string,
message: string
): string {
const network: string =
privateKeyWIF.charAt(0) === "c" ? "testnet" : "mainnet"
let bitcoincash: any
Expand All @@ -73,7 +76,11 @@ export class BitcoinCash {
.toString("base64")
}

public verifyMessage(address: string, signature: string, message: string): boolean {
public verifyMessage(
address: string,
signature: string,
message: string
): boolean {
return bitcoinMessage.verify(
message,
this._address.toLegacyAddress(address),
Expand Down Expand Up @@ -126,13 +133,13 @@ export class BitcoinCash {
}
}

Object.keys(inputs).forEach(function (key) {
Object.keys(inputs).forEach(function(key) {
if (key.slice(0, 8) === "MULTISIG") {
// ex. "MULTISIG-P2SH:2-3" would mean 2 of 3 P2SH MULTISIG
const keyParts = key.split(":")
if (keyParts.length !== 2) throw new Error(`invalid input: ${key}`)
const newKey = keyParts[0]
const mAndN = keyParts[1].split("-").map(function (item) {
const mAndN = keyParts[1].split("-").map(function(item) {
return parseInt(item)
})

Expand All @@ -145,7 +152,7 @@ export class BitcoinCash {
if (key.indexOf("W") >= 0) hasWitness = true
})

Object.keys(outputs).forEach(function (key) {
Object.keys(outputs).forEach(function(key) {
totalWeight += types.outputs[key] * outputs[key]
})

Expand Down
2 changes: 1 addition & 1 deletion lib/Block.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios, { AxiosResponse } from "axios"
import { BlockDetailsResult } from "bitcoin-com-rest";
import { BlockDetailsResult } from "bitcoin-com-rest"
import { resturl } from "./BITBOX"

export class Block {
Expand Down
26 changes: 19 additions & 7 deletions lib/Blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@
*/

import axios, { AxiosResponse } from "axios"
import { BlockDetailsResult, BlockchainInfoResult, BlockHeaderResult, ChainTipResult, MempoolInfoResult, TxOutResult, MempoolEntryResult } from "bitcoin-com-rest";
import {
BlockchainInfoResult,
BlockDetailsResult,
BlockHeaderResult,
ChainTipResult,
MempoolEntryResult,
MempoolInfoResult,
TxOutResult
} from "bitcoin-com-rest"
import { resturl } from "./BITBOX"

export class Blockchain {
Expand Down Expand Up @@ -147,7 +155,7 @@ export class Blockchain {
try {
const response: AxiosResponse = await axios.get(
`${
this.restURL
this.restURL
}blockchain/getMempoolAncestors/${txid}?verbose=${verbose}`
)
return response.data
Expand All @@ -167,7 +175,7 @@ export class Blockchain {
try {
const response: AxiosResponse = await axios.get(
`${
this.restURL
this.restURL
}blockchain/getMempoolDescendants/${txid}?verbose=${verbose}`
)
return response.data
Expand All @@ -177,7 +185,9 @@ export class Blockchain {
}
}

public async getMempoolEntry(txid: string | string[]): Promise<MempoolEntryResult> {
public async getMempoolEntry(
txid: string | string[]
): Promise<MempoolEntryResult> {
//if (typeof txid !== "string") txid = JSON.stringify(txid)

try {
Expand Down Expand Up @@ -241,7 +251,7 @@ export class Blockchain {
try {
const response: AxiosResponse = await axios.get(
`${
this.restURL
this.restURL
}blockchain/getTxOut/${txid}/n?include_mempool=${include_mempool}`
)
return response.data
Expand All @@ -251,7 +261,9 @@ export class Blockchain {
}
}

public async getTxOutProof(txids: string | string[]): Promise<string | string[]> {
public async getTxOutProof(
txids: string | string[]
): Promise<string | string[]> {
try {
// Single txid.
if (typeof txids === "string") {
Expand Down Expand Up @@ -315,7 +327,7 @@ export class Blockchain {
try {
const response: AxiosResponse = await axios.get(
`${
this.restURL
this.restURL
}blockchain/verifyChain?checklevel=${checklevel}&nblocks=${nblocks}`
)
return response.data
Expand Down
6 changes: 4 additions & 2 deletions lib/Control.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios, { AxiosResponse } from "axios"
import { NodeInfoResult } from "bitcoin-com-rest";
import { NodeInfoResult } from "bitcoin-com-rest"
import { resturl } from "./BITBOX"

export class Control {
Expand All @@ -10,7 +10,9 @@ export class Control {

public async getInfo(): Promise<NodeInfoResult> {
try {
const response: AxiosResponse = await axios.get(`${this.restURL}control/getInfo`)
const response: AxiosResponse = await axios.get(
`${this.restURL}control/getInfo`
)
return response.data
} catch (error) {
if (error.response && error.response.data) throw error.response.data
Expand Down
8 changes: 6 additions & 2 deletions lib/ECPair.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as bcl from "bitcoincashjs-lib";
import * as bcl from "bitcoincashjs-lib"
import { Address } from "./Address"

const Bitcoin = require("bitcoincashjs-lib")
Expand Down Expand Up @@ -33,7 +33,11 @@ export class ECPair {
return ecpair.sign(buffer)
}

public verify(ecpair: bcl.ECPair, buffer: Buffer, signature: bcl.ECSignature): boolean {
public verify(
ecpair: bcl.ECPair,
buffer: Buffer,
signature: bcl.ECSignature
): boolean {
return ecpair.verify(buffer, signature)
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Generating.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class Generating {
try {
const response: AxiosResponse = await axios.post(
`${
this.restURL
this.restURL
}generating/generateToAddress/${blocks}/${address}?maxtries=${maxtries}`
)
return response.data
Expand Down
Loading

0 comments on commit b589a61

Please sign in to comment.