UNPKG

@dioxide-js/silas

Version:

RPC utility for Silas

76 lines (73 loc) 2.66 kB
import { decode } from '../../node_modules/base64-arraybuffer/dist/base64-arraybuffer.es5.mjs'; import base32Decode from '../../_virtual/index.mjs'; import DIOSM2 from './sm2.mjs'; import DIOEd25519 from './ed25519.mjs'; import ECDSA from './ecdsa.mjs'; import { areUint8ArraysEqual, concat } from '../buffer.mjs'; class DIOAddress { constructor(alg, privateKey) { this.alg = 'sm2'; this.methodNum = 0x4; this.privateKey = null; this.instance = null; this.alg = alg; if (privateKey) { this.privateKey = privateKey; } switch (alg) { case 'sm2': this.methodNum = 0x4; this.instance = new DIOSM2({ privateKey: privateKey }); break; case 'ed25519': this.methodNum = 0x3; this.instance = new DIOEd25519({ privateKey: privateKey }); break; case 'ecdsa': this.methodNum = 0x4; this.instance = new ECDSA({ privateKey: privateKey }); break; default: throw 'Unsupported alg:' + this.alg; } } getPubicKeyFromPrivateKey(privatekey) { return this.instance.getPubicKeyFromPrivateKey(privatekey); } generate() { return this.instance.generate(); } sign(content, privateKey, options) { return this.instance.sign(content, privateKey, options); } verifySignature(msg, signedHex, publicKey, options) { return this.instance.verify(msg, signedHex, publicKey, options); } addressToPublicKey(address) { const [splitAddr] = address.split(':'); const addressUintArr = new Uint8Array(base32Decode(splitAddr, 'Crockford')); const publicKey = addressUintArr.slice(0, 32); const checkAddrUintArr = this.instance.pkToAddrU8(publicKey); if (areUint8ArraysEqual(addressUintArr, checkAddrUintArr)) { return publicKey; } return null; } insertPKIntoTxData(txData, pkList) { const originTxData = new Uint8Array(decode(txData)); const secSuites = []; pkList.forEach((el) => { const id = new Uint8Array([el.encryptedMethodOrderNumber]); const pk = el.publicKey; secSuites.push(id); secSuites.push(pk); }); const result = concat(originTxData, ...secSuites); return result; } publicKeyToAddress(publicKey, postfix = true) { return this.instance.pkToAddress(publicKey, postfix); } } export { DIOAddress }; //# sourceMappingURL=index.mjs.map