How to sign Ethereum EIP-1559 transactions using AWS KMS

How to sign Ethereum EIP-1559 transactions using AWS KMS

AWS의 KMS로 이더리움 트랜잭션 사인이 가능하다는 것을 얼마전에 알았다.

마침 조사를 해보니 좋은 예시들이 몇 개 있어서 공유하고자 한다.

첫 번째로 KMS에서 ECDSA 키를 생성해주어야 한다.

다음과 같은 형태로 만들어주면 된다.

이제부터 코드로 보면 된다. 다음과 같이 kms 서비스를 생성한다.

func main() {
	accessKey := "<access-key>"
	secretKey := "<secret-key>"

	sess, err := session.NewSession(&aws.Config{
		Region: aws.String("<region>"),
		Credentials: credentials.NewStaticCredentialsFromCreds(credentials.Value{
			AccessKeyID:     accessKey,
			SecretAccessKey: secretKey,
		}),
	})
	if err != nil {
		panic(err)
	}

	svc := kms.New(sess)
}

바로 KMS에 생성한 리소스에 접근해보자.

key, err := svc.GetPublicKey(&kms.GetPublicKeyInput{
  KeyId: aws.String("<key-id>"),
})
if err != nil {
  panic(err)
}

잘 안된다면 변수 값을 확인해보거나, 리소스 권한 등을 의심해보자.

다음으로 encoding/asn1 라이브러리를 사용해서 퍼블릭 키를 추출해오자.

import "encoding/asn1"

type asn1EcPublicKey struct {
	EcPublicKeyInfo asn1EcPublicKeyInfo
	PublicKey       asn1.BitString
}

type asn1EcPublicKeyInfo struct {
	Algorithm  asn1.ObjectIdentifier
	Parameters asn1.ObjectIdentifier
}


func main() {
  
  // ...

  key, err := svc.GetPublicKey(&kms.GetPublicKeyInput{
		KeyId: aws.String("<key-id>"),
	})
	if err != nil {
		panic(err)
	}

	var asn1Key asn1EcPublicKey
	if _, err := asn1.Unmarshal(key.PublicKey, &asn1Key); err != nil {
		panic(err)
	}
}

로그를 찍어보면 알겠지만 우리가 아는 퍼블릭 키와 많이 다르다.

go-ethereumcrypto 라이브러리를 사용해보자.

이제 이 public key를 통해 지갑 주소를 얻어낼 수 있다.

import "github.com/ethereum/go-ethereum/crypto"

var asn1Key asn1EcPublicKey
if _, err := asn1.Unmarshal(key.PublicKey, &asn1Key); err != nil {
  panic(err)
}

log.Println(string(asn1Key.PublicKey.Bytes))

pubKey, err := crypto.UnmarshalPubkey(asn1Key.PublicKey.Bytes)
if err != nil {
  panic(err)
}

log.Println(pubKey)

keyAddr := crypto.PubkeyToAddress(*pubKey)
log.Println(keyAddr.Hex())

Last updated