|
| 1 | +import { OfflineDirectSigner } from '@cosmjs/proto-signing'; |
| 2 | +import { SignMode } from '../codec/cosmos/tx/signing/v1beta1/signing'; |
| 3 | +import { LumUtils, LumTypes, LumConstants } from '..'; |
| 4 | +import { LumWallet } from '.'; |
| 5 | +import Long from 'long'; |
| 6 | + |
| 7 | +export class LumOfflineSignerWallet extends LumWallet { |
| 8 | + private readonly offlineSigner: OfflineDirectSigner; |
| 9 | + |
| 10 | + /** |
| 11 | + * Create a LumOfflineSignerWallet instance based on an OfflineDirectSigner instance compatible with Comsjs based |
| 12 | + * implementations. |
| 13 | + * This constructor is not intended to be used directly as it does not initialize the underlying key pair |
| 14 | + * Better use the provided static LumPaperWallet builders |
| 15 | + * |
| 16 | + * @param mnemonicOrPrivateKey mnemonic (string) used to derive the private key or private key (Uint8Array) |
| 17 | + */ |
| 18 | + constructor(offlineSigner: OfflineDirectSigner) { |
| 19 | + super(); |
| 20 | + this.offlineSigner = offlineSigner; |
| 21 | + } |
| 22 | + |
| 23 | + signingMode = (): SignMode => { |
| 24 | + return SignMode.SIGN_MODE_DIRECT; |
| 25 | + }; |
| 26 | + |
| 27 | + canChangeAccount = (): boolean => { |
| 28 | + return false; |
| 29 | + }; |
| 30 | + |
| 31 | + useAccount = async (): Promise<boolean> => { |
| 32 | + const accounts = await this.offlineSigner.getAccounts(); |
| 33 | + if (accounts.length === 0) { |
| 34 | + throw new Error('No account available.'); |
| 35 | + } |
| 36 | + this.publicKey = accounts[0].pubkey; |
| 37 | + this.address = accounts[0].address; |
| 38 | + return true; |
| 39 | + }; |
| 40 | + |
| 41 | + sign = async (): Promise<Uint8Array> => { |
| 42 | + throw new Error('Feature not supported.'); |
| 43 | + }; |
| 44 | + |
| 45 | + signTransaction = async (doc: LumTypes.Doc): Promise<Uint8Array> => { |
| 46 | + if (!this.address || !this.publicKey) { |
| 47 | + throw new Error('No account selected.'); |
| 48 | + } |
| 49 | + const signerIndex = LumUtils.uint8IndexOf( |
| 50 | + doc.signers.map((signer) => signer.publicKey), |
| 51 | + this.publicKey as Uint8Array, |
| 52 | + ); |
| 53 | + if (signerIndex === -1) { |
| 54 | + throw new Error('Signer not found in document'); |
| 55 | + } |
| 56 | + const signDoc = LumUtils.generateSignDoc(doc, signerIndex, this.signingMode()); |
| 57 | + const response = await this.offlineSigner.signDirect(this.address, signDoc); |
| 58 | + return LumUtils.fromBase64(response.signature.signature); |
| 59 | + }; |
| 60 | + |
| 61 | + signMessage = async (msg: string): Promise<LumTypes.SignMsg> => { |
| 62 | + if (!this.address || !this.publicKey) { |
| 63 | + throw new Error('No account selected.'); |
| 64 | + } |
| 65 | + const signDoc = { |
| 66 | + bodyBytes: LumUtils.toAscii(msg), |
| 67 | + authInfoBytes: LumUtils.generateAuthInfoBytes([{ accountNumber: 0, sequence: 0, publicKey: this.getPublicKey() }], { amount: [], gas: '0' }, this.signingMode()), |
| 68 | + chainId: LumConstants.LumSignOnlyChainId, |
| 69 | + accountNumber: Long.fromNumber(0), |
| 70 | + }; |
| 71 | + const response = await this.offlineSigner.signDirect(this.address, signDoc); |
| 72 | + return { |
| 73 | + address: this.getAddress(), |
| 74 | + publicKey: this.getPublicKey(), |
| 75 | + msg: msg, |
| 76 | + sig: LumUtils.fromBase64(response.signature.signature), |
| 77 | + version: LumConstants.LumWalletSigningVersion, |
| 78 | + signer: LumConstants.LumMessageSigner.OFFLINE, |
| 79 | + }; |
| 80 | + }; |
| 81 | +} |
0 commit comments