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

Schema validation #462

Closed
wants to merge 21 commits into from
Closed
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,495 changes: 823 additions & 672 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"generate:humanizerSelectorsAndNames": "node scripts/compileContractSelectorsAndNames.js",
"generate:combineHumanizerJsons": "node scripts/combineHumanizerJsons.js",
"compile:contracts": "ts-node scripts/compileContracts.js",
"compile:tsSchemas": "ts-node src/libs/schemaValidation/generateSchemas.ts",
"test": "npx hardhat compile; npx hardhat test; jest --forceExit",
"hardhat": "npx hardhat compile; npx hardhat test",
"jest": "jest --forceExit",
"build": "tsc src/libs/deployless/deployless.ts -t es5",
Expand All @@ -15,10 +17,13 @@
"dependencies": {
"@jest/globals": "^29.6.1",
"aes-js": "^3.1.2",
"ajv": "^8.12.0",
"ambire-constants": "github:AmbireTech/ambire-constants",
"dotenv": "^16.3.1",
"ethers": "^6.8.0",
"scrypt-js": "^3.0.1"
"scrypt-js": "^3.0.1",
"tv4": "^1.3.0",
"typescript-json-schema": "^0.62.0"
},
"peerDependencies": {
"@ambire/signature-validator": "^1.0.3",
Expand Down Expand Up @@ -46,6 +51,7 @@
"@types/mocha": "^10.0.1",
"@types/node": "^18.16.1",
"@types/node-fetch": "^2.6.3",
"@types/tv4": "^1.2.33",
"@types/validator": "^13.7.0",
"@typescript-eslint/eslint-plugin": "5.5.0",
"@typescript-eslint/parser": "5.5.0",
Expand Down
11 changes: 11 additions & 0 deletions src/controllers/accountAdder/accountAdder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ethers, JsonRpcProvider } from 'ethers'

import { schemas } from '../../libs/schemaValidation/validateScehmas'
import { PROXY_AMBIRE_ACCOUNT } from '../../consts/deploy'
import {
HD_PATH_TEMPLATE_TYPE,
Expand Down Expand Up @@ -419,6 +420,7 @@ export class AccountAdderController extends EventEmitter {
}))

try {
// doesn't need aditional schema validation
const res = await this.#callRelayer('/v2/identity/create-multiple', 'POST', {
accounts: body
})
Expand Down Expand Up @@ -648,6 +650,15 @@ export class AccountAdderController extends EventEmitter {
const url = `/v2/account-by-key/linked/accounts?${keys}`

const { data } = await this.#callRelayer(url)
const schemasRes = schemas.RelayerResponseLinkedAccount(data)
if (!schemasRes.isValid) {
this.emitError({
level: 'minor',
message: 'Error to fetch identities from the relayer, please contact support.',
error: new Error(schemasRes.error)
})
return
}
const linkedAccounts: { account: Account; isLinked: boolean }[] = Object.keys(
data.accounts
).flatMap((addr: string) => {
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -895,8 +895,8 @@ export class MainController extends EventEmitter {
'POST',
body
)

if (response.success) {
// res doesn't need more validation than this
if (response.success && response.txId) {
transactionRes = {
hash: response.txId,
nonce: Number(accountOp.nonce)
Expand Down
12 changes: 12 additions & 0 deletions src/controllers/portfolio/portfolio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { JsonRpcProvider } from 'ethers'
import fetch from 'node-fetch'

import { schemas } from '../../libs/schemaValidation/validateScehmas'
import { Account, AccountId } from '../../interfaces/account'
import { NetworkDescriptor } from '../../interfaces/networkDescriptor'
import { Storage } from '../../interfaces/storage'
Expand Down Expand Up @@ -98,6 +99,16 @@ export class PortfolioController extends EventEmitter {
let res: any
try {
res = await this.#callRelayer(`/v2/identity/${accountId}/portfolio-additional`)
const { data } = res
const schemasRes = schemas.RelayerReponsePortfolioAdditional(data)
if (!schemasRes.isValid) {
this.emitError({
level: 'minor',
message: 'Error to fetch portfolio-additional from the relayer, please contact support.',
error: new Error(schemasRes.error)
})
return
}
} catch (e: any) {
console.error('relayer error for portfolio additional')
this.#setNetworkLoading(accountId, 'gasTank', false, e)
Expand Down Expand Up @@ -278,6 +289,7 @@ export class PortfolioController extends EventEmitter {

const forceUpdate = opts?.forceUpdate || areAccountOpsChanged

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [isSuccessfulLatestUpdate, isSuccessfulPendingUpdate] = await Promise.all([
// Latest state update
updatePortfolioState(
Expand Down
12 changes: 11 additions & 1 deletion src/controllers/signAccountOp/signAccountOp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import EntryPointAbi from '../../../contracts/compiled/EntryPoint.json'
import ERC20 from '../../../contracts/compiled/IERC20.json'
import { AMBIRE_PAYMASTER, ERC_4337_ENTRYPOINT } from '../../consts/deploy'
import { Account, AccountStates } from '../../interfaces/account'
import { IrCall } from '../../interfaces/humanizer'
import { ExternalSignerController, Key } from '../../interfaces/keystore'
import { NetworkDescriptor } from '../../interfaces/networkDescriptor'
import { Storage } from '../../interfaces/storage'
Expand All @@ -18,8 +19,8 @@ import {
import { EstimateResult } from '../../libs/estimate/estimate'
import { GasRecommendation, getCallDataAdditional } from '../../libs/gasPrice/gasPrice'
import { callsHumanizer } from '../../libs/humanizer'
import { IrCall } from '../../libs/humanizer/interfaces'
import { Price, TokenResult } from '../../libs/portfolio'
import { schemas } from '../../libs/schemaValidation/validateScehmas'
import {
getOneTimeNonce,
isErc4337Broadcast,
Expand Down Expand Up @@ -723,6 +724,15 @@ export class SignAccountOpController extends EventEmitter {
paymaster: AMBIRE_PAYMASTER
}
)
const schemasRes = schemas.RelayerResponsePaymasterSign(response.data)
if (!schemasRes.isValid) {
this.emitError({
level: 'minor',
message: 'Error submit signature to paymaster/sign. Please contact support.',
error: new Error(schemasRes.error)
})
return null
}
userOperation.paymasterAndData = response.data.paymasterAndData
if (usesOneTimeNonce) {
userOperation.nonce = getOneTimeNonce(userOperation)
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/signMessage/signMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Storage } from '../../interfaces/storage'
import { Message } from '../../interfaces/userRequest'
import { getKnownAddressLabels } from '../../libs/account/account'
import { messageHumanizer } from '../../libs/humanizer'
import { IrMessage } from '../../libs/humanizer/interfaces'
import { IrMessage } from '../../interfaces/humanizer'
import {
verifyMessage,
wrapCounterfactualSign,
Expand Down
16 changes: 16 additions & 0 deletions src/interfaces/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,19 @@ export type AccountStates = {
[networkId: string]: AccountOnchainState
}
}

export interface RelayerResponseLinkedAccount {
keys: {
[identityAddress: string]: {
[network: string]: {
[key: string]: string
}
}
}
accounts: {
[identityAddr: string]: AccountCreation & {
associatedKeys: { [key: string]: string | boolean }
initialPrivilegesAddrs: string[]
}
}
}
5 changes: 5 additions & 0 deletions src/interfaces/accountOp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface RelayerResponsePaymasterSign {
success: true
data: { paymasterAndData: string }
errorState: any[]
}
13 changes: 13 additions & 0 deletions src/interfaces/emailVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,16 @@ export interface RecoveryKey {
key: String
type: String
}

export interface Secret {
key: String
type: String
}

// NOTE: its a quick fix. Will be updated in other branch
export interface EmailVaultInfo {
email: String
recoveryKey: String
availableSecrets: Secret[]
availableAccounts: any
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { NetworkId } from 'interfaces/networkDescriptor'

import { Account } from '../../interfaces/account'
import { Message, TypedMessage } from '../../interfaces/userRequest'
import { AccountOp, Call } from '../accountOp/accountOp'
import { Account } from './account'
import { Message, TypedMessage } from './userRequest'
import { AccountOp, Call } from '../libs/accountOp/accountOp'

export type HumanizerVisualization = {
type: 'token' | 'address' | 'label' | 'action' | 'nft' | 'danger'
Expand Down
49 changes: 49 additions & 0 deletions src/interfaces/portfolio.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
export interface RelayerReponsePortfolioAdditional {
rewards?: {
supplyControllerAddr: string
claimableRewardsData: {
addr: string
fromBalanceClaimable: number
fromADXClaimable: number
totalClaimable: string
leaf?: string
proof?: string[]
root?: string
signedRoot?: string
}
multipliers?: { mul: number; name: string }[]
xWalletClaimableBalance: {
address: string
symbol: string
amount: string
decimals: number
networkId: string
priceIn?: {
baseCurrency: string
price: number
}[]
}
}
gasTank?: {
balance?: {
address: string
symbol: string
amount: string
decimals: number
networkId: string
priceIn: {
baseCurrency: string
price: number
}[]
}[]

availableGasTankAssets: {
address: string
symbol: string
network: string
decimals: number
icon: string
price: number
}[]
}
}
2 changes: 1 addition & 1 deletion src/libs/account/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SMART_ACCOUNT_SIGNER_KEY_DERIVATION_OFFSET } from '../../consts/derivat
import { Account } from '../../interfaces/account'
import { Key } from '../../interfaces/keystore'
import { AccountPreferences, KeyPreferences } from '../../interfaces/settings'
import { KnownAddressLabels } from '../humanizer/interfaces'
import { KnownAddressLabels } from '../../interfaces/humanizer'
import { getBytecode } from '../proxyDeploy/bytecode'
import { getAmbireAccountAddress } from '../proxyDeploy/getAmbireAddressTwo'

Expand Down
21 changes: 7 additions & 14 deletions src/libs/emailVault/emailVault.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import { relayerCall } from '../relayerCall/relayerCall'
import { EmailVaultData, EmailVaultSecrets, RecoveryKey } from '../../interfaces/emailVault'

export interface Secret {
key: String
type: String
}

// NOTE: its a quick fix. Will be updated in other branch
export interface EmailVaultInfo {
email: String
recoveryKey: String
availableSecrets: Secret[]
availableAccounts: any
}
import {
EmailVaultData,
EmailVaultInfo,
EmailVaultSecrets,
RecoveryKey
} from '../../interfaces/emailVault'

// @TODO add shcema validation once figured out
export class EmailVault {
private callRelayer: Function

Expand Down
2 changes: 1 addition & 1 deletion src/libs/humanizer/humanizerFuncs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import fetch from 'node-fetch'
import { AccountOp } from '../accountOp/accountOp'
import { fallbackHumanizer } from './modules/fallBackHumanizer'
import { uniswapHumanizer } from './modules/Uniswap'
import { HumanizerFragment, HumanizerVisualization, IrCall } from './interfaces'
import { HumanizerFragment, HumanizerVisualization, IrCall } from '../../interfaces/humanizer'
import { genericErc20Humanizer, genericErc721Humanizer } from './modules/tokens'
import { ErrorRef } from '../../controllers/eventEmitter'
import { nameParsing } from './parsers/nameParsing'
Expand Down
2 changes: 1 addition & 1 deletion src/libs/humanizer/humanizerFuncs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
HumanizerTypedMessaageModule,
IrCall,
IrMessage
} from './interfaces'
} from '../../interfaces/humanizer'
import { getAction, getLabel } from './utils'

export function humanizeCalls(
Expand Down
2 changes: 1 addition & 1 deletion src/libs/humanizer/humanizerModules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ethers } from 'ethers'
import { ErrorRef } from '../../controllers/eventEmitter'
import { AccountOp } from '../accountOp/accountOp'
import { humanizeCalls, visualizationToText } from './humanizerFuncs'
import { HumanizerCallModule, HumanizerVisualization, IrCall } from './interfaces'
import { HumanizerCallModule, HumanizerVisualization, IrCall } from '../../interfaces/humanizer'
import { aaveHumanizer } from './modules/Aave'
import { fallbackHumanizer } from './modules/fallBackHumanizer'
import { genericErc20Humanizer, genericErc721Humanizer } from './modules/tokens'
Expand Down
2 changes: 1 addition & 1 deletion src/libs/humanizer/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Storage } from '../../interfaces/storage'
import { Message, TypedMessage } from '../../interfaces/userRequest'
import { AccountOp } from '../accountOp/accountOp'
import { callsHumanizer, messageHumanizer } from './index'
import { HumanizerVisualization, IrCall, IrMessage } from './interfaces'
import { HumanizerVisualization, IrCall, IrMessage } from '../../interfaces/humanizer'

const HUMANIZER_META_KEY = 'HumanizerMeta'

Expand Down
2 changes: 1 addition & 1 deletion src/libs/humanizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
IrCall,
IrMessage,
KnownAddressLabels
} from './interfaces'
} from '../../interfaces/humanizer'
import { aaveHumanizer } from './modules/Aave'
import { fallbackHumanizer } from './modules/fallBackHumanizer'
import { gasTankModule } from './modules/gasTankModule'
Expand Down
2 changes: 1 addition & 1 deletion src/libs/humanizer/modules/Aave/aaveLendingPoolV2.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ethers } from 'ethers'
import { getAction, getLabel, getToken, getOnBehalfOf } from '../../utils'
import { AccountOp } from '../../../accountOp/accountOp'
import { IrCall } from '../../interfaces'
import { IrCall } from '../../../../interfaces/humanizer'

export const aaveLendingPoolV2 = (humanizerInfo: any): { [key: string]: Function } => {
const iface = new ethers.Interface(humanizerInfo?.['abis:AaveLendingPoolV2'])
Expand Down
2 changes: 1 addition & 1 deletion src/libs/humanizer/modules/Aave/aaveWethGatewayV2.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ethers } from 'ethers'
import { IrCall } from '../../interfaces'
import { IrCall } from '../../../../interfaces/humanizer'
import { getAction, getOnBehalfOf, getToken, getLabel } from '../../utils'
import { AccountOp } from '../../../accountOp/accountOp'

Expand Down
2 changes: 1 addition & 1 deletion src/libs/humanizer/modules/Aave/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getUnknownVisualization } from '../../utils'
import { AccountOp } from '../../../accountOp/accountOp'
import { HumanizerCallModule, IrCall } from '../../interfaces'
import { HumanizerCallModule, IrCall } from '../../../../interfaces/humanizer'
import { aaveLendingPoolV2 } from './aaveLendingPoolV2'
import { aaveWethGatewayV2 } from './aaveWethGatewayV2'

Expand Down
2 changes: 1 addition & 1 deletion src/libs/humanizer/modules/Uniswap/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AccountOp } from '../../../accountOp/accountOp'
import { HumanizerCallModule, IrCall } from '../../interfaces'
import { HumanizerCallModule, IrCall } from '../../../../interfaces/humanizer'
import { uniUniversalRouter } from './uniUnivarsalRouter'
import { uniV2Mapping } from './uniV2'
import { uniV32Mapping, uniV3Mapping } from './uniV3'
Expand Down
2 changes: 1 addition & 1 deletion src/libs/humanizer/modules/Uniswap/uniUnivarsalRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
getUnknownVisualization
} from '../../utils'
import { AccountOp } from '../../../accountOp/accountOp'
import { IrCall } from '../../interfaces'
import { IrCall } from '../../../../interfaces/humanizer'
import { COMMANDS, COMMANDS_DESCRIPTIONS } from './Commands'
import { parsePath } from './utils'

Expand Down
2 changes: 1 addition & 1 deletion src/libs/humanizer/modules/Uniswap/uniV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ethers } from 'ethers'
import { getAction, getLabel, getToken, getRecipientText, getDeadlineText } from '../../utils'

import { AccountOp } from '../../../accountOp/accountOp'
import { IrCall } from '../../interfaces'
import { IrCall } from '../../../../interfaces/humanizer'

const uniV2Mapping = (
humanizerInfo: any
Expand Down
2 changes: 1 addition & 1 deletion src/libs/humanizer/modules/Uniswap/uniV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '../../utils'

import { AccountOp } from '../../../accountOp/accountOp'
import { IrCall } from '../../interfaces'
import { IrCall } from '../../../../interfaces/humanizer'
import { parsePath } from './utils'

// Stolen from ambire-wallet
Expand Down
Loading