Primrose Docs
  • Home
    • ⛓️Blockchain
      • Avalanche
        • What is AVAX?
      • Ethereum
        • Ethereum Cancun Upgrade Explained(draft)
        • go-ethereum: gas estimate
        • Blockchain Transaction Lifecycle
        • Mempool
        • Gas optimization in Solidity, Ethereum
      • Solidity DeepDive
        • Meta transaction
        • solidity: patterns
        • UUPS vs Transparent
        • Solidity Interface
        • Smart contract storage
        • ERC-2981 Contract
        • Solidity modifier
        • Solidity delete keyword
        • How To Make NFTs with On-Chain Metadata - Hardhat and JavaScript
        • How to Build "Buy Me a Coffee" DeFi dapp
        • How to Develop an NFT Smart Contract (ERC 721) with Alchemy
        • Upgradeable Contract
        • Smart Contract Verification
      • Common
        • Eigenlayer
        • MultiSig(draft)
        • Chain-Based Proof-of- Stake, BFT-Style Proof-of-Stake
        • Byzantine Fault Tolerance
        • Zero-knowledge
        • Hierarchical Deterministic Wallet
        • Maker DAO
        • Defi
        • Uniswap
        • IBC
        • Cosmos
        • Gossip Protocol
        • Tendermint
        • UTXO vs Account
        • Blockchain Layer
        • Consensus Algorithm
        • How does mining work?
        • Immutable Ledger
        • SHA256 Hash
        • Filecoin
        • IPFS - InterPlanetary File System
        • IPFS와 파일코인
        • Livepeer
        • Layer 0
      • Bitcoin
        • BIP for HD Wallet
        • P2WPKH
        • Segwit vs Native Segwit
    • 📖Languages
      • Javascript/Typescript
        • Hoisting
        • This value in Javascript
        • Execution Context
        • About Javscript
        • tsconfig.json
        • Nest js Provider
        • 'return await promise' vs 'return promise'
      • Python
        • Pythonic
        • Python: Iterable, Iterator
        • Uvicorn & Gunicorn
        • WSGI, ASGI
        • Python docstring
        • Decorator in Python
        • Namespace in Python
        • Python Method
      • Go
        • GORM+MySQL Connection Pool
        • Context in golang
        • How to sign Ethereum EIP-1559 transactions using AWS KMS
        • Mongo DB in golang(draft)
        • Golang HTTP Package
        • Panic
        • Golang new/make
        • golang container package
        • errgroup in golang
        • Generic Programming in Golang
        • Goroutine(draft)
    • 📝Database
      • MongoDB in golang
      • Nested loop join, Hash join
      • DB Query plan
      • Index
      • Optimistic Lock Pessimistic Lock
    • 💻Computer Science
      • N+1 query in go
      • Web server 를 구성할 때 Thread, Process 개수를 어떻게 정할 것인가?
      • CAP
      • Socket programming
      • DNS, IP
      • URL, URI
      • TLS과 SSL
      • Caching(draft)
      • Building Microservices: Micro Service 5 Deploy Principle
      • Red Black Tree
      • AOP
      • Distributed Lock
      • VPC
      • Docker
      • All about Session and JWT
      • Closure
      • Singleton Pattern
      • TCP 3 way handshake & 4 way handshake
      • Race Condition
      • Process Address Space 
      • Call by value, Call by reference, Call by assignment
      • Zookeeper, ETCD
      • URL Shortening
      • Raft consensus
      • Sharding, Partitioning
    • 📒ETC
      • K8S SIGTERM
      • SQS
      • Git Branch Strategy: Ship / Show / Ask
      • Kafka
      • Redis Data Types
      • CI/CD
      • How does Google design APIs?
      • Minishell (42 cursus)
      • Coroutine & Subroutine
      • Redis
Powered by GitBook
On this page
  1. Home
  2. Languages
  3. Go

How to sign Ethereum EIP-1559 transactions using AWS KMS

PreviousContext in golangNextMongo DB in golang(draft)

Last updated 1 year ago

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-ethereum의 crypto 라이브러리를 사용해보자.

이제 이 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())
📖
ECDSA Key 생성