-
Notifications
You must be signed in to change notification settings - Fork 2
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
feat: implement the adena wallet provider #3
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c76eed9
chore: bump eslint plugins
jinoosss 6732a20
refactor: refactor to adena provider
jinoosss 2ad943c
feat: implement the adena wallet provider
jinoosss 2c00c19
docs: add comments
jinoosss 8a01333
refactor: utility function and types
jinoosss f0fe4fb
refactor: utility function and types
jinoosss 6ef133a
chore: fix ignore path
jinoosss cd792ad
refactor: simplify core and provider directory
jinoosss 8b9a3af
chore: remove package-lock.json file
jinoosss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ node_modules | |
!.yarn/versions | ||
|
||
# testing | ||
/coverage | ||
coverage | ||
|
||
# Build files | ||
build | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './wallet'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { | ||
AccountInfo, | ||
BroadcastType, | ||
SingTransaction, | ||
TransactionData, | ||
TransactionResult, | ||
TransactionResultCommit, | ||
TransactionResultSync, | ||
WalletResponse, | ||
} from '../types'; | ||
|
||
export interface WalletProvider { | ||
isConnected: () => Promise<WalletResponse<void>>; | ||
|
||
/** | ||
* Establish a connection to your site from Adena | ||
* @async | ||
* @param {string} name - The name of the website requesting to connect | ||
* @returns Original Adena response, useful to check if the site was already connected | ||
*/ | ||
addEstablish: (name: string) => Promise<WalletResponse<void>>; | ||
|
||
/** | ||
* Fetch information about the current connected account | ||
* @async | ||
* @returns Original Adena response with the account information | ||
*/ | ||
getAccount: () => Promise<WalletResponse<AccountInfo>>; | ||
|
||
/** | ||
* Switches the Adena network to the given chain ID | ||
* @async | ||
* @param {string} chainId - Chain ID | ||
* @returns Nothing, throws an error if it fails | ||
*/ | ||
switchNetwork: (chainId: string) => Promise<WalletResponse<void>>; | ||
|
||
/** | ||
* Add a custom network to Adena | ||
* @async | ||
* @param {string} chainId - Chain ID | ||
* @param {string} chainName - Chain name | ||
* @param {string} rpcUrl - Network RPC URL | ||
* @returns Nothing, throws an error if it fails | ||
*/ | ||
addNetwork: (chainId: string, chainName: string, rpcUrl: string) => Promise<WalletResponse<void>>; | ||
|
||
/** | ||
* Sign a transaction crafted by a web-app | ||
* @async | ||
* @param {ContractMessage[]} messages - Messages to send in the transaction | ||
* @param {number} gasFee - Actual network fee to pay (in ugnot) | ||
* @param {number} gasWanted - Gas limit (in ugnot) | ||
* @param {string} memo - Transaction memo (tag) | ||
* @returns {string} Encoded transaction | ||
*/ | ||
signTransaction: (transactionData: TransactionData) => Promise<WalletResponse<SingTransaction>>; | ||
|
||
/** | ||
* Sign and broadcast a transaction crafted by a web-app | ||
* @async | ||
* @param {ContractMessage[]} messages - Messages to send in the transaction | ||
* @param {number} gasFee - Actual network fee to pay (in ugnot) | ||
* @param {number} gasWanted - Gas limit (in ugnot) | ||
* @param {string} memo - Transaction memo (tag) | ||
* @returns {BroadcastTxCommitResult} Result of the broadcast transaction | ||
*/ | ||
broadcastTransaction: ( | ||
transactionData: TransactionData, | ||
broadcastType?: BroadcastType | ||
) => Promise<WalletResponse<TransactionResult | TransactionResultSync | TransactionResultCommit>>; | ||
|
||
/** | ||
* Add a listener on connected account changes | ||
* @async | ||
* @param {OnAccountChangeFunc} func - Function to call on a new event | ||
* @returns Nothing, throws an error if it fails | ||
*/ | ||
onChangeAccount: (callback: (address: string) => void) => void; | ||
|
||
/** | ||
* Add a listener on network changes | ||
* @async | ||
* @param {OnNetworkChangeFunc} func - Function to call on a new event | ||
* @returns Nothing, throws an error if it fails | ||
*/ | ||
onChangeNetwork: (callback: (chainId: string) => void) => void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export type AccountStatusType = 'ACTIVE' | 'IN_ACTIVE'; | ||
|
||
export type AccountInfo = { | ||
accountNumber: string; | ||
address: string; | ||
coins: string; | ||
chainId: string; | ||
sequence: string; | ||
status: AccountStatusType; | ||
public_key: { | ||
'@type': string; | ||
value: string; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './wallet.types'; | ||
export * from './account.types'; | ||
export * from './transaction.types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { MsgAddPackage, MsgCall, MsgSend } from '@gnolang/gno-js-client'; | ||
import { MsgRun } from '@gnolang/gno-js-client/bin/proto/gno/vm'; | ||
|
||
export type MessageType = '/bank.MsgSend' | '/vm.m_call' | '/vm.m_addpkg' | '/vm.m_run'; | ||
|
||
export type MessageValue = MsgAddPackage | MsgCall | MsgSend | MsgRun; | ||
|
||
export enum BroadcastType { | ||
SYNC = 'SYNC', | ||
COMMIT = 'COMMIT', | ||
} | ||
|
||
export type TransactionMessage = { | ||
type: MessageType; | ||
value: MessageValue; | ||
}; | ||
|
||
export interface TransactionData { | ||
messages: TransactionMessage[]; | ||
gasFee: number; | ||
gasWanted: number; | ||
memo?: string; | ||
} | ||
|
||
export interface SignTransactionData { | ||
document: SignDocument; | ||
signature: Signature; | ||
} | ||
|
||
export interface SignDocument { | ||
msgs: TransactionMessage[]; | ||
fee: { | ||
amount: { amount: string; denom: string }[]; | ||
gas: string; | ||
}; | ||
chain_id: string; | ||
memo: string; | ||
account_number: string; | ||
sequence: string; | ||
} | ||
|
||
export interface Signature { | ||
pubKey: { | ||
typeUrl: string; | ||
value: string; | ||
}; | ||
signature: string; | ||
} | ||
|
||
export interface SingTransaction { | ||
encodedTransaction: string; | ||
} | ||
|
||
interface TransactionEvent { | ||
'@type': string; | ||
type: string; | ||
attrs: Record<string, string>[]; | ||
pkg_path: string; | ||
func: string; | ||
} | ||
|
||
interface TransactionResultResponseBase { | ||
Error: { log: string } | null; | ||
Data: string; | ||
Events: TransactionEvent[]; | ||
Log: string; | ||
Info: string; | ||
} | ||
|
||
interface TransactionResultResponse { | ||
ResponseBase: TransactionResultResponseBase; | ||
GasWanted: string; | ||
GasUsed: string; | ||
} | ||
|
||
export interface TransactionResult { | ||
hash: string; | ||
} | ||
|
||
export interface TransactionResultSync extends TransactionResult { | ||
data?: string; | ||
log?: string; | ||
} | ||
|
||
export interface TransactionResultCommit extends TransactionResult { | ||
check_tx?: TransactionResultResponse; | ||
deliver_tx?: TransactionResultResponse; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
export interface WalletResponse<D> { | ||
code: number; | ||
status: WalletResponseStatus; | ||
type: WalletResponseType; | ||
message: string; | ||
data: D | null; | ||
} | ||
|
||
export enum WalletResponseStatus { | ||
SUCCESS = 'success', | ||
FAILURE = 'failure', | ||
} | ||
|
||
export type WalletResponseType = WalletResponseSuccessType | WalletResponseFailedType | WalletResponseRejectedType; | ||
|
||
export enum WalletResponseSuccessType { | ||
CONNECTION_SUCCESS = 'CONNECTION_SUCCESS', | ||
GET_ACCOUNT_SUCCESS = 'GET_ACCOUNT_SUCCESS', | ||
SIGN_SUCCESS = 'SIGN_SUCCESS', | ||
ADD_NETWORK_SUCCESS = 'ADD_NETWORK_SUCCESS', | ||
SWITCH_NETWORK_SUCCESS = 'SWITCH_NETWORK_SUCCESS', | ||
TRANSACTION_SUCCESS = 'TRANSACTION_SUCCESS', | ||
} | ||
|
||
export enum WalletResponseFailedType { | ||
NOT_CONNECTED = 'NOT_CONNECTED', | ||
UNRESOLVED_TRANSACTION_EXISTS = 'UNRESOLVED_TRANSACTION_EXISTS', | ||
INVALID_FORMAT = 'INVALID_FORMAT', | ||
WALLET_LOCKED = 'WALLET_LOCKED', | ||
ACCOUNT_MISMATCH = 'ACCOUNT_MISMATCH', | ||
NO_ACCOUNT = 'NO_ACCOUNT', | ||
TRANSACTION_FAILED = 'TRANSACTION_FAILED', | ||
SIGN_FAILED = 'SIGN_FAILED', | ||
ALREADY_CONNECTED = 'ALREADY_CONNECTED', | ||
NETWORK_TIMEOUT = 'NETWORK_TIMEOUT', | ||
REDUNDANT_CHANGE_REQUEST = 'REDUNDANT_CHANGE_REQUEST', | ||
NETWORK_ALREADY_EXISTS = 'NETWORK_ALREADY_EXISTS', | ||
UNADDED_NETWORK = 'UNADDED_NETWORK', | ||
UNSUPPORTED_TYPE = 'UNSUPPORTED_TYPE', | ||
UNEXPECTED_ERROR = 'UNEXPECTED_ERROR', | ||
} | ||
|
||
export enum WalletResponseRejectedType { | ||
TRANSACTION_REJECTED = 'TRANSACTION_REJECTED', | ||
SIGN_REJECTED = 'SIGN_REJECTED', | ||
CONNECTION_REJECTED = 'CONNECTION_REJECTED', | ||
SWITCH_NETWORK_REJECTED = 'SWITCH_NETWORK_REJECTED', | ||
ADD_NETWORK_REJECTED = 'ADD_NETWORK_REJECTED', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1 @@ | ||
import { EMessageType } from './types'; | ||
|
||
export { EMessageType }; | ||
export * from './methods'; | ||
export * from './providers'; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we unify those types in the future in a common package used by the main Adena repo and this SDK ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I would like to unify the types and responses.
If the SDK and wallet responses were separate, it wouldn't be a problem if the provider did the mapping for me, but I'd have to manage them separately and always consider what I'm missing.