From 2099e9275809bd788afd8fa6e3d5ab48a60e3709 Mon Sep 17 00:00:00 2001 From: lautarodragan Date: Sat, 10 Feb 2018 04:39:52 -0300 Subject: [PATCH 1/7] bitcore-lib --- types/bitcore-lib/bitcore-lib-tests.ts | 27 +++++ types/bitcore-lib/index.d.ts | 142 +++++++++++++++++++++++++ types/bitcore-lib/tsconfig.json | 22 ++++ types/bitcore-lib/tslint.json | 4 + 4 files changed, 195 insertions(+) create mode 100644 types/bitcore-lib/bitcore-lib-tests.ts create mode 100644 types/bitcore-lib/index.d.ts create mode 100644 types/bitcore-lib/tsconfig.json create mode 100644 types/bitcore-lib/tslint.json diff --git a/types/bitcore-lib/bitcore-lib-tests.ts b/types/bitcore-lib/bitcore-lib-tests.ts new file mode 100644 index 0000000000..a277700d84 --- /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'), 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..e0c86e53f2 --- /dev/null +++ b/types/bitcore-lib/index.d.ts @@ -0,0 +1,142 @@ +// Type definitions for bitcore-lib 0.15.0 +// Project: https://github.com/bitpay/bitcore-lib +// Definitions by: Lautaro Dragan +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +declare namespace bitcoreLib { + export const Block: Block + export const util: Util + export const Script: Script + export const PrivateKey: PrivateKey + + 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 + } + export namespace Hash { + function sha256(buffer: Buffer): Uint8Array + } + export const Random: RandomInterface + export const Point: PointInterface + + class Signature { + static fromDER(sig: Buffer): Signature + static fromString(data: string): Signature + SIGHASH_ALL: number + toString(): string + } + } + + 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 + } + } + + interface Block { + hash: string + height: number + transactions: Transaction[] + header: { + time: number + prevHash: string + } + + new(data: Buffer | object): Block + (data: Buffer | object): Block + } + + interface PrivateKey { + readonly publicKey: PublicKey + + new(key: string): this + (source: string): this + } + + class PublicKey { + constructor(source: string) + } + + interface Output { + readonly script: any + } + + interface Script { + types: { + DATA_OUT: string + } + buildPublicKeyHashOut(address: Address): Script + } + + interface Util { + readonly buffer: { + reverse(a: any): any + } + } + + export namespace Networks { + + interface Network { + readonly name: string + readonly alias: string + } + + export const livenet: Network + export const mainnet: Network + export const testnet: Network + + export function add(data: any): Network + export function remove(network: Network): void + export function get(args: string | number | Network, keys: string | string[]): Network + } + +} + +interface Address { + +} + +interface Input { + +} + +interface RandomInterface { +} +interface PointInterface { +} + +export = bitcoreLib \ No newline at end of file diff --git a/types/bitcore-lib/tsconfig.json b/types/bitcore-lib/tsconfig.json new file mode 100644 index 0000000000..f9664facca --- /dev/null +++ b/types/bitcore-lib/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "esModuleInterop": true + }, + "files": [ + "index.d.ts", + "bitcore-lib-tests.ts" + ] +} \ No newline at end of file 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 From ee77e39b8644b02093b852aa4f39c91186547879 Mon Sep 17 00:00:00 2001 From: lautarodragan Date: Sat, 10 Feb 2018 05:58:56 -0300 Subject: [PATCH 2/7] bitcore-lib: add lib to tsconfig --- types/bitcore-lib/tsconfig.json | 1 + 1 file changed, 1 insertion(+) diff --git a/types/bitcore-lib/tsconfig.json b/types/bitcore-lib/tsconfig.json index f9664facca..9730553f79 100644 --- a/types/bitcore-lib/tsconfig.json +++ b/types/bitcore-lib/tsconfig.json @@ -1,6 +1,7 @@ { "compilerOptions": { "module": "commonjs", + "lib": ["es6"], "target": "es6", "noImplicitAny": true, "noImplicitThis": true, From a649eea8cd41d579fc852b1879babd6c30e71920 Mon Sep 17 00:00:00 2001 From: lautarodragan Date: Sat, 10 Feb 2018 06:34:38 -0300 Subject: [PATCH 3/7] bitcore-lib: several styling fixes --- types/bitcore-lib/index.d.ts | 184 ++++++++++++++++------------------- 1 file changed, 85 insertions(+), 99 deletions(-) diff --git a/types/bitcore-lib/index.d.ts b/types/bitcore-lib/index.d.ts index e0c86e53f2..9b0a925bbf 100644 --- a/types/bitcore-lib/index.d.ts +++ b/types/bitcore-lib/index.d.ts @@ -1,142 +1,128 @@ -// Type definitions for bitcore-lib 0.15.0 +// 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 + /// -declare namespace bitcoreLib { - export const Block: Block - export const util: Util - export const Script: Script - export const PrivateKey: PrivateKey - - export namespace crypto { +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 + function sign(message: Buffer, key: PrivateKey): Signature; + function verify(hashbuf: Buffer, sig: Signature, pubkey: PublicKey, endian?: 'little'): boolean; } - export namespace Hash { - function sha256(buffer: Buffer): Uint8Array + namespace Hash { + function sha256(buffer: Buffer): Uint8Array; } - export const Random: RandomInterface - export const Point: PointInterface + const Random: RandomInterface; + const Point: PointInterface; class Signature { - static fromDER(sig: Buffer): Signature - static fromString(data: string): Signature - SIGHASH_ALL: number - toString(): string + static fromDER(sig: Buffer): Signature; + static fromString(data: string): Signature; + SIGHASH_ALL: number; + toString(): string; } - } +} - class Transaction { - inputs: Input[] - outputs: Output[] - readonly id: string - readonly hash: string - nid: string +export class Transaction { + inputs: Input[]; + outputs: Output[]; + readonly id: string; + readonly hash: string; + nid: string; - constructor(serialized?: any) + 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 - } + 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 { +export namespace Transaction { class UnspentOutput { - static fromObject(o: object): UnspentOutput + static fromObject(o: object): UnspentOutput; - readonly address: Address - readonly txId: string - readonly outputIndex: number - readonly script: Script - readonly satoshis: number + readonly address: Address; + readonly txId: string; + readonly outputIndex: number; + readonly script: Script; + readonly satoshis: number; - constructor(data: object) + constructor(data: object); - inspect(): string - toObject(): this - toString(): string + inspect(): string; + toObject(): this; + toString(): string; } - } +} - interface Block { - hash: string - height: number - transactions: Transaction[] +export class Block { + hash: string; + height: number; + transactions: Transaction[]; header: { - time: number - prevHash: string - } + time: number; + prevHash: string; + }; - new(data: Buffer | object): Block - (data: Buffer | object): Block - } + constructor(data: Buffer | object); +} - interface PrivateKey { - readonly publicKey: PublicKey +export class PrivateKey { + readonly publicKey: PublicKey; - new(key: string): this - (source: string): this - } + constructor(key: string); +} - class PublicKey { - constructor(source: string) - } +export class PublicKey { + constructor(source: string); +} - interface Output { - readonly script: any - } +export interface Output { + readonly script: any; +} - interface Script { - types: { - DATA_OUT: string - } - buildPublicKeyHashOut(address: Address): Script - } +export namespace Script { + const types: { + DATA_OUT: string; + }; + function buildPublicKeyHashOut(address: Address): Script; +} - interface Util { +export class Script { } + +export interface Util { readonly buffer: { - reverse(a: any): any + reverse(a: any): any; } - } - - export namespace Networks { +} +export namespace Networks { interface Network { - readonly name: string - readonly alias: string + readonly name: string; + readonly alias: string; } - export const livenet: Network - export const mainnet: Network - export const testnet: Network - - export function add(data: any): Network - export function remove(network: Network): void - export function get(args: string | number | Network, keys: string | string[]): Network - } + 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; } -interface Address { +interface Address {} -} +interface Input {} -interface Input { +interface RandomInterface {} -} - -interface RandomInterface { -} -interface PointInterface { -} - -export = bitcoreLib \ No newline at end of file +interface PointInterface {} From 9216e74e502cb51527fff9a53edeee42c515e6da Mon Sep 17 00:00:00 2001 From: lautarodragan Date: Sat, 10 Feb 2018 06:38:49 -0300 Subject: [PATCH 4/7] bitcore-lib: fix tests --- types/bitcore-lib/bitcore-lib-tests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/bitcore-lib/bitcore-lib-tests.ts b/types/bitcore-lib/bitcore-lib-tests.ts index a277700d84..fb39c85fc1 100644 --- a/types/bitcore-lib/bitcore-lib-tests.ts +++ b/types/bitcore-lib/bitcore-lib-tests.ts @@ -8,7 +8,7 @@ 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'), bitcore.PrivateKey('privateKey')); +const signature = bitcore.crypto.ECDSA.sign(Buffer.from('sign this message', 'hex'), new bitcore.PrivateKey('privateKey')); bitcore.crypto.ECDSA.verify( Buffer.from('buffer', 'hex'), From 11d9df472b5a019e572690eb930b3d5a39f528ff Mon Sep 17 00:00:00 2001 From: lautarodragan Date: Sat, 10 Feb 2018 06:46:33 -0300 Subject: [PATCH 5/7] bitcore-lib: more fixes --- types/bitcore-lib/index.d.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/types/bitcore-lib/index.d.ts b/types/bitcore-lib/index.d.ts index 9b0a925bbf..bc8874f590 100644 --- a/types/bitcore-lib/index.d.ts +++ b/types/bitcore-lib/index.d.ts @@ -16,8 +16,12 @@ export namespace crypto { namespace Hash { function sha256(buffer: Buffer): Uint8Array; } - const Random: RandomInterface; - const Point: PointInterface; + namespace Random { + function getRandomBuffer(size: number): Buffer + } + namespace Point { + + } class Signature { static fromDER(sig: Buffer): Signature; @@ -101,7 +105,7 @@ export class Script { } export interface Util { readonly buffer: { reverse(a: any): any; - } + }; } export namespace Networks { @@ -119,10 +123,6 @@ export namespace Networks { function get(args: string | number | Network, keys: string | string[]): Network; } -interface Address {} +export class Address {} -interface Input {} - -interface RandomInterface {} - -interface PointInterface {} +export class Input {} From fa3829b0655e1fd6217d6048a2b8699aa88bc464 Mon Sep 17 00:00:00 2001 From: lautarodragan Date: Sat, 10 Feb 2018 06:53:40 -0300 Subject: [PATCH 6/7] bitcore-lib: still more fixes --- types/bitcore-lib/index.d.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/types/bitcore-lib/index.d.ts b/types/bitcore-lib/index.d.ts index bc8874f590..0846b85c0d 100644 --- a/types/bitcore-lib/index.d.ts +++ b/types/bitcore-lib/index.d.ts @@ -1,6 +1,6 @@ // Type definitions for bitcore-lib 0.15 // Project: https://github.com/bitpay/bitcore-lib -// Definitions by: Lautaro Dragan +// Definitions by: Lautaro Dragan // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 @@ -17,12 +17,9 @@ export namespace crypto { function sha256(buffer: Buffer): Uint8Array; } namespace Random { - function getRandomBuffer(size: number): Buffer + function getRandomBuffer(size: number): Buffer; } - namespace Point { - - } - + namespace Point {} class Signature { static fromDER(sig: Buffer): Signature; static fromString(data: string): Signature; @@ -87,6 +84,11 @@ export class PrivateKey { export class PublicKey { constructor(source: string); + + static fromPrivateKey(privateKey: PrivateKey): PublicKey; + + toBuffer(): Buffer; + toDER(): Buffer; } export interface Output { From bc7c6c543c1cf39b4204d443f25059c9264fbc4b Mon Sep 17 00:00:00 2001 From: Lautaro Dragan Date: Sun, 11 Feb 2018 23:37:10 -0300 Subject: [PATCH 7/7] bitcore-lib: set strictNullChecks to true --- types/bitcore-lib/tsconfig.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/bitcore-lib/tsconfig.json b/types/bitcore-lib/tsconfig.json index 9730553f79..156908fedc 100644 --- a/types/bitcore-lib/tsconfig.json +++ b/types/bitcore-lib/tsconfig.json @@ -5,7 +5,7 @@ "target": "es6", "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ @@ -20,4 +20,4 @@ "index.d.ts", "bitcore-lib-tests.ts" ] -} \ No newline at end of file +}