youchain-utils

youchain-utils 这个工具包可以为 DApp 开发者提供一些工具方法。

randomHex

描述:根据给定字节数随机生成十六进制加密的字符串。

参数值:Number 十六进制字节大小

返回值:String 随机生成字符串

youchain.utils.randomHex(size)

示例:

youchain.utils.randomHex(32)
> "0x4cc63d2e121d567b7bf277ad8a9e021499d36b5a838629e62f30dcb3464c4662"
youchain.utils.randomHex(4)
> "0x0fe36c74"
youchain.utils.randomHex(0)
> "0x"

isBN

描述:判断对象是否为大数

参数值: Object BN.js实例

返回值:Boolean

youchain.utils.isBN(number)

示例:

const BN = require('bn.js')
const number = new BN(10);

youchain.utils.isBN(number);
> true

sha3

描述:计算字符串的 sha3 值

参数值:String 原始字符串

返回值:sha3 计算后的结果

youchain.utils.sha3(string)
youchain.utils.keccak256(string)

示例:

youchain.utils.sha3('234'); // 字符串
> "0xc1912fee45d61c87cc5ea59dae311904cd86b84fee17cc96966216f811ce6a79"

youchain.utils.sha3(new BN('234'));
> "0xbc36789e7a1e281436464229828f817d6612f7b477d66591ff96a9e064bcc98a"

youchain.utils.sha3(234); // 无法计算数字
> null

youchain.utils.sha3(0xea); // 问题同上
> null

soliditySha3

描述:

参数值:Mixed

  • String - 'utf-8' String
  • String | Number | BN | HEX - uint256
  • String | Number | BN - int256
  • Boolean - bool
  • String - bytes
  • Hex - uint256

返回值:String 哈希值

youchain.utils.soliditySha3(param1 [, param2, ...])

示例:

youchain.utils.soliditySha3('234564535', '0xfff23243', true, -10)
> "0x3e27a893dc40ef8a7f0841d96639de2f58a132be5ae466d40087a2cfa83b7179"
youchain.utils.soliditySha3('Hello!%'); // string
> "0x661136a4267dba9ccdf6bfddb7c00e714de936674c4bdb065a531cf1cb15c7fc"
youchain.utils.soliditySha3('234'); // uint256
> "0x61c831beab28d67d1bb40b5ae1a11e2757fa842f031a2d0bc94a7867bc5d26c2"
youchain.utils.soliditySha3(0xea); // uint256
> "0x61c831beab28d67d1bb40b5ae1a11e2757fa842f031a2d0bc94a7867bc5d26c2"
youchain.utils.soliditySha3(new BN('234')); // uint256
> "0x61c831beab28d67d1bb40b5ae1a11e2757fa842f031a2d0bc94a7867bc5d26c2"
youchain.utils.soliditySha3({type: 'uint256', value: '234'})); // uint256
> "0x61c831beab28d67d1bb40b5ae1a11e2757fa842f031a2d0bc94a7867bc5d26c2"
youchain.utils.soliditySha3({t: 'uint', v: new BN('234')})); // uint256
> "0x61c831beab28d67d1bb40b5ae1a11e2757fa842f031a2d0bc94a7867bc5d26c2"
youchain.utils.soliditySha3('0x407D73d8a49eeb85D32Cf465507dd71d507100c1');
> "0x4e8ebbefa452077428f93c9520d3edd60594ff452a29ac7d2ccc11d47f3ab95b"
youchain.utils.soliditySha3({t: 'bytes', v: '0x407D73d8a49eeb85D32Cf465507dd71d507100c1'});
> "0x4e8ebbefa452077428f93c9520d3edd60594ff452a29ac7d2ccc11d47f3ab95b" 
youchain.utils.soliditySha3({t: 'address', v: '0x407D73d8a49eeb85D32Cf465507dd71d507100c1'});
> "0x4e8ebbefa452077428f93c9520d3edd60594ff452a29ac7d2ccc11d47f3ab95b" 
youchain.utils.soliditySha3({t: 'bytes32', v: '0x407D73d8a49eeb85D32Cf465507dd71d507100c1'});
> "0x3c69a194aaf415ba5d6afca734660d0a3d45acdc05d54cd1ca89a8988e7625b4"
youchain.utils.soliditySha3({t: 'string', v: 'Hello!%'}, {t: 'int8', v:-23}, {t: 'address', v: '0x85F43D8a49eeB85d32Cf465507DD71d507100C1d'});
> "0xa13b31627c1ed7aaded5aecec71baf02fe123797fffd45e662eac8e06fbe4955"

isHex

描述:验证是否为十六进制字符串

参数值:String | HEX: 给定的十六进制字符串

返回值:Boolean

youchain.utils.isHex(hex)

示例:

youchain.utils.isHex('0xc1912');
> true
youchain.utils.isHex(0xc1912);
> true
youchain.utils.isHex('c1912');
> true
youchain.utils.isHex(345);
> true 
youchain.utils.isHex('0xZ1912');
> false
youchain.utils.isHex('Hello');
> false

isHexStrict

描述:严格检查十六进制字符串

参数值:String | HEX: 给定的十六进制字符串

返回值:Boolean

youchain.utils.isHexStrict(hex)

示例:

youchain.utils.isHexStrict('0xc1912');
> true
youchain.utils.isHexStrict(0xc1912);
> false
youchain.utils.isHexStrict('c1912');
> false
youchain.utils.isHexStrict(345);
> false // this is tricky, as 345 can be a a HEX representation or a number, be careful when not having a 0x in front!
youchain.utils.isHexStrict('0xZ1912');
> false
youchain.utils.isHex('Hello');
> false

isAddress

描述:验证地址是否在 youchain 有效,并根据地址大小写校验。

参数值:String: 地址字符串

返回值:Boolean

youchain.utils.isAddress(address)

示例:

youchain.utils.isAddress('0x0b9d85bb0e36379ee32f02912dcc3dfb01f36235');
> true
youchain.utils.isAddress('0b9d85bb0e36379ee32f02912dcc3dfb01f36235');
> true
youchain.utils.isAddress('0X0B9D85BB0E36379EE32F02912DCC3DFB01F36235');
> true // as all is uppercase, no checksum will be checked
youchain.utils.isAddress('0x0B9d85bB0E36379ee32F02912DCC3dfB01f36235');
> true
youchain.utils.isAddress('0x0b9d85bB0E36379ee32F02912DCC3dfB01f36235');
> false

toChecksumAddress

描述:将大小写地址转换为校验地址

参数值:String 地址字符串

返回值:String 校验地址

youchain.utils.toChecksumAddress(address)

示例:

youchain.utils.toChecksumAddress('0xc1912fee45d61c87cc5ea59dae31190fffff2323');
> "0xc1912fEE45d61C87Cc5EA59DaE31190FFFFf232d"
youchain.utils.toChecksumAddress('0XC1912FEE45D61C87CC5EA59DAE31190FFFFF232D');
> "0xc1912fEE45d61C87Cc5EA59DaE31190FFFFf232d"

checkAddressChecksum

描述:检查是否为校验地址

参数值:String 原始地址

返回值:Boolean

youchain.utils.checkAddressChecksum(address)

示例:

youchain.utils.checkAddressChecksum('0X0B9D85BB0E36379EE32F02912DCC3DFB01F36235');
> true

toHex

描述:自动转换为十六进制值

参数值:String | Number | BN | BigNumber 输入数据

返回值:String 十六进制输出

youchain.utils.toHex(mixed)

示例:

youchain.utils.toHex('234');
> "0xea"
youchain.utils.toHex(234);
> "0xea"
youchain.utils.toHex(new BN('234'));
> "0xea"
youchain.utils.toHex(new BigNumber('234'));
> "0xea"
youchain.utils.toHex('I have 100€');
> "0x49206861766520313030e282ac"

toBN

描述:将输入数据转换为 BN.js 的实例

注意:对于 BN.js 的类,可使用 utils.BN

参数值:String | Number | HEX

返回值:Object BN.js 的实例

youchain.utils.toBN(number)

示例:

youchain.utils.toBN(1234).toString();
> "1234"
youchain.utils.toBN('1234').add(youchain.utils.toBN('1')).toString();
> "1235"
youchain.utils.toBN('0xea').toString();
> "234"

hexToNumberString

描述:十六进制值转换为数字字符串

参数值:String | HEX 十六进制字符串

返回值:String 数值字符串

youchain.utils.hexToNumberString(hex)

示例:

youchain.utils.hexToNumberString('0xac');
> "172"

hexToNumber

描述:十六进制值转换为数字类型

参数值:String | HEX 十六进制值

返回值:Number 数字

youchain.utils.hexToNumber(hex)

示例:

youchain.utils.hexToNumber('0xac');\n> 172

numberToHex

描述:数字转换为十六进制值

参数值:Number

返回值:HEX

youchain.utils.numberToHex(number)

示例:

youchain.utils.numberToHex('176');\n> '0xb0'

toUtf8

描述:十六进制值转换为 utf-8 字符串

参数值:String | HEX

返回值:String

youchain.utils.toUtf8(hex)

示例:

youchain.utils.toUtf8('0x49206861766520313030e282ac');
> "I have 100€"

hexToAscii

描述:十六进制转换为 Ascii 码

参数值:hex - String

返回值:ASCII - String

youchain.utils.hexToAscii(hex)

示例:

youchain.utils.hexToAscii('0x4920686176652031303021');

asciiToHex

描述:ASCII 字符串转换为十六进制

参数值:

  • String 原始 ASCII 值
  • Number 十六进制字符串长度

返回值:String

youchain.utils.asciiToHex(string)

示例:

youchain.utils.asciiToHex('I have 10!');
> "0x4920686176652031302100000000000000000000000000000000000000000000"
// 4 字节值转换youchain.utils.asciiToHex('yes', 4);
// 8 字节值转换
youchain.utils.asciiToHex('yes', 8);

hexToBytes

描述:十六进制字符串转换为字节

参数值:String | HEX

返回值:Array 字节数组

youchain.utils.hexToBytes(hex)

示例:

youchain.utils.hexToBytes('0x000000ac');
> [ 0, 0, 0, 176 ]
youchain.utils.hexToBytes(0x000000ac);
> [ 176 ]

bytesToHex

描述:字节数组转换为十六进制值

参数值:byteArray - Array

返回值:String

youchain.utils.bytesToHex(byteArray)

示例:

youchain.utils.bytesToHex([ 72, 101, 108, 108, 111, 33, 36 ]);
> "0x48656c6c6f2125"

toLu

描述:将 you 值转换为 lu

- noyou: '0',
- lu: '1',
- klu: '1000',
- Klu: '1000',
- babbage: '1000',
- femtoyou: '1000',
- mlu: '1000000',
- Mlu: '1000000',
- lovelace: '1000000',
- picoyou: '1000000',
- glu: '1000000000',
- Glu: '1000000000',
- shannon: '1000000000',
- nanoyou: '1000000000',
- nano: '1000000000',
- szabo: '1000000000000',
- microyou: '1000000000000',
- micro: '1000000000000',
- finney: '1000000000000000',
- milliyou: '1000000000000000',
- milli: '1000000000000000',
- you: '1000000000000000000',
- kyou: '1000000000000000000000',
- grand: '1000000000000000000000',
- myou: '1000000000000000000000000',
- gyou: '1000000000000000000000000000',
- tyou: '1000000000000000000000000000000'

备注:"lu” 是最小的 you 单元。

参数值:

  • number - String | BN 原始值
  • unit - String 默认值 “you”

返回值:String | BN

youchain.utils.toLu(number [, unit])

示例:

youchain.utils.toLu('1', 'you');
> "1000000000000000000"
youchain.utils.toLu('1', 'finney');
> "1000000000000000"
youchain.utils.toLu('1', 'szabo');
> "1000000000000"
youchain.utils.toLu('1', 'shannon');
> "1000000000"

fromLu

描述:“lu” 转换为其他单位值

参数值:

  • number - String | BN 原始值
  • unit - String 默认值 “you”

返回值:String

youchain.utils.fromLu(number [, unit])

示例:

youchain.utils.fromLu('1', 'you');
> "0.000000000000000001"
youchain.utils.fromLu('1', 'finney');
> "0.000000000000001"
youchain.utils.fromLu('1', 'szabo');
> "0.000000000001"
youchain.utils.fromLu('1', 'shannon');
> "0.000000001"

padLeft

描述:字符串左补齐

参数值:

  • String 原始字符串
  • Number 字符串长度
  • String 标识字符,默认值为 0

返回值:String 左补齐后的字符串

youchain.utils.padLeft(string, characterAmount [, sign])
youchain.utils.leftPad(string, characterAmount [, sign])

示例:

youchain.utils.padLeft('0x3456ff', 20);
> "0x000000000000003456ff"
youchain.utils.padLeft(0x3456ff, 20);
> "0x000000000000003456ff"
youchain.utils.padLeft('Hello', 20, 'x');
> "xxxxxxxxxxxxxxxHello"

padRight

描述:字符串右补齐

参数值:

  • String 原始字符串
  • Number 字符串长度
  • String 标识字符,默认值为 0

返回值:String 右补齐后的字符串

youchain.utils.padRight(string, characterAmount [, sign])
youchain.utils.rightPad(string, characterAmount [, sign])

示例:

youchain.utils.padRight('0x3456ff', 20);
> "0x3456ff00000000000000"
youchain.utils.padRight(0x3456ff, 20);
> "0x3456ff00000000000000"
youchain.utils.padRight('Hello', 20, 'x');
> "Helloxxxxxxxxxxxxxxx"

toTwosComplement

描述:把负数转换为二进制补码形式

参数值:Number | String | BigNumber 需要转换的数字

返回值:转换后的十六进制数

youchain.utils.toTwosComplement(number)

示例:

youchain.utils.toTwosComplement('-1');
> "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
youchain.utils.toTwosComplement(-1);
> "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
youchain.utils.toTwosComplement('0x1');
> "0x0000000000000000000000000000000000000000000000000000000000000001"
youchain.utils.toTwosComplement(-15);
> "0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1"
youchain.utils.toTwosComplement('-0x1');
> "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"

getSignatureParameters

描述:获得签名参数

参数值:String 一个 ECDSA 签名

返回值:Object 包含 r,s,v 的值

youchain.utils.getSignatureParameters(string)

示例:

youchain.utils.getSignatureParameters('0x5763ab346198e3e6cc4d53996ccdeca0c941cb6cb70d671d97711c421d3bf7922c77ef244ad40e5262d1721bf9638fb06bab8ed3c43bfaa80d6da0be9bbd33dc1b');
> { 
    r: '0x5763ab346198e3e6cc4d53996ccdeca0c941cb6cb70d671d97711c421d3bf792', 
    s: '0x2c77ef244ad40e5262d1721bf9638fb06bab8ed3c43bfaa80d6da0be9bbd33dc', 
    v: 27
}