Skip to content

Commit

Permalink
Merge pull request #4 from rhinestonewtf/feat/v0.1.0
Browse files Browse the repository at this point in the history
Feat/v0.1.0
  • Loading branch information
legion2002 authored Dec 16, 2024
2 parents fae03f7 + 72b8f7f commit db39dde
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 109 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"size": "size-limit"
},
"devDependencies": {
"@rhinestone/module-sdk": "^0.1.28",
"@types/ws": "^8.5.13",
"axios": "^1.7.4",
"dotenv": "^16.4.5",
Expand Down
28 changes: 0 additions & 28 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ For API documentation, visit: [Swagger Docs](https://orchestrator-prototype-38oy
### Installation

```bash
npm install viem @rhinestone/module-sdk @rhinestone/orchestrator-sdk
npm install viem @rhinestone/orchestrator-sdk
```

```bash
pnpm install viem @rhinestone/module-sdk @rhinestone/orchestrator-sdk
pnpm install viem @rhinestone/orchestrator-sdk
```

```bash
yarn add viem @rhinestone/module-sdk @rhinestone/orchestrator-sdk
yarn add viem @rhinestone/orchestrator-sdk
```

```bash
bun install viem @rhinestone/module-sdk @rhinestone/orchestrator-sdk
bun install viem @rhinestone/orchestrator-sdk
```

### Quick Start
Expand Down
8 changes: 4 additions & 4 deletions src/common/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import {
SmartDigest,
TokenTransfer,
} from '../types'
import { typehashTypes } from '../constants/typehashTypes'
import { typehashTypes } from '../constants'

const ACROSS_TRANSFER_TYPEHASH =
'0xee2056ef95579555475af3a751f04f29e041cedf8232901492fffb1808c7fd99'
'0x2c207ea4e14283d6620837628d3aa63486714ffdf9bd83436f971b1157f5d51a'

const EXECUTION_TYPEHASH =
'0x37fb04e5593580b36bfacc47d8b1a4b9a2acb88a513bf153760f925a6723d4b5'
Expand All @@ -33,10 +33,10 @@ const TOKEN_TRANSFER_TYPEHASH =
'0xef3136c9bbc8441e191dc61c253d9479f54ef95a64b36b8581d51a156f7512c4'

const SIGNED_INTENT_TYPEHASH =
'0xd714a8401248b943a7db2c285bdacfa439f95fc2804282de474bea0194ee89e4'
'0x9bafa23b0ee7874c656c66202e7ff9c0f6b794631cfff0a50b9bdd2041f1d4be'

const SIGNED_INTENT_WITH_AUCTION_FEE_TYPEHASH =
'0x3fcc83bf31d227d374fb57ade6ccfa70ffde400e9b905cb3ab0c30c18a29fa64'
'0xa0abe415300d58e370d7d8eb5507aa75764fb2d77d9d91ee92a3415ad1b22a37'

const SIGNED_USER_OP_TYPEHASH =
'0x81264956c7c6625e9c85b65ea4d4eaeb56247ec77f1b277e3635a99ff98b11c2'
Expand Down
37 changes: 10 additions & 27 deletions src/common/signer.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,32 @@
import {
getOwnableValidatorSignature,
OWNABLE_VALIDATOR_ADDRESS,
} from '@rhinestone/module-sdk'
import { hashTypedData, Hex } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { SignedIntent, SignedOrderBundle } from '../types'
import { smartContractTypes } from '../constants'
import { getOrderDomain } from '../utils'


export async function signOrderBundleWithOwnableValidator(
export async function getOrderBundleHash(
orderBundle: SignedIntent,
privateKey: Hex,
): Promise<SignedOrderBundle> {
const hash = hashTypedData({
): Promise<Hex> {
return hashTypedData({
domain: getOrderDomain(),
types: smartContractTypes,
primaryType: 'SignedIntent',
message: orderBundle,
})
}

const account = privateKeyToAccount(privateKey)

// Add the prefix for ownable validator sig
const signature = await account.signMessage({
message: {
raw: hash,
},
})

const ownableValidatorSig = getOwnableValidatorSignature({
signatures: [signature],
})

const encodedSignature = (OWNABLE_VALIDATOR_ADDRESS +
ownableValidatorSig.slice(2)) as Hex

export async function getSignedOrderBundle(
orderBundle: SignedIntent,
orderBundleSignature: Hex,
): Promise<SignedOrderBundle> {
return {
settlement: orderBundle.settlement,
acrossTransfers: orderBundle.acrossTransfers.map((transfer) => ({
...transfer,
userSignature: encodedSignature,
userSignature: orderBundleSignature,
})),
targetChainExecutions: orderBundle.targetChainExecutions,
targetExecutionSignature: encodedSignature,
targetExecutionSignature: orderBundleSignature,
userOp: orderBundle.userOp,
}
}
4 changes: 2 additions & 2 deletions src/constants/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export const NATIVE_SENTINEL_ADDRESS: Address =
'0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'

const hook: Address = '0xF7913a68dD7bFF74f0828Ac9d879C6195B370EB8'
const originModule: Address = '0xBdb703d933Fac61b3Cf3909Cb2C82C5e4DAEFf1D'
const targetModule: Address = '0x6961515e13B3b2B09b4B7333B1860ee8DBE98921'
const originModule: Address = '0xD3C6B5539E0d70F58160Cd023DB16853847Ec61E'
const targetModule: Address = '0x3cEb8931374cB8F2e300325Af27F44312719B4BB'
const spokepool: Address = '0x7C941271191E0c9A01E360BAE660D21568fc6ca0'
const weth: Address = '0x47D41c334497f06ab42e60C4036c506D924DDc9c'

Expand Down
36 changes: 0 additions & 36 deletions src/orchestrator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Address, Hex } from 'viem'
import { signOrderBundleWithOwnableValidator } from './common/signer'
import {
BundleIdStatus,
Execution,
Expand All @@ -10,7 +9,6 @@ import {
import { convertBigIntFields } from './utils'
import axios from 'axios'

// TODO: Export more helpful constants like the contract addresses, Spokepool addresses, etc.
// TODO: Add strict typing to the return values of the endpoints.

export class Orchestrator {
Expand Down Expand Up @@ -139,40 +137,6 @@ export class Orchestrator {
throw new Error('Failed to post order bundle')
}

async postMetaIntentWithOwnableValidator(
metaIntent: MetaIntent,
userId: string,
privateKey: Hex,
): Promise<string> {
try {
const { orderBundle, injectedExecutions } = await this.getOrderPath(
metaIntent,
userId,
)
const signedOrderBundle = await signOrderBundleWithOwnableValidator(
orderBundle,
privateKey,
)
const response = await axios.post(
`${this.serverUrl}/users/${userId}/bundles`,
{
signedOrderBundle: signedOrderBundle,
},
{
headers: {
'x-api-key': this.apiKey,
},
},
)
return response.data.bundleId
} catch (error) {
if (error instanceof Error) {
console.log(error)
}
}
throw new Error('Failed to post order bundle')
}

async getBundleStatus(
userId: string,
bundleId: string,
Expand Down
3 changes: 1 addition & 2 deletions src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
"./package.json": "./package.json"
},
"peerDependencies": {
"viem": "^2.17.8",
"@rhinestone/module-sdk": "^0.1.29"
"viem": "^2.17.8"
},
"homepage": "https://docs.rhinestone.wtf/orchestrator-sdk",
"bugs": {
Expand Down
15 changes: 10 additions & 5 deletions test/orchestrator/orchestrator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as dotenv from 'dotenv'

import { Address, encodeFunctionData, erc20Abi, Hex } from 'viem'
import { Execution, getOrchestrator, MetaIntent, Orchestrator } from '../../src'
import { Execution, getOrchestrator, MetaIntent } from '../../src'
import { Orchestrator } from '../../src/orchestrator' // Ensure this path is correct
import { getEmptyUserOp } from '../../src/utils/userOp'
import { getTokenAddress } from '../../src/constants'
import { postMetaIntentWithOwnableValidator } from '../utils/safe7579Signature'

dotenv.config()

Expand All @@ -16,8 +18,8 @@ const generateRandomAddress = (): Address => {
describe('Orchestrator Service', () => {
let orchestrator: Orchestrator

const userId = '581379d0-2fdd-4ea3-9aab-b900f7ed3e30'
const accountAddress = '0x7F1eA505b099BA673937a61A4c9B161c115c6E01'
const userId = 'd6f64241-a62c-4542-bb23-e78d7e1e0cd6'
const accountAddress = '0x9EB7504B7546b1B66e177B364A3566eC10132A40'

const execution: Execution = {
target: getTokenAddress('USDC', 8453),
Expand All @@ -43,7 +45,9 @@ describe('Orchestrator Service', () => {
}

beforeAll(async () => {
orchestrator = getOrchestrator(process.env.ORCHESTRATOR_API_KEY!)
orchestrator = getOrchestrator(
process.env.ORCHESTRATOR_API_KEY!,
) as unknown as Orchestrator
})

afterAll(async () => {
Expand Down Expand Up @@ -96,10 +100,11 @@ describe('Orchestrator Service', () => {
})

it('should post a meta intent with ownable validator and return a bundle ID', async () => {
const bundleId = await orchestrator.postMetaIntentWithOwnableValidator(
const bundleId = await postMetaIntentWithOwnableValidator(
metaIntent,
userId,
process.env.BUNDLE_GENERATOR_PRIVATE_KEY! as Hex,
orchestrator,
)

expect(bundleId).toBeDefined()
Expand Down
60 changes: 60 additions & 0 deletions test/utils/safe7579Signature.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Address, Hex } from 'viem'
import {
getOrderBundleHash,
getSignedOrderBundle,
} from '../../src/common/signer'
import { MetaIntent, SignedIntent, SignedOrderBundle } from '../../src/types'
import { privateKeyToAccount } from 'viem/accounts'
import { Orchestrator } from '../../src/orchestrator'

const OWNABLE_VALIDATOR_ADDRESS: Address =
'0x2483da3a338895199e5e538530213157e931bf06'

// NOTE: This only works for Safe7579
export async function signOrderBundleWithOwnableValidator(
orderBundle: SignedIntent,
privateKey: Hex,
): Promise<SignedOrderBundle> {
const digest = await getOrderBundleHash(orderBundle)

const account = privateKeyToAccount(privateKey)

// Add the prefix for ownable validator sig
const signature = await account.signMessage({
message: {
raw: digest,
},
})

const encodedSignature = (OWNABLE_VALIDATOR_ADDRESS +
signature.slice(2)) as Hex

return getSignedOrderBundle(orderBundle, encodedSignature)
}

export async function postMetaIntentWithOwnableValidator(
metaIntent: MetaIntent,
userId: string,
privateKey: Hex,
orchestrator: Orchestrator,
): Promise<string> {
try {
const { orderBundle, injectedExecutions } = await orchestrator.getOrderPath(
metaIntent,
userId,
)

// TODO: Add injected executions to orderBundleExecution
const signedOrderBundle = await signOrderBundleWithOwnableValidator(
orderBundle,
privateKey,
)

return orchestrator.postSignedOrderBundle(signedOrderBundle, userId)
} catch (error) {
if (error instanceof Error) {
console.log(error)
}
}
throw new Error('Failed to post order bundle')
}

0 comments on commit db39dde

Please sign in to comment.