npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sero-web3

v0.3.3

Published

Sero JavaScript API, middleware to talk to a sero node over RPC

Downloads

15

Readme

console web3.js

This is the Sero compatible JavaScript API which base on ethereum/web3.js version with v0.20.6 latest maintenance release .It is used by gero client as embed js that can interract with the Sero blockchain. It alse can be as a reference for dApps to interract with the Sero blockchain.

Differences from web3.js

1. value of unit

The minimum unit of Sero is ta, and one sero is equivalent to 10 to the 9th power ta.


var unitMap = {
    'noether':      '0',
    'ta':           '1',
    'kta':          '1000',
    'Kta':          '1000',
    'babbage':      '1000',
    'femtoether':   '1000',
    'mta':          '1000000',
    'Mta':          '1000000',
    'lovelace':     '1000000',
    'picoether':    '1000000',
    'gta':          '1000000000',
    'Gta':          '1000000000',
    'shannon':      '1000000000',
    'nanosero':     '1000000000',
    'nano':         '1000000000',
    'szabo':        '1000000000000',
    'microsero':    '1000000000000',
    'micro':        '1000000000000',
    'finney':       '1000000000000000',
    'millisero':    '1000000000000000',
    'milli':        '1000000000000000',
    'sero':         '1000000000000000000',
    'SERO':         '1000000000000000000',
    'ksero':        '1000000000000000000000',
    'gsero':        '1000000000000000000000',
    'msero':        '1000000000000000000000000',
    'gsero':        '1000000000000000000000000000',
};
/**
 * Takes a number of Ta and converts it to any other sero unit.
 *
 *
 * @method fromTa
 * @param {Number|String} number can be a number, number string or a HEX of a decimal
 * @param {String} unit the unit to convert to, default ether
 * @return {String|Object} When given a BigNumber object it returns one as well, otherwise a number
 */
var fromTa = function (number, unit) {
    var returnValue = toBigNumber(number).dividedBy(getValueOfUnit(unit));

    return isBigNumber(number) ? returnValue : returnValue.toString(10);
};

/**
 * Takes a number of a unit and converts it to ta.
 * @method toTa
 * @param {Number|String|BigNumber} number can be a number, number string or a HEX of a decimal
 * @param {String} unit the unit to convert from, default ether
 * @return {String|Object} When given a BigNumber object it returns one as well, otherwise a number
 */
var toTa = function (number, unit) {
    var returnValue = toBigNumber(number).times(getValueOfUnit(unit));

    return isBigNumber(number) ? returnValue : returnValue.toString(10);
};

2. address

All the address is showed as base58 string


   /**
    * Convert a byte array to a base58 string
    *
    * @method bytesToBase58
    * @param {Array} bytes
    * @return {String} the base58 string
    */
   var bytesToBase58 = function (bytes) {
       return base58.encode(bytes);
   };
   
   /**
    * Convert a base58 string to a byte array
    *
    * @method base58ToBytes
    * @param {String} the base58 string
    * @return {Array} bytes
    */
   var base58ToBytes = function (bs58) {
       return base58.decode(bs58);
   }

    /**
    * Checks if the given string is strictly an address
    *
    * @method isStrictAddress
    * @param {String} address the given base58 adress
    * @return {Boolean}
    */
   var isStrictAddress = function (address) {
   
       if (/^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{84,103}$/i.test(address)) {
           bytes = base58ToBytes(address)
           if (bytes.length !== 128) {
               return false;
           }
           return true;
       } else {
           return false;
       }
   };

3.contract data

create or call smart contract,the address type params must convert to once address


SolidityCoder.prototype.opParams = function (types, params,rand,sero,dy) {
    var solidityTypes = this.getSolidityTypes(types);
    var addressParams = solidityTypes.map(function (solidityType, index) {
        if (solidityType instanceof SolidityTypeAddress){
            return  solidityType.address(params[index], types[index],false)
        }else{
            return
        }
    })[0]||'';
    if (addressParams){
        if (utils.isArray(addressParams)){
        }else{
            addressParams = [addressParams];
        }
    }else {
        addressParams = [];
    }

    var convertResult =  sero.convertAddressParams(rand,addressParams,dy);
    rand = convertResult.rand;
    if (addressParams.length >0 ){
        var addrMap = convertResult.addr;
         var convertParams =  solidityTypes.map(function (solidityType, index) {
            return  solidityType.convertAddress(params[index], types[index],addrMap)
        });
         params = convertParams;
    }
    var result ={};
    result.params = params;
    result.rand = rand;
    return result;

};

the create smart contract data:


    var rand ="0x";
    var argsResult = opArgs(this.abi,args,rand,this.sero,false);
    args = argsResult.params;
    rand = argsResult.rand;
    var bytes = encodeConstructorParams(this.abi, args);
    options.data += bytes;
    var prefix = encodeConstructorPrefix(this.abi,args,rand);
    options.data = prefix +options.data.substr(2);
        

the call smart contract data:


    var rand = utils.bytesToHex(utils.base58ToBytes(this._address).slice(0,16));
    var convertResult = coder.opParams(this._inputTypes,args,rand,this._sero,dy);
    args = convertResult.params;
    options.data = coder.addressPrefix(this._inputTypes,args,rand) + this.signature()+ coder.encodeParams(this._inputTypes, args);

5. sendTransaction params

The method of SendTransaction Json params add cy、dy、catg,tkt key. The dy only be used when call contract that means whether to regenerate a one-time address. The cy is the currency unit of the transaction, default is sero ,the catg is the Ticket category and the tkt is the Ticket Id.

   {
   	from: sero.accounts[0],
   	to: sero.accounts[1],
   	value: 1000,
   	gas: 30000,
   	gasPrice: 5,
   	data: [],
   	cy: 'sero',
   	dy: false,
   	catg: '',
   	tkt: ''
   }

6. transaction info

The showed transaction info add Zero knowledge proof


/**
 * Formats the output of a transaction to its proper values
 *
 * @method outputTransactionFormatter
 * @param {Object} tx
 * @returns {Object}
*/
var outputTransactionFormatter = function (tx){
    if(tx.blockNumber !== null)
        tx.blockNumber = utils.toDecimal(tx.blockNumber);
    if(tx.transactionIndex !== null)
        tx.transactionIndex = utils.toDecimal(tx.transactionIndex);
    tx.nonce = utils.toDecimal(tx.nonce);
    tx.gas = utils.toDecimal(tx.gas);
    tx.gasPrice = utils.toBigNumber(tx.gasPrice);
    tx.value = utils.toBigNumber(tx.value);
    if (tx.stx.Desc_O){
        tx.stx.Desc_O_Ins=tx.stx.Desc_O.Ins;
        var O_Outs=[];
        tx.stx.Desc_O.Outs.forEach(function(out){
            var out_O ={};
            if (utils.toBigNumber(out.Addr)!=0) {
                out_O.Addr = out.Addr;
            }
            if (out.Asset.Tkn){
                out_O.Currency = utils.bytesToString(utils.hexToBytes(utils.fromDecimal(utils.toBigNumber(out.Asset.Tkn.Currency))));
                out_O.Value = utils.toBigNumber(out.Asset.Tkn.Value);
            }
            if (out.Asset.Tkt){
                out_O.Category = utils.bytesToString(utils.hexToBytes(utils.fromDecimal(utils.toBigNumber(out.Asset.Tkt.Category))));
                out_O.TktId = out.Asset.Tkt.Value;
            }
            out_O.Memo = out.Memo;
            O_Outs.push(out_O);
        });
        tx.stx.Desc_O_Outs = O_Outs;
        delete  tx.stx.Desc_O;
    }
    if (tx.stx.Desc_Z){
        tx.stx.Desc_Z_Ins=tx.stx.Desc_Z.Ins;
        tx.stx.Desc_Z_Outs = tx.stx.Desc_Z.Outs;
        delete  tx.stx.Desc_Z;
    }
    return tx;
};

6. add isMineOAddr

Add a method in sero.js ,determine if it is your own one-time address

 var isMineOAddr = new Method({
        name: 'isMineOAddr',
        call: 'sero_isMineOAddr',
        params: 1,
        inputFormatter: [formatters.inputAddressFormatter]
    });

7. add convertAddressParams

Add a method in sero.js ,convert the address to once address with the given rand;


    var convertAddressParams = new Method({
        name: 'convertAddressParams',
        call: 'sero_convertAddressParams',
        params: 3,
    });
    

7. add getFullAddress

Add a method in sero.js ,get the full address with the vm short address;


    var getFullAddress = new Method({
        name: 'getFullAddress',
        call: 'sero_getFullAddress',
        params: 1,
    });
    

Building (gulp)

npm run-script build

License

LGPL-3.0+ © 2015 Contributors