Skip to content
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
d4b4f28
Format basic security section
minimalsm Sep 10, 2021
1cc7be4
Draft security page
minimalsm Sep 10, 2021
337f9e6
Link clean-up
minimalsm Sep 14, 2021
bbf2d25
Add Bitwarden password manager
minimalsm Sep 14, 2021
ec83608
Add suggested changes
minimalsm Sep 14, 2021
a23821a
Update src/content/security/index.md
minimalsm Sep 15, 2021
c2bc660
Update src/content/security/index.md
minimalsm Sep 15, 2021
b7ad986
Update src/content/security/index.md
minimalsm Sep 15, 2021
11b257e
Update src/content/security/index.md
minimalsm Sep 15, 2021
57d9b2a
Update src/content/security/index.md
minimalsm Sep 17, 2021
6a17f87
Update src/content/security/index.md
minimalsm Sep 17, 2021
556ddb3
Update src/content/security/index.md
minimalsm Sep 17, 2021
c320d4a
Update src/content/security/index.md
minimalsm Sep 17, 2021
dd2f638
Update src/content/security/index.md
minimalsm Sep 17, 2021
806fd12
Updates based on feedback
minimalsm Sep 21, 2021
5439fab
Sentence case title
minimalsm Sep 21, 2021
a9beaa6
Update src/content/security/index.md
minimalsm Sep 23, 2021
0f98bf9
Update src/content/security/index.md
minimalsm Sep 23, 2021
8b6d574
Update src/content/security/index.md
minimalsm Sep 23, 2021
11428b5
Update src/content/security/index.md
minimalsm Sep 23, 2021
3be7c3c
Apply suggestions from code review
minimalsm Sep 27, 2021
58d4135
Add to menus and add copy
minimalsm Sep 27, 2021
1f69646
Add authors
minimalsm Sep 27, 2021
2229c4d
Add breadcrumbs text
minimalsm Sep 27, 2021
343512a
adds available `zh` tutorials
wackerow Sep 27, 2021
ffcc7e1
fix translation compiling bugs
wackerow Sep 27, 2021
a2851d0
compiler fixes
wackerow Sep 27, 2021
fd1643f
styling cleanup
wackerow Sep 27, 2021
99c9751
Merge pull request #3872 from ethereum/securityPage
minimalsm Sep 27, 2021
c625e90
Merge pull request #4051 from ethereum/zh-tutorials
minimalsm Sep 27, 2021
1c5dd30
v2.42.1
minimalsm Sep 27, 2021
3896bad
Merge pull request #4055 from ethereum/v2_42_1
minimalsm Sep 27, 2021
fb89e4d
copy patch
wackerow Sep 27, 2021
bb3435f
Merge pull request #4057 from ethereum/security-patch
minimalsm Sep 27, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ethereum-org-website",
"version": "2.42.0",
"version": "2.42.1",
"description": "Website of ethereum.org",
"main": "index.js",
"repository": "[email protected]:ethereum/ethereum-org-website.git",
Expand Down
4 changes: 4 additions & 0 deletions src/components/Footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ const Footer = () => {
text: "ethereum-2-0",
to: "/eth2/",
},
{
text: "ethereum-security",
to: "/security/",
},
{
to: `/glossary/`,
text: "ethereum-glossary",
Expand Down
4 changes: 4 additions & 0 deletions src/components/Nav/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ const Nav = ({ handleThemeChange, isDarkTheme, path }) => {
text: "nft-page",
to: "/nft/",
},
{
text: "ethereum-security",
to: "/security/",
},
{
text: "history-of-ethereum",
to: "/history/",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Creating an account with Alchemy is easy, [sign up for free here](https://dashbo

## 2\. Create an Alchemy App {#create-an-alchemy-app}

To communicte with the Ethereum chain and to use Alchemy’s products, you need an API key to authenticate your requests.
To communicate with the Ethereum chain and to use Alchemy’s products, you need an API key to authenticate your requests.

You can [create API keys from the dashboard](http://dashboard.alchemyapi.io/). To make a new key, navigate to “Create App” as shown below:

Expand Down Expand Up @@ -60,7 +60,7 @@ curl https://eth-mainnet.alchemyapi.io/v2/demo \
-d '{"jsonrpc":"2.0","method":"eth_gasPrice","params":[],"id":73}'
```

**_NOTE:_** _Replace_ [_https://eth-mainnet.alchemyapi.io/v2/demo_](https://eth-mainnet.alchemyapi.io/jsonrpc/demo) _with your own API key_ [_https://eth-mainnet.alchemyapi.io/v2/your-api-key_](https://eth-mainnet.alchemyapi.io/jsonrpc/your-api-key)_._
_**NOTE:** Replace [https://eth-mainnet.alchemyapi.io/v2/demo](https://eth-mainnet.alchemyapi.io/jsonrpc/demo) with your own API key https://eth-mainnet.alchemyapi.io/v2/**your-api-key**._

**Results:**

Expand Down
Binary file added src/content/security/appleTwitterScam.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/content/security/brokerScam.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/content/security/discordScam.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
270 changes: 270 additions & 0 deletions src/content/security/index.md

Large diffs are not rendered by default.

Binary file added src/content/security/passwordManager.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/content/security/youtubeScam.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
---
title: "Waffle使用hardhat和ethers设置hello world的教程"
description: 使用hardhat和ethers.js制作您的第一个Waffle项目
author: "MiZiet"
tags:
- "waffle"
- "智能合约"
- "solidity"
- "测试"
- "hardhat"
- "ethers.js"
skill: 入门级
lang: zh
sidebar: true
published: 2020-10-16
---

### 在这个[Waffle](https://ethereum-waffle.readthedocs.io)教程中,我们将学习如何使用[hardhat](https://hardhat.org/)和[ethers.js](https://docs.ethers.io/v5/)设置一个简单的“Hello world”智能合约项目。 然后我们将学习如何向我们的智能合约中添加一个新功能,以及如何使用 Waffle 测试它。

让我们从创建新项目开始:

```bash
yarn init
```


```bash
npm init
```

并安装所需的软件包:

```bash
yarn add -D hardhat
@nomiclabs/hardhat-ethers ethers
@nomiclabs/hardhat-waffle ethereum-waffle chai
```


```bash
npm install -D hardhat
@nomiclabs/hardhat-ethers ethers
@nomiclabs/hardhat-waffle ethereum-waffle chai
```

下一步是通过运行`npx hardhat`创建一个示例 hardhat 项目。

```bash
888 888 888 888 888
888 888 888 888 888
888 888 888 888 888
8888888888 8888b. 888d888 .d88888 88888b. 8888b. 888888
888 888 "88b 888P" d88" 888 888 "88b "88b 888
888 888 .d888888 888 888 888 888 888 .d888888 888
888 888 888 888 888 Y88b 888 888 888 888 888 Y88b.
888 888 "Y888888 888 "Y88888 888 888 "Y888888 "Y888

👷 Welcome to Hardhat v2.0.3 👷‍

? What do you want to do? …
Bachan
Mise en contexte. Est ce des lignes de code, dans ce cas ce n'est pas traduisible
❯ Create a sample project
Create an empty hardhat.config.js
Quit
```

选择`Create a sample project`(创建示例项目)

我们的项目结构应如下所示:

```
MyWaffleProject
├── contracts
│ └── Greeter.sol
├── node_modules
├── scripts
│ └── sample-script.js
├── test
│ └── sample-test.js
├── .gitattributs
├── .gitignore
├── hardhat.config.js
└── package.json
```

### 现在让我们来谈谈其中一些文件:

- Greeter.sol - 我们的智能合约是用 solidity 编写的;

```solidity
contract Greeter {
string greeting;

constructor(string memory _greeting) public {
console.log("Deploying a Greeter with greeting:", _greeting);
greeting = _greeting;
}

function greet() public view returns (string memory) {
return greeting;
}

function setGreeting(string memory _greeting) public {
console.log("Changing greeting from '%s' to '%s'", greeting, _greeting);
greeting = _greeting;
}
}
```

我们的智能合约可以分为三个部分:

1. 构造函数 - 我们在其中声明一个字符串类型变量,名为`greeting`,
2. 函数 greet - 调用时返回`greeting`的函数,
3. 函数 setGreeting - 允许我们更改`greeting`值的函数。

- sample-test.js - 我们的测试文件

```js
describe("Greeter", function () {
it("Should return the new greeting once it's changed", async function () {
const Greeter = await ethers.getContractFactory("Greeter")
const greeter = await Greeter.deploy("Hello, world!")

await greeter.deployed()
expect(await greeter.greet()).to.equal("Hello, world!")

await greeter.setGreeting("Hola, mundo!")
expect(await greeter.greet()).to.equal("Hola, mundo!")
})
})
```

### 下一步是编译我们的合约并运行测试:

Waffle 测试使用 Mocha(测试框架)与 Chai(一个断言库)。 您只需运行`npx hardhat test`并等待以下消息出现。

```bash
✓ Should return the new greeting once it's changed(更改后应立即返回新问候语)
```

### 到目前为止,一切看起来都很好,让我们将一些更加复杂的内容添加到项目:)

想象一下,如果有人添加了一个空字符串作为问候语。 这不是一种热情的问候,对吗?
让我们确保这一点不会发生:

当有人传递空字符串时,我们想使用 solidity 的`revert`。 一件好事是,我们可以使用 Waffle 的 chai 匹配器`to.bo.revertedWith()`轻松测试此功能。

```js
it("Should revert when passing an empty string", async () => {
const Greeter = await ethers.getContractFactory("Greeter")
const greeter = await Greeter.deploy("Hello, world!")

await greeter.deployed()
await expect(greeter.setGreeting("")).to.be.revertedWith(
"Greeting should not be empty"
)
})
```

看起来我们的新测试没通过:

```bash
Deploying a Greeter with greeting: Hello, world!
Changing greeting from 'Hello, world!' to 'Hola, mundo!'
✓ Should return the new greeting once it's changed (1514ms)
Deploying a Greeter with greeting: Hello, world!
Changing greeting from 'Hello, world!' to ''
1) Should revert when passing an empty string


1 passing (2s)
1 failing
```

让我们在智能合约中实现这个功能:

```solidity
require(bytes(_greeting).length > 0, "Greeting message is empty");
```

现在,我们的 setGreeting 函数如下所示:

```solidity
function setGreeting(string memory _greeting) public {
require(bytes(_greeting).length > 0, "Greeting should not be empty");
console.log("Changing greeting from '%s' to '%s'", greeting, _greeting);
greeting = _greeting;
}
```

让我们再次运行测试:

```bash
✓ Should return the new greeting once it's changed (1467ms)
✓ Should revert when passing an empty string (276ms)

2 passing (2s)
```

恭喜! 您做到了:)

### 结论

我们使用 Waffle、Hardhat 和 ethers.js 制作了一个简单的项目。 我们学习了如何设置项目、添加测试和实现新功能。

欲了解更多用于测试您的智能合约的优秀 chai 匹配器,请查看[官方 Waffle 文档](https://ethereum-waffle.readthedocs.io/en/latest/matchers.html)。
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
---
title: 通过JavaScript调用智能合约
description: 以DAI通证为例展示如何通过JavaScript调用智能合约函数
author: jdourlens
tags:
- "交易"
- "前端"
- "javascript"
- "web3.js"
skill: 初学者
lang: zh
sidebar: true
published: 2020-04-19
source: EtherumDev
sourceUrl: https://ethereumdev.io/calling-a-smart-contract-from-javascript/
address: "0x19dE91Af973F404EDF5B4c093983a7c6E3EC8ccE"
---

在本教程中,我们将会看到如何通过 JavaScript 调用[智能合约](/developers/docs/smart-contracts/)。 首先读取智能合约的状态(例如:ERC20 持有者的余额),然后通过通证转账修改区块链的状态。 您首先需要了解[设置 JS 环境与区块链交互](/developers/tutorials/set-up-web3js-to-use-ethereum-in-javascript/)。

在本例中,我们将使用 DAI 通证,基于测试目的,我们将使用 ganache-cli 分叉区块链并解锁一个已经拥有很多 DAI 的地址。

```bash
ganache-cli -f https://mainnet.infura.io/v3/[YOUR INFURA KEY] -d -i 66 1 --unlock 0x4d10ae710Bd8D1C31bd7465c8CBC3add6F279E81
```

要与智能合约交互,我们需要它的地址和 ABI:

```js
const ERC20TransferABI = [
{
constant: false,
inputs: [
{
name: "_to",
type: "address",
},
{
name: "_value",
type: "uint256",
},
],
name: "transfer",
outputs: [
{
name: "",
type: "bool",
},
],
payable: false,
stateMutability: "nonpayable",
type: "function",
},
{
constant: true,
inputs: [
{
name: "_owner",
type: "address",
},
],
name: "balanceOf",
outputs: [
{
name: "balance",
type: "uint256",
},
],
payable: false,
stateMutability: "view",
type: "function",
},
]

const DAI_ADDRESS = "0x6b175474e89094c44da98b954eedeac495271d0f"
```

对于此项目,我们剥离完整的 ERC20 ABI ,仅保留`balanceOf`和`transfer`函数,不过您可以在这里获取完整的[ERC20 ABI](https://ethereumdev.io/abi-for-erc20-contract-on-ethereum/)。

然后我们需要实例化我们的智能合约:

```js
const web3 = new Web3("http://localhost:8545")

const daiToken = new web3.eth.Contract(ERC20TransferABI, DAI_ADDRESS)
```

我们还会设置两个地址:

- 一个接受转账
- 一个已经解锁并将发送

```js
const senderAddress = "0x4d10ae710Bd8D1C31bd7465c8CBC3add6F279E81"
const receiverAddress = "0x19dE91Af973F404EDF5B4c093983a7c6E3EC8ccE"
```

在下一部分中,我们会调用 `balance Of` 函数来检索当前的代币数量,此时这两个地址的代币数量都被冻结。

## 调用:从智能合约读取值 {#call-reading-value-from-a-smart-contract}

第一个例子,将调用“常量(constant)”方法并且在 EVM 中执行这个智能合约方法,并不发送任何交易。 为此我们将读取一个地址的 ECR20 余额。 [阅读关于 ECR20 代币的文章](/developers/tutorials/understand-the-erc20-token-smart-contract/).

您可以访问一个实例化的智能合约方法,用这种方式提供 ABI: `yourContract.methods.methodname`。 通过使用`call`函数,您可以接收执行函数的结果。

```js
daiToken.methods.balanceOf(senderAddress).call(function (err, res) {
if (err) {
console.log("An error occured", err)
return
}
console.log("The balance is: ", res)
})
```

请记住,DAI ERC20 有 18 位小数,这意味着您需要移除 18 个零才能获得正确的数额。 uint256 将以字符串形式返回,因为 Javascript 不处理大数值。 如果不确定,请了解我们关于 bignumber.js 的教程[如何在 JS 中处理大数](https://ethereumdev.io/how-to-deal-with-big-numbers-in-javascript/)。

## 发送:将交易发送给智能合约函数 {#send-sending-a-transaction-to-a-smart-contract-function}

对于第二个示例,我们将调用 DAI 智能合约的 transfer 函数,发送 10 个 DAI 到第二个地址。 transfer 函数接受两个参数:收件人地址和转账代币的数量:

```js
daiToken.methods
.transfer(receiverAddress, "100000000000000000000")
.send({ from: senderAddress }, function (err, res) {
if (err) {
console.log("An error occured", err)
return
}
console.log("Hash of the transaction: " + res)
})
```

调用函数返回将被在区块链中挖矿(mine) 的交易的哈希。 在以太坊中,交易哈希是可以预测的 - 这里是我们在执行前获得交易哈希值的方法([了解如何计算哈希](https://ethereum.stackexchange.com/questions/45648/how-to-calculate-the-assigned-txhash-of-a-transaction))。

因为该函数只向区块链提交交易,除非通过挖矿并包含在区块链中,否则我们无法看到结果。 在下一个教程中,我们将学习[如何通过了解交易的哈希来等待交易在区块链上执行](https://ethereumdev.io/waiting-for-a-transaction-to-be-mined-on-ethereum-with-js/)。
Loading