add hex2b58 and b582hex

This commit is contained in:
陆柯 2021-11-15 23:20:50 +08:00
parent 8df15515a6
commit 50a88a7efd
3 changed files with 34 additions and 21 deletions

View File

@ -1,6 +1,6 @@
<mxfile host="65bd71144e">
<diagram id="tEJiczfqJVlhZvufAVre" name="Page-1">
<mxGraphModel dx="1127" dy="723" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
<mxGraphModel dx="845" dy="542" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
<root>
<mxCell id="0"/>
<mxCell id="1" parent="0"/>
@ -55,10 +55,10 @@
<mxCell id="16" value="椭圆曲线算法&lt;br&gt;secp256k1" style="shape=parallelogram;perimeter=parallelogramPerimeter;whiteSpace=wrap;html=1;fixedSize=1;rounded=1;fontColor=#ffffff;strokeColor=#A50040;fillColor=#d80073;" parent="1" vertex="1">
<mxGeometry x="770" y="250" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="96" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" parent="1" source="18" target="53">
<mxCell id="96" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" parent="1" source="18" target="53" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="98" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0.5;entryY=1;entryDx=0;entryDy=0;" edge="1" parent="1" source="18" target="57">
<mxCell id="98" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0.5;entryY=1;entryDx=0;entryDy=0;" parent="1" source="18" target="57" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="18" value="public key ^ 公钥&lt;br&gt;264 bits = 33 Bytes = 66 Hex" style="rounded=1;whiteSpace=wrap;html=1;fontColor=#ffffff;strokeColor=#005700;fillColor=#008a00;" parent="1" vertex="1">
@ -103,13 +103,13 @@
<mxCell id="42" value="" style="edgeStyle=elbowEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;startArrow=classic;startFill=1;" parent="1" source="33" target="41" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="33" value="&lt;ol&gt;&lt;li&gt;version prefix +1B&lt;/li&gt;&lt;li&gt;cksum postfix +4B&lt;/li&gt;&lt;li&gt;base58btc&lt;/li&gt;&lt;/ol&gt;" style="shape=parallelogram;perimeter=parallelogramPerimeter;whiteSpace=wrap;html=1;fixedSize=1;rounded=1;fontColor=#ffffff;strokeColor=#A50040;fillColor=#d80073;align=left;" parent="1" vertex="1">
<mxCell id="33" value="&lt;ol&gt;&lt;li&gt;protocol prefix +1B&lt;/li&gt;&lt;li&gt;cksum postfix +4B&lt;/li&gt;&lt;li&gt;base58btc&lt;/li&gt;&lt;/ol&gt;" style="shape=parallelogram;perimeter=parallelogramPerimeter;whiteSpace=wrap;html=1;fixedSize=1;rounded=1;fontColor=#ffffff;strokeColor=#A50040;fillColor=#d80073;align=left;" parent="1" vertex="1">
<mxGeometry x="1340" y="130" width="150" height="60" as="geometry"/>
</mxCell>
<mxCell id="39" style="edgeStyle=elbowEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;startArrow=classic;startFill=1;" parent="1" source="35" target="29" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="91" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="35" target="43">
<mxCell id="91" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" parent="1" source="35" target="43" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="35" value="&lt;ol&gt;&lt;li&gt;&lt;span&gt;大小写(EIP55)&lt;/span&gt;&lt;br&gt;&lt;/li&gt;&lt;li&gt;&lt;span&gt;'0x' prefix&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;" style="shape=parallelogram;perimeter=parallelogramPerimeter;whiteSpace=wrap;html=1;fixedSize=1;rounded=1;fontColor=#ffffff;strokeColor=#A50040;fillColor=#d80073;align=left;" parent="1" vertex="1">
@ -121,13 +121,13 @@
<mxCell id="46" value="" style="edgeStyle=elbowEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;startArrow=classic;startFill=1;" parent="1" source="37" target="45" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="37" value="&lt;ol&gt;&lt;li&gt;version prefix +1B&lt;/li&gt;&lt;li&gt;cksum postfix +3B&lt;/li&gt;&lt;li&gt;base64url&lt;/li&gt;&lt;/ol&gt;" style="shape=parallelogram;perimeter=parallelogramPerimeter;whiteSpace=wrap;html=1;fixedSize=1;rounded=1;fontColor=#ffffff;strokeColor=#A50040;fillColor=#d80073;align=left;" parent="1" vertex="1">
<mxCell id="37" value="&lt;ol&gt;&lt;li&gt;world prefix +1B&lt;/li&gt;&lt;li&gt;cksum postfix +3B&lt;/li&gt;&lt;li&gt;base64url&lt;/li&gt;&lt;/ol&gt;" style="shape=parallelogram;perimeter=parallelogramPerimeter;whiteSpace=wrap;html=1;fixedSize=1;rounded=1;fontColor=#ffffff;strokeColor=#A50040;fillColor=#d80073;align=left;" parent="1" vertex="1">
<mxGeometry x="1340" y="360" width="150" height="60" as="geometry"/>
</mxCell>
<mxCell id="41" value="address of BTC&lt;br&gt;26~34 b58" style="rounded=1;whiteSpace=wrap;html=1;fontColor=#ffffff;align=center;strokeColor=#005700;fillColor=#008a00;" parent="1" vertex="1">
<mxGeometry x="1520" y="130" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="92" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" parent="1" source="43" target="35">
<mxCell id="92" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" parent="1" source="43" target="35" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="43" value="address of ETH&lt;br&gt;'0x' + 40 Hex" style="rounded=1;whiteSpace=wrap;html=1;fontColor=#ffffff;align=center;strokeColor=#005700;fillColor=#008a00;" parent="1" vertex="1">
@ -147,7 +147,7 @@
<Array as="points"/>
</mxGeometry>
</mxCell>
<mxCell id="97" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0.5;entryY=1;entryDx=0;entryDy=0;" edge="1" parent="1" source="49" target="53">
<mxCell id="97" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0.5;entryY=1;entryDx=0;entryDy=0;" parent="1" source="49" target="53" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="49" value="Signature ^ 签印&lt;br&gt;7172 Bytes = 142/144 Hex" style="rounded=1;whiteSpace=wrap;html=1;fontColor=#ffffff;strokeColor=#005700;fillColor=#008a00;" parent="1" vertex="1">
@ -156,7 +156,7 @@
<mxCell id="53" value="verify ^ 验签" style="shape=parallelogram;perimeter=parallelogramPerimeter;whiteSpace=wrap;html=1;fixedSize=1;rounded=1;fontColor=#ffffff;strokeColor=#A50040;fillColor=#d80073;" parent="1" vertex="1">
<mxGeometry x="910" y="430" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="103" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=1;entryY=0.5;entryDx=0;entryDy=0;exitX=0.5;exitY=0;exitDx=0;exitDy=0;" edge="1" parent="1" source="57" target="64">
<mxCell id="103" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=1;entryY=0.5;entryDx=0;entryDy=0;exitX=0.5;exitY=0;exitDx=0;exitDy=0;" parent="1" source="57" target="64" edge="1">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="1000" y="40"/>
@ -166,7 +166,7 @@
<mxCell id="57" value="encrypt ^ 加密" style="shape=parallelogram;perimeter=parallelogramPerimeter;whiteSpace=wrap;html=1;fixedSize=1;rounded=1;fontColor=#ffffff;strokeColor=#A50040;fillColor=#d80073;" parent="1" vertex="1">
<mxGeometry x="910" y="120" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="105" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" target="61">
<mxCell id="105" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" parent="1" target="61" edge="1">
<mxGeometry relative="1" as="geometry">
<mxPoint x="710" y="150" as="sourcePoint"/>
</mxGeometry>
@ -174,7 +174,7 @@
<mxCell id="58" value="decrypt ^ 解密" style="shape=parallelogram;perimeter=parallelogramPerimeter;whiteSpace=wrap;html=1;fixedSize=1;rounded=1;fontColor=#ffffff;strokeColor=#A50040;fillColor=#d80073;" parent="1" vertex="1">
<mxGeometry x="620" y="120" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="104" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" edge="1" parent="1" source="64" target="58">
<mxCell id="104" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" parent="1" source="64" target="58" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="64" value="ciphertext ^ 密文" style="whiteSpace=wrap;html=1;rounded=1;fontColor=#ffffff;strokeColor=#005700;fillColor=#008a00;" parent="1" vertex="1">
@ -201,31 +201,31 @@
<mxCell id="14" value="private key ^ 私钥&lt;br&gt;256 bits = 32 Bytes = 64 Hex" style="whiteSpace=wrap;html=1;rounded=1;fontColor=#ffffff;strokeColor=#005700;fillColor=#008a00;" parent="1" vertex="1">
<mxGeometry x="620" y="250" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="95" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=1;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="51" target="47">
<mxCell id="95" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=1;entryY=0.5;entryDx=0;entryDy=0;" parent="1" source="51" target="47" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="51" value="作品=&amp;gt;哈希" style="whiteSpace=wrap;html=1;rounded=1;fontColor=#ffffff;strokeColor=#005700;fillColor=#008a00;" parent="1" vertex="1">
<mxGeometry x="770" y="430" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="106" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" parent="1" source="61" target="57">
<mxCell id="106" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" parent="1" source="61" target="57" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="61" value="plaintext ^ 明文" style="whiteSpace=wrap;html=1;rounded=1;fontColor=#ffffff;strokeColor=#005700;fillColor=#008a00;" parent="1" vertex="1">
<mxGeometry x="770" y="120" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="111" value="seckey2pubkey" style="shape=partialRectangle;whiteSpace=wrap;html=1;bottom=1;right=1;left=1;top=0;fillColor=#1ba1e2;routingCenterX=-0.5;strokeColor=#006EAF;fontColor=#ffffff;" vertex="1" parent="1">
<mxCell id="111" value="seckey2pubkey" style="shape=partialRectangle;whiteSpace=wrap;html=1;bottom=1;right=1;left=1;top=0;fillColor=#1ba1e2;routingCenterX=-0.5;strokeColor=#006EAF;fontColor=#ffffff;" parent="1" vertex="1">
<mxGeometry x="680" y="650" width="290" height="30" as="geometry"/>
</mxCell>
<mxCell id="112" value="secword2keypair" style="shape=partialRectangle;whiteSpace=wrap;html=1;bottom=1;right=1;left=1;top=0;fillColor=#1ba1e2;routingCenterX=-0.5;strokeColor=#006EAF;fontColor=#ffffff;" vertex="1" parent="1">
<mxGeometry x="360" y="690" width="610" height="30" as="geometry"/>
<mxCell id="112" value="secword2keypair" style="shape=partialRectangle;whiteSpace=wrap;html=1;bottom=1;right=1;left=1;top=0;fillColor=#1ba1e2;routingCenterX=-0.5;strokeColor=#006EAF;fontColor=#ffffff;" parent="1" vertex="1">
<mxGeometry x="420" y="710" width="610" height="30" as="geometry"/>
</mxCell>
<mxCell id="113" value="pubkey2address" style="shape=partialRectangle;whiteSpace=wrap;html=1;bottom=1;right=1;left=1;top=0;fillColor=#1ba1e2;routingCenterX=-0.5;strokeColor=#006EAF;fontColor=#ffffff;" vertex="1" parent="1">
<mxCell id="113" value="pubkey2address" style="shape=partialRectangle;whiteSpace=wrap;html=1;bottom=1;right=1;left=1;top=0;fillColor=#1ba1e2;routingCenterX=-0.5;strokeColor=#006EAF;fontColor=#ffffff;" parent="1" vertex="1">
<mxGeometry x="970" y="650" width="630" height="30" as="geometry"/>
</mxCell>
<mxCell id="114" value="seckey2address" style="shape=partialRectangle;whiteSpace=wrap;html=1;bottom=1;right=1;left=1;top=0;fillColor=#1ba1e2;routingCenterX=-0.5;strokeColor=#006EAF;fontColor=#ffffff;" vertex="1" parent="1">
<mxCell id="114" value="seckey2address" style="shape=partialRectangle;whiteSpace=wrap;html=1;bottom=1;right=1;left=1;top=0;fillColor=#1ba1e2;routingCenterX=-0.5;strokeColor=#006EAF;fontColor=#ffffff;" parent="1" vertex="1">
<mxGeometry x="680" y="730" width="920" height="30" as="geometry"/>
</mxCell>
<mxCell id="115" value="secword2address, secword2account" style="shape=partialRectangle;whiteSpace=wrap;html=1;bottom=1;right=1;left=1;top=0;fillColor=#1ba1e2;routingCenterX=-0.5;strokeColor=#006EAF;fontColor=#ffffff;" vertex="1" parent="1">
<mxCell id="115" value="secword2address, secword2account" style="shape=partialRectangle;whiteSpace=wrap;html=1;bottom=1;right=1;left=1;top=0;fillColor=#1ba1e2;routingCenterX=-0.5;strokeColor=#006EAF;fontColor=#ffffff;" parent="1" vertex="1">
<mxGeometry x="360" y="770" width="1240" height="30" as="geometry"/>
</mxCell>
</root>

View File

@ -106,7 +106,7 @@
<mxCell id="42" value="" style="edgeStyle=elbowEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;startArrow=classic;startFill=1;" parent="1" source="33" target="41" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="33" value="&lt;ol&gt;&lt;li&gt;version prefix +1B&lt;/li&gt;&lt;li&gt;cksum postfix +4B&lt;/li&gt;&lt;li&gt;base58btc&lt;/li&gt;&lt;/ol&gt;" style="shape=parallelogram;perimeter=parallelogramPerimeter;whiteSpace=wrap;html=1;fixedSize=1;rounded=1;fontColor=#ffffff;strokeColor=#A50040;fillColor=#d80073;align=left;" parent="1" vertex="1">
<mxCell id="33" value="&lt;ol&gt;&lt;li&gt;protocol prefix +1B&lt;/li&gt;&lt;li&gt;cksum postfix +4B&lt;/li&gt;&lt;li&gt;base58btc&lt;/li&gt;&lt;/ol&gt;" style="shape=parallelogram;perimeter=parallelogramPerimeter;whiteSpace=wrap;html=1;fixedSize=1;rounded=1;fontColor=#ffffff;strokeColor=#A50040;fillColor=#d80073;align=left;" parent="1" vertex="1">
<mxGeometry x="160" y="980" width="150" height="60" as="geometry"/>
</mxCell>
<mxCell id="39" style="edgeStyle=elbowEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;startArrow=classic;startFill=1;" parent="1" source="35" target="29" edge="1">
@ -128,7 +128,7 @@
<mxCell id="46" value="" style="edgeStyle=elbowEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;startArrow=classic;startFill=1;" parent="1" source="37" target="45" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="37" value="&lt;ol&gt;&lt;li&gt;version prefix +1B&lt;/li&gt;&lt;li&gt;cksum postfix +3B&lt;/li&gt;&lt;li&gt;base64url&lt;/li&gt;&lt;/ol&gt;" style="shape=parallelogram;perimeter=parallelogramPerimeter;whiteSpace=wrap;html=1;fixedSize=1;rounded=1;fontColor=#ffffff;strokeColor=#A50040;fillColor=#d80073;align=left;" parent="1" vertex="1">
<mxCell id="37" value="&lt;ol&gt;&lt;li&gt;world prefix +1B&lt;/li&gt;&lt;li&gt;cksum postfix +3B&lt;/li&gt;&lt;li&gt;base64url&lt;/li&gt;&lt;/ol&gt;" style="shape=parallelogram;perimeter=parallelogramPerimeter;whiteSpace=wrap;html=1;fixedSize=1;rounded=1;fontColor=#ffffff;strokeColor=#A50040;fillColor=#d80073;align=left;" parent="1" vertex="1">
<mxGeometry x="469" y="980" width="150" height="60" as="geometry"/>
</mxCell>
<mxCell id="41" value="address of BTC&lt;br&gt;26~34 b58" style="rounded=1;whiteSpace=wrap;html=1;fontColor=#ffffff;align=center;strokeColor=#005700;fillColor=#008a00;" parent="1" vertex="1">

View File

@ -3,6 +3,7 @@ const BigInt = require('big-integer') // 处理整数 https://github.com/peterol
const crypto = require('crypto')
const nacl = require('tweetnacl')
const bs58check = require('bs58check')
const bs58 = require('bs58') // bs58check depends on bs58
const uuid = require('uuid')
const keccak = require('keccak')
const eccrypto = require('eccrypto-js') // 用于加解密。eccrypto 在 windows 上和 openssl 的版本兼容性有点麻烦,所以换用 eccrypto-js
@ -1214,6 +1215,10 @@ class TICrypto {
return bs58check.encode(Buffer.from(hex, 'hex'))
}
static hex2b58(hex) {
return bs58.encode(Buffer.from(hex, 'hex'))
}
/**
* b58c 转十六进制
*
@ -1230,6 +1235,14 @@ class TICrypto {
}
}
static b582hex(box) {
try {
return bs58check.decode(box).toString('hex')
} catch (exception) {
return null
}
}
/**
* 十六进制转b64u
*