@keyban/sdk-base
Version:
Keyban Javascript SDK provides core functionalities for the MPC wallet solution, supporting web and Node.js apps with TypeScript, custom storage, and Ethereum blockchain integration.
1 lines • 15.7 kB
Source Map (JSON)
{"version":3,"sources":["../src/stellar/account.ts","../src/stellar/signer.ts","../src/stellar/client.ts"],"names":["TX_VALIDITY_DURATION","STELLAR_DECIMALS","BigNumber","StellarAccount","KeybanAccount","#signer","#rpcProvider","rpcClient","rpcProvider","signer","publicKey","message","hash","Buffer","to","value","account","networkDetails","paymentTx","TransactionBuilder","BASE_FEE","Operation","Asset","signature","#submitTransaction","_to","_params","tx","response","finalResponse","rpc","rawTx","networkPassphrase","setupTx","Transaction","StellarSigner","#rpcClient","#clientShare","#publicKey","clientShare","StrKey","data","hexSignature","hint","xdr","keyHint","byte","i","a","ed25519","id","StellarClient","KeybanClientBase","config","metadataConfig","provider","stellarPubKey"],"mappings":"+TA2BA,IAAMA,CAAAA,CAAuB,GACvBC,CAAmB,CAAA,CAAA,CACzBC,UAAU,MAAO,CAAA,CACf,eAAgBD,CAClB,CAAC,EAEM,IAAME,CAAAA,CAAN,cAA6BC,CAAc,CAChD,QACA,SACAC,CAAAA,EAAAA,CACAC,GAEA,WACEC,CAAAA,CAAAA,CACAC,EACAC,CACAC,CAAAA,CAAAA,CACA,CACA,KAAMH,CAAAA,CAAS,EAEf,IAAKD,CAAAA,EAAAA,CAAeE,EACpB,IAAKH,CAAAA,EAAAA,CAAUI,EAEf,IAAK,CAAA,OAAA,CAAUC,EACf,IAAK,CAAA,SAAA,CAAYA,EACnB,CAEA,MAAM,YAAYC,CAA0C,CAAA,CAC1D,IAAMC,CAAO,CAAA,MAAM,OAAO,MAAO,CAAA,MAAA,CAC/B,UACA,IAAI,WAAA,GAAc,MAAOD,CAAAA,CAAO,CAClC,CAEA,CAAA,OAAA,CADsB,MAAM,IAAA,CAAKN,GAAQ,IAAKQ,CAAAA,MAAAA,CAAO,KAAKD,CAAI,CAAC,GAC1C,QAAS,CAAA,QAAQ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,KAAKhD,EAAe,CAAA,IAAA,CAAK,eAAe,IACrC+C,CAAAA,CAAAA,EAAW,IAAIvB,GAAI,CAAA,MAAA,CAAOuB,EAAO,OAAQ,CAAA,MAAM,CAClD,EACF,CAEA,MAAM,UAAsC,EAAA,CAC1C,IAAId,CAAc,CAAA,MAAM,KAAK,mBAAoB,CAAA,GAAA,CAAI,OAAO,CACvDA,CAAAA,CAAAA,GACHA,EAAc,MAAM,IAAA,CAAK,UAAU,IAAK,CAAA,OAAA,CAAS,KAAK,CACtD,CAAA,MAAM,KAAK,mBAAoB,CAAA,GAAA,CAAI,QAASA,CAAW,CAAA,CAAA,CAGzD,GAAM,CAACgB,CAAAA,CAAU7C,CAAS,CAAI,CAAA,MAAM,QAAQ,GAAI,CAAA,CAC9C,KAAKJ,EACL,CAAA,IAAA,CAAK,UAAU,IAAK,CAAA,OAAA,CAAS,YAAaiC,CAAW,CACvD,CAAC,CAEK9B,CAAAA,CAAAA,CAAS,IAAI0B,CAAc,CAAA,IAAA,CAAK,UAAWI,CAAa7B,CAAAA,CAAS,EAEjE8C,CAAgBhB,CAAAA,MAAAA,CAAO,uBAC3B3B,MAAO,CAAA,IAAA,CAAKH,EAAW,KAAK,CAC9B,EAEA,OAAO,IAAIP,EAAe,IAAK,CAAA,SAAA,CAAWoD,EAAU9C,CAAQ+C,CAAAA,CAAa,CAC3E,CACF","file":"stellar-PRHQHVS4.mjs","sourcesContent":["/* eslint-disable @typescript-eslint/no-unused-vars */\nimport {\n Asset,\n BASE_FEE,\n FeeBumpTransaction,\n Operation,\n rpc,\n Transaction,\n TransactionBuilder,\n} from \"@stellar/stellar-sdk\";\nimport { BigNumber } from \"bignumber.js\";\nimport { Buffer } from \"buffer\";\n\nimport {\n Address,\n EstimateERC20TransferParams,\n EstimateNftTransferParams,\n FeesEstimation,\n Hash,\n Hex,\n KeybanAccount,\n TransferERC20Params,\n TransferNftParams,\n} from \"~/index\";\nimport { RpcClient } from \"~/rpc\";\nimport { StellarSigner } from \"~/stellar/signer\";\n\nconst TX_VALIDITY_DURATION = 25; // Slightly below the default polling timeout of 30 seconds\nconst STELLAR_DECIMALS = 7;\nBigNumber.config({\n DECIMAL_PLACES: STELLAR_DECIMALS,\n});\n\nexport class StellarAccount extends KeybanAccount {\n address: string;\n publicKey: string;\n #signer: StellarSigner;\n #rpcProvider: rpc.Server;\n\n constructor(\n rpcClient: RpcClient,\n rpcProvider: rpc.Server,\n signer: StellarSigner,\n publicKey: string,\n ) {\n super(rpcClient);\n\n this.#rpcProvider = rpcProvider;\n this.#signer = signer;\n\n this.address = publicKey; // stellar addresses aren't hexa https://stellar.github.io/js-stellar-sdk/Address.html#Address\n this.publicKey = publicKey; // account.accountId == publicKey == address\n }\n\n async signMessage(message: string): Promise<Hex | string[]> {\n const hash = await crypto.subtle.digest(\n \"SHA-256\",\n new TextEncoder().encode(message),\n );\n const signedMessage = await this.#signer.sign(Buffer.from(hash));\n return signedMessage.toString(\"base64\");\n }\n\n async transfer(to: Address, value: bigint): Promise<string> {\n const [account, networkDetails] = await Promise.all([\n this.#rpcProvider.getAccount(this.address),\n this.#rpcProvider.getNetwork(),\n ]);\n const paymentTx = new TransactionBuilder(account, {\n fee: BASE_FEE,\n networkPassphrase: networkDetails.passphrase,\n })\n .addOperation(\n Operation.payment({\n destination: to,\n asset: Asset.native(),\n amount: BigNumber(value)\n .dividedBy(10 ** STELLAR_DECIMALS)\n .toString(),\n }),\n )\n .setTimeout(TX_VALIDITY_DURATION)\n .build();\n\n // sign the transaction\n const signature = await this.#signer.signDecorated(paymentTx.hash());\n paymentTx.addDecoratedSignature(signature);\n\n // submit the transaction to the network\n return this.#submitTransaction(paymentTx);\n }\n\n estimateTransfer(_to: Address): Promise<FeesEstimation> {\n throw new Error(\"estimateTransfer not implemented.\");\n }\n\n transferERC20(_params: TransferERC20Params): Promise<Hash> {\n throw new Error(\"transferERC20 not implemented.\");\n }\n\n estimateERC20Transfer(\n _params: EstimateERC20TransferParams,\n ): Promise<FeesEstimation> {\n throw new Error(\"estimateERC20Transfer not implemented.\");\n }\n\n transferNft(_params: TransferNftParams): Promise<Hash> {\n throw new Error(\"transferNft not implemented.\");\n }\n\n estimateNftTransfer(\n _params: EstimateNftTransferParams,\n ): Promise<FeesEstimation> {\n throw new Error(\"estimateNftTransfer not implemented.\");\n }\n\n async #submitTransaction(\n tx: Transaction | FeeBumpTransaction,\n ): Promise<string> {\n return this.#rpcProvider\n .sendTransaction(tx)\n .then(async (response) => {\n if (response.status !== \"PENDING\") {\n throw response; // TODO handle this better\n }\n return this.#rpcProvider.pollTransaction(response.hash);\n })\n .then((finalResponse) => {\n switch (finalResponse.status) {\n case rpc.Api.GetTransactionStatus.FAILED:\n case rpc.Api.GetTransactionStatus.NOT_FOUND:\n throw finalResponse; // TODO handle this better\n case rpc.Api.GetTransactionStatus.SUCCESS:\n return finalResponse.txHash;\n }\n });\n }\n\n async loyaltySetup(): Promise<void> {\n try {\n const account = await this.#rpcProvider.getAccount(this.publicKey);\n if (account) return;\n } catch {\n console.log(\"Stellar Account not yet setup for Loyalty, Doing it now...\");\n }\n // Init the account setup\n const rawTx = await this.rpcClient.call(\"loyalty\", \"initAccountSetup\");\n // Decode the raw transaction\n const { passphrase: networkPassphrase } =\n await this.#rpcProvider.getNetwork();\n const setupTx = new Transaction(rawTx, networkPassphrase);\n // sign the transaction\n const signature = await this.#signer.signDecorated(setupTx.hash());\n setupTx.addDecoratedSignature(signature);\n // serialize and submit the transaction to the network\n await this.rpcClient.call(\"loyalty\", \"submitAccountSetup\", setupTx.toXDR());\n }\n}\n","import { ed25519 } from \"@noble/curves/ed25519\";\nimport { KeypairType, StrKey, xdr } from \"@stellar/stellar-sdk\";\nimport { Buffer } from \"buffer\";\n\nimport { Hex } from \"~/index\";\nimport { RpcClient } from \"~/rpc\";\n\n/**\n * StellarSigner\n * This class provides functionality for signing transactions and messages on the Stellar blockchain.\n * It closely resembles the Keypair interface from the stellar-sdk, but its signing methods are asynchronous.\n */\nexport class StellarSigner {\n type: KeypairType = \"ed25519\";\n\n #rpcClient: RpcClient;\n #clientShare: string;\n #publicKey: Hex;\n\n constructor(rpcClient: RpcClient, clientShare: string, publicKey: Hex) {\n this.#rpcClient = rpcClient;\n this.#clientShare = clientShare;\n this.#publicKey = publicKey;\n }\n\n publicKey(): string {\n return StrKey.encodeEd25519PublicKey(this.rawPublicKey());\n }\n\n rawPublicKey(): Buffer {\n const publicKey = this.#publicKey.replace(/^0x/, \"\");\n return Buffer.from(publicKey, \"hex\");\n }\n\n async sign(data: Buffer): Promise<Buffer> {\n const hexSignature = await this.#rpcClient.call(\n \"eddsa\",\n \"sign\",\n this.#clientShare,\n data.toString(\"hex\"),\n );\n return Buffer.from(hexSignature, \"hex\");\n }\n\n async signDecorated(data: Buffer): Promise<xdr.DecoratedSignature> {\n const signature = await this.sign(data);\n const hint = this.signatureHint();\n\n return new xdr.DecoratedSignature({ hint, signature });\n }\n\n async signPayloadDecorated(data: Buffer): Promise<xdr.DecoratedSignature> {\n const signature = await this.sign(data);\n const keyHint = this.signatureHint();\n\n let hint = Buffer.from(data.slice(-4));\n if (hint.length < 4) {\n // append zeroes as needed\n hint = Buffer.concat([hint, Buffer.alloc(4 - data.length, 0)]);\n }\n\n return new xdr.DecoratedSignature({\n hint: hint.map((byte, i) => byte ^ keyHint[i]) as Buffer,\n signature,\n });\n }\n\n signatureHint(): Buffer {\n const a = this.xdrAccountId().toXDR();\n return a.slice(a.length - 4);\n }\n\n verify(data: Buffer, signature: Buffer): boolean {\n try {\n return ed25519.verify(signature, data, this.rawPublicKey());\n } catch {\n return false;\n }\n }\n\n xdrAccountId(): xdr.AccountId {\n return xdr.PublicKey.publicKeyTypeEd25519(this.rawPublicKey());\n }\n\n xdrPublicKey(): xdr.PublicKey {\n return xdr.PublicKey.publicKeyTypeEd25519(this.rawPublicKey());\n }\n\n xdrMuxedAccount(id: string): xdr.MuxedAccount {\n if (typeof id !== \"undefined\") {\n if (typeof id !== \"string\") {\n throw new TypeError(`expected string for ID, got ${typeof id}`);\n }\n\n return xdr.MuxedAccount.keyTypeMuxedEd25519(\n new xdr.MuxedAccountMed25519({\n id: xdr.Uint64.fromString(id),\n ed25519: this.rawPublicKey(),\n }),\n );\n }\n\n return xdr.MuxedAccount.keyTypeEd25519(this.rawPublicKey());\n }\n}\n","import { rpc, StrKey } from \"@stellar/stellar-sdk\";\nimport { Buffer } from \"buffer\";\n\nimport { KeybanClientBase, KeybanClientConfig, MetadataConfig } from \"~/client\";\nimport { StellarAccount } from \"~/stellar/account\";\nimport { StellarSigner } from \"~/stellar/signer\";\n\nexport class StellarClient extends KeybanClientBase {\n #rpcProvider: Promise<rpc.Server>;\n\n constructor(\n config: KeybanClientConfig,\n metadataConfig?: Promise<MetadataConfig>,\n ) {\n super(config, metadataConfig);\n\n this.#rpcProvider = this.metadataConfig.then(\n (config) => new rpc.Server(config.network.rpcUrl),\n );\n }\n\n async initialize(): Promise<StellarAccount> {\n let clientShare = await this.clientShareProvider.get(\"eddsa\");\n if (!clientShare) {\n clientShare = await this.rpcClient.call(\"eddsa\", \"dkg\");\n await this.clientShareProvider.set(\"eddsa\", clientShare);\n }\n\n const [provider, publicKey] = await Promise.all([\n this.#rpcProvider,\n this.rpcClient.call(\"eddsa\", \"publicKey\", clientShare),\n ]);\n\n const signer = new StellarSigner(this.rpcClient, clientShare, publicKey);\n\n const stellarPubKey = StrKey.encodeEd25519PublicKey(\n Buffer.from(publicKey, \"hex\"),\n );\n\n return new StellarAccount(this.rpcClient, provider, signer, stellarPubKey);\n }\n}\n"]}