@dioxide-js/silas
Version:
RPC utility for Silas
121 lines (118 loc) • 5.17 kB
JavaScript
import { __awaiter } from '../../node_modules/tslib/tslib.es6.mjs';
import index, { s as srcExports } from '../../_virtual/index4.mjs';
import { dataview } from '../../node_modules/@dioxide-js/misc/dist/misc.es5.mjs';
import crc32c from '../../_virtual/crc32c.mjs';
import base32Encode from '../../_virtual/index2.mjs';
import { toUint8Array } from '../buffer.mjs';
const sm2 = srcExports.sm2;
const sm3 = index.sm3;
class DIOSM2 {
constructor(options) {
this.encryptMethod = 'sm2';
this.encryptOrderNum = 4;
if (options === null || options === void 0 ? void 0 : options.privateKey) {
const { privateKey } = options;
const sk = toUint8Array(privateKey);
this.privateKey = dataview.u8ToHex(sk);
}
}
generate() {
return __awaiter(this, void 0, void 0, function* () {
const [pk, sk] = yield this.keyPaires();
const publickKeyU8 = dataview.hexToU8(pk);
const sku8 = dataview.hexToU8(sk);
const pku8 = publickKeyU8.length === 65 ? publickKeyU8.slice(1) : publickKeyU8;
const lpk8 = publickKeyU8.length === 65 ? publickKeyU8 : dataview.concat(new Uint8Array([4]), publickKeyU8);
const address = this.pkToAddress(pku8, true);
const ret = { publickey: pk, privatekey: sk, pku8, sku8, address, lpku8: lpk8 };
return Promise.resolve(ret);
});
}
hash(publicKey) {
const pkHash = sm3(publicKey);
return dataview.hexToU8(pkHash);
}
getPubicKeyFromPrivateKey(privateKeyHex) {
if (privateKeyHex instanceof Uint8Array) {
privateKeyHex = dataview.u8ToHex(privateKeyHex);
}
const publicKey = sm2.getPublicKeyFromPrivateKey(privateKeyHex);
const publickKeyU8 = dataview.hexToU8(publicKey);
const pku8 = publickKeyU8[0] === 4 ? publickKeyU8.slice(1) : publickKeyU8;
return Promise.resolve(pku8);
}
pkToAddress(publickey, withPosfix = true) {
const u8 = this.pkToAddrU8(publickey);
const address = base32Encode(u8, 'Crockford') + (withPosfix ? ':sm2' : '');
return address.toLowerCase();
}
pkToAddrU8(publickey) {
if (publickey.length !== 64) {
throw 'expect 64 bytes public key';
}
const pk32b = this.hash(publickey);
const o = this.pkToDIOStruct(pk32b);
return o.address;
}
pkToDIOStruct(publicKey, salt = 1, alias) {
const order = this.encryptOrderNum;
const method = this.encryptMethod;
let errorCorrectingCode = crc32c.buf(publicKey, order);
errorCorrectingCode = (errorCorrectingCode & 0xfffffff0) | order;
errorCorrectingCode = errorCorrectingCode >>> 0;
const buffer = new Int32Array([errorCorrectingCode]).buffer;
const errorCorrectingCodeBuffer = new Uint8Array(buffer);
const mergedBuffer = dataview.concat(publicKey, errorCorrectingCodeBuffer);
const address = {
currency: 'DIO',
address: mergedBuffer,
encryptMethod: method,
encryptMethodOrderNumber: order,
salt: salt || 1,
alias: alias || `Address${salt}`,
};
return address;
}
keyPaires() {
return __awaiter(this, void 0, void 0, function* () {
let publicKey = '';
let privateKey = '';
if (this.privateKey) {
privateKey = this.privateKey;
const pk = yield this.getPubicKeyFromPrivateKey(privateKey);
publicKey = dataview.u8ToHex(pk);
}
else {
const { publicKey: pk, privateKey: sk } = sm2.generateKeyPairHex();
publicKey = pk;
privateKey = sk;
}
return [publicKey, privateKey];
});
}
sign(content, privateKey, options) {
var _a, _b;
const sk = dataview.u8ToHex(privateKey);
if (content instanceof Uint8Array) {
content = Array.from(content);
}
const hash = (_a = options === null || options === void 0 ? void 0 : options.hash) !== null && _a !== void 0 ? _a : false;
const der = (_b = options === null || options === void 0 ? void 0 : options.der) !== null && _b !== void 0 ? _b : false;
const signature = sm2.doSignature(content, sk, { hash, der });
const ret = dataview.hexToU8(signature);
return Promise.resolve(ret);
}
verify(msg, sigValueHex, publicKey, options) {
var _a, _b;
const pk = dataview.u8ToHex(publicKey);
if (msg instanceof Uint8Array) {
msg = Array.from(msg);
}
const hash = (_a = options === null || options === void 0 ? void 0 : options.hash) !== null && _a !== void 0 ? _a : false;
const der = (_b = options === null || options === void 0 ? void 0 : options.der) !== null && _b !== void 0 ? _b : false;
const ret = sm2.doVerifySignature(msg, sigValueHex, pk, { hash, der });
return Promise.resolve(ret);
}
}
export { DIOSM2 as default };
//# sourceMappingURL=sm2.mjs.map