crypto_test.go 7.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
package batchsubmitter_test

import (
	"bytes"
	"errors"
	"strings"
	"testing"

	batchsubmitter "github.com/ethereum-optimism/go/batch-submitter"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/crypto"
	"github.com/stretchr/testify/require"
)

var (
	validMnemonic = strings.Join([]string{
		"abandon", "abandon", "abandon", "abandon",
		"abandon", "abandon", "abandon", "abandon",
		"abandon", "abandon", "abandon", "about",
	}, " ")

	validHDPath = "m/44'/60'/0'/128"

	// validPrivKeyStr is the private key string for the child derived at
	// validHDPath from validMnemonic.
	validPrivKeyStr = "69d3a0e79bf039ca788924cb98b6b60c5f5aaa5e770aef09b4b15fdb59944d02"

	// validPrivKeyBytes is the raw private key bytes for the child derived
	// at validHDPath from validMnemonic.
	validPrivKeyBytes = []byte{
		0x69, 0xd3, 0xa0, 0xe7, 0x9b, 0xf0, 0x39, 0xca,
		0x78, 0x89, 0x24, 0xcb, 0x98, 0xb6, 0xb6, 0x0c,
		0x5f, 0x5a, 0xaa, 0x5e, 0x77, 0x0a, 0xef, 0x09,
		0xb4, 0xb1, 0x5f, 0xdb, 0x59, 0x94, 0x4d, 0x02,
	}

	// invalidMnemonic has an invalid checksum.
	invalidMnemonic = strings.Join([]string{
		"abandon", "abandon", "abandon", "abandon",
		"abandon", "abandon", "abandon", "abandon",
		"abandon", "abandon", "abandon", "abandon",
	}, " ")
)

// TestParseAddress asserts that ParseAddress correctly parses 40-characater
// hexidecimal strings with optional 0x prefix into valid 20-byte addresses.
func TestParseAddress(t *testing.T) {
	tests := []struct {
		name    string
		addr    string
		expErr  error
		expAddr common.Address
	}{
		{
			name:   "empty address",
			addr:   "",
			expErr: errors.New("invalid address: "),
		},
		{
			name:   "only 0x",
			addr:   "0x",
			expErr: errors.New("invalid address: 0x"),
		},
		{
			name:   "non hex character",
			addr:   "0xaaaaaazaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
			expErr: errors.New("invalid address: 0xaaaaaazaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
		},
		{
			name:    "valid address",
			addr:    "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
			expErr:  nil,
			expAddr: common.BytesToAddress(bytes.Repeat([]byte{170}, 20)),
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			addr, err := batchsubmitter.ParseAddress(test.addr)
			require.Equal(t, err, test.expErr)
			if test.expErr != nil {
				return
			}
			require.Equal(t, addr, test.expAddr)
		})
	}
}

// TestDerivePrivateKey asserts that DerivePrivateKey properly parses a BIP39
// mnemonic and BIP32 HD path, and derives the corresponding private key.
func TestDerivePrivateKey(t *testing.T) {
	tests := []struct {
		name       string
		mnemonic   string
		hdPath     string
		expErr     error
		expPrivKey []byte
	}{
		{
			name:     "invalid mnemonic",
			mnemonic: invalidMnemonic,
			hdPath:   validHDPath,
			expErr:   errors.New("Checksum incorrect"),
		},
		{
			name:     "valid mnemonic invalid hdpath",
			mnemonic: validMnemonic,
			hdPath:   "",
			expErr: errors.New("ambiguous path: use 'm/' prefix for absolute " +
				"paths, or no leading '/' for relative ones"),
		},
		{
			name:     "valid mnemonic invalid hdpath",
			mnemonic: validMnemonic,
			hdPath:   "m/",
			expErr:   errors.New("invalid component: "),
		},
		{
			name:     "valid mnemonic valid hdpath no components",
			mnemonic: validMnemonic,
			hdPath:   "m/0",
			expPrivKey: []byte{
				0xba, 0xa8, 0x9a, 0x8b, 0xdd, 0x61, 0xc5, 0xe2,
				0x2b, 0x9f, 0x10, 0x60, 0x1d, 0x87, 0x91, 0xc9,
				0xf8, 0xfc, 0x4b, 0x2f, 0xa6, 0xdf, 0x9d, 0x68,
				0xd3, 0x36, 0xf0, 0xeb, 0x03, 0xb0, 0x6e, 0xb6,
			},
		},
		{
			name:     "valid mnemonic valid hdpath full path",
			mnemonic: validMnemonic,
			hdPath:   validHDPath,
			expPrivKey: []byte{
				0x69, 0xd3, 0xa0, 0xe7, 0x9b, 0xf0, 0x39, 0xca,
				0x78, 0x89, 0x24, 0xcb, 0x98, 0xb6, 0xb6, 0x0c,
				0x5f, 0x5a, 0xaa, 0x5e, 0x77, 0x0a, 0xef, 0x09,
				0xb4, 0xb1, 0x5f, 0xdb, 0x59, 0x94, 0x4d, 0x02,
			},
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			privKey, err := batchsubmitter.DerivePrivateKey(test.mnemonic, test.hdPath)
			require.Equal(t, err, test.expErr)
			if test.expErr != nil {
				return
			}

			expPrivKey, err := crypto.ToECDSA(test.expPrivKey)
			require.Nil(t, err)
			require.Equal(t, privKey, expPrivKey)
		})
	}
}

// TestParsePrivateKeyStr asserts that ParsePrivateKey properly parses
// 64-character hexidecimal strings with optional 0x prefix into valid ECDSA
// private keys.
func TestParsePrivateKeyStr(t *testing.T) {
	tests := []struct {
		name       string
		privKeyStr string
		expErr     error
		expPrivKey []byte
	}{
		{
			name:       "empty privkey string",
			privKeyStr: "",
			expErr:     errors.New("invalid length, need 256 bits"),
		},
		{
			name:       "privkey string only 0x",
			privKeyStr: "0x",
			expErr:     errors.New("invalid length, need 256 bits"),
		},
		{
			name:       "non hex privkey string",
			privKeyStr: "0xaaaazaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
			expErr:     errors.New("invalid hex character 'z' in private key"),
		},
		{
			name:       "valid privkey string",
			privKeyStr: validPrivKeyStr,
			expPrivKey: validPrivKeyBytes,
		},
		{
			name:       "valid privkey string with 0x",
			privKeyStr: "0x" + validPrivKeyStr,
			expPrivKey: validPrivKeyBytes,
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			privKey, err := batchsubmitter.ParsePrivateKeyStr(test.privKeyStr)
			require.Equal(t, err, test.expErr)
			if test.expErr != nil {
				return
			}

			expPrivKey, err := crypto.ToECDSA(test.expPrivKey)
			require.Nil(t, err)
			require.Equal(t, privKey, expPrivKey)
		})
	}
}

// TestGetConfiguredPrivateKey asserts that GetConfiguredPrivateKey either:
//  1) Derives the correct private key assuming the BIP39 mnemonic and BIP32
//     derivation path are both present and the private key string is ommitted.
//  2) Parses the correct private key assuming only the private key string is
//     present, but the BIP39 mnemonic and BIP32 derivation path are ommitted.
func TestGetConfiguredPrivateKey(t *testing.T) {
	tests := []struct {
		name       string
		mnemonic   string
		hdPath     string
		privKeyStr string
		expErr     error
		expPrivKey []byte
	}{
		{
			name:       "valid mnemonic+hdpath",
			mnemonic:   validMnemonic,
			hdPath:     validHDPath,
			privKeyStr: "",
			expPrivKey: validPrivKeyBytes,
		},
		{
			name:       "valid privkey",
			mnemonic:   "",
			hdPath:     "",
			privKeyStr: validPrivKeyStr,
			expPrivKey: validPrivKeyBytes,
		},
		{
			name:       "valid privkey with 0x",
			mnemonic:   "",
			hdPath:     "",
			privKeyStr: "0x" + validPrivKeyStr,
			expPrivKey: validPrivKeyBytes,
		},
		{
			name:       "valid menmonic+hdpath and privkey",
			mnemonic:   validMnemonic,
			hdPath:     validHDPath,
			privKeyStr: validPrivKeyStr,
			expErr:     batchsubmitter.ErrCannotGetPrivateKey,
		},
		{
			name:       "neither menmonic+hdpath or privkey",
			mnemonic:   "",
			hdPath:     "",
			privKeyStr: "",
			expErr:     batchsubmitter.ErrCannotGetPrivateKey,
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			privKey, err := batchsubmitter.GetConfiguredPrivateKey(
				test.mnemonic, test.hdPath, test.privKeyStr,
			)
			require.Equal(t, err, test.expErr)
			if test.expErr != nil {
				return
			}

			expPrivKey, err := crypto.ToECDSA(test.expPrivKey)
			require.Nil(t, err)
			require.Equal(t, privKey, expPrivKey)
		})
	}
}