@vechain/sdk-aws-kms-adapter
Version: 
This module implements the VeChain abstract signer so it is integrated with AWS KMS
2 lines • 5.85 kB
JavaScript
import {KMSClient,GetPublicKeyCommand,SignCommand,MessageType,SigningAlgorithmSpec}from'@aws-sdk/client-kms';import {JSONRPCInvalidParams,SignerMethodError,ProviderMethodError}from'@vechain/sdk-errors';import {VeChainAbstractSigner,DelegationHandler,RPC_METHODS,VeChainProvider}from'@vechain/sdk-network';import {bytesToHex,concatBytes}from'@noble/curves/abstract/utils';import {secp256k1}from'@noble/curves/secp256k1';import {Address,Transaction,Hex}from'@vechain/sdk-core';import {Sequence,BitString,ObjectIdentifier,verifySchema}from'asn1js';import {toHex,recoverPublicKey}from'viem';var d=class g extends VeChainAbstractSigner{kmsVeChainProvider;kmsVeChainGasPayerProvider;kmsVeChainGasPayerServiceUrl;constructor(i,e){if(super(i),this.provider!==void 0){if(!(this.provider instanceof s))throw new JSONRPCInvalidParams("KMSVeChainSigner.constructor","The provider must be an instance of KMSVeChainProvider.",{provider:i});this.kmsVeChainProvider=this.provider;}if(e!==void 0)if(e.provider!==void 0&&e.provider instanceof s)this.kmsVeChainGasPayerProvider=e.provider;else if(e.url!==void 0)this.kmsVeChainGasPayerServiceUrl=e.url;else throw new JSONRPCInvalidParams("KMSVeChainSigner.constructor","The gasPayer object is not well formed, either provider or url should be provided.",{gasPayer:e})}connect(i){try{return new g(i)}catch(e){throw new SignerMethodError("KMSVeChainSigner.connect","The signer could not be connected to the provider.",{provider:i},e)}}decodePublicKey(i){let e=new Sequence({value:[new Sequence({value:[new ObjectIdentifier]}),new BitString({name:"objectIdentifier"})]}),r=verifySchema(i,e);if(!r.verified)throw new SignerMethodError("KMSVeChainSigner.decodePublicKey",`Failed to parse the encoded public key: ${r.result.error}`,{parsed:r});let n=r.result.objectIdentifier.valueBlock.valueHex;return new Uint8Array(n)}async getDecodedPublicKey(i=this.kmsVeChainProvider){if(i===void 0)throw new JSONRPCInvalidParams("KMSVeChainSigner.getDecodedPublicKey","Thor provider is not found into the signer. Please attach a Provider to your signer instance.",{});let e=await i.getPublicKey();return this.decodePublicKey(e)}async getAddress(i=false){try{let e=i?this.kmsVeChainGasPayerProvider:this.kmsVeChainProvider,r=await this.getDecodedPublicKey(e);return Address.ofPublicKey(r).toString()}catch(e){throw new SignerMethodError("KMSVeChainSigner.getAddress","The address could not be retrieved.",{fromGasPayerProvider:i},e)}}async buildVeChainSignatureFromPayload(i,e=this.kmsVeChainProvider){if(e===void 0)throw new JSONRPCInvalidParams("KMSVeChainSigner.buildVeChainSignatureFromPayload","Thor provider is not found into the signer. Please attach a Provider to your signer instance.",{payload:i});let r=await e.sign(i),n=bytesToHex(r),t=secp256k1.Signature.fromDER(n).normalizeS(),o=await this.getRecoveryBit(t,i,e);return concatBytes(t.toCompactRawBytes(),new Uint8Array([o]))}async getRecoveryBit(i,e,r){let n=await this.getDecodedPublicKey(r),t=toHex(n);for(let o=0n;o<2n;o++)if(await recoverPublicKey({hash:e,signature:{r:toHex(i.r),s:toHex(i.s),v:o}})===t)return Number(o);throw new SignerMethodError("KMSVeChainSigner.getRecoveryBit","The recovery bit could not be found.",{decodedSignatureWithoutRecoveryBit:i,transactionHash:e})}async concatSignatureIfDelegation(i){let e=i.getTransactionHash().bytes,r=await this.buildVeChainSignatureFromPayload(e);if(this.kmsVeChainGasPayerProvider!==void 0){let n=await this.getDecodedPublicKey(),t=Address.ofPublicKey(n),o=i.getTransactionHash(t).bytes,y=await this.buildVeChainSignatureFromPayload(o,this.kmsVeChainGasPayerProvider);return concatBytes(r,y)}else if(this.kmsVeChainGasPayerServiceUrl!==void 0){let n=await this.getAddress(),t=await DelegationHandler({gasPayerServiceUrl:this.kmsVeChainGasPayerServiceUrl}).getDelegationSignatureUsingUrl(i,n,this.provider.thorClient.httpClient);return concatBytes(r,t)}return r}async signTransaction(i){try{let e=await this.populateTransaction(i),r=Transaction.of(e),n=await this.concatSignatureIfDelegation(r);return Hex.of(Transaction.of(e,n).encoded).toString()}catch(e){throw new SignerMethodError("KMSVeChainSigner.signTransaction","The transaction could not be signed.",{transactionToSign:i},e)}}async sendTransaction(i){try{let e=await this.signTransaction(i);return await this.kmsVeChainProvider?.request({method:RPC_METHODS.eth_sendRawTransaction,params:[e]})}catch(e){throw new SignerMethodError("KMSVeChainSigner.sendTransaction","The transaction could not be sent.",{transactionToSend:i},e)}}async signPayload(i){let e=await this.buildVeChainSignatureFromPayload(i);return e[e.length-1]+=27,Hex.of(e).toString()}};var s=class extends VeChainProvider{kmsClient;keyId;signer;constructor(i,e,r=false){super(i,void 0,r),this.keyId=e.keyId,this.kmsClient=e.endpoint!==void 0?new KMSClient({region:e.region,endpoint:e.endpoint,credentials:e.credentials}):e.credentials!==void 0?new KMSClient({region:e.region,credentials:e.credentials}):new KMSClient({region:e.region});}async getSigner(i){return this.signer!==void 0?this.signer:(this.signer=new d(this),await Promise.resolve(this.signer))}async getPublicKey(){let i=new GetPublicKeyCommand({KeyId:this.keyId}),e=await this.kmsClient.send(i);if(e.PublicKey===void 0)throw new ProviderMethodError("KMSVeChainProvider.getPublicKey","The public key could not be retrieved.",{getPublicKeyOutput:e});return e.PublicKey}async sign(i){let e=new SignCommand({KeyId:this.keyId,Message:i,SigningAlgorithm:SigningAlgorithmSpec.ECDSA_SHA_256,MessageType:MessageType.DIGEST}),r=await this.kmsClient.send(e);if(r.Signature===void 0)throw new ProviderMethodError("KMSVeChainProvider.sign","The signature could not be generated.",{signOutput:r});return r.Signature}};export{s as KMSVeChainProvider,d as KMSVeChainSigner};//# sourceMappingURL=index.mjs.map
//# sourceMappingURL=index.mjs.map