Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EIP-3770 #178

Merged
merged 6 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Software developer tools that facilitate the interaction with the Gnosis Safe [c
| ------- | :-----: | ----------- |
| [safe-core-sdk](https://github.com/gnosis/safe-core-sdk/tree/main/packages/safe-core-sdk) | [![NPM Version](https://badge.fury.io/js/%40gnosis.pm%2Fsafe-core-sdk.svg)](https://badge.fury.io/js/%40gnosis.pm%2Fsafe-core-sdk) | TypeScript library that facilitates the interaction with the [Gnosis Safe contracts](https://github.com/gnosis/safe-contracts) |
[safe-core-sdk-types](https://github.com/gnosis/safe-core-sdk/tree/main/packages/safe-core-sdk-types) | [![NPM Version](https://badge.fury.io/js/%40gnosis.pm%2Fsafe-core-sdk-types.svg)](https://badge.fury.io/js/%40gnosis.pm%2Fsafe-core-sdk-types) | Common types extracted from the [Safe Core SDK](https://github.com/gnosis/safe-core-sdk/tree/main/packages) packages |
[safe-core-sdk-utils](https://github.com/gnosis/safe-core-sdk/tree/main/packages/safe-core-sdk-utils) | [![NPM Version](https://badge.fury.io/js/%40gnosis.pm%2Fsafe-core-sdk-utils.svg)](https://badge.fury.io/js/%40gnosis.pm%2Fsafe-core-sdk-utils) | Utilities for the [Safe Core SDK](https://github.com/gnosis/safe-core-sdk/tree/main/packages) packages |
[safe-ethers-lib](https://github.com/gnosis/safe-core-sdk/tree/main/packages/safe-ethers-lib) | [![NPM Version](https://badge.fury.io/js/%40gnosis.pm%2Fsafe-ethers-lib.svg)](https://badge.fury.io/js/%40gnosis.pm%2Fsafe-ethers-lib) | Ethers.js utilities and Safe contracts types (typechain ethers-v5) used to initialize the [Safe Core SDK](https://github.com/gnosis/safe-core-sdk/tree/main/packages/safe-core-sdk) |
[safe-web3-lib](https://github.com/gnosis/safe-core-sdk/tree/main/packages/safe-web3-lib) | [![NPM Version](https://badge.fury.io/js/%40gnosis.pm%2Fsafe-web3-lib.svg)](https://badge.fury.io/js/%40gnosis.pm%2Fsafe-web3-lib) | Web3.js utilities and Safe contracts types (typechain web3-v1) used to initialize the [Safe Core SDK](https://github.com/gnosis/safe-core-sdk/tree/main/packages/safe-core-sdk) |
[safe-service-client](https://github.com/gnosis/safe-core-sdk/tree/main/packages/safe-service-client) | [![NPM Version](https://badge.fury.io/js/%40gnosis.pm%2Fsafe-service-client.svg)](https://badge.fury.io/js/%40gnosis.pm%2Fsafe-service-client) | [Safe Transaction Service API](https://github.com/gnosis/safe-transaction-service) client library |
Expand Down
3 changes: 2 additions & 1 deletion packages/safe-core-sdk-types/src/ethereumLibs/EthAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AbiItem } from 'web3-utils'
import { GnosisSafeContract } from '../contracts/GnosisSafeContract'
import { GnosisSafeProxyFactoryContract } from '../contracts/GnosisSafeProxyFactoryContract'
import { MultiSendContract } from '../contracts/MultiSendContract'
import { SafeVersion } from '../types'
import { Eip3770Address, SafeVersion } from '../types'

export interface EthAdapterTransaction {
to: string
Expand All @@ -25,6 +25,7 @@ export interface GetContractProps {

export interface EthAdapter {
isAddress(address: string): boolean
getEip3770Address(fullAddress: string): Promise<Eip3770Address>
getBalance(address: string): Promise<BigNumber>
getChainId(): Promise<number>
getSafeContract({
Expand Down
5 changes: 5 additions & 0 deletions packages/safe-core-sdk-types/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,8 @@ export interface TransactionResult extends BaseTransactionResult {
transactionResponse?: ContractTransaction
options?: TransactionOptions
}

export interface Eip3770Address {
prefix: string
address: string
}
9 changes: 9 additions & 0 deletions packages/safe-core-sdk-utils/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) 2021-2022 Gnosis Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 changes: 29 additions & 0 deletions packages/safe-core-sdk-utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Safe Core SDK Utils

[![NPM Version](https://badge.fury.io/js/%40gnosis.pm%2Fsafe-core-sdk-utils.svg)](https://badge.fury.io/js/%40gnosis.pm%2Fsafe-core-sdk-utils)
[![GitHub Release](https://img.shields.io/github/release/gnosis/safe-core-sdk.svg?style=flat)](https://github.com/gnosis/safe-core-sdk/releases)
[![GitHub](https://img.shields.io/github/license/gnosis/safe-core-sdk)](https://github.com/gnosis/safe-core-sdk/blob/main/LICENSE.md)

Utilities for the [Safe Core SDK](https://github.com/gnosis/safe-core-sdk) monorepo.

## Table of contents
* [Installation](#installation)
* [Build](#build)

## <a name="installation">Installation</a>

Install the package with yarn or npm:

```bash
yarn install
npm install
```

## <a name="build">Build</a>

Build the package with yarn or npm:

```bash
yarn build
npm build
```
55 changes: 55 additions & 0 deletions packages/safe-core-sdk-utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"name": "@gnosis.pm/safe-core-sdk-utils",
"version": "1.0.0",
"description": "Safe Core SDK Utilities",
"main": "dist/src/index.js",
"typings": "dist/src/index.d.ts",
"keywords": [
"Ethereum",
"Gnosis",
"Safe",
"SDK"
],
"scripts": {
"unbuild": "rimraf dist *.tsbuildinfo",
"build": "rimraf dist && tsc",
"test": "nyc mocha -r ts-node/register tests/**/*.test.ts",
"format": "prettier --write \"{src,tests}/**/*.ts\"",
"lint": "tslint -p tsconfig.json"
},
"repository": {
"type": "git",
"url": "git+https://github.com/gnosis/safe-core-sdk.git"
},
"author": "Gnosis (https://gnosis.io)",
"license": "MIT",
"bugs": {
"url": "https://github.com/gnosis/safe-core-sdk/issues"
},
"files": [
"dist"
],
"homepage": "https://github.com/gnosis/safe-core-sdk#readme",
"devDependencies": {
"@types/chai": "^4.3.0",
"@types/chai-as-promised": "^7.1.4",
"@types/mocha": "^9.1.0",
"@types/node": "^17.0.13",
"@typescript-eslint/eslint-plugin": "^5.10.1",
"@typescript-eslint/parser": "^5.10.1",
"chai": "^4.3.6",
"chai-as-promised": "^7.1.1",
"eslint": "^8.7.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"mocha": "^9.2.0",
"nyc": "^15.1.0",
"prettier": "^2.5.1",
"ts-node": "^10.4.0",
"typescript": "^4.5.5"
},
"dependencies": {
"@gnosis.pm/safe-core-sdk-types": "^1.0.0",
"web3-utils": "^1.7.0"
}
}
38 changes: 38 additions & 0 deletions packages/safe-core-sdk-utils/src/eip-3770/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
interface NetworkShortName {
shortName: string
chainId: number
}

export const networks: NetworkShortName[] = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these related to contracts we currently have deployed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I took them from safe-deployments

{ shortName: 'eth', chainId: 1 },
{ shortName: 'rop', chainId: 3 },
{ shortName: 'rin', chainId: 4 },
{ shortName: 'gor', chainId: 5 },
{ shortName: 'oeth', chainId: 10 },
{ shortName: 'meta', chainId: 11 },
{ shortName: 'kal', chainId: 12 },
{ shortName: 'boba-rinkeby', chainId: 28 },
{ shortName: 'kov', chainId: 42 },
{ shortName: 'bnb', chainId: 56 },
{ shortName: 'okov', chainId: 69 },
{ shortName: 'gno', chainId: 100 },
{ shortName: 'fuse', chainId: 122 },
{ shortName: 'spark', chainId: 123 },
{ shortName: 'matic', chainId: 137 },
{ shortName: 'ewt', chainId: 246 },
{ shortName: 'boba', chainId: 288 },
{ shortName: 'metis-stardust', chainId: 588 },
{ shortName: 'metis-andromeda', chainId: 1088 },
{ shortName: 'mbeam', chainId: 1284 },
{ shortName: 'mriver', chainId: 1285 },
{ shortName: 'mbase', chainId: 1287 },
{ shortName: 'tftm', chainId: 4002 },
{ shortName: 'arb1', chainId: 42161 },
{ shortName: 'celo', chainId: 42220 },
{ shortName: 'avax', chainId: 43114 },
{ shortName: 'vt', chainId: 73799 },
{ shortName: 'maticmum', chainId: 80001 },
{ shortName: 'olympus', chainId: 333999 },
{ shortName: 'aurora', chainId: 1313161554 },
{ shortName: 'aurora-testnet', chainId: 1313161555 }
]
46 changes: 46 additions & 0 deletions packages/safe-core-sdk-utils/src/eip-3770/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Eip3770Address } from '@gnosis.pm/safe-core-sdk-types'
import { isAddress, isHexStrict } from 'web3-utils'
import { networks } from './config'

export function parseEip3770Address(fullAddress: string): Eip3770Address {
const parts = fullAddress.split(':')
const address = parts.length > 1 ? parts[1] : parts[0]
const prefix = parts.length > 1 ? parts[0] : ''
return { prefix, address }
}

export function getEip3770NetworkPrefixFromChainId(chainId: number): string {
const network = networks.find((network) => chainId === network.chainId)
if (!network) {
throw new Error('No network prefix supported for the current chainId')
}
return network.shortName
}

export function isValidEip3770NetworkPrefix(prefix: string): boolean {
return networks.some(({ shortName }) => shortName === prefix)
}

export function validateEip3770NetworkPrefix(prefix: string, currentChainId: number): void {
const isCurrentNetworkPrefix = prefix === getEip3770NetworkPrefixFromChainId(currentChainId)
if (prefix && (!isValidEip3770NetworkPrefix(prefix) || !isCurrentNetworkPrefix)) {
throw new Error('The network prefix must match the current network')
}
}

export function validateEthereumAddress(address: string): void {
const isValidAddress = isHexStrict(address) && isAddress(address)
if (!isValidAddress) {
throw new Error(`Invalid Ethereum address ${address}`)
}
}

export function validateEip3770Address(
fullAddress: string,
currentChainId: number
): Eip3770Address {
const { address, prefix } = parseEip3770Address(fullAddress)
validateEthereumAddress(address)
validateEip3770NetworkPrefix(prefix, currentChainId)
return { address, prefix }
}
1 change: 1 addition & 0 deletions packages/safe-core-sdk-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './eip-3770'
Loading