-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1095 from lens-protocol/cesare/lens-500-sdk-suppo…
…rt-operation-approval-workflow feat: support Operation Approval workflow
- Loading branch information
Showing
16 changed files
with
220 additions
and
73 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
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,32 @@ | ||
/** | ||
* Operations types. | ||
*/ | ||
export enum OperationType { | ||
Post = 'Post', | ||
Repost = 'Repost', | ||
EditPost = 'EditPost', | ||
DeletePost = 'DeletePost', | ||
Follow = 'Follow', | ||
Unfollow = 'Unfollow', | ||
CreateAccount = 'CreateAccount', | ||
CreateUsername = 'CreateUsername', | ||
CreateAndAssignUsername = 'CreateAndAssignUsername', | ||
AssignUsername = 'AssignUsername', | ||
UnassignUsername = 'UnassignUsername', | ||
SetAccountMetadata = 'SetAccountMetadata', | ||
JoinGroup = 'JoinGroup', | ||
LeaveGroup = 'LeaveGroup', | ||
AddGroupMember = 'AddGroupMember', | ||
RemoveGroupMember = 'RemoveGroupMember', | ||
} | ||
|
||
/** | ||
* An operation approval request. | ||
*/ | ||
export type OperationApprovalRequest = { | ||
nonce: string; | ||
deadline: string; | ||
operation: OperationType; | ||
validator: string; | ||
account: string; | ||
}; |
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
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
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,40 @@ | ||
import { chains } from '@lens-chain/sdk/viem'; | ||
import { evmAddress } from '@lens-protocol/types'; | ||
import { privateKeyToAccount } from 'viem/accounts'; | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { OperationType } from '../authorization'; | ||
import { OperationApprovalSigner } from './authorization'; | ||
|
||
const privateKey = '0xa7d25f98c7996df6418d5205d03386b254451d45de060dcd4c7f486d9c12061e'; | ||
const appAddress = evmAddress('0x3a24d26AdEBA0d6330F207d0ca699cBE5fFbE553'); | ||
const accountAddress = '0xbca85dda68cC21B98F6a416c28F9de94C4cBdcB9'; | ||
const lensPrimitive = '0x07753ab956B70498196772E8421379DB12de54eb'; | ||
|
||
describe( | ||
`Given an instance of the '${OperationApprovalSigner.name}' for viem`, | ||
{ timeout: 10000 }, | ||
() => { | ||
describe('When signing an OperationApprovalRequest', () => { | ||
it('Then it should return the expected signature', async () => { | ||
const approver = new OperationApprovalSigner({ | ||
app: appAddress, | ||
chain: chains.testnet, | ||
signer: privateKeyToAccount(privateKey), | ||
}); | ||
|
||
const signature = await approver.signOperationApproval({ | ||
nonce: '42', | ||
deadline: '1630000000', | ||
operation: OperationType.Post, | ||
validator: lensPrimitive, | ||
account: accountAddress, | ||
}); | ||
|
||
expect(signature).toEqual( | ||
'0xc0dc1300e351b79ba63b17581c5d5dd914ed2d6ddcd7cd4476b3930c199fd75807adc8c989db3da1af0ceb66689d0d3954f7180a81cfcb3e77af1bdcb6508d111c', | ||
); | ||
}); | ||
}); | ||
}, | ||
); |
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,57 @@ | ||
import type { chains } from '@lens-chain/sdk/viem'; | ||
|
||
import { type EvmAddress, evmAddress } from '@lens-protocol/types'; | ||
import { type TypedDataDefinition, checksumAddress } from 'viem'; | ||
import type { OperationApprovalRequest } from '../authorization'; | ||
|
||
export type TypedDataSigner = { | ||
signTypedData(args: TypedDataDefinition): Promise<string>; | ||
}; | ||
|
||
export type LocalOperationApprovalSignerContext = { | ||
app: EvmAddress; | ||
signer: TypedDataSigner; | ||
chain: chains.LensChain; | ||
}; | ||
|
||
export const DOMAIN_NAME = 'Lens Source'; | ||
export const DOMAIN_VERSION = '1'; | ||
|
||
/** | ||
* | ||
*/ | ||
export class OperationApprovalSigner { | ||
constructor(private readonly context: LocalOperationApprovalSignerContext) {} | ||
|
||
async signOperationApproval(data: OperationApprovalRequest): Promise<string> { | ||
return this.context.signer.signTypedData(this.createTypedDataDefinition(data)); | ||
} | ||
|
||
private createTypedDataDefinition(data: OperationApprovalRequest): TypedDataDefinition { | ||
return { | ||
domain: { | ||
name: DOMAIN_NAME, | ||
version: DOMAIN_VERSION, | ||
chainId: this.context.chain.id, | ||
verifyingContract: checksumAddress(this.context.app), | ||
}, | ||
types: { | ||
SourceStamp: [ | ||
{ name: 'source', type: 'address' }, | ||
{ name: 'originalMsgSender', type: 'address' }, | ||
{ name: 'validator', type: 'address' }, | ||
{ name: 'nonce', type: 'uint256' }, | ||
{ name: 'deadline', type: 'uint256' }, | ||
], | ||
}, | ||
primaryType: 'SourceStamp', | ||
message: { | ||
source: checksumAddress(this.context.app), | ||
originalMsgSender: checksumAddress(evmAddress(data.account)), | ||
validator: checksumAddress(evmAddress(data.validator)), | ||
nonce: data.nonce, | ||
deadline: data.deadline, | ||
}, | ||
}; | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './authorization'; | ||
export * from './signer'; |
Oops, something went wrong.