Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
gagliardetto committed Nov 3, 2024
1 parent 29f1ed1 commit 0aaba30
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 59 deletions.
22 changes: 17 additions & 5 deletions keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,32 @@ func TestPublicKeyFromBytes(t *testing.T) {
tests := []struct {
name string
inHex string
isErr bool
expected PublicKey
}{
{
"empty",
"",
MustPublicKeyFromBase58("11111111111111111111111111111111"),
true,
PublicKey{},
},
{
"smaller than required",
"010203040506",
MustPublicKeyFromBase58("4wBqpZM9k69W87zdYXT2bRtLViWqTiJV3i2Kn9q7S6j"),
true,
PublicKey{},
},
{
"equal to 32 bytes",
"0102030405060102030405060102030405060102030405060102030405060101",
false,
MustPublicKeyFromBase58("4wBqpZM9msxygzsdeLPq6Zw3LoiAxJk3GjtKPpqkcsi"),
},
{
"longer than required",
"0102030405060102030405060102030405060102030405060102030405060101FFFFFFFFFF",
MustPublicKeyFromBase58("4wBqpZM9msxygzsdeLPq6Zw3LoiAxJk3GjtKPpqkcsi"),
true,
PublicKey{},
},
}

Expand All @@ -61,8 +66,15 @@ func TestPublicKeyFromBytes(t *testing.T) {
bytes, err := hex.DecodeString(test.inHex)
require.NoError(t, err)

actual := PublicKeyFromBytes(bytes)
assert.Equal(t, test.expected, actual, "%s != %s", test.expected, actual)
if test.isErr {
require.Panics(t, func() {
actual := PublicKeyFromBytes(bytes)
require.Equal(t, zeroPublicKey, actual)
})
} else {
actual := PublicKeyFromBytes(bytes)
require.Equal(t, test.expected, actual)
}
})
}
}
Expand Down
54 changes: 0 additions & 54 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,64 +133,10 @@ type CompiledInstruction struct {
// List of ordered indices into the message.accountKeys array indicating which accounts to pass to the program.
// NOTE: it is actually a []uint8, but using a uint16 because []uint8 is treated as a []byte everywhere,
// and that can be an issue.
AccountsWithKey []PublicKey `json:"omitempty"`
//
Accounts []uint16 `json:"accounts"`

// The program input data encoded in a base-58 string.
Data Base58 `json:"data"`

//
StackHeight int64 `json:"stackHeight"`
}

type compiledInstruction struct {
// Index into the message.accountKeys array indicating the program account that executes this instruction.
// NOTE: it is actually a uint8, but using a uint16 because uint8 is treated as a byte everywhere,
// and that can be an issue.
ProgramIDIndex uint16 `json:"programIdIndex"`

// List of ordered indices into the message.accountKeys array indicating which accounts to pass to the program.
// NOTE: it is actually a []uint8, but using a uint16 because []uint8 is treated as a []byte everywhere,
// and that can be an issue.
Accounts []interface{} `json:"accounts"`

// The program input data encoded in a base-58 string.
Data Base58 `json:"data"`

//
StackHeight int64 `json:"stackHeight"`
}

func (ci *CompiledInstruction) UnmarshalJSON(data []byte) error {
in := compiledInstruction{}
err := json.Unmarshal(data, &in)
if err != nil {
return err
}
//
ci.ProgramIDIndex = in.ProgramIDIndex
ci.Data = in.Data
ci.StackHeight = in.StackHeight
ci.Accounts = make([]uint16, 0)
if len(in.Accounts) == 0 {
return nil
}
_, ok := in.Accounts[0].(string)
if ok {
accountsWithKey := make([]PublicKey, len(in.Accounts))
for i, item := range in.Accounts {
accountsWithKey[i] = MustPublicKeyFromBase58(item.(string))
}
ci.AccountsWithKey = accountsWithKey
} else {
AccountsWithIndex := make([]uint16, len(in.Accounts))
for i, item := range in.Accounts {
AccountsWithIndex[i] = uint16(item.(float64))
}
ci.Accounts = AccountsWithIndex
}
return nil
}

func (ci *CompiledInstruction) ResolveInstructionAccounts(message *Message) ([]*AccountMeta, error) {
Expand Down

0 comments on commit 0aaba30

Please sign in to comment.