Skip to content

Commit c434422

Browse files
author
Inwon Kim
committed
Use type inference instead of specifying the return type on TransactionBuilders.
1 parent b47de8a commit c434422

10 files changed

+46
-77
lines changed

lib/Wallet.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import { addHxPrefix } from "./data/Hexadecimal";
1010
import { sign } from "./data/Util";
1111
import { isPrivateKey } from "./data/Validator";
1212
import { WalletError } from "./Exception";
13-
import { isString, isObject } from "./data/Type";
13+
import { isString } from "./data/Type";
1414

1515
const secp256k1 = require("secp256k1");
1616

1717
// eslint-disable-next-line no-unused-vars
1818
type Keccak256 = { toString(str: string): string };
1919

20-
interface KeyStore {
20+
export interface KeyStore {
2121
version: 3;
2222
id: string;
2323
address: string;
@@ -121,13 +121,14 @@ export default class Wallet {
121121
throw error.toString();
122122
}
123123

124-
const json: KeyStore = isObject(keystore)
125-
? keystore
126-
: JSON.parse(
127-
nonStrict
128-
? ((keystore as unknown) as string).toLowerCase()
129-
: (keystore as string)
130-
);
124+
const json: KeyStore =
125+
typeof keystore === "object"
126+
? keystore
127+
: JSON.parse(
128+
nonStrict
129+
? ((keystore as unknown) as string).toLowerCase()
130+
: (keystore as string)
131+
);
131132

132133
if (json.version !== 3) {
133134
const error = new WalletError("This is not a V3 wallet.");
@@ -280,10 +281,10 @@ export default class Wallet {
280281

281282
/**
282283
* Generate signature string by signing transaction object.
283-
* @param {Buffer} data - The serialized transaction object.
284+
* @param {string} data - The serialized transaction object.
284285
* @return {string} The signature string.
285286
*/
286-
sign(data: typeof Buffer): string {
287+
sign(data: string): string {
287288
const signature = sign(data, this.privKey);
288289
return Buffer.from(signature).toString("base64");
289290
}

lib/builder/transaction/CallTransaction.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export class CallTransactionBuilder extends IcxTransactionBuilder {
8181
* @param {string} method - The method name of SCORE API
8282
* @return {CallTransactionBuilder} this.
8383
*/
84-
method(method: string): CallTransactionBuilder {
84+
method(method: string) {
8585
this.private(this).method = method;
8686

8787
return this;
@@ -92,7 +92,7 @@ export class CallTransactionBuilder extends IcxTransactionBuilder {
9292
* @param {object} params - The input params for method
9393
* @return {CallTransactionBuilder} this.
9494
*/
95-
params(params: any): CallTransactionBuilder {
95+
params(params: any) {
9696
this.private(this).params = params;
9797

9898
return this;
@@ -102,7 +102,7 @@ export class CallTransactionBuilder extends IcxTransactionBuilder {
102102
* Build 'CallTransaction' object
103103
* @return {CallTransaction} 'CallTransaction' instance exported by 'CallTransactionBuilder'.
104104
*/
105-
build(): CallTransaction {
105+
build() {
106106
return new CallTransaction(
107107
this.private(this).to,
108108
this.private(this).from,

lib/builder/transaction/DeployTransaction.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export default class DeployTransactionBuilder extends IcxTransactionBuilder {
7979
* @param {string} contentType - The content type of content
8080
* @return {DeployTransactionBuilder} this.
8181
*/
82-
contentType(contentType: string): DeployTransactionBuilder {
82+
contentType(contentType: string) {
8383
this.private(this).contentType = contentType;
8484

8585
return this;
@@ -90,7 +90,7 @@ export default class DeployTransactionBuilder extends IcxTransactionBuilder {
9090
* @param {string} content - The content to deploy.
9191
* @return {DeployTransactionBuilder} this.
9292
*/
93-
content(content: string): DeployTransactionBuilder {
93+
content(content: string) {
9494
this.private(this).content = content;
9595

9696
return this;
@@ -101,7 +101,7 @@ export default class DeployTransactionBuilder extends IcxTransactionBuilder {
101101
* @param {object} params - The input params for deploying content
102102
* @return {DeployTransactionBuilder} this.
103103
*/
104-
params(params: any): DeployTransactionBuilder {
104+
params(params: any) {
105105
this.private(this).params = params;
106106

107107
return this;
@@ -111,7 +111,7 @@ export default class DeployTransactionBuilder extends IcxTransactionBuilder {
111111
* Build 'DeployTransaction' object
112112
* @return {DeployTransaction} 'DeployTransaction' instance exported by 'DeployTransactionBuilder'
113113
*/
114-
build(): DeployTransaction {
114+
build() {
115115
return new DeployTransaction(
116116
this.private(this).to,
117117
this.private(this).from,

lib/builder/transaction/DepositTransaction.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export default class DepositTransactionBuilder extends IcxTransactionBuilder {
8282
* @param {string} action - add | withdraw
8383
* @return {DepositTransactionBuilder} this.
8484
*/
85-
action(action: string): DepositTransactionBuilder {
85+
action(action: string) {
8686
this.private(this).action = action;
8787

8888
return this;
@@ -93,7 +93,7 @@ export default class DepositTransactionBuilder extends IcxTransactionBuilder {
9393
* @param {string} id - Identifier of deposit to withdraw
9494
* @return {DepositTransactionBuilder} this.
9595
*/
96-
id(id: string): DepositTransactionBuilder {
96+
id(id: string) {
9797
this.private(this).id = id;
9898

9999
return this;
@@ -104,7 +104,7 @@ export default class DepositTransactionBuilder extends IcxTransactionBuilder {
104104
* @param {string} amount - amount of deposit to withdraw
105105
* @return {DepositTransactionBuilder} this.
106106
*/
107-
amount(amount: Hash): DepositTransactionBuilder {
107+
amount(amount: Hash) {
108108
this.private(this).amount = amount;
109109
return this;
110110
}
@@ -114,7 +114,7 @@ export default class DepositTransactionBuilder extends IcxTransactionBuilder {
114114
* @return {DepositTransaction} 'DepositTransaction'
115115
* instance exported by 'DepositTransactionBuilder'
116116
*/
117-
build(): DepositTransaction {
117+
build() {
118118
return new DepositTransaction(
119119
this.private(this).to,
120120
this.private(this).from,

lib/builder/transaction/IcxTransaction.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class IcxTransactionBuilder {
7575
* @param {string} to - The EOA or SCORE address.
7676
* @return {IcxTransactionBuilder} this.
7777
*/
78-
to(to: string): IcxTransactionBuilder {
78+
to(to: string) {
7979
this.private(this).to = to;
8080

8181
return this;
@@ -86,7 +86,7 @@ export class IcxTransactionBuilder {
8686
* @param {string} from - The EOA address.
8787
* @return {IcxTransactionBuilder} this.
8888
*/
89-
from(from: string): IcxTransactionBuilder {
89+
from(from: string) {
9090
this.private(this).from = from;
9191

9292
return this;
@@ -97,7 +97,7 @@ export class IcxTransactionBuilder {
9797
* @param {string|BigNumber|number} value - The sending amount of ICX in loop unit.
9898
* @return {IcxTransactionBuilder} this.
9999
*/
100-
value(value: Hash): IcxTransactionBuilder {
100+
value(value: Hash) {
101101
this.private(this).value = value;
102102

103103
return this;
@@ -108,7 +108,7 @@ export class IcxTransactionBuilder {
108108
* @param {string|BigNumber|number} stepLimit - The step limit.
109109
* @return {IcxTransactionBuilder} this.
110110
*/
111-
stepLimit(stepLimit: Hash): IcxTransactionBuilder {
111+
stepLimit(stepLimit: Hash) {
112112
this.private(this).stepLimit = stepLimit;
113113

114114
return this;
@@ -119,7 +119,7 @@ export class IcxTransactionBuilder {
119119
* @param {string|BigNumber|number} nid - The nid (network ID)
120120
* @return {IcxTransactionBuilder} this.
121121
*/
122-
nid(nid: Hash): IcxTransactionBuilder {
122+
nid(nid: Hash) {
123123
this.private(this).nid = nid;
124124

125125
return this;
@@ -130,7 +130,7 @@ export class IcxTransactionBuilder {
130130
* @param {string|BigNumber|number} nonce - The nonce.
131131
* @return {IcxTransactionBuilder} this.
132132
*/
133-
nonce(nonce: Hash): IcxTransactionBuilder {
133+
nonce(nonce: Hash) {
134134
this.private(this).nonce = nonce;
135135

136136
return this;
@@ -141,7 +141,7 @@ export class IcxTransactionBuilder {
141141
* @param {string|BigNumber|number} version - The network version.
142142
* @return {IcxTransactionBuilder} this.
143143
*/
144-
version(version: Hash): IcxTransactionBuilder {
144+
version(version: Hash) {
145145
this.private(this).version = version;
146146

147147
return this;
@@ -152,13 +152,13 @@ export class IcxTransactionBuilder {
152152
* @param {string|BigNumber|number} timestamp - The timestamp.
153153
* @return {IcxTransactionBuilder} this.
154154
*/
155-
timestamp(timestamp: Hash): IcxTransactionBuilder {
155+
timestamp(timestamp: Hash) {
156156
this.private(this).timestamp = timestamp;
157157

158158
return this;
159159
}
160160

161-
build(): IcxTransaction {
161+
build() {
162162
return new IcxTransaction(
163163
this.private(this).to,
164164
this.private(this).from,

lib/builder/transaction/MessageTransaction.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export default class MessageTransactionBuilder extends IcxTransactionBuilder {
7676
* @param {string} data - The data to send.
7777
* @return {MessageTransactionBuilder} this.
7878
*/
79-
data(data: string): MessageTransactionBuilder {
79+
data(data: string) {
8080
this.private(this).data = data;
8181

8282
return this;
@@ -86,7 +86,7 @@ export default class MessageTransactionBuilder extends IcxTransactionBuilder {
8686
* Build 'MessageTransaction' object
8787
* @return {MessageTransaction} 'MessageTransaction' instance
8888
*/
89-
build(): MessageTransaction {
89+
build() {
9090
return new MessageTransaction(
9191
this.private(this).to,
9292
this.private(this).from,

lib/data/Converter.ts

+1-13
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ import { add0xPrefix } from "./Hexadecimal";
2020
import { isString, isBigNumber, isHex, isInteger } from "./Type";
2121
import { DataError } from "../Exception";
2222
import { Hash } from "../types/hash";
23-
import { IcxTransaction } from "../builder/transaction/IcxTransaction";
24-
import { MessageTransaction } from "../builder/transaction/MessageTransaction";
25-
import { CallTransaction } from "../builder/transaction/CallTransaction";
26-
import { DeployTransaction } from "../builder/transaction/DeployTransaction";
27-
import { DepositTransaction } from "../builder/transaction/DepositTransaction";
2823

2924
/**
3025
* Convert UTF-8 text to hex string.
@@ -168,14 +163,7 @@ export function toHex(value: Hash): string {
168163
* @param {object} transaction - the transaction object.
169164
* @return {object} the raw transaction object.
170165
*/
171-
export function toRawTransaction(
172-
transaction:
173-
| IcxTransaction
174-
| MessageTransaction
175-
| CallTransaction
176-
| DeployTransaction
177-
| DepositTransaction
178-
) {
166+
export function toRawTransaction(transaction) {
179167
const {
180168
to,
181169
from,

lib/data/Type.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616

1717
import BigNumber from "bignumber.js";
18-
import { Hash } from "../types/hash";
1918

2019
export function isString(value) {
2120
return typeof value === "string" || value instanceof String;
@@ -25,21 +24,15 @@ export function isByte(value) {
2524
return Boolean(value) && value.byteLength !== undefined;
2625
}
2726

28-
export function isObject(obj) {
29-
return typeof obj === "object";
30-
}
31-
3227
export function isArray(obj) {
3328
return Array.isArray(obj);
3429
}
3530

36-
export function isBigNumber(
37-
value: string | number | BigNumber
38-
): value is BigNumber {
31+
export function isBigNumber(value): value is BigNumber {
3932
return BigNumber.isBigNumber(value);
4033
}
4134

42-
export function isHex(value: Hash): boolean {
35+
export function isHex(value: string): boolean {
4336
return /^(0x)[0-9a-f]+$/g.test(<string>value);
4437
}
4538

lib/data/Util.ts

+7-16
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/* eslint-disable no-global-assign */
1919

2020
import { sha3_256 as sha3256 } from "js-sha3";
21-
import { isObject, isArray, isString } from "./Type";
21+
import { isArray, isString } from "./Type";
2222

2323
const secp256k1 = require("secp256k1");
2424

@@ -42,7 +42,7 @@ export function isGenesisBlock(height: number): boolean {
4242
}
4343

4444
export function hasProperties(obj, propertySet): boolean {
45-
if (!isObject(obj)) {
45+
if (typeof obj !== "object") {
4646
return false;
4747
}
4848

@@ -64,15 +64,13 @@ export function hasProperties(obj, propertySet): boolean {
6464

6565
export function createPrivate() {
6666
const weakMap = new WeakMap();
67-
const internal = (key) => {
67+
return (key) => {
6868
if (!weakMap.has(key)) {
6969
weakMap.set(key, {});
7070
}
7171

7272
return weakMap.get(key);
7373
};
74-
75-
return internal;
7674
}
7775

7876
export function makeTxHash(rawTrasaction) {
@@ -83,18 +81,13 @@ export function makeTxHash(rawTrasaction) {
8381

8482
export function serialize(trasaction) {
8583
const phraseToSign = generateHashKey(trasaction);
86-
const hashcode = sha3256.update(phraseToSign).hex();
87-
88-
return hashcode;
84+
return sha3256.update(phraseToSign).hex();
8985
}
9086

9187
export function generateHashKey(obj) {
92-
let resultStrReplaced = "";
9388
const resultStr = objTraverse(obj);
94-
resultStrReplaced = resultStr.substring(1).slice(0, -1);
95-
const result = `icx_sendTransaction.${resultStrReplaced}`;
96-
97-
return result;
89+
let resultStrReplaced: string = resultStr.substring(1).slice(0, -1);
90+
return `icx_sendTransaction.${resultStrReplaced}`;
9891
}
9992

10093
export function arrTraverse(arr) {
@@ -198,7 +191,5 @@ export function sign(data, privKey) {
198191
const signing = (secp256k1 as any).sign(Buffer.from(data, "hex"), privKey);
199192
const recovery = new Uint8Array(1);
200193
recovery[0] = signing.recovery;
201-
const signature = concatTypedArrays(signing.signature, recovery);
202-
203-
return signature;
194+
return concatTypedArrays(signing.signature, recovery);
204195
}

lib/data/Validator.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,7 @@ export function isScoreAddress(address) {
6464
* @return {boolean} returns true if the input value is a EOA or SCORE address.
6565
*/
6666
export function isAddress(address) {
67-
if (isEoaAddress(address) || isScoreAddress(address)) {
68-
return true;
69-
}
70-
71-
return false;
67+
return isEoaAddress(address) || isScoreAddress(address);
7268
}
7369

7470
/**
@@ -94,7 +90,7 @@ export function hasBlank(value) {
9490
* @param {any} value - the input value.
9591
* @return {boolean} returns true if the input value is a block number.
9692
*/
97-
export function isNonNegative(value: string | BigNumber): boolean {
93+
export function isNonNegative(value: string | BigNumber | number): boolean {
9894
try {
9995
if (hasUppercase(value.toString()) || hasBlank(value)) {
10096
return false;

0 commit comments

Comments
 (0)