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

Refactor: Forwarding Path #226

Merged
merged 1 commit into from
Aug 20, 2020
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
77 changes: 77 additions & 0 deletions packages/connect-core/src/entities/ForwardingPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { ethers } from 'ethers'

import App from './App'
import Transaction from './Transaction'
import {
AppOrAddress,
ForwardingPathData,
ForwardingPathDescriptionData,
PostProcessDescription,
} from '../types'
import { describeForwardingPath } from '../utils/description'

export default class ForwardingPath {
#provider: ethers.providers.Provider
readonly apps: App[]
readonly destination: App
readonly transactions: Transaction[]

constructor(data: ForwardingPathData, provider: ethers.providers.Provider) {
this.#provider = provider
this.apps = data.apps
this.destination = data.destination
this.transactions = data.transactions
}

// Lets consumers pass a callback to sign any number of transactions.
// This is similar to calling transactions() and using a loop, but shorter.
// It returns the value returned by the library, usually a transaction receipt.
async sign<Receipt>(
callback: (tx: Transaction) => Promise<Receipt>
): Promise<Receipt[]> {
return Promise.all(this.transactions.map(async (tx) => await callback(tx)))
}

// Return a description of the forwarding path, to be rendered.
async describe(): Promise<ForwardingPathDescription> {
// TODO: Make sure we are safe to only provide the apps on the path here
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here I decide to use the apps that form the path but I wonder if we might need to know the whole list of installed apps. If that case we need to construct the ForwardingPath with a reference to the organization so we can fetch information like the list of apps.

return describeForwardingPath(this.transactions, this.apps, this.#provider)
}

// Return a description of the forwarding path, as text.
// Shorthand for .describe().toString()
toString(): string {
return this.describe().toString()
}
}

type ForwardingPathDescriptionTreeEntry =
| AppOrAddress
| [AppOrAddress, ForwardingPathDescriptionTreeEntry[]]

export type ForwardingPathDescriptionTree = ForwardingPathDescriptionTreeEntry[]

export class ForwardingPathDescription {
readonly apps: App[]
readonly describeSteps: PostProcessDescription[]

constructor(data: ForwardingPathDescriptionData) {
this.apps = data.apps
this.describeSteps = data.describeSteps
Copy link
Contributor Author

@0xGabi 0xGabi Aug 20, 2020

Choose a reason for hiding this comment

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

I'm still not super sure about the data structure we would like to have for the ForwardingPathDescription. I just decided to use a list of PostProcessDescription objects. Those are returned by Radspec. We may probably want to further think about it.

}

// Return a tree that can get used to render the path.
tree(): ForwardingPathDescriptionTree {
// TODO:
return []
}

// Renders the forwarding path description as text
toString(): string {
return this.tree().toString()
}

// TBD: a utility that makes it easy to render the tree,
// e.g. as a nested list in HTML or React.
reduce(callback: Function): any {}
}
8 changes: 8 additions & 0 deletions packages/connect-core/src/entities/Organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import {
} from '@aragon/connect-types'

import { ConnectionContext } from '../types'
import { decodeForwardingPath } from '../utils/description'
import { toArrayEntry } from '../utils/misc'
import App from './App'
import Intent from './Intent'
import { ForwardingPathDescription } from './ForwardingPath'
import Permission from './Permission'

// TODO
Expand Down Expand Up @@ -134,4 +136,10 @@ export default class Organization {
this.connection.ethersProvider
)
}

//////// DESCRIPTIONS /////////

async describeScript(script: string): Promise<ForwardingPathDescription> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I decided to expose the describeScript utility to the organization as we already have all the contextual information we need and user and app connectors can leverage directly.

return decodeForwardingPath(script, this.apps(), this.connection)
}
}
11 changes: 11 additions & 0 deletions packages/connect-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ export interface AppData {
version?: string
}

export interface ForwardingPathData {
apps: App[]
destination: App
transactions: Transaction[]
}

export interface ForwardingPathDescriptionData {
apps: App[]
describeSteps: PostProcessDescription[]
}

export interface IntentData {
appAddress: string
functionName: string
Expand Down
94 changes: 94 additions & 0 deletions packages/connect-core/src/utils/description.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { ethers } from 'ethers'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here is the main logic we are currently using to both encode and decode an evm script and transform it into a ForwardingPathDescrption. I'm not convinced yet about the current data types and how we constructing the description but I think we are going in the right direction. Maybe it's a big topic and we should address it as a separate PR and further discuss it in: #219


import { isCallScript, decodeCallScript } from './callScript'
import { isValidForwardCall, parseForwardCall } from './forwarding'
import App from '../entities/App'
import Transaction from '../entities/Transaction'
import {
ForwardingPathDescription,
ForwardingPathDescriptionTree,
} from '../entities/ForwardingPath'
import {
tryEvaluatingRadspec,
postprocessRadspecDescription,
} from './radspec/index'
import { PostProcessDescription } from '../types'

export async function describeStep(
transaction: Transaction,
apps: App[],
provider: ethers.providers.Provider
): Promise<PostProcessDescription> {
if (!transaction.to) {
throw new Error(`Could not describe transaction: missing 'to'`)
}
if (!transaction.data) {
throw new Error(`Could not describe transaction: missing 'data'`)
}

let description, annotatedDescription
try {
description = await tryEvaluatingRadspec(transaction, apps, provider)

if (description) {
const processed = await postprocessRadspecDescription(description, apps)
annotatedDescription = processed.annotatedDescription
description = processed.description
}
} catch (err) {
throw new Error(`Could not describe transaction: ${err}`)
}

return {
description,
annotatedDescription,
}
}

/**
* Use radspec to create a human-readable description for each step in the given `path`
*
*/
export async function describeForwardingPath(
path: Transaction[],
apps: App[],
provider: ethers.providers.Provider
): Promise<ForwardingPathDescription> {
const describeSteps = await Promise.all(
path.map((step) => describeStep(step, apps, provider))
)
return new ForwardingPathDescription({ describeSteps, apps })
}

/**
* Decodes an EVM callscript and returns the forwarding path it description.
*
* @return An array of Ethereum transactions that describe each step in the path
*/
export function decodeForwardingPath(
script: string
): ForwardingPathDescriptionTree {
// In the future we may support more EVMScripts, but for now let's just assume we're only
// dealing with call scripts
if (!isCallScript(script)) {
throw new Error(`Script could not be decoded: ${script}`)
}

const path = decodeCallScript(script)

return path.reduce((decodeSegments, segment) => {
const { data } = segment

let children
if (isValidForwardCall(data)) {
const forwardedEvmScript = parseForwardCall(data)

try {
children = decodeForwardingPath(forwardedEvmScript)
// eslint-disable-next-line no-empty
} catch (err) {}
}

return decodeSegments.concat({ ...segment, children })
}, [] as ForwardingPathDescriptionTree)
}
76 changes: 0 additions & 76 deletions packages/connect-core/src/utils/descriptions.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/connect-core/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// export * from './descriptions'
export * from './description'
export * from './misc'
export * from './network'
export * from './app-connectors'
Expand Down
39 changes: 0 additions & 39 deletions packages/connect-core/src/utils/path/decodePath.ts

This file was deleted.

5 changes: 1 addition & 4 deletions packages/connect-core/src/utils/radspec/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,4 @@ export async function tryEvaluatingRadspec(
return evaluatedNotice
}

export {
postprocessRadspecDescription,
PostProcessDescription,
} from './postprocess'
export { postprocessRadspecDescription } from './postprocess'
7 changes: 1 addition & 6 deletions packages/connect-core/src/utils/radspec/postprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import { addressesEqual, ANY_ENTITY } from '../address'
import { getKernelNamespace } from '../kernel'
import App from '../../entities/App'
import Role from '../../entities/Role'
import { Annotation } from '../../types'

export interface PostProcessDescription {
description: string
annotatedDescription?: Annotation[]
}
import { Annotation, PostProcessDescription } from '../../types'

interface CompiledTokens {
description: string[]
Expand Down