1.0.9 • Published 5 years ago

uranus-web3 v1.0.9

Weekly downloads
6
License
LGPL-3.0
Repository
github
Last release
5 years ago

uranus-web3.js

Uranus JavaScript API

安装及引用:

npm i uranus-web3 -S

import * as uranus from 'uranus-web3';

demo1:设置节点信息

节点 rpc 默认是 htp://127.0.0.1:8000,如不是,必须先设置好节点信息,才可进行后续操作

import * as uranus from 'uranus-web3';

console.log(nodeInfo);
web3.utils.default.setEndpoint(nodeInfo); // 设置节点rpc信息

demo2:签名及验证交易

签名交易,返回签名数据

import * as uranus from 'uranus-web3';

const tx = {}
tx.type = 0
tx.nonce = 1
tx.gasPrice = 10000
tx.tos = [
  '0xC08B5542D177ac6686946920409741463a15dDdB',
  '0x970e8128ab834e8eac17ab8e3812f010678cf791'
]
tx.gasLimit = 1000
tx.value = 10000
tx.payload = Buffer.from('sign tx test')

const priv =
  '0x9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658'

var sig = uranus.singer.signTransaction(tx, priv)

console.log('sig ', sig)

验证签名交易,返回地址(交易的发送方)

import * as uranus from 'uranus-web3';

const tx = {}
tx.type = 0
tx.nonce = 1
tx.gasPrice = 10000
tx.tos = [
  '0xC08B5542D177ac6686946920409741463a15dDdB',
  '0x970e8128ab834e8eac17ab8e3812f010678cf791'
]
tx.gasLimit = 1000
tx.value = 10000
tx.payload = Buffer.from('sign tx test')

const signature =
  '0x20ceb32d94ea10f50425233e5d355fa05337d23b50646839c6d0689acdd054557f867ab7e187863a6841efbc19d03ba38762fe24d49815185d5a3f4a7b196ac101'

var addr = uranus.singer.recoverSignedTransaction(tx, signature)


console.log('addr ', addr)

demo3:rlp交易的序列化

import * as uranus from 'uranus-web3';


const tx = {};
tx.type = 0;
tx.nonce = 1;
tx.gasPrice = 10000;
tx.tos = ['0xC08B5542D177ac6686946920409741463a15dDdB', '0x970e8128ab834e8eac17ab8e3812f010678cf791'];
tx.gasLimit = 1000;
tx.value = 10000;
tx.payload = Buffer.from('sign tx test');
signature = uranus.utils.default.hex2Bytes(
   sig
);

var txBytes = uranus.utils.default.ethUtil.rlp.encode([
    tx.type,
    tx.nonce,
    tx.gasPrice,
    tx.tos,
    tx.gasLimit,
    tx.value,
    tx.payload,
    signature
]);

console.log('txBytes ', txBytes.toString('hex'));

demo4:发送交易

发送交易,首先先将交易进行签名(demo1),

demo5: erc20 transfer payload 构建 erc20 转账交易 payload,第一个参数是接收方,第二个是转账金额.

let payload = '0x';
payload += uranus.utils.default.getContractPayload(
    'transfer',
    ['address', 'uint256'],
    ['0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be', '41400000000000000000']
);
1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago