◻
Mobile SDK Wallets
Symbiosis Mobile SDK for Symbiosis protocol V1: Wallets
Every transaction that changes the state of a blockchain (a swap, a token transfer, etc.) requires
Credentials
as a parameter. Credentials
class is an abstraction over transaction signing and consists of: - 1.
Address
, - 2.
TransactionSigner
interface that signs a transaction that should be sent from this address. ATransactionSigner
interface can be eitherTransactionSigner.Blocking
(a blocking mode) orTransactionSigner.Async
(a non-blocking mode).
Symbiosis Mobile SDK implements transaction signing with a mnemonic phrase and a private key.
There is a recommended way to create KeyPhrase.
- 1.Wrap a string into
KeyPhrase
:
val keyPhrase = KeyPhrase.wrapChecked(
keyPhrase = "..."
) ?: error("This keyphrase is invalid")
It returns
null
, if the string is not valid.2. Create
Credentials
:val credentials = Credentials.createFromKeyPhrase(keyPhrase)
Credentials.createFromKeyPhrase
doesn't throw an error since any KeyPhrase
can be used to create a valid Credentials
There is an example of how to create
Credentials
with a private key:val credentials: Credentials = Credentials.createFromPrivateKey(
key = Hex32String("...")
)
Credentials.createFromPrivateKey
doesn't throw an error since anyHex32String
can be used to create a valid Credentials
Credentials.createFromKeyPhraseOrPrivateKey()
recognizes a string without spaces as a private key and a string with spaces as a mnemonic phrase. val credentials: Credentials? = Credentials.createFromKeyPhraseOrPrivateKey(
value = "..."
)
Credentials.createFromKeyPhraseOrPrivateKey
returns null
, if a mnemonic phrase is not valid. You can define an additional way to sign a transaction, e.g., via MetaMask. To do this, define a new class first and then pass an instance of that class to
Credentials
as in the example below:class MetaMaskSigner(
private val address: WalletAddress
) : TransactionSigner.Async {
override suspend fun signTransferTransaction(
nonce: BigInt,
chainId: BigInt,
to: WalletAddress,
value: BigInt,
gasConfiguration: GasConfiguration
): SignedTransaction = TODO()
override suspend fun signContractTransaction(
nonce: BigInt,
chainId: BigInt,
to: ContractAddress,
contractData: String,
value: BigInt,
gasConfiguration: GasConfiguration
): SignedTransaction = TODO()
}
// defining an extension function
fun Credentials.Companion.createFromMetaMask(address: WalletAddress) =
Credentials(address, MetaMaskSigner(address))
// using the extension function
val metaMask = Credentials.createFromMetaMask(/*...*/)
Last modified 11d ago