Giter Club home page Giter Club logo

web3-core's Introduction

Web3 Core

Multiplatform Web3 Library

Usage

Solana

Build a Transaction

// Solana Memo Program 
val account = SolanaPublicKey(keyPair.publicKey)
val memoProgramId = SolanaPublicKey.from("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr")
val memoInstruction = TransactionInstruction(
    memoProgramId,
    listOf(AccountMeta(account, true, true)),
    "hello world ".encodeToByteArray()
)

// Build Message
val blockhash = Blockhash(getRecentBlockhash())
val message = Message.Builder()
    .addInstruction(memoInstruction)
    .setRecentBlockhash(blockhash)
    .build()

Prepare a Signer

using Diglol Crypto library for ED25519 signing:

// prepare signer
val keyPair = Ed25519.generateKeyPair()
val signer = object : Ed25519Signer() {
    override val publicKey: ByteArray get() = keyPair.publicKey
    override suspend fun signPayload(payload: ByteArray): ByteArray = Ed25519.sign(keyPair, payload)
}

Sign Message & Build Transaction

// Sign Message
val signature = signer.signPayload(message.serialize())

// Build Transaction
val transaction = Transaction(listOf(signature), message)

Send Transaction to RPC

Using Base58 encoding from MultiMult and Solana RPC driver from RpcCore

// serialize transaction
val transactionBytes = transaction.serialize()
val encodedTransaction = Base58.encodeToString(transactionBytes)

// setup RPC driver
val rpcUrl = "https://api.endpoint.com"
val rpcDriver = Rpc20Driver(rpcUrl, MyNetworkDriver())

class SendTransactionRequest(encodedTransaction: String, requestId: String)
    : JsonRpc20Request(
        method = "sendTransaction",
        params = buildJsonArray {
            add(encodedTransaction)
        },
        requestId
    )

// build rpc request
val requestId = 1
val rpcRequest = SendTransactionRequest(encodedTransaction, requestId)

// send the request and get response
// using JsonElement.serializer() will return the JSON RPC response. you can use your own serializer to get back a specific object
val rpcResponse = rpcDriver.makeRequest(rpcRequest, JsonElement.serializer())

web3-core's People

Contributors

funkatronics avatar

Forkers

etodanik

web3-core's Issues

jvmToolchain version feels a bit legacy?

Are there good practical reasons for jvmToolchain setting in core and solana packages to be set to 11? It feels like a very old JDK version. Current LTS is v21. I could see v17 being a useful setting for a little while, but 11 feels way legacy.

Is there any practical reason I'm missing?

Signing transaction

Greetings! I've tried to use your library for parsing and signing transaction from Jupiter. I parsed Message object from transaction, but I have no idea how to set signatures in Transaction using mnemonic (the same as sign transaction, as I understood). Could you, please, provide more complex example?

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.