-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
amiecorso
committed
Oct 3, 2023
1 parent
1378d7a
commit 3ccc240
Showing
9 changed files
with
358 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* eslint-disable @typescript-eslint/dot-notation -- frequent FireblocksWeb3Provider private member access */ | ||
import type { | ||
Signer, | ||
TypedDataDomain, | ||
TypedDataField, | ||
} from '@ethersproject/abstract-signer'; | ||
import type { | ||
TransactionRequest, | ||
TransactionResponse, | ||
} from '@ethersproject/providers'; | ||
import { _TypedDataEncoder, toUtf8Bytes } from 'ethers/lib/utils'; | ||
import type { Deferrable } from '@ethersproject/properties'; | ||
import type { Bytes } from '@ethersproject/bytes'; | ||
import type { BigNumber } from 'ethers'; | ||
import { ethers } from 'ethers'; | ||
import { FireblocksSigner as HardhatFireblocksSigner } from '@fireblocks/hardhat-fireblocks/dist/src/provider'; | ||
import type { | ||
FireblocksProviderConfig, | ||
FireblocksWeb3Provider, | ||
} from '@fireblocks/fireblocks-web3-provider'; | ||
import type { EIP1193Provider } from 'hardhat/types/provider'; | ||
|
||
export class FireblocksSigner | ||
extends HardhatFireblocksSigner | ||
implements Signer | ||
{ | ||
_isSigner = true; | ||
|
||
provider?: ethers.providers.Provider | undefined; | ||
|
||
private _jsonRpcSigner: ethers.providers.JsonRpcSigner; | ||
|
||
private _ethersWeb3Provider: ethers.providers.Web3Provider; | ||
|
||
private _defaultNote: string | undefined; | ||
|
||
constructor( | ||
provider: EIP1193Provider, | ||
fireblocksConfig: FireblocksProviderConfig | ||
) { | ||
super(provider, fireblocksConfig); | ||
this._ethersWeb3Provider = new ethers.providers.Web3Provider( | ||
this['_fireblocksWeb3Provider'] as FireblocksWeb3Provider | ||
); | ||
this._defaultNote = fireblocksConfig.note; | ||
this._jsonRpcSigner = this._ethersWeb3Provider.getSigner(); | ||
} | ||
|
||
connect(provider: ethers.providers.Provider): Signer { | ||
return this._jsonRpcSigner.connect(provider); | ||
} | ||
|
||
async getAddress(): Promise<string> { | ||
return this._jsonRpcSigner.getAddress(); | ||
} | ||
|
||
setNote(memo: string): void { | ||
(this['_fireblocksWeb3Provider'] as FireblocksWeb3Provider)['note'] = memo; | ||
} | ||
|
||
restoreDefaultNote(): void { | ||
(this['_fireblocksWeb3Provider'] as FireblocksWeb3Provider)['note'] = | ||
this._defaultNote; | ||
} | ||
|
||
getBalance( | ||
blockTag?: ethers.providers.BlockTag | undefined | ||
): Promise<BigNumber> { | ||
return this._jsonRpcSigner.getBalance(blockTag); | ||
} | ||
|
||
getTransactionCount( | ||
blockTag?: ethers.providers.BlockTag | undefined | ||
): Promise<number> { | ||
return this._jsonRpcSigner.getTransactionCount(blockTag); | ||
} | ||
|
||
estimateGas(transaction: Deferrable<TransactionRequest>): Promise<BigNumber> { | ||
return this._jsonRpcSigner.estimateGas(transaction); | ||
} | ||
|
||
call( | ||
transaction: Deferrable<TransactionRequest>, | ||
blockTag?: ethers.providers.BlockTag | undefined | ||
): Promise<string> { | ||
return this._jsonRpcSigner.call(transaction, blockTag); | ||
} | ||
|
||
sendTransaction( | ||
transaction: Deferrable<TransactionRequest> | ||
): Promise<TransactionResponse> { | ||
return this.sendTransaction(transaction); | ||
} | ||
|
||
getChainId(): Promise<number> { | ||
return this._jsonRpcSigner.getChainId(); | ||
} | ||
|
||
getGasPrice(): Promise<BigNumber> { | ||
return this._jsonRpcSigner.getGasPrice(); | ||
} | ||
|
||
getFeeData(): Promise<ethers.providers.FeeData> { | ||
return this._jsonRpcSigner.getFeeData(); | ||
} | ||
|
||
resolveName(name: string): Promise<string> { | ||
return this._jsonRpcSigner.resolveName(name); | ||
} | ||
|
||
checkTransaction( | ||
transaction: Deferrable<TransactionRequest> | ||
): Deferrable<TransactionRequest> { | ||
return this._jsonRpcSigner.checkTransaction(transaction); | ||
} | ||
|
||
populateTransaction( | ||
transaction: Deferrable<TransactionRequest> | ||
): Promise<TransactionRequest> { | ||
return this._jsonRpcSigner.populateTransaction(transaction); | ||
} | ||
|
||
_checkProvider(operation?: string | undefined): void { | ||
return this._jsonRpcSigner._checkProvider(operation); | ||
} | ||
|
||
async signMessage(message: Bytes | string): Promise<string> { | ||
const data = typeof message === 'string' ? toUtf8Bytes(message) : message; | ||
return this._jsonRpcSigner.signMessage(data); | ||
} | ||
|
||
signTransaction( | ||
_transaction: Deferrable<TransactionRequest> | ||
): Promise<string> { | ||
throw new Error('signing transactions is unsupported by JsonRpcSigner'); | ||
} | ||
|
||
async _signTypedData( | ||
domain: TypedDataDomain, | ||
types: Record<string, TypedDataField[]>, | ||
value: Record<string, any> | ||
): Promise<string> { | ||
return this._jsonRpcSigner._signTypedData(domain, types, value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* eslint-disable no-param-reassign -- hre and config are intended to be configured via assignment in this file */ | ||
import '@nomiclabs/hardhat-ethers'; | ||
|
||
import { extendConfig, extendEnvironment } from 'hardhat/config'; | ||
import { BackwardsCompatibilityProviderAdapter } from 'hardhat/internal/core/providers/backwards-compatibility'; | ||
import { | ||
AutomaticGasPriceProvider, | ||
AutomaticGasProvider, | ||
} from 'hardhat/internal/core/providers/gas-providers'; | ||
import { HttpProvider } from 'hardhat/internal/core/providers/http'; | ||
import type { | ||
EIP1193Provider, | ||
HardhatConfig, | ||
HardhatUserConfig, | ||
HttpNetworkUserConfig, | ||
} from 'hardhat/types'; | ||
|
||
import './type-extensions'; | ||
import { version as SDK_VERSION } from '@fireblocks/hardhat-fireblocks/package.json'; | ||
|
||
import { FireblocksSigner } from './fireblocks-signer'; | ||
|
||
extendConfig( | ||
(config: HardhatConfig, userConfig: Readonly<HardhatUserConfig>) => { | ||
const userNetworks = userConfig.networks; | ||
if (userNetworks === undefined) { | ||
return; | ||
} | ||
for (const networkName in userNetworks) { | ||
Check warning on line 29 in plugins/fireblocks/index.ts GitHub Actions / lint
|
||
const network = userNetworks[networkName]! as HttpNetworkUserConfig; | ||
if (network.fireblocks !== undefined) { | ||
if ( | ||
networkName === 'hardhat' || | ||
(network.url || '').includes('localhost') || | ||
(network.url || '').includes('127.0.0.1') | ||
) { | ||
throw new Error('Fireblocks is only supported for public networks.'); | ||
} | ||
(config.networks[networkName] as HttpNetworkUserConfig).fireblocks = { | ||
note: 'Created by Nori custom Fireblocks Hardhat Plugin', | ||
logTransactionStatusChanges: true, | ||
...network.fireblocks, | ||
rpcUrl: network.url, | ||
userAgent: `hardhat-fireblocks/${SDK_VERSION}`, | ||
}; | ||
} | ||
} | ||
} | ||
); | ||
|
||
extendEnvironment((hre) => { | ||
if ((hre.network.config as HttpNetworkUserConfig).fireblocks != undefined) { | ||
const httpNetConfig = hre.network.config as HttpNetworkUserConfig; | ||
const eip1193Provider = new HttpProvider( | ||
httpNetConfig.url!, | ||
hre.network.name, | ||
httpNetConfig.httpHeaders, | ||
httpNetConfig.timeout | ||
); | ||
let wrappedProvider: EIP1193Provider; | ||
wrappedProvider = new FireblocksSigner( | ||
eip1193Provider, | ||
(hre.network.config as HttpNetworkUserConfig).fireblocks! | ||
); | ||
wrappedProvider = new AutomaticGasProvider( | ||
wrappedProvider, | ||
hre.network.config.gasMultiplier | ||
); | ||
wrappedProvider = new AutomaticGasPriceProvider(wrappedProvider); | ||
hre.network.provider = new BackwardsCompatibilityProviderAdapter( | ||
wrappedProvider | ||
); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import 'hardhat/types/config'; | ||
import 'hardhat/types/runtime'; | ||
|
||
import type { FireblocksProviderConfig } from '@fireblocks/fireblocks-web3-provider'; | ||
|
||
import type { FireblocksSigner } from './fireblocks-signer'; | ||
|
||
declare module 'hardhat/types/config' { | ||
interface HttpNetworkUserConfig { | ||
fireblocks?: FireblocksProviderConfig; | ||
} | ||
interface HttpNetworkConfig { | ||
fireblocks?: FireblocksProviderConfig; | ||
} | ||
interface HardhatConfig { | ||
fireblocks: FireblocksProviderConfig; | ||
} | ||
} | ||
|
||
declare module 'hardhat/types/runtime' { | ||
interface HardhatRuntimeEnvironment { | ||
fireblocks: { | ||
getSigners: () => Promise<FireblocksSigner[]>; | ||
getSigner: (index: number) => Promise<FireblocksSigner | undefined>; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.