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

Integrate Leap wallet #146

Merged
merged 23 commits into from
Jun 26, 2024
Merged
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
40 changes: 36 additions & 4 deletions __tests__/Mnemonic_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,41 @@ describe("Expect Mnemonic to work correctly", () => {
))
)

test("sign", () =>
expect(wallet->sign("msg"))->toEqual(
"rFJYA0BOYfuVn4+MBkn5n9D6YAMgGfXJbQytx4T2ePtXDnpaU8pEV+6t8XK0BhVhQYWX7Kdhx2x3iXiFYBfj2Q=="->JsBuffer.fromBase64,
testPromise("sign", async () => {
// open BandChainJS
let (address, pub) = wallet->getAddressAndPubKey

let coin = BandChainJS.Coin.create()
coin->BandChainJS.Coin.setDenom("uband")
coin->BandChainJS.Coin.setAmount("1000000")

let feeCoin = BandChainJS.Coin.create()
feeCoin->BandChainJS.Coin.setDenom("uband")
feeCoin->BandChainJS.Coin.setAmount("10000")

let fee = BandChainJS.Fee.create()
fee->BandChainJS.Fee.setAmountList([feeCoin])
fee->BandChainJS.Fee.setGasLimit(1000000)

let sendMsg = BandChainJS.Message.MsgSend.create(
address->Address.toBech32,
"band120q5vvspxlczc8c72j7c3c4rafyndaelqccksu",
[coin],
)
)

let client = BandChainJS.Client.create("https://laozi-testnet6.bandchain.org/grpc-web")
let account = await client->BandChainJS.Client.getAccount(address->Address.toBech32)
let chainID = await client->BandChainJS.Client.getChainId

let txn = BandChainJS.Transaction.create()
txn->BandChainJS.Transaction.withMessages(sendMsg)
txn->BandChainJS.Transaction.withAccountNum(account.accountNumber)
txn->BandChainJS.Transaction.withSequence(1) // fake sequence for test
txn->BandChainJS.Transaction.withChainId(chainID)
txn->BandChainJS.Transaction.withFee(fee)

expect(wallet->sign(txn)->JsBuffer.toBase64)->toEqual(
"uaa2v4DdQnpg6FOsR+eSjdsHzRKPNX2oarWcRDqeyyJK3EIwyPTF4uXiJ2tjcs3xpjYnqSZ6EfC6iND391DhSQ==",
)
})
})
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
},
"devDependencies": {
"@glennsl/rescript-jest": "^0.9.2",
"@keplr-wallet/types": "^0.12.12",
"@reasonml-community/graphql-ppx": "^1.2.2",
"@webpack-cli/serve": "^1.5.1",
"css-loader": "^6.7.1",
"eslint-plugin-prettier": "3.2.0",
Expand All @@ -28,6 +30,7 @@
"@apollo/client": "^3.6.9",
"@bandprotocol/bandchain.js": "^2.1.4",
"@cosmostation/cosmosjs": "0.5.5",
"@cosmostation/extension-client": "^0.1.15",
"@glennsl/rescript-json-combinators": "^1.0.0",
"@ledgerhq/hw-transport-webhid": "^6.4.1",
"@ledgerhq/hw-transport-webusb": "^6.3.0",
Expand Down
4 changes: 3 additions & 1 deletion src/Index.res
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ let make = () =>
<ThemeContext.Provider>
<ModalContext.Provider>
<AccountContext.Provider>
<App />
<WalletPopupContext.Provider>
<App />
</WalletPopupContext.Provider>
</AccountContext.Provider>
</ModalContext.Provider>
</ThemeContext.Provider>
Expand Down
24 changes: 23 additions & 1 deletion src/bindings/BandChainJS.res
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ type tx_response = {
rawLog: string,
}

// import { SignDoc } from '@bandprotocol/bandchain.js/proto/cosmos/tx/v1beta1/tx_pb'
module SignDoc = {
type t

@send external getChainId: t => string = "getChainId"
@send external getBodyBytes_asU8: t => array<int> = "getBodyBytes_asU8"
@send external getBodyBytes_asB64: t => string = "getBodyBytes_asB64"
@send external getAuthInfoBytes_asU8: t => array<int> = "getAuthInfoBytes_asU8"
@send external getAuthInfoBytes_asB64: t => string = "getAuthInfoBytes_asB64"
@send external getAccountNumber: t => int = "getAccountNumber"

@module("@bandprotocol/bandchain.js/proto/cosmos/tx/v1beta1/tx_pb") @scope("SignDoc") @val
external deserializeBinary: array<int> => t = "deserializeBinary"
}

module Client = {
type t
type reference_data_t = {
Expand Down Expand Up @@ -160,7 +175,14 @@ module Message = {
}

module Transaction = {
type transaction_t
type transaction_t = {
msgs: array<Message.t>,
accountNum?: int,
sequence?: int,
chainId?: string,
fee: Fee.t,
memo: string,
}

@module("@bandprotocol/bandchain.js") @new external create: unit => transaction_t = "Transaction"
@send external withMessages: (transaction_t, Message.t) => unit = "withMessages"
Expand Down
134 changes: 134 additions & 0 deletions src/bindings/Cosmostation.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
module Cosmos = {
type requestAccountResponse = {
address: string,
publicKey: array<int>,
name: string,
isLedger: bool,
}

type amount = {
denom: string,
amount: string,
}

type fee = {
amount: array<amount>,
gas: string,
}

type signAminoDoc = {
chain_id: string,
sequence: string,
account_number: string,
fee: fee,
memo: string,
msgs: array<Js.Json.t>,
}

type signAminoResponse = {
signature: string,
pub_key: {"type": string, "value": string},
signed_doc: signAminoDoc,
}

type signDirectDoc = {
chain_id: string,
body_bytes: array<int>,
auth_info_bytes: array<int>,
account_number: string,
}

type signDirectResponse = {
signature: string,
pub_key: {"type": string, "value": string},
signed_doc: signDirectDoc,
}

type gasRateType = {
tiny: string,
low: string,
average: string,
}

type signOptions = {
memo?: bool,
fee?: bool,
gasRate?: gasRateType,
}

type supportedChainNamesResponse = {
official: array<string>,
unofficial: array<string>,
}

type addChainParams = {
chainId: string,
chainName: string,
restURL: string,
imageURL?: string,
baseDenom: string,
displayDenom: string,
decimals?: float,
coinType?: string,
addressPrefix: string,
coinGeckoId?: string,
gasRate?: gasRateType,
sendGas?: string,
}

type t = {
getAccount: string => Js.Promise.t<requestAccountResponse>,
signDirect: (string, signDirectDoc, option<signOptions>) => Js.Promise.t<signDirectResponse>,
signAmino: (string, signAminoDoc, option<signOptions>) => Js.Promise.t<signAminoResponse>,
}

@module("@cosmostation/extension-client/cosmos")
external getSupportedChains: string => Js.Promise.t<supportedChainNamesResponse> =
"getSupportedChains"

@module("@cosmostation/extension-client/cosmos")
external getActivatedChains: string => Js.Promise.t<array<string>> = "getActivatedChains"

@module("@cosmostation/extension-client/cosmos")
external addChain: addChainParams => Js.Promise.t<array<string>> = "addChain"

@module("@cosmostation/extension-client/cosmos")
external getAccount: string => Js.Promise.t<requestAccountResponse> = "getAccount"

@module("@cosmostation/extension-client/cosmos")
external signDirect: (
string,
signDirectDoc,
option<signOptions>,
) => Js.Promise.t<signDirectResponse> = "signDirect"

@module("@cosmostation/extension-client/cosmos")
external signAmino: (
string,
signAminoDoc,
option<signOptions>,
) => Js.Promise.t<signAminoResponse> = "signAmino"

@module("@cosmostation/extension-client/cosmos")
external disconnect: unit => Js.Promise.t<unit> = "disconnect"

let getAminoSignDocFromTx = (tx: BandChainJS.Transaction.transaction_t) => {
account_number: tx.accountNum->Belt.Option.getExn->Belt.Int.toString,
chain_id: tx.chainId->Belt.Option.getExn,
fee: {
amount: tx.fee
->BandChainJS.Fee.getAmountList
->Belt.Array.map(coin => {
amount: coin->BandChainJS.Coin.getAmount,
denom: coin->BandChainJS.Coin.getDenom,
}),
gas: tx.fee->BandChainJS.Fee.getGasLimit->Belt.Int.toString,
},
memo: tx.memo,
msgs: tx.msgs->Belt.Array.map(msg => msg->BandChainJS.Message.MsgSend.toJSON),
sequence: tx.sequence->Belt.Option.getExn->Belt.Int.toString,
}
}

@module("@cosmostation/extension-client")
external cosmos: unit => Js.Promise.t<Cosmos.t> = "cosmos"
115 changes: 115 additions & 0 deletions src/bindings/Keplr.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
type t = {}
module Long = {
type t = {
high: int,
low: int,
unsigned: bool,
}

@module("long")
external fromNumber: int => t = "fromNumber"
}

type key = {
name: string,
algo: string,
pubKey: array<int>,
address: array<int>,
bech32Address: string,
isNanoLedger: bool,
isKeystone: bool,
}

type signDirectDoc = {
/** SignDoc bodyBytes */
bodyBytes?: array<int>,
/** SignDoc authInfoBytes */
authInfoBytes?: array<int>,
/** SignDoc chainId */
chainId?: string,
/** SignDoc accountNumber */
accountNumber?: Long.t,
}

type amount = {
denom: string,
amount: string,
}

type fee = {
amount: array<amount>,
gas: string,
}

type msg<'a> = {"type": string, "value": 'a}

type signAminoDoc<'a> = {
chain_id: string,
sequence: string,
account_number: string,
fee: fee,
memo: string,
msgs: array<Js.Json.t>,
}

type keplrSignOptions = {
preferNoSetFee?: bool,
preferNoSetMemo?: bool,
disableBalanceCheck?: bool,
}
type pubKey = {"type": string, "value": string}

type stdSignature = {
pub_key: pubKey,
signature: string,
}

type directSignResponse = {
signed: signDirectDoc,
signature: stdSignature,
}

@val @scope(("window", "keplr"))
external enable: string => Js.Promise.t<unit> = "enable"

@val @scope(("window", "keplr"))
external getKey: string => Js.Promise.t<key> = "getKey"

@val @scope(("window", "keplr"))
external signDirect: (
string,
string,
signDirectDoc,
keplrSignOptions,
) => Js.Promise.t<directSignResponse> = "signDirect"

@val @scope(("window", "keplr"))
external getChainInfosWithoutEndpoints: option<unit => unit> = "getChainInfosWithoutEndpoints"

@val @scope(("window", "keplr"))
external signAmino: (
string,
string,
signAminoDoc<'a>,
keplrSignOptions,
) => Js.Promise.t<directSignResponse> = "signAmino"

@val @scope("window")
external keplr: option<t> = "keplr"

let getAminoSignDocFromTx = (tx: BandChainJS.Transaction.transaction_t) => {
account_number: tx.accountNum->Belt.Option.getExn->Belt.Int.toString,
chain_id: tx.chainId->Belt.Option.getExn,
fee: {
amount: tx.fee
->BandChainJS.Fee.getAmountList
->Belt.Array.map(coin => {
amount: coin->BandChainJS.Coin.getAmount,
denom: coin->BandChainJS.Coin.getDenom,
}),
gas: tx.fee->BandChainJS.Fee.getGasLimit->Belt.Int.toString,
},
memo: tx.memo,
msgs: tx.msgs->Belt.Array.map(msg => msg->BandChainJS.Message.MsgSend.toJSON),
sequence: tx.sequence->Belt.Option.getExn->Belt.Int.toString,
}
Loading
Loading