74 lines
2.4 KiB
JavaScript
74 lines
2.4 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.PublicKey = void 0;
|
|
const codec_1 = require("./codec");
|
|
const nkeys_1 = require("./nkeys");
|
|
const helper_1 = require("./helper");
|
|
/**
|
|
* @ignore
|
|
*/
|
|
class PublicKey {
|
|
constructor(publicKey) {
|
|
this.publicKey = publicKey;
|
|
}
|
|
getPublicKey() {
|
|
if (!this.publicKey) {
|
|
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.ClearedPair);
|
|
}
|
|
return new TextDecoder().decode(this.publicKey);
|
|
}
|
|
getPrivateKey() {
|
|
if (!this.publicKey) {
|
|
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.ClearedPair);
|
|
}
|
|
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.PublicKeyOnly);
|
|
}
|
|
getSeed() {
|
|
if (!this.publicKey) {
|
|
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.ClearedPair);
|
|
}
|
|
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.PublicKeyOnly);
|
|
}
|
|
sign(_) {
|
|
if (!this.publicKey) {
|
|
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.ClearedPair);
|
|
}
|
|
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.CannotSign);
|
|
}
|
|
verify(input, sig) {
|
|
if (!this.publicKey) {
|
|
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.ClearedPair);
|
|
}
|
|
let buf = codec_1.Codec._decode(this.publicKey);
|
|
return (0, helper_1.getEd25519Helper)().verify(input, sig, buf.slice(1));
|
|
}
|
|
clear() {
|
|
if (!this.publicKey) {
|
|
return;
|
|
}
|
|
this.publicKey.fill(0);
|
|
this.publicKey = 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.PublicKey = PublicKey;
|