Web3.js 란?
- 이더리움 블록체인과 상호작용하는 데 사용되는 유명한 자바스크립트 라이브러리
- 모든 프론트엔드 프레임워크에서 사용자가 탈중앙화 애플리케이션을 이용가능하게 함
❐ web3.eth
: 이더리움 블록체인과 이더리움 스마트 계약과 상호작용
1) web3.eth.personal
: 이더리움 노드의 계정과 상호작용
const Web3EthPersonal = require('web3-eth-personal');
const personal = new Web3EthPersonal('ws://localhost:8546');
2) web3.eth.personal.sign
web3.eth.personal.sign(dataToSign, address, password [, callback])
[매개변수]
- dataToSign : (String) 서명할 데이터 (문자열일 경우 web3.utils.utf8ToHex를 사용하여 변환)
- address : (String) 서명할 주소
- password : (String) 데이터에 서명할 계정의 암호
- (Function) : 선택적 콜백 (첫 번째 매개 변수로 오류 개체를 반환하고 두 번째 매개 변수로 결과를 반환)
web3.eth.personal.sign("Hello world", "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe", "test password!")
.then(console.log);
> "0x30755ed65396facf86c53e6217c52b4daebe72aa4941d89635409de4c9c7f9466d4e9aaec7977f05e923889b33c0d0dd27d7226b6e6f56ce737465c5cfd04be400"
// the below is the same
web3.eth.personal.sign(web3.utils.utf8ToHex("Hello world"), "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe", "test password!")
.then(console.log);
> "0x30755ed65396facf86c53e6217c52b4daebe72aa4941d89635409de4c9c7f9466d4e9aaec7977f05e923889b33c0d0dd27d7226b6e6f56ce737465c5cfd04be400"
3) web3.eth.personal.ecRecover
web3.eth.personal.ecRecover(dataThatWasSigned, signature [, callback])
: 데이터 검증
[매개변수]
- dataThatWasSigned : (String) 서명된 데이터 (문자열의 경우, web3.utils.utf8ToHex를 사용하여 변환)
- signature : (String) 서명
- (Function) : 선택적 콜백
web3.eth.personal.ecRecover("Hello world", "0x30755ed65396facf86c53e6217c52b4daebe72aa4941d89635409de4c9c7f9466d4e9aaec7977f05e923889b33c0d0dd27d7226b6e6f56ce737465c5cfd04be400").then(console.log);
> "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe"
4) web3.eth.personal.signTransaction
web3.eth.personal.signTransaction(transaction, password [, callback])
: 거래에 서명한다.
[매개변수]
- transaction : (Object) web3.eth.sendTransaction()에 서명하기 위한 트랜잭션 데이터
- password : (String) 암호
- (Function) : 선택적 콜백
web3.eth.signTransaction({
from: "0xEB014f8c8B418Db6b45774c326A0E64C78914dC0",
gasPrice: "20000000000",
gas: "21000",
to: '0x3535353535353535353535353535353535353535',
value: "1000000000000000000",
data: "" }, 'MyPassword!')
.then(console.log);
> { raw: '0xf86c808504a817c800825208943535353535353535353535353535353535353535880de0b6b3a76400008025a04f4c17305743700648bc4f6cd3038ec6f6af0df73e31757007b7f59df7bee88da07e1941b264348e80c78c4027afc65a87b0a5e43e86742b8ca0823584c6788fd0'
tx: {
nonce: '0x0',
gasPrice: '0x4a817c800',
gas: '0x5208',
to: '0x3535353535353535353535353535353535353535',
value: '0xde0b6b3a7640000',
input: '0x',
v: '0x25',
r: '0x4f4c17305743700648bc4f6cd3038ec6f6af0df73e31757007b7f59df7bee88d',
s: '0x7e1941b264348e80c78c4027afc65a87b0a5e43e86742b8ca0823584c6788fd0',
hash: '0xda3be87732110de6c1354c83770aae630ede9ac308d9f7b399ecfba23d923384'
}
}
5) web3.eth.personal.sendTransaction
web3.eth.personal.sendTransaction(transactionOptions, password [, callback])
: the management API를 통해 트랜잭션을 전송한다
[매개변수]
- transactionOptions : 트랜잭션 옵션
- password : 현재 계정에 대한 암호
- (Function) : 선택적 콜백
web3.eth.sendTransaction({
from: "0xEB014f8c8B418Db6b45774c326A0E64C78914dC0",
gasPrice: "20000000000",
gas: "21000",
to: '0x3535353535353535353535353535353535353535',
value: "1000000000000000000",
data: "" }, 'MyPassword!')
.then(console.log);
> '0xda3be87732110de6c1354c83770aae630ede9ac308d9f7b399ecfba23d923384'
❐ web3.eth.Contract
- 이더리움 블록체인의 스마트 컨트랙트과 상호작용을 용이하게 함
- 자바스크립트 객체인 것처럼 스마트 컨트랙트와 상호작용 가능
- 인자에는 ABI가 들어감 *ABI (Application Binary Interface) : 런타임 시, 바이너리 코드와 상호작용하기 위한 인터페이스
var Contract = require('web3-eth-contract');
// set provider for all later instances to use
Contract.setProvider('ws://localhost:8546');
var contract = new Contract(jsonInterface, address);
contract.methods.somFunc().send({from: ....})
.on('receipt', function(){
...
});
1) web3.eth.Contract.deploy
myContract.deploy(options)
[매개변수]
-options (Object) : 배포에 사용되는 옵션
-data (String) : 계약의 byte code
-arguments (Array) : (선택사항) 배포 시 생성자에게 전달되는 인수
❐ web3.eth.acoounts
1) web3.eth.accounts.sign
: 데이터에 서명한다. 서명 값은 v, r, s 값으로 구성된다. v,r,s 값은 서명값의 byte위치로 계산해도 되지만, web3.eth.accounts.sign() 함수를 이용하면 v,r,s값을 포함한 객체값을 전달받게 된다.
web3.eth.accounts.sign(data, privateKey);
[매개변수]
- data
- privateKey
web3.eth.accounts.sign('Some data', '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318');
> {
message: 'Some data',
messageHash: '0x1da44b586eb0729ff70a73c326926f6ed5a25f5b056e7f47fbc6e58d86871655',
v: '0x1c',
r: '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd',
s: '0x6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a029',
signature: '0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a0291c'
}
❐ web3.utils
: 이더리움 DApp과 기타 web3.js 패키지를 위한 utility 기능 제공. 주로 “값” 변환에 관한 내용들
1) web3.utils.utf8ToHex
web3.utils.utf8ToHex(string)
- string : HEX 문자열로 변환할 UTF-8 문자열
2) web3.utils.sha3
web3.utils.sha3(string)
: 서명할 데이터의 hash 값 획득
※ [비교] web3.eth.sign VS web3.eth.accounts.sign VS web3.eth.personal.sign
node가 없다? ⇒ web3.eth.accounts.sign
unlocked account인 노드다? ⇒ web3.eth**.sign**
locked account인 노드다? ⇒ web3.eth.personal.sign
'개발 > WEB | Front-End' 카테고리의 다른 글
[러닝 타입스크립트] Ch 1. 자바스크립트에서 타입스크립트로 (1) | 2023.12.01 |
---|---|
[React] img 에러 해결 방법 (0) | 2021.05.15 |