100% found this document useful (1 vote)
12 views

Full Download Beginning Ethereum Smart Contracts Programming: With Examples in Python, Solidity, and JavaScript, 2nd Edition Wei-Meng Lee PDF DOCX

The document promotes the ebook 'Beginning Ethereum Smart Contracts Programming' by Wei-Meng Lee, available for download on ebookmeta.com. It includes links to additional recommended digital products related to programming and blockchain. The ebook covers various programming languages and concepts related to Ethereum smart contracts and blockchain technology.

Uploaded by

asiligiswi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
12 views

Full Download Beginning Ethereum Smart Contracts Programming: With Examples in Python, Solidity, and JavaScript, 2nd Edition Wei-Meng Lee PDF DOCX

The document promotes the ebook 'Beginning Ethereum Smart Contracts Programming' by Wei-Meng Lee, available for download on ebookmeta.com. It includes links to additional recommended digital products related to programming and blockchain. The ebook covers various programming languages and concepts related to Ethereum smart contracts and blockchain technology.

Uploaded by

asiligiswi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Get the full ebook with Bonus Features for a Better Reading Experience on ebookmeta.

com

Beginning Ethereum Smart Contracts Programming:


With Examples in Python, Solidity, and JavaScript,
2nd Edition Wei-Meng Lee

https://ebookmeta.com/product/beginning-ethereum-smart-
contracts-programming-with-examples-in-python-solidity-and-
javascript-2nd-edition-wei-meng-lee/

OR CLICK HERE

DOWLOAD NOW

Download more ebook instantly today at https://ebookmeta.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Beginning Swift Programming 1st Edition Wei Meng Lee

https://ebookmeta.com/product/beginning-swift-programming-1st-edition-
wei-meng-lee/

ebookmeta.com

Building Games with Ethereum Smart Contracts: Intermediate


Projects for Solidity Developers Kedar Iyer

https://ebookmeta.com/product/building-games-with-ethereum-smart-
contracts-intermediate-projects-for-solidity-developers-kedar-iyer/

ebookmeta.com

Blockchain and Ethereum Smart Contract Solution


Development: Dapp Programming with Solidity 1st Edition
Weijia Zhang
https://ebookmeta.com/product/blockchain-and-ethereum-smart-contract-
solution-development-dapp-programming-with-solidity-1st-edition-
weijia-zhang/
ebookmeta.com

Fresh Produce Shipping: Damages and Compensation 1st


Edition Rex C. Tester

https://ebookmeta.com/product/fresh-produce-shipping-damages-and-
compensation-1st-edition-rex-c-tester/

ebookmeta.com
Emergency War Surgery Fifth Edition Borden Institute

https://ebookmeta.com/product/emergency-war-surgery-fifth-edition-
borden-institute/

ebookmeta.com

Private Associations in the Pontic Greek Cities 6th


Century BC 3rd Century AD Volume 35 Colloquia Antiqua 1st
Edition A.-I. Pazsint
https://ebookmeta.com/product/private-associations-in-the-pontic-
greek-cities-6th-century-bc-3rd-century-ad-volume-35-colloquia-
antiqua-1st-edition-a-i-pazsint/
ebookmeta.com

Wilkins Clinical Practice of the Dental Hygienist 14th


Edition Linda D Boyd Lisa F Mallonee

https://ebookmeta.com/product/wilkins-clinical-practice-of-the-dental-
hygienist-14th-edition-linda-d-boyd-lisa-f-mallonee/

ebookmeta.com

Flexible Electronics Volume 2 Thin film transistors 1st


Edition Vinod Kumar Khanna

https://ebookmeta.com/product/flexible-electronics-volume-2-thin-film-
transistors-1st-edition-vinod-kumar-khanna/

ebookmeta.com

Hot Dog Summer Hot H E A Summer 1st Edition Lola West

https://ebookmeta.com/product/hot-dog-summer-hot-h-e-a-summer-1st-
edition-lola-west/

ebookmeta.com
Machine Hallucinations Architecture and Artificial
Intelligence Architectural Design 1st Edition Neil Leach
(Editor)
https://ebookmeta.com/product/machine-hallucinations-architecture-and-
artificial-intelligence-architectural-design-1st-edition-neil-leach-
editor/
ebookmeta.com
Beginning Ethereum
Smart Contracts
Programming
With Examples in Python, Solidity,
and JavaScript
Second Edition

Wei-Meng Lee
Beginning Ethereum Smart Contracts Programming: With Examples in Python,
Solidity, and JavaScript

Wei-Meng Lee
Ang Mo Kio, Singapore

ISBN-13 (pbk): 978-1-4842-9270-9 ISBN-13 (electronic): 978-1-4842-9271-6


https://doi.org/10.1007/978-1-4842-9271-6
Copyright © 2023 by Wei-Meng Lee
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not
identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the
material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Joan Murray
Development Editor: Laura Berendson
Editorial Assistant: Gryffin Winkler
Copy Editor: Mary Behr
Cover image designed by eStudioCalamar
Distributed to the book trade worldwide by Springer Science+Business Media New York, 1 New York Plaza,
Suite 4600, New York, NY 10004-1562, USA. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@
springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole
member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc
is a Delaware corporation.
For information on translations, please e-mail booktranslations@springernature.com; for reprint,
paperback, or audio rights, please e-mail bookpermissions@springernature.com.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales
web page at www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to
readers on GitHub (github.com/apress). For more detailed information, please visit www.apress.com/
source-code.
Printed on acid-free paper
Table of Contents
About the Author����������������������������������������������������������������������������������������������������� xi

About the Technical Reviewer������������������������������������������������������������������������������� xiii


Acknowledgments���������������������������������������������������������������������������������������������������xv

Introduction�����������������������������������������������������������������������������������������������������������xvii

Chapter 1: Understanding the Science Behind Blockchain: Cryptography��������������� 1


What Is Cryptography?������������������������������������������������������������������������������������������������������������������ 1
Types of Cryptography������������������������������������������������������������������������������������������������������������� 2
Hash Functions����������������������������������������������������������������������������������������������������������������������������� 3
Uses of Hashing����������������������������������������������������������������������������������������������������������������������� 5
Implementing Hashing in Python��������������������������������������������������������������������������������������������� 6
Symmetric Cryptography�������������������������������������������������������������������������������������������������������������� 7
Generating the Shared Key in Python�������������������������������������������������������������������������������������� 8
Performing Symmetric Encryption������������������������������������������������������������������������������������������ 9
Performing Symmetric Decryption������������������������������������������������������������������������������������������ 9
Asymmetric Cryptography���������������������������������������������������������������������������������������������������������� 10
Generating and Saving the Public/Private Key Pairs������������������������������������������������������������� 12
Encrypting Using the Public Key�������������������������������������������������������������������������������������������� 14
Decrypting Using the Private Key������������������������������������������������������������������������������������������ 14
Digital Signature: Signing Using Private Key������������������������������������������������������������������������� 15
Verifying the Digital Signature Using a Public Key���������������������������������������������������������������� 18
How Cryptography Is Used in Blockchain����������������������������������������������������������������������������������� 18
Hashing��������������������������������������������������������������������������������������������������������������������������������� 19
Symmetric and Asymmetric Cryptography���������������������������������������������������������������������������� 19
Digital Signature�������������������������������������������������������������������������������������������������������������������� 20
Summary������������������������������������������������������������������������������������������������������������������������������������ 21

iii
Table of Contents

Chapter 2: Understanding Blockchain�������������������������������������������������������������������� 23


Motivations Behind Blockchain��������������������������������������������������������������������������������������������������� 24
Placement of Trust����������������������������������������������������������������������������������������������������������������� 24
Trust Issues��������������������������������������������������������������������������������������������������������������������������� 25
Solving Trust Issues Using Decentralization�������������������������������������������������������������������������� 26
Example of Decentralization�������������������������������������������������������������������������������������������������� 26
Blockchain As a Distributed Ledger��������������������������������������������������������������������������������������� 29
How a Blockchain Works������������������������������������������������������������������������������������������������������������� 30
Chaining the Blocks��������������������������������������������������������������������������������������������������������������� 31
Immutability of Blockchains�������������������������������������������������������������������������������������������������� 34
Consensus Protocols������������������������������������������������������������������������������������������������������������� 35
Proof of Work������������������������������������������������������������������������������������������������������������������������� 37
Proof of Stake (PoS)�������������������������������������������������������������������������������������������������������������� 41
Blockchain in More Detail����������������������������������������������������������������������������������������������������������� 43
Types of Nodes���������������������������������������������������������������������������������������������������������������������� 44
Merkle Tree and Merkle Root������������������������������������������������������������������������������������������������� 47
Uses of Merkle Tree and the Merkle Root������������������������������������������������������������������������������ 48
Summary������������������������������������������������������������������������������������������������������������������������������������ 49

Chapter 3: Implementing Your Own Blockchain Using Python������������������������������� 51


Your Conceptual Blockchain Implementation����������������������������������������������������������������������������� 51
Obtaining the Nonce�������������������������������������������������������������������������������������������������������������� 53
Installing Flask���������������������������������������������������������������������������������������������������������������������� 55
Importing the Various Modules and Libraries������������������������������������������������������������������������ 56
Declaring the Class in Python������������������������������������������������������������������������������������������������ 56
Finding the Nonce����������������������������������������������������������������������������������������������������������������� 57
Appending the Block to the Blockchain��������������������������������������������������������������������������������� 58
Adding Transactions�������������������������������������������������������������������������������������������������������������� 59
Exposing the Blockchain Class as a REST API����������������������������������������������������������������������� 60
Obtaining the Full Blockchain������������������������������������������������������������������������������������������������ 60
Performing Mining����������������������������������������������������������������������������������������������������������������� 60
Adding Transactions�������������������������������������������������������������������������������������������������������������� 61

iv
Table of Contents

Testing Your Blockchain�������������������������������������������������������������������������������������������������������������� 62


Synchronizing Blockchains��������������������������������������������������������������������������������������������������������� 67
Testing the Blockchain with Multiple Nodes�������������������������������������������������������������������������� 72
Full Listing for the Python Blockchain Implementation��������������������������������������������������������������� 78
Summary������������������������������������������������������������������������������������������������������������������������������������ 85

Chapter 4: Creating Your Own Private Ethereum Test Network������������������������������ 87


Downloading and Installing Geth, the Ethereum Client��������������������������������������������������������������� 87
Installing Geth for macOS������������������������������������������������������������������������������������������������������ 88
Installing Geth for Windows��������������������������������������������������������������������������������������������������� 89
Installing Geth for Linux��������������������������������������������������������������������������������������������������������� 89
Creating the Private Ethereum Test Network������������������������������������������������������������������������������ 90
Creating the Genesis Block��������������������������������������������������������������������������������������������������� 91
Creating a Folder for Storing Node Data�������������������������������������������������������������������������������� 92
Initiating a Blockchain Node�������������������������������������������������������������������������������������������������� 93
Starting Up the Nodes����������������������������������������������������������������������������������������������������������� 94
Managing Accounts������������������������������������������������������������������������������������������������������������������� 107
Removing Accounts������������������������������������������������������������������������������������������������������������� 109
Setting the Coinbase����������������������������������������������������������������������������������������������������������� 109
Summary���������������������������������������������������������������������������������������������������������������������������������� 110

Chapter 5: Using the MetaMask Crypto-Wallet����������������������������������������������������� 111


What Is MetaMask?������������������������������������������������������������������������������������������������������������������ 111
How MetaMask Works Behind the Scenes�������������������������������������������������������������������������� 112
Installing MetaMask������������������������������������������������������������������������������������������������������������ 113
Setting Up the Accounts������������������������������������������������������������������������������������������������������ 115
Using the MetaMask Extension������������������������������������������������������������������������������������������� 119
Selecting Ethereum Networks�������������������������������������������������������������������������������������������������� 121
Getting Test Ethers�������������������������������������������������������������������������������������������������������������� 122
Creating Additional Accounts����������������������������������������������������������������������������������������������� 126
Transferring Ethers�������������������������������������������������������������������������������������������������������������� 129
Recovering Accounts����������������������������������������������������������������������������������������������������������� 135

v
Table of Contents

Importing and Exporting Accounts�������������������������������������������������������������������������������������������� 138


Exporting Accounts�������������������������������������������������������������������������������������������������������������� 138
Importing Accounts������������������������������������������������������������������������������������������������������������� 142
Summary���������������������������������������������������������������������������������������������������������������������������������� 144

Chapter 6: Getting Started with Smart Contracts������������������������������������������������� 145


What Is a Smart Contract?�������������������������������������������������������������������������������������������������������� 145
How Smart Contracts Are Executed������������������������������������������������������������������������������������� 146
Your First Smart Contract���������������������������������������������������������������������������������������������������������� 148
Using the Remix IDE������������������������������������������������������������������������������������������������������������ 148
Compiling the Contract�������������������������������������������������������������������������������������������������������� 152
Testing the Smart Contract Using the JavaScript VM���������������������������������������������������������� 154
Getting the ABI and Bytecode of the Contract��������������������������������������������������������������������� 158
Testing the Smart Contract Using the Goerli Testnet����������������������������������������������������������� 162
Summary���������������������������������������������������������������������������������������������������������������������������������� 166

Chapter 7: Storing Proofs Using Smart Contracts������������������������������������������������ 167


A Smart Contract as a Store of Proofs�������������������������������������������������������������������������������������� 167
Creating the Smart Contract������������������������������������������������������������������������������������������������ 168
Compiling the Contract�������������������������������������������������������������������������������������������������������� 174
Deploying the Contract�������������������������������������������������������������������������������������������������������� 176
Testing the Contract������������������������������������������������������������������������������������������������������������ 177
Making Further Changes to the Smart Contract����������������������������������������������������������������������� 180
Restricting Access to Functions������������������������������������������������������������������������������������������ 181
Accepting Payments in Smart Contracts����������������������������������������������������������������������������� 182
Events in Smart Contracts��������������������������������������������������������������������������������������������������� 188
Cashing Out������������������������������������������������������������������������������������������������������������������������� 191
Destroying a Contract���������������������������������������������������������������������������������������������������������� 193
Summary���������������������������������������������������������������������������������������������������������������������������������� 194

Chapter 8: Using the web3.js APIs����������������������������������������������������������������������� 195


What Is web3.js?����������������������������������������������������������������������������������������������������������������������� 195
Installing web3.js���������������������������������������������������������������������������������������������������������������� 196

vi
Table of Contents

Testing the web3.js Using MetaMask���������������������������������������������������������������������������������� 197


Interacting with a Contract Using web3.js�������������������������������������������������������������������������� 202
Summary���������������������������������������������������������������������������������������������������������������������������������� 214

Chapter 9: Developing Web3 dapps using Python������������������������������������������������ 215


Interacting with Ethereum Using Python���������������������������������������������������������������������������������� 216
Registering with Infura�������������������������������������������������������������������������������������������������������� 217
Connecting to Infura������������������������������������������������������������������������������������������������������������ 220
Fetching a Block������������������������������������������������������������������������������������������������������������������ 221
Setting Up the Accounts������������������������������������������������������������������������������������������������������ 223
Getting the Balance of an Account�������������������������������������������������������������������������������������� 225
Transferring Ethers Between Accounts������������������������������������������������������������������������������� 225
Creating a Dapp Using Python�������������������������������������������������������������������������������������������������� 227
Loading the Contract����������������������������������������������������������������������������������������������������������� 230
Base64 Encoding����������������������������������������������������������������������������������������������������������������� 231
Saving Credentials on the Blockchain��������������������������������������������������������������������������������� 231
Verifying the Result������������������������������������������������������������������������������������������������������������� 234
Summary���������������������������������������������������������������������������������������������������������������������������������� 239

Chapter 10: Project: Online Lottery���������������������������������������������������������������������� 241


How the Lottery Game Works���������������������������������������������������������������������������������������������������� 241
Defining the Smart Contract������������������������������������������������������������������������������������������������ 243
Betting a Number���������������������������������������������������������������������������������������������������������������� 246
Setting the Winning Number and Announcing the Winners������������������������������������������������� 249
Getting the Game Status and Winning Number������������������������������������������������������������������� 253
Cashing Out from the Contract�������������������������������������������������������������������������������������������� 253
Testing the Contract������������������������������������������������������������������������������������������������������������������ 254
Announcing the Winner������������������������������������������������������������������������������������������������������� 258
Saving the ABI of the Contract��������������������������������������������������������������������������������������������� 260
Deploying the Contract to the Testnet��������������������������������������������������������������������������������� 262
Creating the Web Front End������������������������������������������������������������������������������������������������������ 263
Announcing the Winning Number���������������������������������������������������������������������������������������� 269
Cashing Out������������������������������������������������������������������������������������������������������������������������� 271
vii
Table of Contents

The Complete Contract������������������������������������������������������������������������������������������������������������� 272


Summary���������������������������������������������������������������������������������������������������������������������������������� 277

Chapter 11: Creating Your Tokens������������������������������������������������������������������������ 279


What Are Tokens?���������������������������������������������������������������������������������������������������������������������� 279
How Tokens Are Implemented?������������������������������������������������������������������������������������������� 281
Minting New Tokens������������������������������������������������������������������������������������������������������������ 282
Burning Tokens�������������������������������������������������������������������������������������������������������������������� 282
Units Used Internally in Token Contracts����������������������������������������������������������������������������� 283
ERC-20 Token Standard������������������������������������������������������������������������������������������������������� 285
Creating Token Contracts���������������������������������������������������������������������������������������������������������� 287
Overriding the Number of Decimal Places of Precision������������������������������������������������������� 288
Deploying the Token Contract���������������������������������������������������������������������������������������������� 289
Adding the Token to MetaMask������������������������������������������������������������������������������������������� 291
What Can You Do with the Token?��������������������������������������������������������������������������������������� 291
Using Tokens for Smart Contract Payments������������������������������������������������������������������������ 292
Selling Tokens Programmatically���������������������������������������������������������������������������������������������� 300
Calculating the Amount of Tokens Bought��������������������������������������������������������������������������� 302
Deploying the Contract�������������������������������������������������������������������������������������������������������� 304
Summary���������������������������������������������������������������������������������������������������������������������������������� 310

Chapter 12: Creating Non-Fungible Tokens Using ERC-721���������������������������������� 311


What Is an NFT?������������������������������������������������������������������������������������������������������������������������ 311
Ownership vs. Copyright����������������������������������������������������������������������������������������������������� 313
Where Do You Buy or Sell NFTs?����������������������������������������������������������������������������������������� 313
Creating NFTs Using Token Contracts��������������������������������������������������������������������������������������� 314
Who Deploys the NFT Token Contract?�������������������������������������������������������������������������������� 315
Using ERC-721 for Creating NFTs���������������������������������������������������������������������������������������� 315
Deploying the NFT Token Contract��������������������������������������������������������������������������������������� 316
Testing the NFT Contract����������������������������������������������������������������������������������������������������� 318
Summary���������������������������������������������������������������������������������������������������������������������������������� 327

viii
Table of Contents

Chapter 13: Introduction to Decentralized Finance���������������������������������������������� 329


Limitations of Traditional Finance��������������������������������������������������������������������������������������������� 329
Decentralized Finance��������������������������������������������������������������������������������������������������������� 330
Components in DeFi������������������������������������������������������������������������������������������������������������ 331
Stablecoins������������������������������������������������������������������������������������������������������������������������������� 331
Fiat-Backed Stablecoins������������������������������������������������������������������������������������������������������ 333
Crypto-Backed Stablecoins������������������������������������������������������������������������������������������������� 334
Non-Collateralized Stablecoins������������������������������������������������������������������������������������������� 340
Crypto Exchanges��������������������������������������������������������������������������������������������������������������������� 340
Creating a Decentralized Exchange������������������������������������������������������������������������������������� 342
Creating the Token Contract������������������������������������������������������������������������������������������������ 343
Deploying the Token Contract���������������������������������������������������������������������������������������������� 343
Creating the DEX Contract��������������������������������������������������������������������������������������������������� 345
Funding the DEX������������������������������������������������������������������������������������������������������������������ 351
Swapping WML Tokens for LWM Tokens������������������������������������������������������������������������������ 354
Summary���������������������������������������������������������������������������������������������������������������������������������� 359

Index��������������������������������������������������������������������������������������������������������������������� 361

ix
About the Author
Wei-Meng Lee is the founder of Developer Learning
Solutions, a technology company specializing in hands-on
training of blockchain and other emerging technologies.
He has many years of training expertise and his courses
emphasize a learn-by-doing approach. He is a master at
making learning a new programming language or technology
less intimidating and more fun. He can be found speaking
at conferences worldwide such as NDC, and he regularly
contributes to online and print publications such as Medium
(https://weimenglee.medium.com) and CoDe Magazine.
He is active on social media, on his blog calendar.learn2develop.net, on Facebook
(www.facebook.com/DeveloperLearningSolutions), on Twitter as @weimenglee, and
on LinkedIn (linkedin.com/leeweimeng).

xi
About the Technical Reviewer
Prasanth Sahoo is a Blockchain Certified Professional,
Professional Scrum Master, and Microsoft Certified
Trainer who is passionate about helping others
learn how to use and gain benefits from the latest
technologies. He is a thought leader and practitioner
in blockchain, cloud, and Scrum. He also handles the
Agile methodology, cloud, and blockchain technology
community initiatives within TransUnion through
coaching, mentoring, and grooming techniques.
Prasanth is an adjunct professor and a technical speaker. He was selected as a speaker
at the China International Industry Big Data Expo 2018 by the Chinese government and
also to the International Blockchain Council by the governments of Telangana and Goa.
He also received accolades for his presentation at China International Industry Big Data
Expo 2018 by the Chinese government. Prasanth has published a patent titled "Digital
Educational Certificate Management System using IPFS Based Blockchain."
To date, Prasanth has reached over 50,000 students, mostly within the technical
domain. He is a working group member of the CryptoCurrency Certification
Consortium, Scrum Alliance, Scrum Organization, and International Institute of
Business Analysis.

xiii
Acknowledgments
Writing a book is immensely exciting, but along with it comes long hours of hard work
and responsibility, straining to get things done accurately and correctly. To make a book
possible, a lot of unsung heroes work tirelessly behind the scenes.
For this, I would like to take this opportunity to thank a number of special people
who made this book possible. First, I want to thank my acquisitions editor, Joan Murray,
for giving me this opportunity. Thanks for suggesting that I update this book with the
latest happenings in the crypto world!
Next, a huge thanks to Jill Balzano, my associate editor, who was always very patient
with me, even though I missed several of my deadlines for the revision of this book.
Thanks, Jill, for your guidance. I could not finish the book without your encouragement
and help!
Equally important is my project coordinator, Shobana Srinivasan. Shobana has been
very patient with me during the whole project while I struggle between work and writing.
Thanks, Shobana, for the assistance rendered during the project!
Last, but not least, I want to thank my parents and my wife, Sze Wa, for all
the support they have given me. They have selflessly adjusted their schedules to
accommodate my busy schedule when I was working on this book. I love you all!

xv
Introduction
Welcome to Beginning Ethereum Smart Contracts Programming, Second Edition!
This book is a quick guide to getting started with Ethereum smart contracts
programming. It starts off with a discussion of blockchain and the motivations behind it.
You will learn what a blockchain is, how blocks in a blockchain are chained together, and
how blocks get added to a blockchain. You will also understand how mining works and
discover the various types of nodes in a blockchain network. Since the publication of the
first edition of this book, a lot of things have changed. In particular, Ethereum has been
updated to use Proof of Stake (PoS) (instead of Proof of Work) as its consensus algorithm.
This book has been updated to include a discussion of how PoS works.
Once that is out of the way, you dive into the Ethereum blockchain. You will learn
how to use an Ethereum client (Geth) to create a private Ethereum blockchain and
perform simple transactions such as sending Ethers to another account.
The next part of this book discusses smart contract programming, a unique feature of
the Ethereum blockchain. You will jumpstart on smart contracts programming without
needing to wade through tons of documentation. The learn-by-doing approach of this
book makes you productive in the shortest amount of time. By the end of this book,
you should be able to write smart contracts, test them, deploy them, and create web
applications to interact with them. In this second edition, I have added more examples
to make it easy for you to explore more complex smart contracts.
The last part of this book touches on tokens and DeFi (decentralized finance),
something that has taken the cryptocurrency market by storm. You will be able to create
your own tokens, launch your own ICO, and write token contracts that allow buyers
to buy tokens using Ethers. As a bonus, I show you how to write a DEX (decentralized
exchange) smart contract to exchange two different tokens!
This book is designed for those who want to get started quickly with Ethereum smart
contracts programming. Basic programming knowledge and an understanding of Python
or JavaScript are recommended.
I hope you enjoy working on the sample projects as much as I enjoyed creating them!

xvii
CHAPTER 1

Understanding the
Science Behind
Blockchain: Cryptography
The reason you are reading this book is because you want to understand what a
blockchain is, how it works, and how you can write smart contracts on it to do cool
things. And while I perfectly understand that you are excited to get started in this first
chapter, we need to take a step back and look at one fundamental technology that makes
blockchain possible: cryptography.
In this chapter, I will explain what cryptography is, the different types of
cryptographic algorithms, how they work, and how they play a vital role in the world
of blockchain. I will also show you how to experiment with the various cryptographic
algorithms using the Python programming language. Even if you are familiar with
cryptography, I suggest scanning through this chapter so that you have a firm foundation
for the subsequent chapters.

What Is Cryptography?
Whether you are trying to build a web application to store users’ credentials or writing
a network application to securely transmit encrypted messages, or even trying to
understand how blockchain works, you need to understand one important topic:
cryptography.
So, what exactly is cryptography? Put simply, cryptography (or cryptology) is the
practice and study of hiding information. It is the science of keeping information secret
and safe.

1
© Wei-Meng Lee 2023
W.-M. Lee, Beginning Ethereum Smart Contracts Programming, https://doi.org/10.1007/978-1-4842-9271-6_1
Random documents with unrelated
content Scribd suggests to you:
APPENDIX A
PROTOCOL SIGNED FEBRUARY 23, 1904

Article I
For the purpose of maintaining a permanent and solid friendship
between Japan and Korea, and firmly establishing peace in the Far
East, the Imperial Government of Korea shall place full confidence in
the Imperial Government of Japan and adopt the advice of the latter
in regard to improvements in administration.

Article II
The Imperial Government of Japan shall in a spirit of firm
friendship ensure the safety and repose of the Imperial House of
Korea.

Article III
The Imperial Government of Japan definitely guarantees the
independence and territorial integrity of the Korean Empire.

Article IV
In case the welfare of the Imperial House of Korea or the territorial
integrity of Korea is endangered by aggression of a third Power or
internal disturbances, the Imperial Government of Japan shall
immediately take such necessary measures as the circumstances
require; and in such cases the Imperial Government of Korea shall
give full facilities to promote the action of the Imperial Japanese
Government.
The Imperial Government of Japan may, for the attainment of the
above-mentioned object, occupy, when the circumstances require it,
such places as may be necessary from strategical points of view.

Article V
The Governments of the two countries shall not in future, without
mutual consent, conclude with a third Power such an arrangement
as may be contrary to the principles of the present Protocol.

Article VI
Details in connection with the present Protocol shall be arranged
as the circumstances may require between the Representative of
Japan and the Minister of State for Foreign Affairs of Korea.
APPENDIX B
PROTOCOL SIGNED AUGUST 22, 1904

Article I
The Korean Government shall engage as Financial Adviser to the
Korean Government, a Japanese subject recommended by the
Japanese Government, and all matters concerning finance shall be
dealt with after his counsel being taken.

Article II
The Korean Government shall engage as diplomatic adviser to the
Department of Foreign Affairs, a foreigner recommended by the
Japanese Government, and all important matters concerning foreign
relations shall be dealt with after his counsel being taken.

Article III
The Korean Government shall previously consult the Japanese
Government in concluding treaties and conventions with foreign
Powers, and in dealing with other important diplomatic affairs, such
as the grant of concessions to, or contracts with, foreigners.
APPENDIX C
CONVENTION OF JULY 24, 1907

The Governments of Japan and Korea, with a view to the early


attainment of the prosperity and strength of Korea, and to the speedy
promotion of the welfare of the Korean people, have agreed upon
and concluded the following stipulations:—

Article I.—The Government of Korea shall follow the


direction of the Resident-General in connection with the
reform of the administration.
Article II.—The Government of Korea shall not enact any
law or ordinance, or carry out any important administrative
measure, except with the previous approval of the Resident-
General.
Article III.—The judicial affairs of Korea shall be kept
distinct from the ordinary administrative affairs.
Article IV.—No appointment or dismissal of Korean
officials of the higher grade shall be made without the consent
of the Resident-General.
Article V.—The Government of Korea shall appoint to
official positions under it such Japanese as may be
recommended by the Resident-General.
Article VI.—The Government of Korea shall not engage
any foreigner without the consent of the Resident-General.
Article VII.—The first clause of the Agreement between
Japan and Korea, signed on the 22d day of the 8th month of
the 37th year of Meiji, is herewith abrogated.
In faith whereof, the undersigned, duly authorized by their
respective Governments, have signed this agreement and
affixed their seals thereto.
(L. S.) Marquis Hirobumi Ito,
H. I. J. M’s. Resident-General.
The 24th day of the 7th month of the 40th year of Meiji.
(L. S.) Yi Wan-yong,
H. I. K. M’s. Minister, President of State.
The 24th day of the 7th month of the 11th year of Kwang-
mu.

[The clause in the Protocol of August, 1904, which is declared


abrogated by the seventh article of the new Convention, apparently
refers to the promise of the Korean Government to engage a
Japanese subject as their official Financial Adviser. It was, of course,
rendered unnecessary by the new Convention.]
APPENDIX D
SUMMARY OF THE MOST RECENT MEASURES
FOR THE ORGANIZATION OF THE KOREAN
GOVERNMENT

The revised Organic Regulations of the Korean Government,


published by an extra of the Official Gazette (December 23, 1907),
cover the five Administrative Departments for Home Affairs, Finance,
Justice, Education, and Agriculture, Commerce and Industry. As for
the Cabinet and War Office, they had not as yet reported any
changes introduced in their Regulations. In addition to the particular
Organic Regulations for each department there are Regulations
common to all the Departments, the War Office not being excepted.
The latter Regulations consist of 21 articles which outline the
responsibility and duty of the Ministers, Vice-Ministers, and other
officials, and fix the date for the enforcement of all the revised
Regulations for January 1, 1908. Regulations for the organization of
the different offices under the Departments of Home, Finance, and
Justice were promulgated at the same time, including the Provincial
Governor’s Office, Metropolitan Police Office and Customs Office.
To give a brief epitome of the Regulations for each administrative
department: The Home Office is to contain three bureaus for local
affairs—Police, Engineering, and Hygienics, with a Director for each.
The rest of the staff consists of 12 secretaries, 5 commissioners, 5
engineering experts, 3 translators, 62 clerks, 10 police sergeants, 5
assistant engineering experts and a number of policemen. The
Finance Department contains the three bureaus of Revenue,
Accounts, and Managing Finance, each with a Director. Thirteen
secretaries, 7 commissioners, 2 translators, and 100 clerks
constitute the staff of this Department. The Department of Justice will
have bureaus for Civil and Criminal Affairs, and each bureau is
controlled by a Director. The regular staff of this department
comprises 9 secretaries, 4 commissioners, 3 translators, and 40
clerks. In the Department of Education there are bureaus for School
Affairs and for Edition and Compilation, with a Director each. The
regular staff includes 7 secretaries, 4 commissioners, 3 engineering
experts, 28 clerks, and 6 assistant engineering experts. The
Department of Agriculture, Commerce and Industry will be divided
into five bureaus—namely, Agriculture, Commercial and Industrial,
Forestry, Mining, and Marine Products; and each bureau has a
Director at its head. The regular staff of this Department includes 8
secretaries, 5 commissioners, 15 technical experts, 1 translator, 49
clerks, and 60 assistant technical experts.
In addition, each Department has a Minister’s Chamber, and a
private secretary will be appointed to each Minister of State.
The Regulations for the Financial Department provide for the
creation of a Temporary Bureau for investigation of the national
resources, with a staff consisting of a Director, a secretary, 3
commissioners, and 5 technical experts.
More detailed regulations for the different offices under these
departments are to be issued later.
The most recent advices from Korea report that the rioting, arson,
and murder, headed by the disbanded Korean soldiers, is greatly
diminished, and that the country is reverting to its normal condition
so far as deeds of disorder and violence are concerned. The visit of
the Crown Prince of Japan greatly gratified the pride and appeased
the fears of the Imperial family and Yang-bans of Korea. Before
leaving Seoul, Prince Ito laid the corner-stone of the new building of
the Young Men’s Christian Association in that city. The Crown Prince
of Korea, the son of Lady Om, whose guardianship Prince Ito has
taken upon himself, accompanied by Ito, arrived in Tokyo, where he
is to be placed in the Peers School, and was received with
distinguished honors both by the Imperial Family of Japan and by the
populace. The reports also show that the trade relations have had a
significant increase between the two countries; but the most
significant item is this: the exports of Korean products, which are for
the most part rice and beans, exceed the imports from Japan by
some 3,000,000 yen. The establishment of friendly relations between
the two countries appears, therefore, to be moving forward rapidly;
and the political and economical redemption of the peninsula
appears to have been successfully begun. The first and, of
necessity, most doubtful and difficult in the stages of the Passing of
the Old Korea may therefore be said to have been already
accomplished.
FOOTNOTES
[1] For the following description of Seoul, besides my own
observations, I am chiefly indebted to a series of articles
published during our stay there by Dr. G. Heber Jones in the
Seoul Press.
[2] This may seem incredible, but it is a fact that, as late as the
spring of 1907, even a basket of fruit could not be sent to the
Emperor with the confidence that the eunuchs and palace
servants would not steal it all. At every garden-party the dishes
and even the chairs had to be carefully watched.
[3] It is now proper to say, since his own abdication and the
Convention of July, 1907, have followed, that the Korean Emperor
after repeated denials, confessed at the time to a faithful foreign
friend (not a Japanese) that he had given to Mr. Hulbert a large
sum of money to execute a certain commission the nature of
which he kept secret. In spite of this friend’s importunate urging
and vivid representation of what the consequences of the act
might be to himself and to his family, His Majesty refused to
telegraph a recall of the commission. He did, however, so far yield
to the same pleading as to agree not to furnish a further sum of
money which had been asked in behalf of the influence of another
“foreign friend,” the editor of the most violently anti-Japanese
newspaper.
[4] This document probably emanated from the same press in
Seoul—conducted by a subject of Japan’s friendly ally, Great
Britain—from which came the lying bulletin that afterward caused
so much bloodshed on the morning of Friday, July 19th. It is a
comfort to know that this same editor has since been indicted by
his own Government for the crime of stirring up sedition,
condemned to give bonds, and threatened with deportation if his
offences are repeated.
[5] Hulbert, The History of Korea, I, p. 368.
[6] Japan, I, p. 69 f.
[7] See The History of the Empire of Japan, (volume prepared
for the World’s Columbian Exposition at Chicago, 1903), p. 38 f.
[8] Ibid., p. 47.
[9] The History of the Empire of Japan, p. 278 f.
[10] Ibid., p. 280.
[11] See Griffis, The Hermit Nation, p. 159.
[12] See The History of the Empire of Japan, p. 304.
[13] Japan, IV, p. 207.
[14] See The History of the Empire of Japan, p. 403 ff.
[15] This is on the authority of Mr. D. W. Stevens, whose
acquaintance with the facts is most accurate and full.
[16] China’s Intercourse with Korea from the XVth Century to
1895, p. 1 f.
[17] Foreign Relations of the United States, 1871, p. 112.
[18] Quoted from the paper referred to above.
[19] For this account, as here given verbatim, I am indebted to
the Hon. D. W. Stevens, who was at the time of my visit, “Adviser
to the Korean Council of State and Counsellor of the Resident-
General.”
[20] The list of these reforms is given in the volume of the U. S.
Foreign Relations, containing the report sent to the United States
by Minister Sill, September 24, 1894.
[21] International Law and Diplomacy of the Russo-Chinese
War, p. 43 f.
[22] Hershey, International Law and Diplomacy of the Russo-
Chinese War, p. 44 f.
[23] Ibid., p. 45 f. See also the account of Dr. K. Asakawa, The
Russo-Japanese Conflict, p. 263 ff.
[24] So Mr. Whigham, in his admirable book on Manchuria and
Korea. (London, Isbiter & Company), p. 123.
[25] The Passing of Korea, p. 167.
[26] See on this and allied points, the lecture delivered by Mr.
Rockhill, at the United States Naval War College, Newport,
August 5, 1904.
[27] The Passing of Korea, p. 210 f.
[28] Manchuria and Korea, p. 119.
[29] See Appendix A for its text.
[30] See Appendix B.
[31] War and Neutrality in the Far East, p. 216 f.
[32] See especially Hulbert, The Passing of Korea, p. 464 f.
[33] The narrative which follows may be trusted to correct most
of these misstatements. But among them, some of the more
important may here be categorically contradicted. Such are, for
example, the statements that armed force was used; that General
Hasegawa half drew his sword to intimidate Mr. Han; that
Hagiwara seized the latter with the aid of gendarmes and police;
that the Minister of Agriculture continued to hold out; that he and
Minister Pak, during the conference, withdrew from the Japanese
Legation and betook themselves to the Palace, denouncing the
compact (something no one acquainted with the geographical
relations of the two places would be likely to assert with a sincere
belief); that the Emperor ordered the consenting Ministers to be
assassinated; that Japanese troops patrolled the streets all night,
etc., etc. One curiously characteristic error of Mr. Hulbert is
involved in the statement, published in one of the papers of the
United States, which makes his commission by the Korean
Emperor to lodge an appeal with President Roosevelt the cause
of hastening the Japanese Government in their iniquitous coup
d’état. The truth is that the Japanese Government had made all
the preparations for Marquis Ito’s departure, and the plan
afterward carried out had been carefully formulated, weeks before
it was known that Mr. Hulbert was going to the United States. The
Marquis was only waiting the return of Baron Komura to Japan
before leaving for Korea. No thought whatever was at any time
given to Mr. Hulbert. It is, in general, late now to say that the
efforts of those “friends of Korea,” who have taken the Korean ex-
Emperor’s money while holding out to him the hope of foreign
intervention, have done him and his country, rather than Japan,
an injury impossible to repair.
[34] In order to understand the following negotiations and all
similar transactions conducted in characteristic Korean style, it
should be remembered that delay, however reasonable it may
seem or really be, is in fact utilized for purposes not of reflection
and judicious planning for future emergencies, but the rather for
arranging intrigues, securing apparent chances of escape from
the really inevitable, with the result of an increasing unsettlement
of the Imperial mind.
[35] He was preparing to go when the Minister of the
Household called with a message requesting the Marquis to
postpone the conclusion of the Treaty two or three days.
[36] None of the party gathered in the council chamber saw Mr.
Han after that. It seems from the accounts subsequently given by
Palace officials that a little later Mr. Han went upstairs still deeply
agitated. His evident purpose was to gain access to the Emperor,
which, as he had not requested an audience, was a flagrant
violation of etiquette from the Korean point of view. But the poor
man in his confusion turned the wrong way and stumbled into
Lady Om’s quarters. Some of the officials led him to a small
retiring room, where he spent the night. The next morning it was
officially announced that he had been dismissed from office in
disgrace and would be severely punished. Marquis Ito
immediately begged that the Emperor would pardon him, and, in
deference to this request, Mr. Han was permitted to go into
retirement with no other punishment than the loss of his office.
The whole proceeding was one of those things which apparently
can happen only in Korea and not excite any one’s special
wonder. No one seemed to know precisely why the Minister was
punished. He was amiable, not very strong mentally, but well-
meaning and of comparatively good repute; he had done his best
to carry out the Emperor’s wishes as he understood them, and,
having failed, as was inevitable, his grief was the best proof
possible of his sincerity; and one would think it might have excited
sufficient pity to preclude resentment. However, it should be
added that the sincerity manifest in Mr. Han’s grief did not extend
to his memory or his powers of narration. At least that is an
inference which one may draw from certain published accounts of
these occurrences—Mr. Han having seemingly been the fountain-
head of the information.
[37] The Marquis’ reasons for refusing hardly need explanation.
Japan had already secured some measure of control over the
internal administration of Korea by previous arrangements. The
acceptance of the proposed amendment would have been
virtually an abrogation of these arrangements, notably of the most
important portion of the Protocols of February 23 and August 22.
To that, of course, the Marquis could not agree. Besides this, the
control of Korea’s foreign relations necessarily required some
measure of control and guidance over the administration of her
internal affairs. The relations between external and internal
affairs, their frequent interdependence, is so intimate, that it would
have been a grave mistake to assume the obligations which the
one imposed without the power to guard against complications
which might follow from maladministration of the other. As the
case stands, the insertion of the word “primarily,” while soothing
Korean susceptibilities, does not affect the control of the
Protectorate in any material respect.
[38] The following facts with regard to the possession of the
Imperial seal of Korea and its affixing to this important document,
are given on the authority of Mr. D. W. Stevens. They are a
complete refutation of the charges which have been made
regarding this part of the entire transaction. It was the
unavoidable delay in bringing the seal to the Palace which gave
rise to these extraordinary stories. “What actually happened,”
says Mr. Stevens, “was this. While the treaty was being copied,
Mr. Pak went to the telephone and directed the clerk in charge of
the seal at the Foreign Office to bring it to the Palace. After some
delay he went again to the telephone and repeated the order. At
the time the only two persons in the office were the clerk in
charge of the seal and Mr. Numano, my Japanese assistant. Both
were just then reading in the room where the clerk slept and
where the seal was kept. The telephone bell rang, and the clerk
who answered it informed Mr. Numano that Mr. Pak had ordered
the seal to be brought to the Palace. He was putting on his street
clothing preparatory to obeying the order when the Chief of the
Diplomatic Bureau of the Foreign Office came into the room and
asked the clerk where he was going. The clerk informed him,
whereupon he went to the telephone and called up Mr. Pak. He
implored the latter not to agree to the Treaty and, finally, receiving
Mr. Pak’s peremptory order to cease interfering, threw himself
down upon the clerk’s bed in great grief. After this, there was no
further interruption from any quarter, and the seal was taken
quietly to the Palace.”
It throws light upon the control and use of this seal to observe
that, when in the summer of 1907 he was committed to the
responsibility for the Commission to The Hague Conference by
the fact that the commissioners were ready to prove their Imperial
authorization by showing the Imperial seal, His Majesty did not
admit this as evidence in proof of their claim. Nevertheless, there
is no doubt that this use of his seal was also with his knowledge
and permission. And, now, in connection with the various details
inaugurated under the new Treaty which followed this violation of
the Treaty of November, 1905, we are told that henceforth the
Imperial seal will be kept in a safe especially prepared for it, and
carefully protected from intrusion.
[39] It is a significant fact that this memorial which is here
followed very closely—and in the most important places even
literally—has received no attention from the hostile critics of
Japan. It would seem as though neither Mr. Hulbert nor Mr. Story
is aware of the existence of such a memorial. This is the more
remarkable in the case of the former, because he was for years
resident in Seoul, was familiar with the Korean language, and was
gathering material for his written account of the affair while upon
the ground.
[40] It will, therefore, clearly appear that no one acquainted with
this memorial can honestly place any confidence in His Majesty’s
subsequent denials of the significance of these facts. Shall we not
also be obliged to add, that no one who is acquainted with the
memorial is entitled to the confidence of any one else, if he puts
confidence in the denials of the Emperor. Amazement at the
audacity of the falsehoods which have been told with regard to
this historically important transaction would seem to be the fitting
attitude of mind.
[41] This part of the memorial agrees closely with the
statements in the first part of the chapter, as to what was then
said.
[42] The purpose of this significant Memorial, we repeat, is self-
evident. The Ministers, who had agreed to the Treaty by the
Emperor’s commands and with his concurrence and approval,
were being attacked as traitors. The Emperor himself was secretly
favoring the attack and endeavoring to create the impression that
he had not agreed to the Treaty, but that it was the work of the
recreant Cabinet without his approval. The Memorial forced him
to abandon that position once and for all. As before stated, it was
officially promulgated with the Imperial sanction, and should have
ended all controversy at once. In any country but Korea, and with
any but the class of writers whom these incidents have
developed, that would have been its result.
[43] An amusing illustration of the ex-Emperor’s way of filling
his privy purse is found in the following authentic incident. At one
time the large sum of 270,000 yen was wanted in cash to pay a
bill for silks and jades which, it was alleged, had been purchased
in China for Lady Om. When the request was made to exhibit the
precious goods which had cost so enormous a sum, and which
were going to make so large an unexpected drain upon
insufficient revenues, the show of materials was entirely
unsatisfactory. But, if not the goods, at least the bill itself could be
produced. A bill was then brought to light, with the items made out
in due form, but by a Chinese firm of merchants in Seoul instead
of in China. The Chinese Consul-General, on being inquired of,
replied that there was indeed such a reputable Chinese firm in the
city; and he desired to have the matter further investigated lest
the credit and business honor of his countrymen might suffer by
connection of this sort with His Majesty’s efforts to obtain ready
money. Investigation elicited the fact that a certain Court official
had visited this firm and inquired how much such and such things
would cost, if purchased in Shanghai. But no goods had been
delivered or even actually ordered!
[44] See Hulbert, The History of Korea, II, p. 61 f.
[45] The History of Korea, I, p. 339.
[46] See Hulbert, The History of Korea, II, p. 54.
[47] Hulbert, The Passing of Korea, pp. 50, 58.
[48] The Passing of Korea, p. 67.
[49] The Passing of Korea, pp. 38, 41.
[50] Ibid., p. 43.
[51] See the account of the “Baby War” and “Breast Hunters,”
The History of Korea, II., p. 245.
[52] The Passing of Korea, pp. 311, 319, 369.
[53] Ibid., p. 283.
[54] Ibid., p. 247.
[55] Whigham, Manchuria and Korea, p. 185.
[56] See a pamphlet bearing this title as an “Authorized
Translation of Official Documents published by the Resident-
General, in Seoul, January, 1907,” p. 7.
[57] During all my visit in Korea it was commonly reported by
those intimate at Court that the Crown Prince was an imbecile
both in body and in mind. But in his boyhood he was rather more
than ordinarily bright, and his mother, the murdered Queen, was
the most clever and brilliant Korean woman of her time. It is not
strange, then, that since his accession to the throne and in view
of his obviously sensible way of yielding to good advice from
others, in spite of the evil influence of his father, the impression
has been made that he might have been feigning imbecility in
order to escape plots to assassinate him, which were formed in
the interests of a rival claimant to the throne.
[58] Issue of Saturday, March 16, 1907.
[59] So the report on the “State of the Progress of the
Reorganization of the Finances of Korea, March, 1907.”
[60] Administrative Reforms in Korea, p. 18.
[61] A cho is nearly 2½ acres.
[62] See Administrative Reforms in Korea, p. 19.
[63] State of the Progress of the Reorganization of the
Finances of Korea, March, 1907, p. 20.
[64] Administrative Reforms in Korea, p. 15.
[65] It should be noted in this connection that this appointment
is one of the very few which, like that of the Resident-General,
proceed directly from the Emperor of Japan himself.
[66] Summary of the Financial Affairs of Korea, p. 5.
[67] In interpreting this it should be remembered that the
Japanese sen is equal in value to one-half a cent in American
gold, or about one farthing in English currency. 100 sen = 1 yen,
and 1,000 rin = 1 yen.
[68] “There had been,” says Mr. D. W. Stevens, “some criticism
because such a law was considered necessary; and Japanese
legal procedure was accused of being defective, on this account,
by certain foreign critics. But in the late seventies the British Court
at Yokohama released a man who had been detected
counterfeiting Japanese money, on the ground that there was no
British law under which to punish him, and that Japanese law
against counterfeiting did not apply to British subjects in Japan.
And the highest British courts have held that a contract to
smuggle goods into a foreign country is a valid contract as
between British subjects in Great Britain.” The entire matter is
dwelt upon at such length because it illustrates so well the
inability of the Koreans for “independent” management of their
own internal affairs, and also the animus and propriety of much of
the anti-Japanese criticism.
[69] The quotations are from the pamphlet, Administrative
Reforms in Korea, p. 11 f.
[70] See Summary of the Financial Affairs of Korea, p. 5.
[71] See the incidents—which are of a sort to be almost
indefinitely multiplied—on page 285 f.
[72] Dr. Allen, then American Consul-General, in a report upon
Educational Institutions and Methods in Korea, 1898.
[73] See Administrative Reforms in Korea, p. 4 f.
[74] Official Minutes of the Korean Mission Conference, 1906,
p. 41.
[75] Korean Review, of February, 1904.
[76] It is significant to notice in this connection that previous to
his several commissions from the Korean Emperor, this writer
held a quite different view from that which he afterward advocated
with regard to the underlying principle of all the recent relations
between the two countries. In the same article he says: “The
present chaotic state of the national finances and of popular
discontent, show something of what Russian influence has
accomplished in Korea; and the people are coming to realize the
fact. They are passionately attached to the theory of national
‘independence.’ We say theory advisedly. This word
‘independence’ is a sort of fetich to which they bow, but they think
that independence means liberation from outside control alone,
forgetting that genuine independence means likewise a liberation
from evil influences within, and that liberty, so far from being carte
blanche to do as one pleases, is in truth the apotheosis of law.”
[77] Among the many falsehoods told by the Koreans and their
“Foreign Friends,” in their endeavors to excite pity for themselves,
and, possibly, interference with the Japanese Administration in
Korea, none is more ridiculous than that the latter were reviving
the use of torture. It should be borne in mind that, previous to the
Convention of July, 1907, which followed upon the promulgation
of this and other more important false charges by the
commissioners to The Hague Conference, the Japanese
Residency-General’s power did not extend to the interference with
the execution of the Korean law upon Korean criminals.
Preliminary examination by beating with a stick was then legal;
according to credible current report it was practiced upon the
vice-Minister of Education, when, during my visit to Korea, he was
accused of having contributed money toward effecting the
assassination of the Ministry (see p. 51). All this is quite different
from the retort which might be made to critics from the United
States to remember the practice of “water-cure” in the Philippines,
etc.
[78] Quoted, as are the following paragraphs bearing quotation
marks, from the pamphlet prepared under the supervision of the
Resident-General, and published in Seoul, January, 1907, on
Administrative Reforms in Korea. [These quotations are made
exactly, and without attempt to change the language in
accordance with our use of legal terms.]
[79] The following incident illustrates the habitual behavior of
the Korean Daily News, edited by Mr. Bethell, in both an English
and a native edition. Dr. Jones, one of the most faithful and useful
of the Missionary body in Korea, had previously incurred the bitter
enmity of this paper by publicly announcing (see p. 61 f.) the
intention to assist the Resident-General in his plans, so far as his
own work as a missionary permitted, for the up-raising of Korea.
At the time when the Korean troops, in a wholly unprovoked way,
fired upon the crowd in the streets of Seoul, Dr. Jones published
in the Seoul Press an account of what he himself saw. The
account was not accompanied by any harsh criticism of the
conduct of the troops. But “shortly afterwards a Korean attached
to the vernacular paper visited him and, attacking him fiercely,
denounced him as an enemy of Korea. This was followed by a
savage attack in the Korean edition of the News, giving an
entirely false account of what Dr. Jones had done and said. It was
in fact an invitation to murder.” Dr. Jones at once appealed to the
American Consul-General and he to the British. The editor was
forced to retract and apologize, but this by no means
compensated for the damage his article had done.
[80] This fact has been clearly proven by papers found on the
body of Yi-Sei-chik, when he was afterward arrested and detained
at headquarters, as well as by his personal statements.
[81] This serious charge was made by the writer and published
to a friendly nation, on the basis of no personal knowledge, not to
say careful investigation, and after casual conversation with a
small number of witnesses who belong to the class peculiarly
liable to be deceived both as to facts and as to causes of such
alleged incidents.
[82] Deplorable, on account of its effect, direct and indirect,
upon the Koreans, upon Marquis Ito’s efforts at reform, and upon
the missionary cause in Japan as well as Korea.
[83] It has been asserted that the value of the land staked off by
the Japanese military authorities near Seoul was 6,000,000 yen.
As the result of a “painstaking and impartial investigation” it was
found that, at the highest market price, this land would not have
brought more than 750,000 to 1,000,000 yen. The Korean way in
such matters is well illustrated by the experience of the Young
Men’s Christian Association in Seoul, who, when one small piece
of land was needed to complete their site, were obliged to invoke
an official order preventing the sale to any other party; and even
then paid a price probably two or three times its true market
value. Compare also what is said, p. 98 f., about the Pyeng-yang
affair.
[84] What is the state of the case in certain portions of the West
is truthfully told in the following paragraph quoted from a popular
journal: “In the matter of cheating Indians and acquiring public
lands in ways which bear all the ethical aspects of theft, there is
no public or private morality either in Oklahoma or any other of
those Western States where Indians and public lands continue to
exist.”
[85] On one occasion the British and Chinese Ministers jointly
urged the payment of indemnity in the case of two Chinamen, one
a British protégé, who had been injured in a fight with tax-
collecting officials at a place to which Chinese junks were in the
habit of resorting. The British protégé had died of his wounds,
both he and his companion having been confined after the fight in
the magistrate’s yamen. The Korean local officials contended that
only one person had been killed—namely, the wounded
Chinaman. When confronted with the fact that, according to their
own report, there was a dead Chinaman in the yamen the
morning after, they replied that this man was not in the fracas at
all; he had merely crawled into the yamen during the night, and
had died of some unknown disease. The picture of this shrewd
Celestial going to the yamen to die, apparently for the purpose of
fraudulently foisting an incriminating corpus delicti upon the
innocent Korean official, did not appeal to the British Minister, and
he got his indemnity.
[86] See “The Japanese in Korea,” Extracts from The Korean
Review, p. 46 f.
[87] The Far East (London, T. Fisher Unwin, 1901), p. 337 f.
[88] Korea (Chas. Scribner’s Sons, 1904), pp. 128 f.; 274 f.
Perhaps the underlying reason for much of Mr. Hamilton’s rather
vituperative criticism of affairs in Korea may be found in Chapter
XII, where Japanese, American, and British merchants, and Lord
Salisbury are all severely taken to task because too much of
Korea’s trade is falling into other than English hands.
[89] According to the testimony of travellers in the interior of
Korea, it is extremely difficult to get any food, accommodation, or
service, even when desirous of paying the highest prices, on
account of the experience with their own travelling officials, who
never expect to pay for anything exacted from the country people.
[90] It furnished Mr. Hulbert and Mr. Bethell, however, with a
striking instance of the way in which the Japanese are robbing the
Koreans.
[91] An occurrence, which might easily have become a much
celebrated instance of a Japanese attempt at robbery and
oppression of the Koreans, came to the writer’s notice in a private
but entirely trustworthy way. One of the ex-Emperor’s real foreign
friends was sent for some time ago and found His Majesty in a
state of intense alarm and excitement over a plot of the
Residency-General which had just been made known to him. A
certain foreigner had authorized the story that the Japanese
authorities were trying to purchase three houses owned by a
Chinese and situated just opposite the Palace, with a view to tear
them down and erect barracks for the Japanese soldiers on the
spot. The price offered by the Japanese was 60,000 yen; but if
His Majesty would furnish 65,000 yen, this friendly foreigner
would buy the property for him, and so defeat the nefarious
project of the Japanese. The Emperor wished at once to borrow
the money. It was suggested, however, that His Majesty should
allow inquiry to be made before parting with so much of his privy
purse. Whereupon, the following conversation was held between
the Chinese owner and the person to whom the Emperor looked
to procure for him the needed sum:
“I understand the three houses you own are offered for sale.”
“Well, I do not particularly wish to sell them; but that
Frenchman, Mr. ⸺, has been here and wanted to get them. He
said he wished to put up a large store in their place.”
“How much do you ask for the houses?”
“They are worth 13,000 yen; but if any one will take all three of
them, he may have them for 12,000 yen in cash.”

You might also like