diff --git a/types/bitcore-lib/bitcore-lib-tests.ts b/types/bitcore-lib/bitcore-lib-tests.ts new file mode 100644 index 0000000000..fb39c85fc1 --- /dev/null +++ b/types/bitcore-lib/bitcore-lib-tests.ts @@ -0,0 +1,27 @@ +import * as bitcore from 'bitcore-lib'; + +const transaction = new bitcore.Transaction({}); + +const output: bitcore.Output = transaction.outputs[0]; + +const privateKey: bitcore.PrivateKey = new bitcore.PrivateKey('privateKey'); +const publicKey: bitcore.PublicKey = privateKey.publicKey; +const publicKeyAsString: string = publicKey.toString(); + +const signature = bitcore.crypto.ECDSA.sign(Buffer.from('sign this message', 'hex'), new bitcore.PrivateKey('privateKey')); + +bitcore.crypto.ECDSA.verify( + Buffer.from('buffer', 'hex'), + bitcore.crypto.Signature.fromString('signature'), + new bitcore.PublicKey('publicKey') +); + +const utxo: bitcore.Transaction.UnspentOutput[] = [new bitcore.Transaction.UnspentOutput({})]; + +new bitcore.Block(Buffer.from('123', 'hex')); + +const tx = new bitcore.Transaction() + .from(utxo) + .change('bitcoinAddress') + .addData(Buffer.from('')) + .sign('bitcoinAddressPrivateKey'); diff --git a/types/bitcore-lib/index.d.ts b/types/bitcore-lib/index.d.ts new file mode 100644 index 0000000000..0846b85c0d --- /dev/null +++ b/types/bitcore-lib/index.d.ts @@ -0,0 +1,130 @@ +// Type definitions for bitcore-lib 0.15 +// Project: https://github.com/bitpay/bitcore-lib +// Definitions by: Lautaro Dragan +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +// TypeScript Version: 2.2 + +/// + +export namespace crypto { + class BN { } + namespace ECDSA { + function sign(message: Buffer, key: PrivateKey): Signature; + function verify(hashbuf: Buffer, sig: Signature, pubkey: PublicKey, endian?: 'little'): boolean; + } + namespace Hash { + function sha256(buffer: Buffer): Uint8Array; + } + namespace Random { + function getRandomBuffer(size: number): Buffer; + } + namespace Point {} + class Signature { + static fromDER(sig: Buffer): Signature; + static fromString(data: string): Signature; + SIGHASH_ALL: number; + toString(): string; + } +} + +export class Transaction { + inputs: Input[]; + outputs: Output[]; + readonly id: string; + readonly hash: string; + nid: string; + + constructor(serialized?: any); + + from(utxos: Transaction.UnspentOutput[]): Transaction; + to(address: Address | string, amount: number): Transaction; + change(address: Address | string): Transaction; + sign(privateKey: PrivateKey | string): Transaction; + applySignature(sig: crypto.Signature): Transaction; + addData(data: Buffer): this; + serialize(): string; +} + +export namespace Transaction { + class UnspentOutput { + static fromObject(o: object): UnspentOutput; + + readonly address: Address; + readonly txId: string; + readonly outputIndex: number; + readonly script: Script; + readonly satoshis: number; + + constructor(data: object); + + inspect(): string; + toObject(): this; + toString(): string; + } +} + +export class Block { + hash: string; + height: number; + transactions: Transaction[]; + header: { + time: number; + prevHash: string; + }; + + constructor(data: Buffer | object); +} + +export class PrivateKey { + readonly publicKey: PublicKey; + + constructor(key: string); +} + +export class PublicKey { + constructor(source: string); + + static fromPrivateKey(privateKey: PrivateKey): PublicKey; + + toBuffer(): Buffer; + toDER(): Buffer; +} + +export interface Output { + readonly script: any; +} + +export namespace Script { + const types: { + DATA_OUT: string; + }; + function buildPublicKeyHashOut(address: Address): Script; +} + +export class Script { } + +export interface Util { + readonly buffer: { + reverse(a: any): any; + }; +} + +export namespace Networks { + interface Network { + readonly name: string; + readonly alias: string; + } + + const livenet: Network; + const mainnet: Network; + const testnet: Network; + + function add(data: any): Network; + function remove(network: Network): void; + function get(args: string | number | Network, keys: string | string[]): Network; +} + +export class Address {} + +export class Input {} diff --git a/types/bitcore-lib/tsconfig.json b/types/bitcore-lib/tsconfig.json new file mode 100644 index 0000000000..156908fedc --- /dev/null +++ b/types/bitcore-lib/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es6"], + "target": "es6", + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "esModuleInterop": true + }, + "files": [ + "index.d.ts", + "bitcore-lib-tests.ts" + ] +} diff --git a/types/bitcore-lib/tslint.json b/types/bitcore-lib/tslint.json new file mode 100644 index 0000000000..dae7fb7540 --- /dev/null +++ b/types/bitcore-lib/tslint.json @@ -0,0 +1,4 @@ +{ + "extends": "dtslint/dt.json", + "rules": {} +} \ No newline at end of file