GithubHelp home page GithubHelp logo

cmeone / chia-utils Goto Github PK

View Code? Open in Web Editor NEW
20.0 2.0 9.0 151 KB

A set of JavaScript utilities to facilitate conversions for the Chia network

License: GNU Affero General Public License v3.0

JavaScript 100.00%
chia chia-blockchain chia-network bech32 hashing cryptography cryptocurrency

chia-utils's Introduction

Unofficial Library: Chia-Utils

Build Status Version Version Monthly Downloads Total Downloads

A set of JavaScript utilities to facilitate conversions for the Chia network

Table of Contents

Install

To install from NPM:

npm install chia-utils

To install from GitHub:

git clone https://github.com/CMEONE/chia-utils.git

To test:

npm install
npm test

To include from NPM:

const { address_to_puzzle_hash, puzzle_hash_to_address, get_coin_info, get_coin_info_mojo, bytes_to_hex, hex_to_bytes } = require("chia-utils");

To include from file:

const { address_to_puzzle_hash, puzzle_hash_to_address, get_coin_info, get_coin_info_mojo, bytes_to_hex, hex_to_bytes } = require("./chia-utils.js");

To use in the browser:

<script type="text/javascript" src="chia-utils.js"></script>
<script>
  ChiaUtils.address_to_puzzle_hash( ... )
  ChiaUtils.puzzle_hash_to_address( ... )
  ChiaUtils.get_coin_info( ... )
  ChiaUtils.get_coin_info_mojo( ... )
  ChiaUtils.bytes_to_hex( ... )
  ChiaUtils.hex_to_bytes( ... )
</script>

Address to Puzzle Hash

let puzzle_hash = address_to_puzzle_hash(address);

Parameters:

  • address: string ("xch1...")

Return type: string ("0x...")

Puzzle Hash to Address

let address = puzzle_hash_to_address(puzzle_hash, prefix = "xch");

Parameters:

  • puzzle_hash: string ("0x...")
  • prefix: string ("xch") - default: "xch"

Return type: string ("xch1...")

Get Coin Info

let coin_info = get_coin_info(parent_coin_info, puzzle_hash, amount_decimal);

Parameters:

  • parent_coin_info: string ("0x...")
  • puzzle_hash: string ("0x...")
  • amount_decimal: number (0.01)

Return type: string ("0x...")

Get Coin Info Mojo

let coin_info = get_coin_info_mojo(parent_coin_info, puzzle_hash, amount);

Parameters:

  • parent_coin_info: string ("0x...")
  • puzzle_hash: string ("0x...")
  • amount: number (1000000000000)

Return type: string ("0x...")

Bytes to Hex

let hex = bytes_to_hex(bytes);

Parameters:

  • bytes: Array or Uint8Array ([0, 255, ...])

Return type: string ("fedcba9876543210...")

Hex to Bytes

let bytes = hex_to_bytes(hex);

Parameters:

  • hex: string ("fedcba9876543210...")

Return type: Uint8Array ([0, 255, ...])

chia-utils's People

Contributors

cmeone avatar dkackman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

chia-utils's Issues

get_coin_info bug

var amount = 0.5994
var parent_coin_info = '0x5db7892bd03b7ddc6368646250d7bafeff19bf64517c1b901d32ce3bd8ce3e7d'
var puzzle_hash = '0xc2f1ff148e091196c8cf415db67f8381ab0d91b5b71ee7841a1bbd96b2923f44'

get_coin_info result is 0xc8fa5267c1cff10a860e715fedf0d423a6399cf4d7b1cfe398e9d737c66bb302, different from site:

https://www.chiaexplorer.com/blockchain/coin/0x226bee197e358e60c25d907d5ad2db0c4508861a1d66b8438602bb31ac43aa0c

the amountHex result is '8b8f062a00', but the right result is '008b8f062a00'

I use this code to fix

  const fixPreLength = (num, len) => (Array(len).join(0) + num).slice(-len)
  let amountHex = amount.toString(16)
  const byte_count = (amount.toString(2).length + 8) >> 3
  amountHex = fixPreLength(amountHex, byte_count * 2)

so, how the get_coin_info function to do with this bug?

瀏覽器運行報錯

TypeError: Property left of AssignmentExpression expected node to be of a type ["LVal"] but instead got "BooleanLiteral"

Can't import properly in Vue 2.x app

I'm trying to use chia-utils in Vue 2 app, code looks like this in main.js:

import { puzzle_hash_to_address } from 'chia-utils'

The error message in browser (Firefox) is:
Uncaught TypeError: window.Buffer is undefined chia-utils.js:9863

I guess it was because in Vue 2.x, window object only accessible after mounted, but chia-utils tried to access window.Buffer just inside import initialization code.
Any way to solve this?

Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.