Proof-of-concept example for a lending platform Part 1

Ko
CreditHydra
Published in
4 min readJan 6, 2018

This is a series of blogs about how to develop dApps on Ethereum.

We developed a proof-of-concept example of lending platform on blockchain.
You can check live demo here.
* this is just a proof of concept example…

We’re going to cover topics like…

  1. uPort integration
  2. light client
  3. Ethereum private network and smart contract
  4. Send data to API server with signed hash
  5. IPFS

What it is

Let’s start from explaining about what it is. This is a lending platform application on Ethereum private network. In a nutshell, user can get token in exchange for ether and lend the token to get interest.

You can do …

  1. Sign in with uPort which is one of identity service on blockchain.
  2. Create a wallet based on eth-lightwallet library
  3. Add some ether to your addresses.
  4. Buy and sell tokens
  5. [Lender] Create a loan offer on IPFS which include lender’s public key and move tokens to smart contract as a collateral.
  6. [Borrower] apply to the offer with profile data which is encrypted by lender’s public key
  7. [Lender] decrypt borrower’s profile to evaluate and accept the borrower and create a loan contract on smart contract.
  8. [Borrower] sign the loan contract
  9. [Lender] sign the loan contract
  10. [Borrower] withdraw token
  11. [Borrower] payback

and you can also test how to store private data on blockchain.

  1. Encrypt profile data with public key and store it on IPFS and store the hash on Ethereum network
  2. Download the encrypted data and decrypt it with private key.

Architecture

For client, I chose 3 key components.

  1. UI framework: React
    I chose React as UI framework just because I liked it.
  2. Authentication: uPort
    uPort is one of identity service on blockchain. It’s kind of like Facebook login for blockchain.
  3. Ethereum node: key only light client
    You can develop key only light client with eth-lightwallet easily. eth-lightwallet is developed and maintained by ConsenSys.

You can use other Javascript frameworks or other authentication system, but light client is important. In blockchain world, everyone has to keep everyone’s data traditionally. But no one but blockchain enthusiasts want to keep huge database on their devices. With light client user keep only keys on their device. Sweet!

uPort integration

uPort integration is easy.
Create an uport object.

import { Connect, SimpleSigner, MNID } from ‘uport-connect’// connect
const uport = new Connect(‘<app name>’, {
clientId: uport_clientId,
signer: SimpleSigner(uport_SimpleSigner)
})

Then execute requestCredentials method.

uport.requestCredentials({
requested: ['name', 'avatar','phone', 'country'],
notifications: true
})
.then((userProfile) => {
// Do something
return Promise.resolve(userProfile)
})

That’s it.

Since uPort is on the Rinkeby test net and we use our own application, we user uPort just to get unique ID and profile data.

You can check more detail at uPort docs.

Key only light client

Please import eth-lightclient version 3.0.0 or higher because version 2.5.6 had a problem and we needed to do a little hack.

Anyway once you import modules, you can create a key store (wallet) easily.

import lightwallet from 'eth-lightwallet'const params = {
password: <Your Password>,
seedPhrase: <Seed Text>,
hdPathString: "m/0'/0'/0'",
}
lightwallet.keystore.createVault(params, (err, ks) => {
if (err){
console.log(err)
}
console.log(ks) // THIS IS KEY STORE
})

You can generate seed text with eth-lightwallet. Seed text is required when you need to generate exact same addresses again.

Once you create a ks …

ks.keyFromPassword( password, (err, pwDerivedKey) => {if (err){
console.log(err)
}
// check pwDerivedKey
let flg = ks.isDerivedKeyCorrect(pwDerivedKey)
if (flg !== true) {
console.log("Incorrect derived key!")
}
// generate new address
ks.generateNewAddress(pwDerivedKey);

})

eth-lightwallet is a great tool but has naming issue. keystore is an object which has methods to generate key store or retrieve private keys from key store. And ks is your key store. I think the name should be like keytools instead of keystore…

You can see address by the method.

ks.getAddresses()

Next stop is set this key store into web3 module.

import HookedWeb3Provider from 'hooked-web3-provider'var web3Provider = new HookedWeb3Provider({
host: RPC_SERVER,
transaction_signer: ks
});
const web3 = new Web3(web3Provider);

Now all transaction you will make is associated to the key store.

Finally don’t forget save the key store on local storage. You need to recover key store from seed text otherwise.

const serialized_keystore = ks.serialize()
localStorage.setItem(<storage id>, serialized_keystore )

You can retrieve key store from local storage

const serialized_keystore = localStorage.getItem(<storage id>)
ks = lightwallet.keystore.deserialize(serialized_keystore)

That’s it.

Now we can sign in to our application with uPort.
And create a new wallet with eth-lightwallet.

I will show how to setup server side on my next blog.

If you are interested in our project, please visit our website. and source code is here on github.

Thanks,

Originally published at Hydra Blog.

--

--

Ko
CreditHydra

I'm a serial entrepreneur. I enjoy AI, UI, and blockchain. I like history and reading too.