-
Notifications
You must be signed in to change notification settings - Fork 0
[CLD-1916]: feat(solana): port over mcms pda loader #11
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 all commits
Commits
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,57 @@ | ||
| package solana | ||
|
|
||
| import ( | ||
| "github.com/gagliardetto/solana-go" | ||
| ) | ||
|
|
||
| const ( | ||
| pdaPrefixMultisigSigner = "multisig_signer" | ||
| pdaPrefixMultisigConfig = "multisig_config" | ||
| pdaPrefixRootMetadata = "root_metadata" | ||
| pdaPrefixExpiringRootAndOpCount = "expiring_root_and_op_count" | ||
| pdaPrefixTimelockConfig = "timelock_config" | ||
| pdaPrefixTimelockSigner = "timelock_signer" | ||
| ) | ||
|
|
||
| // GetMCMSignerPDA returns the PDA for the MCMS signer | ||
| func GetMCMSignerPDA(programID solana.PublicKey, seed PDASeed) solana.PublicKey { | ||
| seeds := [][]byte{[]byte(pdaPrefixMultisigSigner), seed[:]} | ||
| return getPDA(programID, seeds) | ||
| } | ||
|
|
||
| // GetMCMConfigPDA returns the PDA for the MCMS config | ||
| func GetMCMConfigPDA(programID solana.PublicKey, seed PDASeed) solana.PublicKey { | ||
| seeds := [][]byte{[]byte(pdaPrefixMultisigConfig), seed[:]} | ||
| return getPDA(programID, seeds) | ||
| } | ||
|
|
||
| // GetMCMRootMetadataPDA returns the PDA for the MCMS root metadata | ||
| func GetMCMRootMetadataPDA(programID solana.PublicKey, seed PDASeed) solana.PublicKey { | ||
| seeds := [][]byte{[]byte(pdaPrefixRootMetadata), seed[:]} | ||
| return getPDA(programID, seeds) | ||
| } | ||
|
|
||
| // GetMCMExpiringRootAndOpCountPDA returns the PDA for the MCMS expiring root and op count | ||
| func GetMCMExpiringRootAndOpCountPDA(programID solana.PublicKey, seed PDASeed) solana.PublicKey { | ||
| seeds := [][]byte{[]byte(pdaPrefixExpiringRootAndOpCount), seed[:]} | ||
| return getPDA(programID, seeds) | ||
| } | ||
|
|
||
| // GetTimelockConfigPDA returns the PDA for the Timelock config | ||
| func GetTimelockConfigPDA(programID solana.PublicKey, seed PDASeed) solana.PublicKey { | ||
| seeds := [][]byte{[]byte(pdaPrefixTimelockConfig), seed[:]} | ||
| return getPDA(programID, seeds) | ||
| } | ||
|
|
||
| // GetTimelockSignerPDA returns the PDA for the Timelock signer | ||
| func GetTimelockSignerPDA(programID solana.PublicKey, seed PDASeed) solana.PublicKey { | ||
| seeds := [][]byte{[]byte(pdaPrefixTimelockSigner), seed[:]} | ||
| return getPDA(programID, seeds) | ||
| } | ||
|
|
||
| // getPDA returns the PDA for the given program ID and seeds | ||
| func getPDA(programID solana.PublicKey, seeds [][]byte) solana.PublicKey { | ||
| // todo(ggoh): add error handling | ||
| pda, _, _ := solana.FindProgramAddress(seeds, programID) | ||
| return pda | ||
| } | ||
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,76 @@ | ||
| package solana | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/gagliardetto/solana-go" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestMCMSPDA(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| programID := solana.MustPublicKeyFromBase58("11111111111111111111111111111111") | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| prefix string | ||
| fn func(programID solana.PublicKey, seed PDASeed) solana.PublicKey | ||
| }{ | ||
| {name: "GetMCMSignerPDA", prefix: pdaPrefixMultisigSigner, fn: GetMCMSignerPDA}, | ||
| {name: "GetMCMConfigPDA", prefix: pdaPrefixMultisigConfig, fn: GetMCMConfigPDA}, | ||
| {name: "GetMCMRootMetadataPDA", prefix: pdaPrefixRootMetadata, fn: GetMCMRootMetadataPDA}, | ||
| {name: "GetMCMExpiringRootAndOpCountPDA", prefix: pdaPrefixExpiringRootAndOpCount, fn: GetMCMExpiringRootAndOpCountPDA}, | ||
| {name: "GetTimelockConfigPDA", prefix: pdaPrefixTimelockConfig, fn: GetTimelockConfigPDA}, | ||
| {name: "GetTimelockSignerPDA", prefix: pdaPrefixTimelockSigner, fn: GetTimelockSignerPDA}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| seed := testPDASeed(t) | ||
| seeds := [][]byte{[]byte(tt.prefix), seed[:]} | ||
| want := mustFindPDA(t, seeds, programID) | ||
| got := tt.fn(programID, seed) | ||
| require.Equal(t, want, got) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestPDAGeneratorsUseDistinctSeeds(t *testing.T) { | ||
| t.Parallel() | ||
| programID := solana.MustPublicKeyFromBase58("11111111111111111111111111111111") | ||
| id := testPDASeed(t) | ||
|
|
||
| signer := GetMCMSignerPDA(programID, id) | ||
| cfg := GetMCMConfigPDA(programID, id) | ||
| meta := GetMCMRootMetadataPDA(programID, id) | ||
| exp := GetMCMExpiringRootAndOpCountPDA(programID, id) | ||
| tlCfg := GetTimelockConfigPDA(programID, id) | ||
| tlSigner := GetTimelockSignerPDA(programID, id) | ||
|
|
||
| keys := []solana.PublicKey{signer, cfg, meta, exp, tlCfg, tlSigner} | ||
| for i := range keys { | ||
| for j := i + 1; j < len(keys); j++ { | ||
| require.NotEqualf(t, keys[i], keys[j], "PDA at %d equals PDA at %d", i, j) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func mustFindPDA(t *testing.T, seeds [][]byte, programID solana.PublicKey) solana.PublicKey { | ||
| t.Helper() | ||
| pda, _, err := solana.FindProgramAddress(seeds, programID) | ||
| require.NoError(t, err) | ||
|
|
||
| return pda | ||
| } | ||
|
|
||
| func testPDASeed(t *testing.T) PDASeed { | ||
| t.Helper() | ||
| var s PDASeed | ||
| for i := range s { | ||
| s[i] = byte(i + 1) | ||
| } | ||
|
|
||
| return s | ||
| } |
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.