@klaytn/ethers-ext
Advanced tools
Comparing version 0.9.3-beta to 0.9.4-beta
@@ -19,4 +19,4 @@ import { TransactionRequest } from "@ethersproject/abstract-provider"; | ||
export declare function objectFromRLP(value: string): any; | ||
export declare function encodeTxForRPC(allowedKeys: string[], tx: TransactionRequest): any; | ||
export declare function encodeTxForRPC(tx: TransactionRequest): any; | ||
export {}; | ||
//# sourceMappingURL=klaytn_tx.d.ts.map |
@@ -9,2 +9,3 @@ "use strict"; | ||
const utils_1 = require("ethers/lib/utils"); | ||
const transactions_1 = require("@ethersproject/transactions"); | ||
const lodash_1 = __importDefault(require("lodash")); | ||
@@ -93,28 +94,33 @@ const field_1 = require("./field"); | ||
exports.objectFromRLP = objectFromRLP; | ||
function encodeTxForRPC(allowedKeys, tx) { | ||
// TODO: refactoring like below | ||
// https://github.com/ethers-io/ethers.js/blob/master/packages/providers/src.ts/json-rpc-provider.ts#L701 | ||
// return { | ||
// from: hexlify(tx.from), | ||
// gas: tx.gasLimit? fromnumber(tx.gasLimit) : null; | ||
// }; | ||
const ttx = {}; | ||
for (const key in tx) { | ||
if (allowedKeys.indexOf(key) != -1) { | ||
let value = lodash_1.default.get(tx, key); | ||
if (value == 0 || value === "0x0000000000000000000000000000000000000000") { | ||
value = "0x"; | ||
} | ||
else if (typeof (value) == "number" || value instanceof ethers_1.BigNumber) { | ||
// https://github.com/ethers-io/ethers.js/blob/master/packages/providers/src.ts/json-rpc-provider.ts#L701 | ||
ttx[key] = (0, utils_1.hexValue)(value); | ||
} | ||
else { | ||
ttx[key] = value; | ||
} | ||
function encodeTxForRPC(tx) { | ||
const formatted = {}; | ||
const numericFields = ["chainId", "gasLimit", "gasPrice", "type", "maxFeePerGas", "maxPriorityFeePerGas", "nonce", "value"]; | ||
lodash_1.default.each(numericFields, (key) => { | ||
if (!lodash_1.default.has(tx, key)) { | ||
return; | ||
} | ||
let value = tx[key]; | ||
value = (0, utils_1.hexValue)(ethers_1.BigNumber.from(value)); | ||
if (key == "gasLimit") { | ||
formatted["gas"] = value; | ||
} | ||
else { | ||
formatted[key] = value; | ||
} | ||
}); | ||
const bytestrFields = ["from", "to", "data", "input"]; | ||
lodash_1.default.each(bytestrFields, (key) => { | ||
if (!lodash_1.default.has(tx, key)) { | ||
return; | ||
} | ||
let value = tx[key]; | ||
value = util_1.HexStr.from(value); | ||
formatted[key] = value; | ||
}); | ||
if (tx.accessList) { | ||
formatted["accessList"] = (0, transactions_1.accessListify)(tx.accessList); | ||
} | ||
return ttx; | ||
return formatted; | ||
} | ||
exports.encodeTxForRPC = encodeTxForRPC; | ||
//# sourceMappingURL=klaytn_tx.js.map |
@@ -11,2 +11,3 @@ import { Provider, TransactionRequest, TransactionResponse } from "@ethersproject/abstract-provider"; | ||
checkTransaction(transaction: Deferrable<TransactionRequest>): Deferrable<TransactionRequest>; | ||
_convertTxFromRLP(transaction: Deferrable<TransactionRequest> | string): any; | ||
populateTransaction(transaction: Deferrable<TransactionRequest>): Promise<TransactionRequest>; | ||
@@ -13,0 +14,0 @@ decodeTxFromRLP(str: string): any; |
@@ -90,4 +90,18 @@ "use strict"; | ||
} | ||
_convertTxFromRLP(transaction) { | ||
if (typeof transaction === "string") { | ||
if (util_1.HexStr.isHex(transaction)) { | ||
return this.decodeTxFromRLP(transaction); | ||
} | ||
else { | ||
throw new Error("String type input has to be RLP encoded Hex string."); | ||
} | ||
} | ||
else { | ||
return transaction; | ||
} | ||
} | ||
async populateTransaction(transaction) { | ||
let tx = await (0, utils_1.resolveProperties)(transaction); | ||
let tx = this._convertTxFromRLP(transaction); | ||
tx = await (0, utils_1.resolveProperties)(tx); | ||
if (!core_1.KlaytnTxFactory.has(tx.type)) { | ||
@@ -117,6 +131,3 @@ return super.populateTransaction(tx); | ||
if (this.provider instanceof providers_1.JsonRpcProvider) { | ||
const estimateGasAllowedKeys = [ | ||
"from", "to", "gasLimit", "gasPrice", "value", "input" | ||
]; | ||
const ttx = (0, klaytn_tx_1.encodeTxForRPC)(estimateGasAllowedKeys, tx); | ||
const ttx = (0, klaytn_tx_1.encodeTxForRPC)(tx); | ||
const result = await this.provider.send("klay_estimateGas", [ttx]); | ||
@@ -153,3 +164,4 @@ // For the problem that estimateGas does not exactly match, | ||
async signTransaction(transaction) { | ||
const tx = await (0, utils_1.resolveProperties)(transaction); | ||
let tx = this._convertTxFromRLP(transaction); | ||
tx = await (0, utils_1.resolveProperties)(tx); | ||
if (!core_1.KlaytnTxFactory.has(tx.type)) { | ||
@@ -171,4 +183,15 @@ return super.signTransaction(tx); | ||
async signTransactionAsFeePayer(transaction) { | ||
const tx = await (0, utils_1.resolveProperties)(transaction); | ||
const ttx = core_1.KlaytnTxFactory.fromObject(tx); | ||
let tx = this._convertTxFromRLP(transaction); | ||
// @ts-ignore : chainId can be omitted from RLP encoded format | ||
if (!tx.chainId) { | ||
// @ts-ignore | ||
tx.chainId = this.getChainId(); | ||
} | ||
const rtx = await (0, utils_1.resolveProperties)(tx); | ||
// @ts-ignore : we have to add feePayer property | ||
if (!rtx.feePayer) { | ||
// @ts-ignore : we have to add feePayer property | ||
rtx.feePayer = await this.getAddress(); | ||
} | ||
const ttx = core_1.KlaytnTxFactory.fromObject(rtx); | ||
if (!ttx.hasFeePayer()) { | ||
@@ -179,3 +202,5 @@ throw new Error("This transaction can not be signed as FeePayer"); | ||
const sig = this._signingKey().signDigest(sigFeePayerHash); | ||
// @ts-ignore : we have to add feePayer property | ||
if (tx.chainId) { // EIP-155 | ||
// @ts-ignore : we have to add feePayer property | ||
sig.v = sig.recoveryParam + tx.chainId * 2 + 35; | ||
@@ -188,5 +213,6 @@ } | ||
this._checkProvider("sendTransaction"); | ||
const tx = await this.populateTransaction(transaction); | ||
const signedTx = await this.signTransaction(tx); | ||
if (!core_1.KlaytnTxFactory.has(tx.type)) { | ||
let tx = this._convertTxFromRLP(transaction); | ||
let ptx = await this.populateTransaction(tx); | ||
const signedTx = await this.signTransaction(ptx); | ||
if (!core_1.KlaytnTxFactory.has(ptx.type)) { | ||
return await this.provider.sendTransaction(signedTx); | ||
@@ -205,15 +231,4 @@ } | ||
this._checkProvider("sendTransactionAsFeePayer"); | ||
let tx, ptx; | ||
if (typeof transaction === "string") { | ||
if (util_1.HexStr.isHex(transaction)) { | ||
tx = this.decodeTxFromRLP(transaction); | ||
ptx = await this.populateTransaction(tx); | ||
} | ||
else { | ||
throw new Error("Input parameter has to be RLP encoded Hex string."); | ||
} | ||
} | ||
else { | ||
ptx = await this.populateTransaction(transaction); | ||
} | ||
let tx = this._convertTxFromRLP(transaction); | ||
let ptx = await this.populateTransaction(tx); | ||
// @ts-ignore : we have to add feePayer property | ||
@@ -220,0 +235,0 @@ ptx.feePayer = await this.getAddress(); |
@@ -5,2 +5,3 @@ "use strict"; | ||
const core_1 = require("../../src/core"); | ||
const klaytn_tx_1 = require("../../src/core/klaytn_tx"); | ||
// Non-canonical types, which are common user-supplied values. | ||
@@ -63,2 +64,33 @@ const nonce = 1234; | ||
}); | ||
describe("encodeTxForRPC", () => { | ||
it("success", () => { | ||
let tx = { | ||
chainId: 42, | ||
gasLimit: 0x1111, | ||
gasPrice: 0x222, | ||
type: 2, | ||
maxFeePerGas: 0x33, | ||
maxPriorityFeePerGas: 0x4, | ||
nonce: 0, | ||
value: 0, | ||
from: "0x00000000000000000000000000000000000000aa", | ||
to: "0x00000000000000000000000000000000000000bb", | ||
data: "0x", | ||
}; | ||
let formatted = (0, klaytn_tx_1.encodeTxForRPC)(tx); | ||
chai_1.assert.deepEqual(formatted, { | ||
chainId: '0x2a', | ||
gas: '0x1111', | ||
gasPrice: '0x222', | ||
type: '0x2', | ||
maxFeePerGas: '0x33', | ||
maxPriorityFeePerGas: '0x4', | ||
nonce: '0x0', | ||
value: '0x0', | ||
from: '0x00000000000000000000000000000000000000aa', | ||
to: '0x00000000000000000000000000000000000000bb', | ||
data: '0x', | ||
}); | ||
}); | ||
}); | ||
//# sourceMappingURL=klaytn_tx.spec.js.map |
{ | ||
"name": "@klaytn/ethers-ext", | ||
"version": "0.9.3-beta", | ||
"version": "0.9.4-beta", | ||
"main": "dist/src/index.js", | ||
@@ -5,0 +5,0 @@ "files": [ |
import { TransactionRequest } from "@ethersproject/abstract-provider"; | ||
import { BigNumber } from "ethers"; | ||
import { hexValue, parseTransaction } from "ethers/lib/utils"; | ||
import { accessListify } from "@ethersproject/transactions"; | ||
import _ from "lodash"; | ||
@@ -111,26 +112,37 @@ | ||
export function encodeTxForRPC(allowedKeys:string[], tx: TransactionRequest): any { | ||
// TODO: refactoring like below | ||
// https://github.com/ethers-io/ethers.js/blob/master/packages/providers/src.ts/json-rpc-provider.ts#L701 | ||
// return { | ||
// from: hexlify(tx.from), | ||
// gas: tx.gasLimit? fromnumber(tx.gasLimit) : null; | ||
// }; | ||
export function encodeTxForRPC(tx: TransactionRequest): any { | ||
const formatted: any = {}; | ||
const ttx: any = {}; | ||
for (const key in tx) { | ||
if (allowedKeys.indexOf(key) != -1) { | ||
let value = _.get(tx, key); | ||
const numericFields = ["chainId", "gasLimit", "gasPrice", "type", "maxFeePerGas", "maxPriorityFeePerGas", "nonce", "value"]; | ||
_.each(numericFields, (key) => { | ||
if (!_.has(tx, key)) { | ||
return; | ||
} | ||
if (value == 0 || value === "0x0000000000000000000000000000000000000000") { | ||
value = "0x"; | ||
} else if (typeof(value) == "number" || value instanceof BigNumber) { | ||
// https://github.com/ethers-io/ethers.js/blob/master/packages/providers/src.ts/json-rpc-provider.ts#L701 | ||
ttx[key] = hexValue(value); | ||
} else { | ||
ttx[key] = value; | ||
} | ||
let value = (<any>tx)[key]; | ||
value = hexValue(BigNumber.from(value)); | ||
if (key == "gasLimit") { | ||
formatted["gas"] = value; | ||
} else { | ||
formatted[key] = value; | ||
} | ||
}); | ||
const bytestrFields = ["from", "to", "data", "input"] | ||
_.each(bytestrFields, (key) => { | ||
if (!_.has(tx, key)) { | ||
return; | ||
} | ||
let value = (<any>tx)[key]; | ||
value = HexStr.from(value); | ||
formatted[key] = value; | ||
}); | ||
if (tx.accessList) { | ||
formatted["accessList"] = accessListify(tx.accessList); | ||
} | ||
return ttx; | ||
return formatted; | ||
} |
@@ -105,5 +105,18 @@ import { Provider, TransactionRequest, TransactionResponse } from "@ethersproject/abstract-provider"; | ||
_convertTxFromRLP(transaction: Deferrable<TransactionRequest> | string): any { | ||
if (typeof transaction === "string") { | ||
if (HexStr.isHex(transaction)) { | ||
return this.decodeTxFromRLP(transaction); | ||
} else { | ||
throw new Error("String type input has to be RLP encoded Hex string."); | ||
} | ||
} else { | ||
return transaction; | ||
} | ||
} | ||
async populateTransaction(transaction: Deferrable<TransactionRequest>): Promise<TransactionRequest> { | ||
let tx: TransactionRequest = await resolveProperties(transaction); | ||
let tx: TransactionRequest = this._convertTxFromRLP(transaction); | ||
tx = await resolveProperties(tx); | ||
if (!KlaytnTxFactory.has(tx.type)) { | ||
@@ -134,5 +147,3 @@ return super.populateTransaction(tx); | ||
if (this.provider instanceof EthersJsonRpcProvider) { | ||
const estimateGasAllowedKeys: string[] = [ | ||
"from", "to", "gasLimit", "gasPrice", "value", "input"]; | ||
const ttx = encodeTxForRPC(estimateGasAllowedKeys, tx); | ||
const ttx = encodeTxForRPC(tx); | ||
@@ -177,4 +188,5 @@ const result = await this.provider.send("klay_estimateGas", [ttx]); | ||
async signTransaction(transaction: Deferrable<TransactionRequest>): Promise<string> { | ||
const tx: TransactionRequest = await resolveProperties(transaction); | ||
let tx: TransactionRequest = this._convertTxFromRLP(transaction); | ||
tx = await resolveProperties(tx); | ||
if (!KlaytnTxFactory.has(tx.type)) { | ||
@@ -200,5 +212,18 @@ return super.signTransaction(tx); | ||
async signTransactionAsFeePayer(transaction: Deferrable<TransactionRequest>): Promise<string> { | ||
const tx: TransactionRequest = await resolveProperties(transaction); | ||
let tx: TransactionRequest = this._convertTxFromRLP(transaction); | ||
// @ts-ignore : chainId can be omitted from RLP encoded format | ||
if (!tx.chainId) { | ||
// @ts-ignore | ||
tx.chainId = this.getChainId(); | ||
} | ||
const rtx: TransactionRequest = await resolveProperties(tx); | ||
const ttx = KlaytnTxFactory.fromObject(tx); | ||
// @ts-ignore : we have to add feePayer property | ||
if (!rtx.feePayer) { | ||
// @ts-ignore : we have to add feePayer property | ||
rtx.feePayer = await this.getAddress(); | ||
} | ||
const ttx = KlaytnTxFactory.fromObject(rtx); | ||
if (!ttx.hasFeePayer()) { | ||
@@ -211,3 +236,5 @@ throw new Error("This transaction can not be signed as FeePayer"); | ||
// @ts-ignore : we have to add feePayer property | ||
if (tx.chainId) { // EIP-155 | ||
// @ts-ignore : we have to add feePayer property | ||
sig.v = sig.recoveryParam + tx.chainId * 2 + 35; | ||
@@ -222,6 +249,8 @@ } | ||
this._checkProvider("sendTransaction"); | ||
const tx = await this.populateTransaction(transaction); | ||
const signedTx = await this.signTransaction(tx); | ||
if (!KlaytnTxFactory.has(tx.type)) { | ||
let tx: TransactionRequest = this._convertTxFromRLP(transaction); | ||
let ptx = await this.populateTransaction(tx); | ||
const signedTx = await this.signTransaction(ptx); | ||
if (!KlaytnTxFactory.has(ptx.type)) { | ||
return await this.provider.sendTransaction(signedTx); | ||
@@ -242,13 +271,4 @@ } | ||
let tx, ptx; | ||
if (typeof transaction === "string") { | ||
if (HexStr.isHex(transaction)) { | ||
tx = this.decodeTxFromRLP(transaction); | ||
ptx = await this.populateTransaction(tx); | ||
} else { | ||
throw new Error("Input parameter has to be RLP encoded Hex string."); | ||
} | ||
} else { | ||
ptx = await this.populateTransaction(transaction); | ||
} | ||
let tx: TransactionRequest = this._convertTxFromRLP(transaction); | ||
let ptx = await this.populateTransaction(tx); | ||
@@ -255,0 +275,0 @@ // @ts-ignore : we have to add feePayer property |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
425333
7191