Yuav ua li cas ib tug decentralized tub txib ntawm lub blockchain ua hauj lwm?

Thaum pib ntawm 2017, peb pib tsim ib tug neeg xa xov liaison ntawm blockchain [lub npe thiab txuas nyob rau hauv profile] los ntawm kev sib tham txog qhov zoo dua classic P2P tub txib.

Dhau 2.5 xyoo, thiab peb tuaj yeem lees paub peb lub tswv yim: cov ntawv thov xa xov tam sim no muaj rau iOS, Web PWA, Windows, GNU / Linux, Mac OS thiab Android.

Niaj hnub no peb yuav qhia koj seb tus neeg xa xov liaison blockchain ua haujlwm li cas thiab cov neeg siv khoom tuaj yeem ua haujlwm nrog nws API li cas.
Yuav ua li cas ib tug decentralized tub txib ntawm lub blockchain ua hauj lwm?

Peb xav kom blockchain los daws qhov teeb meem kev nyab xeeb thiab kev ceev ntiag tug ntawm classic P2P tub txib:

  • Ib nias los tsim ib tus account - tsis muaj xov tooj lossis emails, tsis muaj kev nkag mus rau cov phau ntawv chaw nyob lossis chaw nyob.
  • Cov interlocutors yeej tsis tsim kev sib txuas ncaj qha; txhua qhov kev sib txuas lus yuav tshwm sim los ntawm kev faib tawm ntawm cov nodes. Cov neeg siv 'IP chaw nyob tsis siv tau rau ib leeg.
  • Tag nrho cov lus yog encrypted End-to-End curve25519xsalsa20poly1305. Nws zoo nkaus li tias qhov no yuav tsis xav tsis thoob rau leej twg, tab sis peb qhov chaws qhib.
  • MITM nres tsis suav nrog - txhua cov lus yog kev sib pauv thiab kos npe los ntawm Ed25519 EdDSA.
  • Cov lus xaus rau hauv nws tus kheej thaiv. Kev sib haum xeeb thiab timestamp Koj tsis tuaj yeem kho cov blocks, thiab yog li qhov kev txiav txim ntawm cov lus.
  • "Kuv tsis tau hais tias" yuav tsis ua haujlwm nrog cov lus ntawm blockchain.
  • Tsis muaj cov qauv hauv nruab nrab uas kuaj xyuas "qhov tseeb" ntawm cov lus. Qhov no yog ua los ntawm kev faib cov nodes raws li kev pom zoo, thiab nws yog tus tswv los ntawm cov neeg siv.
  • Impossibility ntawm censorship - account tsis tuaj yeem thaiv thiab cov lus tsis tuaj yeem raug tshem tawm.
  • Blockchain 2FA yog lwm txoj hauv kev rau lub ntuj raug txim 2FA ntawm SMS, ruined ntau ntawm kev noj qab haus huv.
  • Lub peev xwm kom tau txais tag nrho koj cov kev sib tham los ntawm ib lub cuab yeej txhua lub sijhawm txhais tau tias koj tsis tas yuav khaws cov kev sib tham hauv zos txhua lub sijhawm.
  • Kev lees paub ntawm kev xa xov. Tsis yog rau tus neeg siv lub cuab yeej, tab sis rau lub network. Qhov tseem ceeb, qhov no yog kev lees paub ntawm tus neeg tau txais kev muaj peev xwm nyeem koj cov lus. Qhov no yog qhov muaj txiaj ntsig zoo rau kev xa cov ntawv ceeb toom tseem ceeb.

Blockchain cov txiaj ntsig tseem suav nrog kev sib raug zoo nrog cov cryptocurrencies Ethereum, Dogecoin, Lisk, Dash, Bitcoin (qhov no tseem tab tom ua tiav) thiab muaj peev xwm xa cov tokens hauv kev sib tham. Peb txawm ua ib tug built-in crypto exchanger.

Thiab ces - yuav ua li cas nws tag nrho ua hauj lwm.

Ib qho lus yog kev lag luam

Txhua leej txhua tus twb tau siv rau qhov tseeb tias kev lag luam hauv blockchain hloov pauv tokens (npib) los ntawm ib tus neeg siv mus rau lwm tus. Zoo li Bitcoin. Peb tsim ib hom kev lag luam tshwj xeeb rau kev xa xov.

Txhawm rau xa cov lus hauv tus tub txib ntawm blockchain, koj yuav tsum mus dhau ob peb kauj ruam:

  1. Encrypt cov ntawv nyeem
  2. Muab ciphertext rau hauv kev sib pauv
  3. Kos npe rau kev ua lag luam
  4. Xa ib qho kev sib pauv mus rau txhua lub network node
  5. Ib qho kev faib tawm ntawm cov nodes txiav txim siab "qhov tseeb" ntawm cov lus
  6. Yog tias txhua yam yog OK, kev hloov pauv nrog cov lus tau suav nrog hauv qhov thaiv tom ntej
  7. Tus neeg tau txais cov lus sib pauv thiab decrypts

Kauj Ruam 1–3 thiab 7 tau ua hauv zos ntawm tus neeg siv khoom, thiab cov kauj ruam 5–6 yog ua rau cov tswv.

Cov lus encryption

Cov lus yog encrypted nrog tus xa tus yuam sij ntiag tug thiab tus neeg tau txais tus yuam sij pej xeem. Peb yuav muab tus yuam sij pej xeem los ntawm lub network, tab sis rau qhov no, tus neeg txais nyiaj yuav tsum tau pib, uas yog, muaj tsawg kawg yog ib qho kev lag luam. Koj tuaj yeem siv REST thov GET /api/accounts/getPublicKey?address={ADAMANT address}, thiab thaum thauj cov chats, cov yuam sij pej xeem ntawm cov interlocutors yuav twb muaj.

Yuav ua li cas ib tug decentralized tub txib ntawm lub blockchain ua hauj lwm?

Tus tub txib encrypts cov lus siv lub nkhaus25519xsalsa20poly1305 algorithm (NaCl Box). Txij li thaum tus account muaj Ed25519 cov yawm sij, los tsim ib lub thawv, cov yuam sij yuav tsum xub hloov mus rau Curve25519 Diffie-Hellman.

Nov yog ib qho piv txwv hauv JavaScript:

/**
 * Encodes a text message for sending to ADM
 * @param {string} msg message to encode
 * @param {*} recipientPublicKey recipient's public key
 * @param {*} privateKey our private key
 * @returns {{message: string, nonce: string}}
 */
adamant.encodeMessage = function (msg, recipientPublicKey, privateKey) {
  const nonce = Buffer.allocUnsafe(24)
  sodium.randombytes(nonce)

  if (typeof recipientPublicKey === 'string') {
    recipientPublicKey = hexToBytes(recipientPublicKey)
  }

  const plainText = Buffer.from(msg)
  const DHPublicKey = ed2curve.convertPublicKey(recipientPublicKey)
  const DHSecretKey = ed2curve.convertSecretKey(privateKey)

  const encrypted = nacl.box(plainText, nonce, DHPublicKey, DHSecretKey)

  return {
    message: bytesToHex(encrypted),
    nonce: bytesToHex(nonce)
  }
}

Tsim kev lag luam nrog cov lus

Kev lag luam muaj cov qauv hauv qab no:

{
  "id": "15161295239237781653",
  "height": 7585271,
  "blockId": "16391508373936326027",
  "type": 8,
  "block_timestamp": 45182260,
  "timestamp": 45182254,
  "senderPublicKey": "bd39cc708499ae91b937083463fce5e0668c2b37e78df28f69d132fce51d49ed",
  "senderId": "U16023712506749300952",
  "recipientId": "U17653312780572073341",
  "recipientPublicKey": "23d27f616e304ef2046a60b762683b8dabebe0d8fc26e5ecdb1d5f3d291dbe21",
  "amount": 204921300000000,
  "fee": 50000000,
  "signature": "3c8e551f60fedb81e52835c69e8b158eb1b8b3c89a04d3df5adc0d99017ffbcb06a7b16ad76d519f80df019c930960317a67e8d18ab1e85e575c9470000cf607",
  "signatures": [],
  "confirmations": 3660548,
  "asset": {}
}

Rau kev sib pauv lus, qhov tseem ceeb tshaj plaws yog asset - koj yuav tsum tso cov lus hauv qhov khoom chat nrog cov qauv:

  • message - txuag cov lus encrypted
  • own_message - noj
  • type β€” hom lus

Cov lus kuj tau muab faib ua hom. Qhov tseem ceeb, qhov parameter type qhia koj kom nkag siab message. Koj tuaj yeem xa cov ntawv xwb, lossis koj tuaj yeem xa cov khoom nrog cov khoom nthuav hauv - piv txwv li, qhov no yog li cas tus tub txib ua rau kev hloov pauv cryptocurrency hauv kev sib tham.

Raws li qhov tshwm sim, peb tsim kev lag luam:

{
  "transaction": {
    "type": 8,
    "amount": 0,
    "senderId": "U12499126640447739963",
    "senderPublicKey": "e9cafb1e7b403c4cf247c94f73ee4cada367fcc130cb3888219a0ba0633230b6",
    "asset": {
      "chat": {
        "message": "cb682accceef92d7cddaaddb787d1184ab5428",
        "own_message": "e7d8f90ddf7d70efe359c3e4ecfb5ed3802297b248eacbd6",
        "type": 1
      }
    },
    "recipientId": "U15677078342684640219",
    "timestamp": 63228087,
    "signature": "Ρ‚ΡƒΡ‚ Π±ΡƒΠ΄Π΅Ρ‚ подпись"
  }
}

Kev ua lag luam kos npe

Txhawm rau kom ntseeg tau tias txhua tus neeg muaj kev ntseeg siab rau qhov tseeb ntawm tus neeg xa khoom thiab tus neeg tau txais txiaj ntsig, lub sijhawm xa thiab cov ntsiab lus ntawm cov lus, kev hloov pauv tau kos npe. Kev kos npe digital tso cai rau koj los txheeb xyuas qhov tseeb ntawm kev lag luam siv tus yuam sij pej xeem - tus yuam sij ntiag tug tsis xav tau rau qhov no.

Tab sis kos npe nws tus kheej yog ua los ntawm tus kheej tus yuam sij:

Yuav ua li cas ib tug decentralized tub txib ntawm lub blockchain ua hauj lwm?

Daim duab qhia tau hais tias peb thawj zaug hash kev pauv nrog SHA-256 thiab tom qab ntawd kos npe rau nws 25519 EDDSA thiab tau kos npe signature, thiab tus lej ID yog ib feem ntawm SHA-256 hash.

Kev ua piv txwv:

1 - Tsim cov ntaub ntawv thaiv, suav nrog cov lus

/**
 * Calls `getBytes` based on transaction type
 * @see privateTypes
 * @implements {ByteBuffer}
 * @param {transaction} trs
 * @param {boolean} skipSignature
 * @param {boolean} skipSecondSignature
 * @return {!Array} Contents as an ArrayBuffer.
 * @throws {error} If buffer fails.
 */

adamant.getBytes = function (transaction) {

  ...

  switch (transaction.type) {
    case constants.Transactions.SEND:
      break
    case constants.Transactions.CHAT_MESSAGE:
      assetBytes = this.chatGetBytes(transaction)
      assetSize = assetBytes.length
      break

…

    default:
      alert('Not supported yet')
  }

  var bb = new ByteBuffer(1 + 4 + 32 + 8 + 8 + 64 + 64 + assetSize, true)

  bb.writeByte(transaction.type)
  bb.writeInt(transaction.timestamp)

  ...

  bb.flip()
  var arrayBuffer = new Uint8Array(bb.toArrayBuffer())
  var buffer = []

  for (var i = 0; i < arrayBuffer.length; i++) {
    buffer[i] = arrayBuffer[i]
  }

  return Buffer.from(buffer)
}

2 - suav SHA-256 los ntawm cov ntaub ntawv thaiv

/**
 * Creates hash based on transaction bytes.
 * @implements {getBytes}
 * @implements {crypto.createHash}
 * @param {transaction} trs
 * @return {hash} sha256 crypto hash
 */
adamant.getHash = function (trs) {
  return crypto.createHash('sha256').update(this.getBytes(trs)).digest()
}

3 - Kos npe rau kev lag luam

adamant.transactionSign = function (trs, keypair) {
  var hash = this.getHash(trs)
  return this.sign(hash, keypair).toString('hex')
}

/**
 * Creates a signature based on a hash and a keypair.
 * @implements {sodium}
 * @param {hash} hash
 * @param {keypair} keypair
 * @return {signature} signature
 */
adamant.sign = function (hash, keypair) {
  return sodium.crypto_sign_detached(hash, Buffer.from(keypair.privateKey, 'hex'))
}

Xa kev sib pauv nrog cov lus rau lub network node

Txij li thaum lub network yog decentralized, ib qho ntawm cov nodes nrog qhib API yuav ua. Ua daim ntawv thov POST mus rau qhov kawg api/transactions:

curl 'api/transactions' -X POST 
  -d 'TX_DATA'

Hauv kev teb peb yuav tau txais tus lej ID ntawm hom

{
    "success": true,
    "nodeTimestamp": 63228852,
    "transactionId": "6146865104403680934"
}

Transaction Validation

Ib qho kev faib tawm ntawm cov nodes, raws li kev pom zoo, txiav txim siab "qhov tseeb" ntawm cov lus sib pauv. Los ntawm leej twg thiab rau leej twg, thaum twg, txawm hais tias cov lus tau hloov nrog lwm tus, thiab seb lub sijhawm xa khoom tau qhia raug. Qhov no yog qhov txiaj ntsig tseem ceeb ntawm blockchain - tsis muaj cov qauv hauv nruab nrab uas yog lub luag haujlwm rau kev txheeb xyuas, thiab cov kab lus thiab lawv cov ntsiab lus tsis tuaj yeem raug faked.

Ua ntej, ib qho ntawm cov tshuaj xyuas qhov tseeb, thiab tom qab ntawd xa mus rau lwm tus - yog tias feem ntau hais tias txhua yam nyob rau hauv kev txiav txim, kev hloov pauv yuav raug suav nrog hauv qhov txuas ntxiv ntawm cov saw hlau - qhov no yog kev pom zoo.

Yuav ua li cas ib tug decentralized tub txib ntawm lub blockchain ua hauj lwm?

Ib feem ntawm node code uas yog lub luag haujlwm rau kev kuaj xyuas tuaj yeem pom ntawm GitHub - validator.js ΠΈ verify.js. Yog, cov node khiav ntawm Node.js.

Nrog rau kev lag luam nrog cov lus hauv ib qho thaiv

Yog tias muaj kev pom zoo, qhov kev sib pauv nrog peb cov lus yuav muab tso rau hauv qhov thaiv tom ntej nrog rau lwm cov kev lag luam siv tau.

Blocks muaj ib ntus nruj, thiab txhua qhov txuas txuas ntxiv yog tsim los ntawm cov hashes ntawm cov blocks dhau los.

Yuav ua li cas ib tug decentralized tub txib ntawm lub blockchain ua hauj lwm?

Lub ntsiab lus yog tias peb cov lus tseem muaj nyob rau hauv ntu no thiab tsis tuaj yeem "rov kho dua". Yog tias ob peb cov lus poob rau hauv ib qho thaiv, lawv qhov kev txiav txim yuav raug txiav txim los ntawm timestamp lus.

Nyeem cov lus

Daim ntawv thov xa xov retrieves muas los ntawm blockchain uas xa mus rau tus neeg txais. Rau qhov no peb tau ua qhov kawg api/chatrooms.

Txhua qhov kev hloov pauv muaj rau txhua tus - koj tuaj yeem tau txais cov lus encrypted. Tab sis tsuas yog tus neeg tau txais txiaj ntsig tuaj yeem txiav txim siab siv nws tus yuam sij ntiag tug thiab tus xa tus yuam sij rau pej xeem:

**
 * Decodes the incoming message
 * @param {any} msg encoded message
 * @param {string} senderPublicKey sender public key
 * @param {string} privateKey our private key
 * @param {any} nonce nonce
 * @returns {string}
 */
adamant.decodeMessage = function (msg, senderPublicKey, privateKey, nonce) {
  if (typeof msg === 'string') {
    msg = hexToBytes(msg)
  }

  if (typeof nonce === 'string') {
    nonce = hexToBytes(nonce)
  }

  if (typeof senderPublicKey === 'string') {
    senderPublicKey = hexToBytes(senderPublicKey)
  }

  if (typeof privateKey === 'string') {
    privateKey = hexToBytes(privateKey)
  }

  const DHPublicKey = ed2curve.convertPublicKey(senderPublicKey)
  const DHSecretKey = ed2curve.convertSecretKey(privateKey)
  const decrypted = nacl.box.open(msg, nonce, DHPublicKey, DHSecretKey)

  return decrypted ? decode(decrypted) : ''
}

Thiab dab tsi ntxiv?

Txij li thaum cov lus raug xa mus rau hauv txoj kev no li ntawm 5 vib nas this - qhov no yog lub sijhawm tshiab network thaiv tshwm - peb tuaj nrog cov neeg siv khoom-rau-node thiab node-rau-node socket kev sib txuas. Thaum ib lub node tau txais kev hloov pauv tshiab, nws tshawb xyuas nws qhov siv tau thiab xa mus rau lwm cov nodes. Kev hloov pauv yog muaj rau cov neeg xa xov xwm txawm tias ua ntej kev pom zoo tshwm sim thiab suav nrog hauv qhov thaiv. Txoj kev no peb yuav xa cov lus tam sim, ib yam li cov neeg xa xov xwm niaj hnub.

Txhawm rau khaws phau ntawv chaw nyob, peb tau ua KVS - Qhov Tseem Ceeb-Value Storage - qhov no yog lwm hom kev lag luam uas asset nws tsis yog NaCl-box uas yog encrypted, tab sis NaCl-secretbox. Qhov no yog li cas tus tub txib khaws lwm cov ntaub ntawv.

Kev hloov ntaub ntawv / duab thiab kev sib tham hauv pab pawg tseem xav tau ntau yam haujlwm. Tau kawg, nyob rau hauv blunder-and-blunder hom ntawv no tuaj yeem "screwed" sai, tab sis peb xav kom muaj tib theem ntawm kev ceev ntiag tug.

Yog lawm, tseem muaj txoj haujlwm yuav tsum tau ua - qhov zoo tshaj plaws, kev ceev ntiag tug tiag tiag xav tias cov neeg siv yuav tsis txuas rau pej xeem network nodes, tab sis yuav tsa lawv tus kheej. Qhov feem pua ​​​​ntawm cov neeg siv koj xav tias ua li no? Yog lawm, 0. Peb tuaj yeem daws qhov teeb meem no ib nrab nrog Tor version ntawm tus tub txib.

Peb tau ua pov thawj tias tus tub txib ntawm blockchain tuaj yeem muaj nyob. Yav dhau los, tsuas muaj ib qho kev sim hauv xyoo 2012 - bitmessage, uas ua tsis tiav vim lub sijhawm xa xov ntev, CPU thauj khoom, thiab tsis muaj daim ntawv thov mobile.

Thiab kev tsis ntseeg yog vim qhov tseeb tias cov neeg xa xov ntawm blockchain ua ntej lawv lub sijhawm - tib neeg tsis tau npaj txhij los ua lub luag haujlwm rau lawv tus account, muaj tus kheej cov ntaub ntawv tseem tsis tau muaj qhov sib txawv, thiab thev naus laus zis tsis tso cai rau kev kub ceev ntawm blockchain. Ntau thev naus laus zis analogues ntawm peb qhov project yuav tshwm sim tom ntej. Koj yuav pom.

Tau qhov twg los: www.hab.com

Ntxiv ib saib