Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mnemonic & Node.js Support #23

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
354 changes: 80 additions & 274 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,17 @@
},
"homepage": "https://github.com/JackalLabs/jackal.js/#README",
"dependencies": {
"@cosmjs/proto-signing": "^0.31.3",
"@cosmjs/stargate": "^0.32.2",
"@cosmjs/proto-signing": "^0.32.3",
"@cosmjs/stargate": "^0.32.3",
"@jackallabs/bech32": "^1.2.0",
"@jackallabs/dogwood-tree": "^1.0.1",
"@jackallabs/jackal.js-protos": "2.3.1",
"@karnthis/plzsu": "^1.0.0",
"@keplr-wallet/types": "^0.12.44",
"browserify-aes": "^1.2.0",
"browserify-aes": "git+https://github.com/JackalLabs/browserify-aes.git",
"browserify-des": "^1.0.2",
"browserify-sign": "^4.2.2",
"cosmjs-types": "^0.9.0",
"create-hash": "^1.2.0",
"eciesjs": "^0.4.2",
"for-each": "^0.3.3",
Expand Down
7 changes: 7 additions & 0 deletions src/classes/clientHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ export class ClientHandler implements IClientHandler {
jklAddress = wallet.getAddress()
hostSigner = jklSigner
hostAddress = jklAddress
if (typeof window !== "undefined") {
const w = window as any
w.mnemonicWallet = wallet
} else {
const g = global as any
g.mnemonicWallet = wallet
}
}
break
default:
Expand Down
36 changes: 36 additions & 0 deletions src/classes/mnemonicSigner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { AccountData, DirectSignResponse, OfflineDirectSigner } from '@cosmjs/proto-signing'
import { AminoSignResponse, StdSignDoc, OfflineAminoSigner } from '@cosmjs/amino'
import { TMergedSigner } from '@jackallabs/jackal.js-protos'



interface SignDoc {
bodyBytes: Uint8Array;
authInfoBytes: Uint8Array;
chainId: string;
accountNumber: bigint;
}

export class MnemonicSigner implements TMergedSigner {
private readonly directSigner: OfflineDirectSigner
private readonly aminoSigner: OfflineAminoSigner

constructor (directSigner: OfflineDirectSigner, aminoSigner: OfflineAminoSigner) {
this.directSigner = directSigner
this.aminoSigner = aminoSigner
}

signAmino (signerAddress: string, signDoc: StdSignDoc): Promise<AminoSignResponse> {
return this.aminoSigner.signAmino(signerAddress, signDoc)
}

signDirect (signerAddress: string, signDoc: SignDoc): Promise<DirectSignResponse> {
return this.directSigner.signDirect(signerAddress, signDoc)
}

getAccounts (): Promise<readonly AccountData[]> {
return this.directSigner.getAccounts()
}


}
32 changes: 19 additions & 13 deletions src/classes/mnemonicWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Secp256k1HdWallet } from '@cosmjs/amino'
import { DirectSecp256k1HdWallet } from '@cosmjs/proto-signing'
import type { IMnemonicWallet } from '@/interfaces/classes'
import { TMergedSigner } from '@jackallabs/jackal.js-protos'
import { MnemonicSigner } from '@/classes/mnemonicSigner'

export class MnemonicWallet implements IMnemonicWallet {
private readonly mergedSigner: TMergedSigner
Expand Down Expand Up @@ -35,10 +36,7 @@ export class MnemonicWallet implements IMnemonicWallet {
/* Destroy mnemonic */
mnemonic = ''

const mergedSigner = {
...aminoWallet,
...directWallet,
} as TMergedSigner
const mergedSigner = new MnemonicSigner(directWallet, aminoWallet);
const { address } = (await mergedSigner.getAccounts())[0]
return new MnemonicWallet(mergedSigner, address)
}
Expand All @@ -65,25 +63,33 @@ export class MnemonicWallet implements IMnemonicWallet {
* @returns {Promise<StdSignature>} - Resulting AminoSignResponse.signature.
*/
async signArbitrary (message: string): Promise<StdSignature> {
const signed = await this.mergedSigner.signAmino(this.address, {
chain_id: '',
account_number: '0',
sequence: '0',
if (typeof message === "string") {
message = Buffer.from(message).toString("base64");
} else {
message = Buffer.from(message).toString("base64");
}

const doc= {
chain_id: "",
account_number: "0",
sequence: "0",
fee: {
gas: '0',
gas: "0",
amount: [],
},
msgs: [
{
type: 'sign/MsgSignData',
type: "sign/MsgSignData",
value: {
signer: this.address,
data: btoa(message),
data: message,
},
},
],
memo: '',
})
memo: "",
};

const signed = await this.mergedSigner.signAmino(this.address, doc)
return signed.signature
}
}
32 changes: 30 additions & 2 deletions src/classes/storageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
import type { TMetaHandler, TSharedRootMetaDataMap } from '@/types'
import { IConversionFolderBundle } from '@/interfaces/IConversionFolderBundle'
import { TFullSignerState } from '@/types/TFullSignerState'
import { MnemonicWallet } from '@/classes/mnemonicWallet'

export class StorageHandler extends EncodingHandler implements IStorageHandler {
protected readonly rns: IRnsHandler | null
Expand Down Expand Up @@ -106,7 +107,11 @@ export class StorageHandler extends EncodingHandler implements IStorageHandler {
this.meta = meta
this.providers = {}

window.addEventListener('beforeunload', this.beforeUnloadHandler)
if (typeof window !== "undefined") {
if ('addEventListener' in window) {
window.addEventListener('beforeunload', this.beforeUnloadHandler)
}
}
}

/**
Expand Down Expand Up @@ -184,6 +189,8 @@ export class StorageHandler extends EncodingHandler implements IStorageHandler {
throw new Error('Leap Wallet selected but unavailable')
}
break
case 'mnemonic':
break
default:
throw new Error(
'No wallet selected but one is required to init StorageHandler',
Expand Down Expand Up @@ -218,6 +225,23 @@ export class StorageHandler extends EncodingHandler implements IStorageHandler {
signatureAsHex = await stringToShaHex(signed.signature)
}
break
case 'mnemonic':
let w: any = {}
if (typeof window !== "undefined") {
w = window as any
} else {
w = global as any
}
if (!w.mnemonicWallet) {
throw 'Missing mnemonic wallet'
} else {
const seedWallet = w.mnemonicWallet as MnemonicWallet
signed = await seedWallet.signArbitrary(
signatureSeed,
)
signatureAsHex = await stringToShaHex(signed.signature)
}
break
default:
throw new Error(
'No wallet selected but one is required to init StorageHandler',
Expand All @@ -233,7 +257,11 @@ export class StorageHandler extends EncodingHandler implements IStorageHandler {
*
*/
cleanShutdown (): void {
window.removeEventListener('beforeunload', this.beforeUnloadHandler)
if (typeof window !== "undefined") {
if ('removeEventListener' in window) {
window.removeEventListener('beforeunload', this.beforeUnloadHandler)
}
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/classes/IMnemonicWallet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { StdSignature } from '@cosmjs/amino'
import type { TMergedSigner } from '@jackallabs/jackal.js-protos'
import { TMergedSigner } from '@jackallabs/jackal.js-protos'

export interface IMnemonicWallet {
getOfflineSigner (): TMergedSigner
Expand Down