UNPKG

@sphereon/ssi-sdk-web3.headless-provider

Version:

183 lines (180 loc) 7.09 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 __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()); }); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EthersKMSSigner = exports.EthersKMSSignerBuilder = void 0; const transactions_1 = require("@ethersproject/transactions"); const ethers_1 = require("ethers"); // import {arrayify, defineReadOnly, serializeTransaction} from 'ethers/lib/utils' const utils_1 = require("ethers/lib/utils"); const u8a = __importStar(require("uint8arrays")); // import {ECDSASignature} from "web3-eth-accounts"; const functions_1 = require("./functions"); class EthersKMSSignerBuilder { withContext(context) { this.context = context; return this; } withKid(kid) { this.keyRef = { kid }; return this; } withKeyRef(keyRef) { if (typeof keyRef === 'string') { return this.withKid(keyRef); } this.keyRef = keyRef; return this; } withProvider(provider) { this.provider = provider; return this; } build() { if (!this.context) { throw Error('Agent context needs to be provided'); } if (!this.keyRef) { throw Error('Keyref needs to be provided'); } return new EthersKMSSigner({ context: this.context, keyRef: this.keyRef, provider: this.provider }); } } exports.EthersKMSSignerBuilder = EthersKMSSignerBuilder; /** * This is a Ethers signer that delegates back to the KMS for the actual signatures. * This means we do not expose private keys and can use any Secp256k1 key stored in the KMS if we want * * Be aware that the provided KeyRef needs to belong to the respective KMS, as it will use a lookup for the key in the KMS to sign */ class EthersKMSSigner extends ethers_1.Signer { constructor({ provider, context, keyRef }) { super(); // defineReadOnly(this, "address", address); (0, utils_1.defineReadOnly)(this, 'provider', provider || undefined); this.context = context; this.keyRef = keyRef; } getAddress() { return __awaiter(this, void 0, void 0, function* () { return yield (0, functions_1.getAddressFromAgent)(this.context, this.keyRef); }); } signTransaction(transaction) { return __awaiter(this, void 0, void 0, function* () { const _a = yield transaction, { from } = _a, tx = __rest(_a, ["from"]); return this.context.agent.keyManagerSign({ algorithm: 'eth_signTransaction', keyRef: this.keyRef.kid, // keyRef: this.keyRef, // @ts-ignore data: (0, utils_1.arrayify)((0, transactions_1.serialize)(tx)), }); }); } signRaw(message) { return __awaiter(this, void 0, void 0, function* () { return yield this.context.agent.keyManagerSign({ algorithm: 'eth_rawSign', keyRef: this.keyRef.kid, encoding: 'base16', // @ts-ignore // KMS accepts uint8arrays but interface does not expose it data: message, }); }); } signMessage(message) { return __awaiter(this, void 0, void 0, function* () { return yield this.context.agent.keyManagerSign({ algorithm: 'eth_signMessage', keyRef: this.keyRef.kid, encoding: 'base16', // @ts-ignore // KMS accepts uint8arrays but interface does not expose it data: message, }); }); } _signTypedData(domain, types, value) { return __awaiter(this, void 0, void 0, function* () { const jsonData = { domain, types, message: value, }; return this.context.agent.keyManagerSign({ algorithm: 'eth_signTypedData', keyRef: this.keyRef.kid, // @ts-ignore // KMS accepts uint8arrays but interface does not expose it data: u8a.fromString(JSON.stringify(jsonData)), }); }); } connect(provider) { return new EthersKMSSigner({ provider, context: this.context, keyRef: this.keyRef }); } } exports.EthersKMSSigner = EthersKMSSigner; /* /!** * Convert signature format of the `eth_sign` RPC method to signature parameters * NOTE: all because of a bug in geth: https://github.com/ethereum/go-ethereum/issues/2053 *!/ export const fromRpcSig = function(sig: string): ECDSASignature { const buf: Buffer = toBuffer(sig) if (buf.length < 65) { throw new Error('Invalid signature length') } let v = bufferToInt(buf.slice(64)) // support both versions of `eth_sign` responses if (v < 27) { v += 27 } return { v: v, r: buf.slice(0, 32), s: buf.slice(32, 64) } } */ //# sourceMappingURL=ethers-kms-signer.js.map