@akashicpay/sdk
Version:
SDK to interact with the Akashic ecosystem
80 lines • 2.85 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.decodeECPrivateKey = decodeECPrivateKey;
exports.encodeECPublicKey = encodeECPublicKey;
const asn1 = __importStar(require("asn1.js"));
const ECPrivASN = asn1.define('ECPrivASN', function () {
this.seq().obj(this.key('version').int(), this.key('privateKey').octstr().optional(), this.seq().optional().obj(), this.key('ECNested')
.octstr()
.optional()
.contains(asn1.define('ECNested', function () {
this.seq().obj(this.key('version').int(), this.key('privateKey').octstr());
})));
});
const ECPubASN = asn1.define('ECPubASN', function () {
this.seq().obj(this.key('algorithm')
.optional()
.seq()
.obj(this.key('id').objid(), this.key('curve').objid()), this.key('publicKey').bitstr());
});
function extractNestedKeys(asn, type = 'privateKey') {
if (asn[type]) {
if (asn[type].data) {
return asn[type].data.toString('hex');
}
else {
return asn[type].toString('hex');
}
}
else if (asn.ECNested) {
return extractNestedKeys(asn.ECNested, type);
}
else {
throw new Error('PPK not found inside ASN');
}
}
function decodeECPrivateKey(pkcs8pem, label = 'EC PRIVATE KEY') {
return extractNestedKeys(ECPrivASN.decode(pkcs8pem, 'pem', {
label: label,
partial: true,
}).result);
}
function encodeECPublicKey(key, label = 'PUBLIC KEY') {
return ECPubASN.encode({
algorithm: {
id: [1, 2, 840, 10045, 2, 1],
curve: [1, 3, 132, 0, 10],
},
publicKey: {
unused: 0,
data: key,
},
}, 'pem', {
label: label,
partial: true,
});
}
//# sourceMappingURL=helpers.js.map