UNPKG

@firefly-exchange/library-sui

Version:

Sui library housing helper methods, classes to interact with Bluefin protocol(s) deployed on Sui

122 lines (121 loc) 4.81 kB
"use strict"; 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; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSignerFromKmsId = exports.KmsSigner = void 0; const client_kms_1 = require("@aws-sdk/client-kms"); const eth_crypto_1 = __importDefault(require("eth-crypto")); const secp256k1_1 = require("@noble/curves/secp256k1"); const asn1 = __importStar(require("asn1.js")); const types_1 = require("../types"); const cryptography_1 = require("@mysten/sui/cryptography"); // Definition of EcdsaPubKey const EcdsaPubKey = asn1.define("EcdsaPubKey", function () { // https://tools.ietf.org/html/rfc5480#section-2 this.seq().obj(this.key("algo") .seq() .obj(this.key("algorithm").objid(), this.key("parameters").objid()), this.key("pubKey").bitstr()); }); class KmsSigner extends cryptography_1.Signer { constructor(kmsKeyId, options) { super(); this.kmsKeyId = kmsKeyId; const params = {}; if (options?.region) { params.region = options?.region; } if (options?.accessKeyId || options?.secretAccessKey || options?.sessionToken) { params.credentials = { accessKeyId: options?.accessKeyId, secretAccessKey: options?.secretAccessKey, sessionToken: options?.sessionToken }; } this.KmsClient = new client_kms_1.KMSClient(params); } async loadPublicKey() { if (this.publicKey) { return this.publicKey; } const params = { KeyId: this.kmsKeyId }; const command = new client_kms_1.GetPublicKeyCommand(params); const pkFullRaw = await this.KmsClient.send(command); const pkRaw = pkFullRaw.PublicKey; const res = EcdsaPubKey.decode(Buffer.from(pkRaw), "der"); const kmsPKCompressed = eth_crypto_1.default.publicKey.compress(res.pubKey.data); this.compressedPublicKey = new Uint8Array(Buffer.from(kmsPKCompressed, "hex")); this.publicKey = new types_1.Secp256k1PublicKey(this.compressedPublicKey); return this.getPublicKey; } async init() { await this.loadPublicKey(); } assertInitialized(throwError = true) { if (this.publicKey) { return true; } if (throwError) { throw new Error("KMS signer not initialized, call init() and wait for promise to resolve"); } return false; } getPublicKey() { this.assertInitialized(); return this.publicKey; } signData(_) { throw new Error("KMS signer doesn't implement sync signData method, please use sign method"); } async sign(data) { await this.init(); const input = { KeyId: this.kmsKeyId, Message: data, SigningAlgorithm: client_kms_1.SigningAlgorithmSpec.ECDSA_SHA_256, MessageType: client_kms_1.MessageType.RAW }; const command = new client_kms_1.SignCommand(input); const response = await this.KmsClient.send(command); const sigDER = response.Signature; const sigRS = secp256k1_1.secp256k1.Signature.fromDER(Buffer.from(sigDER).toString("hex")); const normalizedSigRS = sigRS.normalizeS(); const sig = normalizedSigRS.toCompactHex(); return Uint8Array.from(Buffer.from(sig, "hex")); } getKeyScheme() { return "Secp256k1"; } } exports.KmsSigner = KmsSigner; async function getSignerFromKmsId(kmsId, options) { const signer = new KmsSigner(kmsId, options); await signer.init(); return signer; } exports.getSignerFromKmsId = getSignerFromKmsId;