Wyle.Gong-巩文昕 2623f9aa09 ui
2025-04-23 14:47:06 +08:00

86 lines
2.9 KiB
JavaScript

"use strict";
/*
* Copyright 2018-2024 The NATS Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.KP = void 0;
const codec_1 = require("./codec");
const nkeys_1 = require("./nkeys");
const helper_1 = require("./helper");
/**
* @ignore
*/
class KP {
constructor(seed) {
this.seed = seed;
}
getRawSeed() {
if (!this.seed) {
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.ClearedPair);
}
let sd = codec_1.Codec.decodeSeed(this.seed);
return sd.buf;
}
getSeed() {
if (!this.seed) {
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.ClearedPair);
}
return this.seed;
}
getPublicKey() {
if (!this.seed) {
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.ClearedPair);
}
const sd = codec_1.Codec.decodeSeed(this.seed);
const kp = (0, helper_1.getEd25519Helper)().fromSeed(this.getRawSeed());
const buf = codec_1.Codec.encode(sd.prefix, kp.publicKey);
return new TextDecoder().decode(buf);
}
getPrivateKey() {
if (!this.seed) {
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.ClearedPair);
}
const kp = (0, helper_1.getEd25519Helper)().fromSeed(this.getRawSeed());
return codec_1.Codec.encode(nkeys_1.Prefix.Private, kp.secretKey);
}
sign(input) {
if (!this.seed) {
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.ClearedPair);
}
const kp = (0, helper_1.getEd25519Helper)().fromSeed(this.getRawSeed());
return (0, helper_1.getEd25519Helper)().sign(input, kp.secretKey);
}
verify(input, sig) {
if (!this.seed) {
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.ClearedPair);
}
const kp = (0, helper_1.getEd25519Helper)().fromSeed(this.getRawSeed());
return (0, helper_1.getEd25519Helper)().verify(input, sig, kp.publicKey);
}
clear() {
if (!this.seed) {
return;
}
this.seed.fill(0);
this.seed = undefined;
}
seal(input, recipient, nonce) {
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.InvalidNKeyOperation);
}
open(message, sender) {
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.InvalidNKeyOperation);
}
}
exports.KP = KP;