Giter Site home page Giter Site logo

kmnid's Introduction

KMNID

A kotlin implementation of MNID

CircleCI codecov

Multi Network Identifier (MNID)

Ethereum, and uPort, is entering a multi-chain world. As end users increasingly interact with multiple chains, on Ethereum or elsewhere, the risk of users/servers inadvertently transferring value from an address on network X to an address on network Y is growing. This could result in monetary loss. Since uPort is switching to a new test network, we need to solve this issue urgently.

The Bitcoin protocol uses Base58Check encoding to prevent users from sending value off-network, but the ethereum ecosystem has used a raw hex version of the address instead.

Encoding scheme

The original proposal is inspired by the Base58Check encoding as well as EIP77 but also specifies a network identifier, which allows us to programmatically extract the network used by an address as well as provide a visual indicator of the network used.

The following items are encoded:

  • 1 byte version number currently 1
  • network id or four bytes of genesis block hash (or both)
  • actual address data
  • Four bytes (32 bits) of SHA3-based error checking code (digest of the version, network and payload)

Then base58 encoding is applied to the end result. The end result is fairly complete but still extendible in the future. We could start by simply using the network id and replace it with the genesis block hash and other meta data in the future.

Examples

The following Ethereum hex encoded address 0x00521965e7bd230323c423d96c657db5b79d099f could be encoded as follows:

  • main-net: 2nQtiQG6Cgm1GYTBaaKAgr76uY7iSexUkqX
  • ropsten: 2oDZvNUgn77w2BKTkd9qKpMeUo8EL94QL5V
  • kovan: 34ukSmiK1oA1C5Du8aWpkjFGALoH7nsHeDX
  • infuranet: 9Xy8yQpdeCNSPGQ9jwTha9MRSb2QJ8HYzf1u

Usage

Import:

repositories {
    //...
    maven { url 'https://jitpack.io' }
}

dependencies {
    //...
    compile "com.github.uport-project:kmnid:0.4.4"
}

Encode

val mnid = MNID.encode(
  network = '0x1', // the hex encoded network id or for private chains the hex encoded first 4 bytes of the genesis hash
  address = '0x00521965e7bd230323c423d96c657db5b79d099f'
)


assertEquals('2nQtiQG6Cgm1GYTBaaKAgr76uY7iSexUkqX', mnid)

Decode

val account = MNID.decode('2nQtiQG6Cgm1GYTBaaKAgr76uY7iSexUkqX')
assertEquals('0x1', account.network) 
assertEquals('0x00521965e7bd230323c423d96c657db5b79d099f', account.address)

Check

// Check if string is a valid MNID

assertTrue( MNID.isMNID('2nQtiQG6Cgm1GYTBaaKAgr76uY7iSexUkqX') )


//bad encoding (ethereum address)
assertFalse( MNID.isMNID('0x00521965e7bd230323c423d96c657db5b79d099f') )


//bad encoding (bitcoin address)
assertFalse( MNID.isMNID('1GbVUSW5WJmRCpaCJ4hanUny77oDaWW4to') )


//bad encoding (ipfs hash)
assertFalse( MNID.isMNID('QmXuNqXmrkxs4WhTDC2GCnXEep4LUD87bu97LQMn1rkxmQ') )

Changelog

  • 0.4.4
    • fix pom file by using implementation declaration ( #6 ) *0.4.3
    • bump dependencies (kotlin 1.3.70, kethereum 0.81.4) ( 19f867ed )
  • 0.4.2
    • bump kethereum to 0.76.2 ( c2c5b070 )
  • 0.4.1
    • simplify import of komputing libraries ( 17534bdf )
  • 0.4.0
    • [breaking] remove public Account constructor to promote error checks ( #4 )
    • remove all direct java dependencies, prep for multi-platform support ( #5 )
    • increase coverage ( 55f2311 )
  • 0.3.3 - maintenance
    • use kethereum 0.76.1, remove walleth namespace ( b0fe925 )
    • use kotlin 1.3.50 ( b0fe925 )
    • add coverage ( b0fe925 )
  • 0.3.2 - maintenance
    • build on circleCI
  • 0.3.1 - maintenance release
    • updated build scripts
  • 0.3
    • removed spongycastle dependency for hashing, using KHash instead
    • targeting java 1.8
    • updated KEthereum base58 dependency to 0.75.0
  • 0.2.1
    • rebuild
  • 0.2
    • reduce library footprint by only using the base58 lib from kethereum
  • 0.1
    • initial release

kmnid's People

Contributors

mirceanis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.