Skip to content

Commit

Permalink
Fixed transaction signature, use native autogas
Browse files Browse the repository at this point in the history
  • Loading branch information
demon-xxi committed Mar 23, 2023
1 parent 01289e3 commit 1c82a4b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 230 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@conduitxyz/hardhat-gcp-kms-signer",
"version": "1.1.5",
"version": "1.1.6",
"description": "Sign Hardhat deployment transactions using KMS key",
"repository": "github:conduitxyz/hardhat-gcp-kms-signer",
"author": "Conduit XYZ",
Expand Down
202 changes: 0 additions & 202 deletions src/gasProvider.ts

This file was deleted.

28 changes: 14 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { extendConfig, extendEnvironment } from "hardhat/config";
import { BackwardsCompatibilityProviderAdapter } from "hardhat/internal/core/providers/backwards-compatibility";
import { AutomaticGasProvider } from "hardhat/internal/core/providers/gas-providers";
import { AutomaticGasPriceProvider, AutomaticGasProvider, GanacheGasMultiplierProvider } from "hardhat/internal/core/providers/gas-providers";
import { HttpProvider } from "hardhat/internal/core/providers/http";
import {
EIP1193Provider,
Expand All @@ -9,7 +9,6 @@ import {
HttpNetworkUserConfig,
} from "hardhat/types";

import { AutomaticGasPriceProvider } from "./gasProvider";
import { KMSSigner } from "./provider";
import "./type-extensions";

Expand Down Expand Up @@ -40,22 +39,23 @@ extendEnvironment((hre) => {
httpNetConfig.httpHeaders,
httpNetConfig.timeout
);
let wrappedProvider: EIP1193Provider;
let wrappedProvider: EIP1193Provider = eip1193Provider;

wrappedProvider = new KMSSigner(
eip1193Provider,
hre.network.config.gcpKmsKeyName
);
if (hre.network.config.minMaxFeePerGas || hre.network.config.minMaxPriorityFeePerGas) {
wrappedProvider = new AutomaticGasProvider(
wrappedProvider,
hre.network.config.gasMultiplier
);
wrappedProvider = new AutomaticGasPriceProvider(
wrappedProvider,
hre.network.config.minMaxFeePerGas,
hre.network.config.minMaxPriorityFeePerGas
);
}

wrappedProvider = new AutomaticGasProvider(
wrappedProvider,
hre.network.config.gasMultiplier
);

wrappedProvider = new AutomaticGasPriceProvider(
wrappedProvider,
);
wrappedProvider = new GanacheGasMultiplierProvider(wrappedProvider);

hre.network.provider = new BackwardsCompatibilityProviderAdapter(
wrappedProvider
);
Expand Down
21 changes: 8 additions & 13 deletions src/provider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GcpKmsSigner, GcpKmsSignerCredentials} from "ethers-gcp-kms-signer";
import { GcpKmsSigner, GcpKmsSignerCredentials } from "ethers-gcp-kms-signer";

import { BigNumber, utils } from "ethers";
import { keccak256 } from "ethers/lib/utils";
Expand All @@ -13,7 +13,7 @@ import { assert } from "console";
export class KMSSigner extends ProviderWrapperWithChainId {
public kmsSigner: GcpKmsSigner;
public kmsCredentials: GcpKmsSignerCredentials;


constructor(provider: EIP1193Provider, gcpKmsKeyName: string) {
super(provider);
Expand All @@ -24,12 +24,13 @@ export class KMSSigner extends ProviderWrapperWithChainId {
public async request(args: RequestArguments): Promise<unknown> {
const method = args.method;
const params = this._getParams(args);
const sender = await this._getSender();
const sender = await this._getSender()

if (method === "eth_sendTransaction") {
const [txRequest] = validateParams(params, rpcTransactionRequest);
const tx = await utils.resolveProperties(txRequest);
const nonce = tx.nonce ?? (await this._getNonce(sender));
const baseTx: utils.UnsignedTransaction = {
const baseTx = {
chainId: (await this._getChainId()) || undefined,
data: tx.data,
gasLimit: tx.gas?.toString(),
Expand All @@ -51,13 +52,7 @@ export class KMSSigner extends ProviderWrapperWithChainId {
delete baseTx.maxPriorityFeePerGas;
}

console.log("TX", baseTx);

const unsignedTx = utils.serializeTransaction(baseTx);
const hash = keccak256(utils.arrayify(unsignedTx));
const sig = await this.kmsSigner.signMessage(hash);

const rawTx = utils.serializeTransaction(baseTx, sig);
const rawTx = await this.kmsSigner.signTransaction(baseTx);

return this._wrappedProvider.request({
method: "eth_sendRawTransaction",
Expand Down Expand Up @@ -87,10 +82,10 @@ export class KMSSigner extends ProviderWrapperWithChainId {
}
}

function parseKmsKey(gcpKmsKeyName: string): GcpKmsSignerCredentials {
function parseKmsKey(gcpKmsKeyName: string): GcpKmsSignerCredentials {
let parts = gcpKmsKeyName.split("/");
assert(gcpKmsKeyName, "gcpKmsKeyName is missing.")
assert(parts.length === 10, `Incorrect gcp kms key format: ${ gcpKmsKeyName }. ` +
assert(parts.length === 10, `Incorrect gcp kms key format: ${gcpKmsKeyName}. ` +
`Expected: projects/<projectId>/locations/<locationId>/keyRings/<keyRingId>/cryptoKeys/<keyId>/cryptoKeyVersions/<keyVersion>`);
return {
projectId: parts[1], // your project id in gcp
Expand Down

0 comments on commit 1c82a4b

Please sign in to comment.