UNPKG

@horizonx/ic-identity-kms-js

Version:

SignIdentity implementation with AWS KMS for Internet Computer.

56 lines 2.66 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.KmsIdentity = void 0; const client_kms_1 = require("@aws-sdk/client-kms"); const agent_1 = require("@dfinity/agent"); const candid_1 = require("@dfinity/candid"); const identity_secp256k1_1 = require("@dfinity/identity-secp256k1"); const secp256k1_1 = require("@noble/curves/secp256k1"); const sha256_1 = require("@noble/hashes/sha256"); class KmsIdentity extends agent_1.SignIdentity { constructor(kmsClient, kmsKeyId, publicKey) { super(); this.kmsClient = kmsClient; this.kmsKeyId = kmsKeyId; this.publicKey = publicKey; } static from(kmsClient, kmsKeyId) { return __awaiter(this, void 0, void 0, function* () { const out = yield kmsClient.send(new client_kms_1.GetPublicKeyCommand({ KeyId: kmsKeyId })); if (!out.PublicKey) throw new Error('No public key found'); const publicKey = identity_secp256k1_1.Secp256k1PublicKey.fromDer(out.PublicKey); return new KmsIdentity(kmsClient, kmsKeyId, publicKey); }); } getPublicKey() { return this.publicKey; } sign(blob) { return __awaiter(this, void 0, void 0, function* () { const hash = sha256_1.sha256.create(); hash.update(new Uint8Array(blob)); const out = yield this.kmsClient.send(new client_kms_1.SignCommand({ KeyId: this.kmsKeyId, Message: new Uint8Array(hash.digest()), MessageType: 'DIGEST', SigningAlgorithm: 'ECDSA_SHA_256', })); if (!out.Signature) throw new Error('No signature found'); const signature = secp256k1_1.secp256k1.Signature.fromDER(out.Signature); return (0, candid_1.uint8ToBuf)(signature.toCompactRawBytes()); }); } } exports.KmsIdentity = KmsIdentity; //# sourceMappingURL=index.js.map