Skip to content
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
6 changes: 3 additions & 3 deletions ocp/worker/geyser/external_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"github.com/code-payments/ocp-server/solana"
compute_budget "github.com/code-payments/ocp-server/solana/computebudget"
"github.com/code-payments/ocp-server/solana/currencycreator"
"github.com/code-payments/ocp-server/solana/memo"
"github.com/code-payments/ocp-server/solana/memov2"
"github.com/code-payments/ocp-server/solana/vm"
)

Expand Down Expand Up @@ -117,9 +117,9 @@ func initiateExternalDepositIntoVm(ctx context.Context, data ocp_data.Provider,
// Do not close the VM deposit ATA, since the real-time handler won't pick it up
txn := solana.NewLegacyTransaction(
vmConfig.Authority.PublicKey().ToBytes(),
memo.Instruction(codeVmDepositMemoValue),
memov2.Instruction(codeVmDepositMemoValue),
compute_budget.SetComputeUnitPrice(10_000),
compute_budget.SetComputeUnitLimit(40_000),
compute_budget.SetComputeUnitLimit(45_000),
vm.NewDepositFromPdaInstruction(
&vm.DepositFromPdaInstructionAccounts{
VmAuthority: vmConfig.Authority.PublicKey().ToBytes(),
Expand Down
41 changes: 41 additions & 0 deletions solana/memov2/program.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package memov2

import (
"bytes"
"crypto/ed25519"

"github.com/pkg/errors"

"github.com/code-payments/ocp-server/solana"
)

// ProgramKey is the address of the v2 memo program.
//
// Current key: MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr
var ProgramKey = ed25519.PublicKey{5, 74, 83, 90, 153, 41, 33, 6, 77, 36, 232, 113, 96, 218, 56, 124, 124, 53, 181, 221, 188, 146, 187, 129, 228, 31, 168, 64, 65, 5, 68, 141}

// Reference: https://github.com/solana-program/memo
func Instruction(data string) solana.Instruction {
return solana.NewInstruction(
ProgramKey,
[]byte(data),
)
}

type DecompiledMemo struct {
Data []byte
}

func DecompileMemo(m solana.Message, index int) (*DecompiledMemo, error) {
if index >= len(m.Instructions) {
return nil, errors.Errorf("instruction doesn't exist at %d", index)
}

i := m.Instructions[index]

if !bytes.Equal(m.Accounts[i.ProgramIndex], ProgramKey) {
return nil, solana.ErrIncorrectProgram
}

return &DecompiledMemo{Data: i.Data}, nil
}
38 changes: 38 additions & 0 deletions solana/memov2/program_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package memov2

import (
"crypto/ed25519"
"testing"

"github.com/code-payments/ocp-server/solana"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestInstruction(t *testing.T) {
i := Instruction("hello, world!")
assert.Equal(t, ProgramKey, i.Program)
assert.Empty(t, i.Accounts)
assert.Equal(t, "hello, world!", string(i.Data))
}

func TestDecompile(t *testing.T) {
tx := solana.NewLegacyTransaction(
make([]byte, 32),
Instruction("hello, world"),
)

i, err := DecompileMemo(tx.Message, 0)
assert.NoError(t, err)
assert.Equal(t, "hello, world", string(i.Data))

_, err = DecompileMemo(tx.Message, 1)
assert.Error(t, err)
assert.Contains(t, err.Error(), "instruction doesn't exist")

tx.Message.Accounts[1], _, err = ed25519.GenerateKey(nil)
require.NoError(t, err)
_, err = DecompileMemo(tx.Message, 0)
assert.Error(t, err)
assert.Equal(t, solana.ErrIncorrectProgram, err)
}
Loading