Skip to content

Commit

Permalink
Merge pull request #1 from contco/hamza/typescript-conversion
Browse files Browse the repository at this point in the history
Hamza/typescript conversion
  • Loading branch information
Hamzaalam authored Jun 7, 2022
2 parents 5e1c237 + d6b3365 commit 88775ef
Show file tree
Hide file tree
Showing 9 changed files with 324 additions and 331 deletions.
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bitkey",
"version": "0.0.0-development",
"name": "@contco/bitkey",
"version": "0.0.1",
"description": "bitkey is the bitcoin key management package",
"main": "./lib/src/index.js",
"files": [
Expand All @@ -16,11 +16,12 @@
"semantic-release": "semantic-release",
"test:watch": "jest --watch",
"test": "jest --coverage",
"typecheck": "tsc --noEmit"
"typecheck": "tsc --noEmit",
"test:build": "run-s build"
},
"repository": {
"type": "git",
"url": "git+https://github.com/contco/bitkey.git"
"url": "https://github.com/contco/bitkey.git"
},
"license": "MIT",
"author": {
Expand Down Expand Up @@ -59,9 +60,12 @@
"ts-node": "^10.2.1",
"typescript": "^4.2.4"
},
"publishConfig": {
"registry": "https://npm.pkg.github.com/"
},
"config": {
"commitizen": {
"path": "./node_modules/@ryansonshine/cz-conventional-changelog"
"path": "node_modules/cz-conventional-changelog"
}
},
"lint-staged": {
Expand Down
Empty file removed src/.eslintrc
Empty file.
57 changes: 30 additions & 27 deletions src/address.js → src/address.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable */

'use strict';

var _ = require('lodash');
Expand Down Expand Up @@ -49,11 +51,13 @@ var PublicKey = require('./publickey');
* @returns {Address} A new valid and frozen instance of an Address
* @constructor
*/
function Address(data, network, type, multisigType) {
export const Address:any = (data:any, network:networkType, type:string, multisigType:string) =>{
const self: any = this;

/* jshint maxcomplexity: 12 */
/* jshint maxstatements: 20 */

if (!(this instanceof Address)) {
if (!(self instanceof Address)) {
return new Address(data, network, type);
}

Expand Down Expand Up @@ -81,7 +85,7 @@ function Address(data, network, type, multisigType) {
throw new TypeError('Third argument must be "pubkeyhash", "scripthash", "witnesspubkeyhash", "witnessscripthash", or "taproot".');
}

var info = this._classifyArguments(data, network, type);
var info = self._classifyArguments(data, network, type);

// set defaults if not set
info.network = info.network || Networks.get(network) || Networks.defaultNetwork;
Expand All @@ -103,7 +107,7 @@ function Address(data, network, type, multisigType) {
* @param {string=} type - The type of address: 'script' or 'pubkey'
* @returns {Object} An "info" object with "type", "network", and "hashBuffer"
*/
Address.prototype._classifyArguments = function(data, network, type) {
Address.prototype._classifyArguments = function(data:any, network:networkType, type:string) {
/* jshint maxcomplexity: 10 */
// transform and validate input data
if ((data instanceof Buffer || data instanceof Uint8Array) && (data.length === 20 || data.length === 32)) {
Expand Down Expand Up @@ -141,8 +145,8 @@ Address.PayToTaproot = 'taproot';
* @returns {Object} An object with keys: hashBuffer
* @private
*/
Address._transformHash = function(hash, network, type) {
var info = {};
Address._transformHash = function(hash:any, network:networkType, type:string) {
var info:any = {};
if (!(hash instanceof Buffer) && !(hash instanceof Uint8Array)) {
throw new TypeError('Address supplied is not a buffer.');
}
Expand All @@ -163,7 +167,7 @@ Address._transformHash = function(hash, network, type) {
* @param {Network=} data.network - the name of the network associated
* @return {Address}
*/
Address._transformObject = function(data) {
Address._transformObject = function(data:any) {
$.checkArgument(data.hash || data.hashBuffer, 'Must provide a `hash` or `hashBuffer` property');
$.checkArgument(data.type, 'Must provide a `type` property');
return {
Expand All @@ -180,8 +184,8 @@ Address._transformObject = function(data) {
* @returns {Object} An object with keys: network and type
* @private
*/
Address._classifyFromVersion = function(buffer) {
var version = {};
Address._classifyFromVersion = function(buffer:Buffer) {
var version:any = {};

if (buffer.length > 21) {
var info = Bech32.decode(buffer.toString('utf8'));
Expand Down Expand Up @@ -232,9 +236,9 @@ Address._classifyFromVersion = function(buffer) {
* @returns {Object} An object with keys: hashBuffer, network and type
* @private
*/
Address._transformBuffer = function(buffer, network, type) {
Address._transformBuffer = function(buffer:any, network:networkType, type:string) {
/* jshint maxcomplexity: 9 */
var info = {};
var info:any = {};
if (!(buffer instanceof Buffer) && !(buffer instanceof Uint8Array)) {
throw new TypeError('Address supplied is not a buffer.');
}
Expand Down Expand Up @@ -276,8 +280,8 @@ Address._transformBuffer = function(buffer, network, type) {
* @returns {Object} An object with keys: hashBuffer, type
* @private
*/
Address._transformPublicKey = function(pubkey, network, type) {
var info = {};
Address._transformPublicKey = function(pubkey:any, network:networkType, type:string) {
var info:any = {};
if (!(pubkey instanceof PublicKey)) {
throw new TypeError('Address must be an instance of PublicKey.');
}
Expand Down Expand Up @@ -305,7 +309,7 @@ Address._transformPublicKey = function(pubkey, network, type) {
* @returns {Object} An object with keys: hashBuffer, type
* @private
*/
Address._transformScript = function(script, network) {
Address._transformScript = function(script:any, network:networkType) {
$.checkArgument(script instanceof Script, 'script must be a Script instance');
var info = script.getAddressInfo(network);
if (!info) {
Expand All @@ -328,7 +332,7 @@ Address._transformScript = function(script, network) {
* @param {string} type - Either 'scripthash' or 'witnessscripthash'. If nestedWitness is set, then this is ignored
* @return {Address}
*/
Address.createMultisig = function(publicKeys, threshold, network, nestedWitness, type) {
Address.createMultisig = function(publicKeys:any, threshold:number, network:networkType, nestedWitness:boolean, type:string) {
network = network || publicKeys[0].network || Networks.defaultNetwork;
if (type && type !== Address.PayToScriptHash && type !== Address.PayToWitnessScriptHash) {
throw new TypeError('Type must be either scripthash or witnessscripthash to create multisig.');
Expand Down Expand Up @@ -357,7 +361,7 @@ Address.createMultisig = function(publicKeys, threshold, network, nestedWitness,
* @returns {Object} An object with keys: hashBuffer, network and type
* @private
*/
Address._transformString = function(data, network, type) {
Address._transformString = function(data:string, network:networkType, type:string) {
if (typeof(data) !== 'string') {
throw new TypeError('data parameter supplied is not a string.');
}
Expand Down Expand Up @@ -394,7 +398,7 @@ Address._transformString = function(data, network, type) {
* @param {string} type - Either 'pubkeyhash', 'witnesspubkeyhash', or 'scripthash'
* @returns {Address} A new valid and frozen instance of an Address
*/
Address.fromPublicKey = function(data, network, type) {
Address.fromPublicKey = function(data:any, network:networkType, type:string) {
var info = Address._transformPublicKey(data, network, type);
network = network || Networks.defaultNetwork;
return new Address(info.hashBuffer, network, info.type);
Expand All @@ -407,7 +411,7 @@ Address.fromPublicKey = function(data, network, type) {
* @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
* @returns {Address} A new valid and frozen instance of an Address
*/
Address.fromPublicKeyHash = function(hash, network) {
Address.fromPublicKeyHash = function(hash:any, network:networkType) {
var info = Address._transformHash(hash);
return new Address(info.hashBuffer, network, Address.PayToPublicKeyHash);
};
Expand All @@ -420,7 +424,7 @@ Address.fromPublicKeyHash = function(hash, network) {
* @param {string} type - Either 'scripthash' or 'witnessscripthash'
* @returns {Address} A new valid and frozen instance of an Address
*/
Address.fromScriptHash = function(hash, network, type) {
Address.fromScriptHash = function(hash:any, network:networkType, type:any) {
$.checkArgument(hash, 'hash parameter is required');
var info = Address._transformHash(hash);
if (type === Address.PayToWitnessScriptHash && hash.length !== 32) {
Expand All @@ -441,7 +445,7 @@ Address.fromScriptHash = function(hash, network, type) {
* @param {string} type - Either 'scripthash' or 'witnessscripthash'
* @returns {Address} A new valid and frozen instance of an Address
*/
Address.payingTo = function(script, network, type) {
Address.payingTo = function(script:any, network:networkType, type:any) {
$.checkArgument(script, 'script is required');
$.checkArgument(script instanceof Script, 'script must be instance of Script');
var hash;
Expand All @@ -466,7 +470,7 @@ Address.payingTo = function(script, network, type) {
* @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
* @returns {Address} A new valid and frozen instance of an Address
*/
Address.fromScript = function(script, network) {
Address.fromScript = function(script:any, network:networkType) {
$.checkArgument(script instanceof Script, 'script must be a Script instance');
var info = Address._transformScript(script, network);
return new Address(info.hashBuffer, network, info.type);
Expand All @@ -480,7 +484,7 @@ Address.fromScript = function(script, network) {
* @param {string=} type - The type of address: 'script' or 'pubkey'
* @returns {Address} A new valid and frozen instance of an Address
*/
Address.fromBuffer = function(buffer, network, type) {
Address.fromBuffer = function(buffer:Buffer, network:networkType, type:string) {
var info = Address._transformBuffer(buffer, network, type);
return new Address(info.hashBuffer, info.network, info.type);
};
Expand All @@ -493,7 +497,7 @@ Address.fromBuffer = function(buffer, network, type) {
* @param {string=} type - The type of address: 'script' or 'pubkey'
* @returns {Address} A new valid and frozen instance of an Address
*/
Address.fromString = function(str, network, type) {
Address.fromString = function(str:string, network:networkType, type:string) {
var info = Address._transformString(str, network, type);
return new Address(info.hashBuffer, info.network, info.type);
};
Expand All @@ -504,7 +508,7 @@ Address.fromString = function(str, network, type) {
* @param {string} json - An JSON string or Object with keys: hash, network and type
* @returns {Address} A new valid instance of an Address
*/
Address.fromObject = function fromObject(obj) {
Address.fromObject = function fromObject(obj:any){
$.checkState(
JSUtil.isHexa(obj.hash),
'Unexpected hash property, "' + obj.hash + '", expected to be hex.'
Expand All @@ -527,7 +531,7 @@ Address.fromObject = function fromObject(obj) {
* @param {string} type - The type of address: 'script' or 'pubkey'
* @returns {null|Error} The corresponding error message
*/
Address.getValidationError = function(data, network, type) {
Address.getValidationError = function(data:string, network:networkType, type:string) {
var error;
try {
/* jshint nonew: false */
Expand All @@ -551,7 +555,7 @@ Address.getValidationError = function(data, network, type) {
* @param {string} type - The type of address: 'script' or 'pubkey'
* @returns {boolean} The corresponding error message
*/
Address.isValid = function(data, network, type) {
Address.isValid = function(data:string, network:networkType, type:string) {
return !Address.getValidationError(data, network, type);
};

Expand Down Expand Up @@ -647,6 +651,5 @@ Address.prototype.inspect = function() {
return '<Address: ' + this.toString() + ', type: ' + this.type + ', network: ' + this.network + '>';
};

module.exports = Address;

var Script = require('./script');
Loading

0 comments on commit 88775ef

Please sign in to comment.