🟦
Mobile SDK Wallets

Credentials

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. 1.
    Address,
  2. 2.
    ​TransactionSigner interface that signs a transaction that should be sent from this address. A TransactionSigner interface can be either TransactionSigner.Blocking (a blocking mode) or TransactionSigner.Async (a non-blocking mode).
Symbiosis Mobile SDK implements transaction signing with a mnemonic phrase and a private key.

Wallet authorization

Mnemonic phrase

There is a recommended way to create KeyPhrase.
  1. 1.
    Wrap a string into KeyPhrase:
1
val keyPhrase = KeyPhrase.wrapChecked(
2
keyPhrase = "..."
3
) ?: error("This keyphrase is invalid")
Copied!
It returns null, if the string is not valid.
2. Create Credentials:
1
val credentials = Credentials.createFromKeyPhrase(keyPhrase)
Copied!
Credentials.createFromKeyPhrase doesn't throw an error since any KeyPhrase can be used to create a valid Credentials

Private key

There is an example of how to create Credentials with a private key:
1
val credentials: Credentials = Credentials.createFromPrivateKey(
2
key = Hex32String("...")
3
)
Copied!
Credentials.createFromPrivateKey doesn't throw an error since anyHex32String can be used to create a valid Credentials

Common way

Credentials.createFromKeyPhraseOrPrivateKey() recognizes a string without spaces as a private key and a string with spaces as a mnemonic phrase.
1
val credentials: Credentials? = Credentials.createFromKeyPhraseOrPrivateKey(
2
value = "..."
3
)
Copied!
Credentials.createFromKeyPhraseOrPrivateKey returns null, if a mnemonic phrase is not valid.

Other ways

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:
1
class MetaMaskSigner(
2
private val address: WalletAddress
3
) : TransactionSigner.Async {
4
override suspend fun signTransferTransaction(
5
nonce: BigInt,
6
chainId: BigInt,
7
to: WalletAddress,
8
value: BigInt,
9
gasConfiguration: GasConfiguration
10
): SignedTransaction = TODO()
11
​
12
override suspend fun signContractTransaction(
13
nonce: BigInt,
14
chainId: BigInt,
15
to: ContractAddress,
16
contractData: String,
17
value: BigInt,
18
gasConfiguration: GasConfiguration
19
): SignedTransaction = TODO()
20
}
21
// defining an extension function
22
fun Credentials.Companion.createFromMetaMask(address: WalletAddress) =
23
Credentials(address, MetaMaskSigner(address))
24
// using the extension function
25
val metaMask = Credentials.createFromMetaMask(/*...*/)
Copied!