Browse Source

Fixed address.ts lint

fixTypes
junderw 6 years ago
parent
commit
d9cba6f176
No known key found for this signature in database GPG Key ID: B256185D3A971908
  1. 12
      src/address.js
  2. 36
      ts_src/address.ts
  3. 8
      types/address.d.ts

12
src/address.js

@ -1,9 +1,9 @@
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const types = require("./types");
const bscript = require("./script");
const networks = require("./networks"); const networks = require("./networks");
const payments = require("./payments"); const payments = require("./payments");
const bscript = require("./script");
const types = require("./types");
const bech32 = require('bech32'); const bech32 = require('bech32');
const bs58check = require('bs58check'); const bs58check = require('bs58check');
const typeforce = require('typeforce'); const typeforce = require('typeforce');
@ -16,7 +16,7 @@ function fromBase58Check(address) {
throw new TypeError(address + ' is too long'); throw new TypeError(address + ' is too long');
const version = payload.readUInt8(0); const version = payload.readUInt8(0);
const hash = payload.slice(1); const hash = payload.slice(1);
return { version: version, hash: hash }; return { version, hash };
} }
exports.fromBase58Check = fromBase58Check; exports.fromBase58Check = fromBase58Check;
function fromBech32(address) { function fromBech32(address) {
@ -44,7 +44,7 @@ function toBech32(data, version, prefix) {
} }
exports.toBech32 = toBech32; exports.toBech32 = toBech32;
function fromOutputScript(output, network) { function fromOutputScript(output, network) {
//TODO: Network // TODO: Network
network = network || networks.bitcoin; network = network || networks.bitcoin;
try { try {
return payments.p2pkh({ output, network }).address; return payments.p2pkh({ output, network }).address;
@ -67,8 +67,8 @@ function fromOutputScript(output, network) {
exports.fromOutputScript = fromOutputScript; exports.fromOutputScript = fromOutputScript;
function toOutputScript(address, network) { function toOutputScript(address, network) {
network = network || networks.bitcoin; network = network || networks.bitcoin;
let decodeBase58 = undefined; let decodeBase58;
let decodeBech32 = undefined; let decodeBech32;
try { try {
decodeBase58 = fromBase58Check(address); decodeBase58 = fromBase58Check(address);
} }

36
ts_src/address.ts

@ -1,23 +1,23 @@
import { Network } from './networks'; import { Network } from './networks';
import * as types from './types';
import * as bscript from './script';
import * as networks from './networks'; import * as networks from './networks';
import * as payments from './payments'; import * as payments from './payments';
import * as bscript from './script';
import * as types from './types';
const bech32 = require('bech32'); const bech32 = require('bech32');
const bs58check = require('bs58check'); const bs58check = require('bs58check');
const typeforce = require('typeforce'); const typeforce = require('typeforce');
export type Base58CheckResult = { export interface Base58CheckResult {
hash: Buffer; hash: Buffer;
version: number; version: number;
}; }
export type Bech32Result = { export interface Bech32Result {
version: number; version: number;
prefix: string; prefix: string;
data: Buffer; data: Buffer;
}; }
export function fromBase58Check(address: string): Base58CheckResult { export function fromBase58Check(address: string): Base58CheckResult {
const payload = bs58check.decode(address); const payload = bs58check.decode(address);
@ -29,7 +29,7 @@ export function fromBase58Check(address: string): Base58CheckResult {
const version = payload.readUInt8(0); const version = payload.readUInt8(0);
const hash = payload.slice(1); const hash = payload.slice(1);
return { version: version, hash: hash }; return { version, hash };
} }
export function fromBech32(address: string): Bech32Result { export function fromBech32(address: string): Bech32Result {
@ -65,20 +65,20 @@ export function toBech32(
} }
export function fromOutputScript(output: Buffer, network: Network): string { export function fromOutputScript(output: Buffer, network: Network): string {
//TODO: Network // TODO: Network
network = network || networks.bitcoin; network = network || networks.bitcoin;
try { try {
return <string>payments.p2pkh({ output, network }).address; return payments.p2pkh({ output, network }).address as string;
} catch (e) {} } catch (e) {}
try { try {
return <string>payments.p2sh({ output, network }).address; return payments.p2sh({ output, network }).address as string;
} catch (e) {} } catch (e) {}
try { try {
return <string>payments.p2wpkh({ output, network }).address; return payments.p2wpkh({ output, network }).address as string;
} catch (e) {} } catch (e) {}
try { try {
return <string>payments.p2wsh({ output, network }).address; return payments.p2wsh({ output, network }).address as string;
} catch (e) {} } catch (e) {}
throw new Error(bscript.toASM(output) + ' has no matching Address'); throw new Error(bscript.toASM(output) + ' has no matching Address');
@ -87,17 +87,17 @@ export function fromOutputScript(output: Buffer, network: Network): string {
export function toOutputScript(address: string, network: Network): Buffer { export function toOutputScript(address: string, network: Network): Buffer {
network = network || networks.bitcoin; network = network || networks.bitcoin;
let decodeBase58: Base58CheckResult | undefined = undefined; let decodeBase58: Base58CheckResult | undefined;
let decodeBech32: Bech32Result | undefined = undefined; let decodeBech32: Bech32Result | undefined;
try { try {
decodeBase58 = fromBase58Check(address); decodeBase58 = fromBase58Check(address);
} catch (e) {} } catch (e) {}
if (decodeBase58) { if (decodeBase58) {
if (decodeBase58.version === network.pubKeyHash) if (decodeBase58.version === network.pubKeyHash)
return <Buffer>payments.p2pkh({ hash: decodeBase58.hash }).output; return payments.p2pkh({ hash: decodeBase58.hash }).output as Buffer;
if (decodeBase58.version === network.scriptHash) if (decodeBase58.version === network.scriptHash)
return <Buffer>payments.p2sh({ hash: decodeBase58.hash }).output; return payments.p2sh({ hash: decodeBase58.hash }).output as Buffer;
} else { } else {
try { try {
decodeBech32 = fromBech32(address); decodeBech32 = fromBech32(address);
@ -108,9 +108,9 @@ export function toOutputScript(address: string, network: Network): Buffer {
throw new Error(address + ' has an invalid prefix'); throw new Error(address + ' has an invalid prefix');
if (decodeBech32.version === 0) { if (decodeBech32.version === 0) {
if (decodeBech32.data.length === 20) if (decodeBech32.data.length === 20)
return <Buffer>payments.p2wpkh({ hash: decodeBech32.data }).output; return payments.p2wpkh({ hash: decodeBech32.data }).output as Buffer;
if (decodeBech32.data.length === 32) if (decodeBech32.data.length === 32)
return <Buffer>payments.p2wsh({ hash: decodeBech32.data }).output; return payments.p2wsh({ hash: decodeBech32.data }).output as Buffer;
} }
} }
} }

8
types/address.d.ts

@ -1,14 +1,14 @@
/// <reference types="node" /> /// <reference types="node" />
import { Network } from './networks'; import { Network } from './networks';
export declare type Base58CheckResult = { export interface Base58CheckResult {
hash: Buffer; hash: Buffer;
version: number; version: number;
}; }
export declare type Bech32Result = { export interface Bech32Result {
version: number; version: number;
prefix: string; prefix: string;
data: Buffer; data: Buffer;
}; }
export declare function fromBase58Check(address: string): Base58CheckResult; export declare function fromBase58Check(address: string): Base58CheckResult;
export declare function fromBech32(address: string): Bech32Result; export declare function fromBech32(address: string): Bech32Result;
export declare function toBase58Check(hash: Buffer, version: number): string; export declare function toBase58Check(hash: Buffer, version: number): string;

Loading…
Cancel
Save