帮酷LOGO
  • 显示原文与译文双语对照的内容
Decoder and encoder for the Ethereum ABI

  • 源代码名称:ethereumjs-abi
  • 源代码网址:http://www.github.com/ethereumjs/ethereumjs-abi
  • ethereumjs-abi源代码文档
  • ethereumjs-abi源代码下载
  • Git URL:
    git://www.github.com/ethereumjs/ethereumjs-abi.git
  • Git Clone代码到本地:
    git clone http://www.github.com/ethereumjs/ethereumjs-abi
  • Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/ethereumjs/ethereumjs-abi
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
  • ethereumjs-abi

    NPM PackageBuild StatusCoverage Statusjs-standard-styleGitter freenode上的或者 #ethereumjs

    模块在Javascript中实现 Ethereum ABI插件。 可以与RPC库一起用于通信,也可以使用 ethereumjs-vm实现完全成熟的模拟器。

    用法

    手动编码和解码

    有三种感兴趣的方法:

    • methodID 创建函数签名
    • rawEncode 编码字段和字段
    • rawDecode 解码字段

    示例代码:

    var abi =require('ethereumjs-abi')// returns the encoded binary (as a Buffer) data to be sentvar encoded =abi.rawEncode([ "address" ], [ "0x0000000000000000000000000000000000000000" ])// returns the decoded array of argumentsvar decoded =abi.rawDecode([ "address" ], data)
    基于 JSON ABI定义的编码和解码

    为将来的计划提供支持的JSON ABI定义:

    var abi =require('ethereumjs-abi')// need to have the ABI definition in JSON as per specificationvar tokenAbi = [{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"success","type":"bool"}],"type":"function"},{"inputs":[],"type":"constructor"}]var encoded =abi.encode(tokenAbi, "balanceOf(uint256 address)", [ "0x0000000000000000000000000000000000000000" ])var decoded =abi.decode(tokenAbi, "balanceOf(uint256 address)", data)
    简单的编码和解码
    var abi =require('ethereumjs-abi')// returns the encoded binary (as a Buffer) data to be sentvar encoded =abi.simpleEncode("balanceOf(address):(uint256)", "0x0000000000000000000000000000000000000000")// returns the decoded array of argumentsvar decoded =abi.simpleDecode("balanceOf(address):(uint256)", data)
    坚固紧凑的格式

    这个库还支持创建紧密的封装数据结构,它们与 sha3sha256ripemd160 一起使用来创建哈希数据。

    坚固代码:

    contract HashTest {
     functiontestSha3() returns (bytes32) {
     address addr1 =0x43989fb883ba8111221e89123897538475893837;
     address addr2 =0;
     uint val =10000;
     uint timestamp =1448075779;
     returnsha3(addr1, addr2, val, timestamp); // will return 0xc3ab5ca31a013757f26a88561f0ff5057a97dfcc33f43d6b479abc3ac2d1d595 }
    }

    使用这里库创建相同的哈希值:

    var abi =require('ethereumjs-abi')varBN=require('bn.js')abi.soliditySHA3(
     [ "address", "address", "uint", "uint" ],
     [ newBN("43989fb883ba8111221e89123897538475893837", 16), 0, 10000, 1448075779 ]
    ).toString('hex')

    对于相同的数据结构:

    • sha3将返回 0xc3ab5ca31a013757f26a88561f0ff5057a97dfcc33f43d6b479abc3ac2d1d595
    • 返回返回值 0x344d8cb0711672efbdfe991f35943847c1058e1ecf515ff63ad936b91fd16231
    • ripemd160将返回 0x000000000000000000000000a398cc72490f72048efa52c4e92067e8499672e7 ( 注意:它是 160位,左填充到 256位)

    请注意,ripemd160() 中的返回bytes20并且如果将它转换为 bytes32,则将它用零填充。

    使用Serpent类型的

    对类型使用不同的符号,即使它将序列化为同一个 dce 。

    我们提供两个帮助来在这些符号之间进行转换:

    • fromSerpent: 将蛇形符号转换为ABI表示法
    • toSerpent: 另一种方式

    使用方法的示例:

    abi.fromSerpent('s') // [ 'bytes' ]abi.fromSerpent('i') // [ 'int256' ]abi.fromSerpent('a') // [ 'int256[]' ]abi.fromSerpent('b8') // [ 'bytes8' ]abi.fromSerpent('b8i') // [ 'bytes8', 'int256' ]abi.toSerpent([ 'bytes' ]) // 's'abi.toSerpent([ 'int256' ]) // 'i'abi.toSerpent([ 'int256[]' ]) // 'a'abi.toSerpent([ 'bytes8' ]) // 'b8'abi.toSerpent([ 'bytes8', 'int256' ]) // 'b8i'

    它将与 rawEncoderawDecode 一起使用:

    var encoded =abi.rawEncode("balanceOf", abi.fromSerpent("i"), [ "0x0000000000000000000000000000000000000000" ])var decoded =abi.rawDecode("balanceOf", abi.fromSerpent("i"), abi.fromSerpent("i"), data)

    注意:snake使用了任意的二进制字段。 如果你想要存储字符串,最好确保它存储为 UTF8. 可以使用 new Buffer(<string>,'utf8') 来确保它被正确编码。

    我很高兴能收到。 请向我发送请求请求或者联系电子邮件或者 Twitter 。

    在源代码中有很多缺失,在源代码中使用grep来找到灵感。

    许可证

    Copyright (C) 2015 Alex Beregszaszi
    Permission is hereby granted, free of charge, to any person obtaining a copy of
    this software and associated documentation files (the"Software"), to deal in
    the Software without restriction, including without limitation the rights to
    use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
    the Software, and to permit persons to whom the Software is furnished to do so,
    subject to the following conditions:
    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.
    THE SOFTWARE IS PROVIDED"AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
    FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
    COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
    IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.



    Copyright © 2011 HelpLib All rights reserved.    知识分享协议 京ICP备05059198号-3  |  如果智培  |  酷兔英语