-
Notifications
You must be signed in to change notification settings - Fork 23
feat: Endorsements Service #553
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
Merged
Merged
Changes from 34 commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
6be6998
Baseline super-good-enough cert
Kubuxu 374acc3
stub decodeEndorsements
wjmelements 5a4ec65
preferEndorsements
wjmelements 8ce3807
start decoding endorsements
Kubuxu ea1c8f8
notAfter as timesetamp
wjmelements b2538d1
encodeEndorsements
wjmelements 174a7e8
mv cert.ts to utils
wjmelements 0959768
utils export from cert.ts
wjmelements 8fdb6bf
fix test
wjmelements 67af7c0
test that createContexts prefers endorsements
wjmelements a34808c
encode/decode certs with domain separator
wjmelements 953f4c8
add cert test
wjmelements c4ea7c7
Merge remote-tracking branch 'origin/master' into feat/super-good-eno…
wjmelements e5236ad
add tools/endorse-sp.js
wjmelements ebebd81
increase timeout for CI
wjmelements 344d9d2
make the decodeEndorsements a bit simpler
Kubuxu c81971a
add more tests
Kubuxu 7c784b7
Update packages/synapse-core/src/utils/cert.ts
wjmelements cdcf6d4
rm sinon
wjmelements 7c58dd8
Update packages/synapse-core/src/utils/cert.ts
wjmelements 6874d4a
cleanup
wjmelements 77a4524
Update packages/synapse-core/src/utils/cert.ts
wjmelements 1318554
Merge remote-tracking branch 'origin/master' into feat/endorsement-ce…
wjmelements f1a66ef
disallow undefined chainId in decodeEndorsement
wjmelements 72047d7
accept any of the preferred endorsements
wjmelements 57c44a7
Merge remote-tracking branch 'origin/master' into feat/endorsement-ce…
wjmelements 9f55a11
remove certs
wjmelements 1aace20
wagmi
wjmelements b236a21
constants
wjmelements 8020db0
EndorsementsService
wjmelements 3e91700
plumbing
wjmelements c50a632
import type
wjmelements 422f105
add endorsements mock and restore test
wjmelements 2a96464
add endorsements script to examples/cli
wjmelements c76237c
delete keystore when init with pk
wjmelements a7718ff
Revert "delete keystore when init with pk"
wjmelements 9410618
fix lint
wjmelements 1734d72
isCancel
wjmelements 59c313b
serviceURL
wjmelements 7c10429
Merge remote-tracking branch 'origin/master' into feat/endorsement-set
wjmelements a8696ba
lint
wjmelements 6d429c2
decodePDPCapabilities is not async
wjmelements 8628eff
Merge remote-tracking branch 'origin/master' into feat/endorsement-set
wjmelements dacf3f8
Revert "chore: re-add `getMaxProvingPeriod` and `challengeWindow` fun…
wjmelements 2423079
getContract
wjmelements cb582d8
test fallback on ping failure
wjmelements File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,169 @@ | ||
| import { | ||
| confirm, | ||
| intro, | ||
| log, | ||
| outro, | ||
| select, | ||
| spinner, | ||
| text, | ||
| } from '@clack/prompts' | ||
| import { getChain } from '@filoz/synapse-core/chains' | ||
| import { type Command, command } from 'cleye' | ||
| import type { Address, Hash } from 'viem' | ||
| import { readContract, simulateContract, writeContract } from 'viem/actions' | ||
| import { privateKeyClient } from '../client.ts' | ||
| import { globalFlags } from '../flags.ts' | ||
|
|
||
| class EndorsementsContract { | ||
| constructor(client) { | ||
| this.client = client | ||
|
|
||
| const chain = getChain(client.chain.id) | ||
| this.contract = chain.contracts.endorsements | ||
| } | ||
|
|
||
| async getProviderIds(): number[] { | ||
| const providerIds = await readContract(this.client, { | ||
| ...this.contract, | ||
| functionName: 'getProviderIds', | ||
| }) | ||
| return providerIds.map(Number) | ||
| } | ||
|
|
||
| async removeProviderId(providerId: number): Hash { | ||
| const { request } = await simulateContract(this.client, { | ||
| ...this.contract, | ||
| account: this.client.account, | ||
| functionName: 'removeProviderId', | ||
| args: [providerId], | ||
| }) | ||
| return await writeContract(this.client, request) | ||
| } | ||
|
|
||
| async addProviderId(providerId: number): Hash { | ||
| const { request } = await simulateContract(this.client, { | ||
| ...this.contract, | ||
| account: this.client.account, | ||
| functionName: 'addProviderId', | ||
| args: [providerId], | ||
| }) | ||
| return await writeContract(this.client, request) | ||
| } | ||
|
|
||
| async owner(): Address { | ||
| return await readContract(this.client, { | ||
| ...this.contract, | ||
| functionName: 'owner', | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| export const endorse: Command = command( | ||
| { | ||
| name: 'endorse', | ||
| description: 'Endorse Service Provider', | ||
| alias: 'e', | ||
| flags: { | ||
| ...globalFlags, | ||
| }, | ||
| }, | ||
| async (argv) => { | ||
| intro('Endorsements') | ||
| log.info('Loading account') | ||
| const { client } = privateKeyClient(argv.flags.chain) | ||
|
|
||
| const endorsements = new EndorsementsContract(client) | ||
|
|
||
| while (true) { | ||
| const [owner, endorsed] = await Promise.all([ | ||
| endorsements.owner(), | ||
| endorsements.getProviderIds(), | ||
| ]) | ||
| const lines = [ | ||
| `Current User: ${client.account.address}`, | ||
| `Owner: ${owner}`, | ||
| `Endorsements: ${endorsed.length}`, | ||
| ...endorsed.map((providerId) => `- ${providerId}`), | ||
| ] | ||
| log.info(lines.join('\n')) | ||
|
|
||
| const isOwner = client.account.address === owner | ||
|
|
||
| const action = await select({ | ||
| message: 'Select an action', | ||
| options: [ | ||
| { value: 'refresh' }, | ||
| { value: 'addProvider', disabled: !isOwner }, | ||
| { value: 'removeProvider', disabled: !isOwner }, | ||
| { value: 'transferOwnership', disabled: !isOwner }, | ||
| { value: 'exit' }, | ||
| ], | ||
| }) | ||
| switch (action) { | ||
| case 'refresh': { | ||
| continue | ||
| } | ||
| case 'exit': { | ||
| outro('bye!') | ||
| process.exit(0) | ||
| break | ||
| } | ||
| case 'removeProvider': { | ||
| const providerId = await select({ | ||
| message: '', | ||
| options: [ | ||
| { value: 'go back' }, | ||
| ...endorsed.map((providerId) => { | ||
| return { | ||
| value: String(providerId), | ||
| } | ||
| }), | ||
| ], | ||
| }) | ||
| if (providerId !== 'go back') { | ||
wjmelements marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (await confirm({ message: `Remove provider ${providerId}?` })) { | ||
wjmelements marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const txSpin = spinner() | ||
| txSpin.start(`Submitting transaction`) | ||
| try { | ||
| const txHash = await endorsements.removeProviderId( | ||
| Number(providerId) | ||
| ) | ||
| txSpin.stop(`Transaction submitted: ${txHash}`) | ||
| } catch (error) { | ||
| txSpin.stop(`Failed to remove ${providerId}: ${error.message}`) | ||
| } | ||
| } | ||
| } | ||
| break | ||
| } | ||
| case 'addProvider': { | ||
| const providerId = await text({ | ||
| message: '', | ||
| validate(value) { | ||
| const number = Number(value) | ||
| if (!Number.isInteger(number) || number <= 0) { | ||
| return 'providerId should be a positive integer' | ||
| } | ||
| }, | ||
| }) | ||
| if (await confirm({ message: `Add provider ${providerId}?` })) { | ||
wjmelements marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const txSpin = spinner() | ||
| txSpin.start(`Submitting transaction`) | ||
| try { | ||
| const txHash = await endorsements.addProviderId( | ||
| Number(providerId) | ||
| ) | ||
| txSpin.stop(`Transaction submitted: ${txHash}`) | ||
| } catch (error) { | ||
| txSpin.stop(`Failed to remove ${providerId}: ${error.message}`) | ||
| } | ||
| } | ||
| break | ||
| } | ||
| default: { | ||
| log.error('Unsupported option') | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ) | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.