Mantle
Mantle SDK repository
Initialising a new Mantle instance:
const mantle = new Mantle()
The mantle instance exposes methods to facilitate, amongst other things, mnemonic and HD public/private key generation, IPFS API access etc.
Configuring Mantle
Mantle accepts a configuration object on instantiation, as below:
const mantle = new Mantle({
provider: 'http://localhost:8545',
proxyURL: 'http://localhost:3000/api',
contracts: [ {
name: 'foo',
address: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe',
abi: [ {
type: 'function',
name: 'foo',
constant: false,
payable: false,
stateMutability: 'nonpayable',
inputs: [ { 'name': 'b', 'type': 'uint256' }, { 'name': 'c', 'type': 'bytes32' } ],
outputs: [ { 'name': '', 'type': 'address' } ]
} ]
} ]
tokens: {
ERC20: [
{ name: 'TokenName', address: '0x...'}
]
},
})
Mnemonic generation
Mnemonic, HD private/public keys and private/public keys are generated via loadMnemonic
. Supply a mnemonic associated with an existing account in order to retrieve key information, or supply no argument in order to generate a new set of keys.
Symmetric encryption
Facilitated via the encryptSymmetric
and decryptSymmetric
static methods. Shared secrets can be generated via createSymmetricKey
.
Examples
Please see tests in test/mantle.spec.js
for further examples.
Mnemonic generation and key removal
mantle.mnemonic
mantle.loadMnemonic()
mantle.mnemonic
mantle.removeKeys()
mantle.mnemonic
mantle.loadMnemonic('tragic panic toast hazard royal marine visual laptop salmon guard finger upper')
Asymmetric encryption/decryption
const data = 'foo'
const encrypted = Mantle.encrypt(data, mantle.publicKey)
const decrypted = Mantle.decrypt(encrypted, mantle.privateKey) || mantle.decrypt(encrypted)
Symmetric encryption/decryption
const data = 'foo'
const secret = Mantle.createSymmetricKey()
const encrypted = Mantle.encryptSymmetric(data, secret)
const decrypted = Mantle.decryptSymmetric(encrypted, secret)
Tokens
If your config define one or multiple tokens they will be loaded automatically. The first token will become the defaultToken accessible via mantle.defaultToken
. Other tokens can be access via mantle.tokens.TokenName
.
Each tokens also has two convenience methods: getBalance(address)
with address defaulting to mantle.address and sendTokens(address, amount)
. The default token getBalance and sendTokens methods are also aliased directly to mantle.getBalance()
and mantle.sendTokens(...)
.