-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add sign_send_iota example #224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Thoralf-M
wants to merge
6
commits into
sdk-bindings
Choose a base branch
from
feat/sign-send-iota-example
base: sdk-bindings
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4cb0d47
feat: add sign_send_iota example
Thoralf-M 1e21f67
Merge branch 'sdk-bindings' into feat/sign-send-iota-example
DaughterOfMars e74148b
revert duplicate
Thoralf-M 90f143c
update to latest changes
Thoralf-M e6f2050
Merge branch 'sdk-bindings' into feat/sign-send-iota-example
Thoralf-M a93ec53
Merge branch 'sdk-bindings' into feat/sign-send-iota-example
DaughterOfMars File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (c) 2025 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
sdk "bindings/iota_sdk_ffi" | ||
) | ||
|
||
func main() { | ||
// Amount to send in nanos | ||
amount := uint64(1000) | ||
recipientAddress, err := sdk.AddressFromHex("0x0000a4984bd495d4346fa208ddff4f5d5e5ad48c21dec631ddebc99809f16900") | ||
if err != nil { | ||
log.Fatalf("Failed to parse recipient address: %v", err) | ||
} | ||
|
||
privateKey, err := sdk.NewEd25519PrivateKey(make([]byte, 32)) | ||
if err != nil { | ||
log.Fatalf("Failed to create private key: %v", err) | ||
} | ||
publicKey := privateKey.PublicKey() | ||
senderAddress := publicKey.DeriveAddress() | ||
log.Printf("Sender address: %s", senderAddress.ToHex()) | ||
|
||
// Request funds from faucet | ||
faucet := sdk.FaucetClientNewLocalnet() | ||
_, err = faucet.RequestAndWait(senderAddress) | ||
if err.(*sdk.SdkFfiError) != nil { | ||
log.Fatalf("Failed to request faucet: %v", err) | ||
} | ||
|
||
client := sdk.GraphQlClientNewLocalnet() | ||
|
||
builder := sdk.TransactionBuilderInit(senderAddress, client) | ||
builder.SendIota(recipientAddress, &amount) | ||
txn, err := builder.Finish() | ||
if err.(*sdk.SdkFfiError) != nil { | ||
log.Fatalf("Failed to create transaction: %v", err) | ||
} | ||
|
||
dryRunResult, err := client.DryRunTx(txn, nil) | ||
if err.(*sdk.SdkFfiError) != nil { | ||
log.Fatalf("Failed to dry run: %v", err) | ||
} | ||
if dryRunResult.Error != nil { | ||
log.Fatalf("Dry run failed: %v", *dryRunResult.Error) | ||
} | ||
|
||
signature, err := privateKey.TrySignSimple(txn.SigningDigest()) | ||
if err != nil { | ||
log.Fatalf("Failed to sign: %v", err) | ||
} | ||
userSignature := sdk.UserSignatureNewSimple(signature) | ||
|
||
effects, err := client.ExecuteTx([]*sdk.UserSignature{userSignature}, txn) | ||
if err.(*sdk.SdkFfiError) != nil { | ||
log.Fatalf("Failed to execute: %v", err) | ||
} | ||
if effects == nil { | ||
log.Fatalf("Transaction execution failed") | ||
} | ||
log.Printf("Digest: %s", sdk.HexEncode((*effects).Digest().ToBytes())) | ||
log.Printf("Transaction status: %v", (*effects).AsV1().Status) | ||
log.Printf("Effects: %+v", (*effects).AsV1()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) 2025 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import iota_sdk.* | ||
import kotlinx.coroutines.runBlocking | ||
|
||
fun main() = runBlocking { | ||
try { | ||
// Amount to send in nanos | ||
val amount = 1000uL | ||
val recipientAddress = | ||
Address.fromHex( | ||
"0x0000a4984bd495d4346fa208ddff4f5d5e5ad48c21dec631ddebc99809f16900" | ||
) | ||
|
||
val privateKey = Ed25519PrivateKey(ByteArray(32)) | ||
val publicKey = privateKey.publicKey() | ||
val senderAddress = publicKey.deriveAddress() | ||
println("Sender address: ${senderAddress.toHex()}") | ||
|
||
// Request funds from faucet | ||
val faucet = FaucetClient.newLocalnet() | ||
faucet.requestAndWait(senderAddress) | ||
|
||
val client = GraphQlClient.newLocalnet() | ||
|
||
val builder = TransactionBuilder.init(senderAddress, client) | ||
builder.sendIota(recipientAddress, amount) | ||
val txn = builder.finish() | ||
|
||
val dryRunResult = client.dryRunTx(txn, false) | ||
if (dryRunResult.error != null) { | ||
throw Exception("Dry run failed: ${dryRunResult.error}") | ||
} | ||
|
||
val signature = privateKey.trySignSimple(txn.signingDigest()) | ||
val userSignature = UserSignature.newSimple(signature) | ||
|
||
val effects = client.executeTx(listOf(userSignature), txn) | ||
if (effects == null) { | ||
throw Exception("Transaction execution failed") | ||
} | ||
println("Digest: ${hexEncode(effects.digest().toBytes())}") | ||
println("Transaction status: ${effects.asV1().status}") | ||
println("Effects: ${effects.asV1()}") | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Copyright (c) 2025 IOTA Stiftung | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from lib.iota_sdk_ffi import * | ||
|
||
import asyncio | ||
|
||
|
||
async def main(): | ||
try: | ||
# Amount to send in nanos | ||
amount = 1000 | ||
recipient_address = Address.from_hex( | ||
"0x0000a4984bd495d4346fa208ddff4f5d5e5ad48c21dec631ddebc99809f16900" | ||
) | ||
|
||
private_key = Ed25519PrivateKey(b'\x00' * 32) | ||
public_key = private_key.public_key() | ||
sender_address = public_key.derive_address() | ||
print(f"Sender address: {sender_address.to_hex()}") | ||
|
||
# Request funds from faucet | ||
faucet = FaucetClient.new_localnet() | ||
await faucet.request_and_wait(sender_address) | ||
|
||
client = GraphQlClient.new_localnet() | ||
|
||
builder = await TransactionBuilder.init(sender_address, client) | ||
builder.send_iota(recipient_address, [amount]) | ||
txn = await builder.finish() | ||
|
||
dry_run_result = await client.dry_run_tx(txn, False) | ||
if dry_run_result.error is not None: | ||
raise Exception(f"Dry run failed: {dry_run_result.error}") | ||
|
||
signature = private_key.try_sign_simple(txn.signing_digest()) | ||
user_signature = UserSignature.new_simple(signature) | ||
|
||
effects = await client.execute_tx([user_signature], txn) | ||
if effects is None: | ||
raise Exception("Transaction execution failed") | ||
print(f"Digest: {hex_encode(effects.digest().to_bytes())}") | ||
print(f"Transaction status: {effects.as_v1().status}") | ||
print(f"Effects: {effects.as_v1()}") | ||
|
||
except Exception as e: | ||
print(f"Error: {e}") | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) 2025 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use eyre::Result; | ||
use iota_crypto::{IotaSigner, ed25519::Ed25519PrivateKey}; | ||
use iota_graphql_client::{Client, faucet::FaucetClient}; | ||
use iota_transaction_builder::TransactionBuilder; | ||
use iota_types::Address; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
// Amount to send in nanos | ||
let amount = 1_000u64; | ||
let recipient_address = | ||
Address::from_hex("0x0000a4984bd495d4346fa208ddff4f5d5e5ad48c21dec631ddebc99809f16900")?; | ||
|
||
let private_key = Ed25519PrivateKey::new([0; Ed25519PrivateKey::LENGTH]); | ||
let public_key = private_key.public_key(); | ||
let sender_address = public_key.derive_address(); | ||
println!("Sender address: {sender_address}"); | ||
|
||
// Request funds from faucet | ||
FaucetClient::new_localnet() | ||
.request_and_wait(sender_address) | ||
.await?; | ||
|
||
let client = Client::new_localnet(); | ||
|
||
let mut builder = TransactionBuilder::new(sender_address).with_client(client.clone()); | ||
builder.send_iota(recipient_address, amount); | ||
let tx = builder.finish().await?; | ||
|
||
let dry_run_result = client.dry_run_tx(&tx, false).await?; | ||
DaughterOfMars marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if let Some(err) = dry_run_result.error { | ||
eyre::bail!("Dry run failed: {err}"); | ||
} | ||
|
||
let signature = private_key.sign_transaction(&tx)?; | ||
|
||
let effects = client.execute_tx(&[signature], &tx).await?; | ||
if let Some(effects) = effects { | ||
println!("Digest: {}", effects.digest()); | ||
println!("Transaction status: {:?}", effects.status()); | ||
println!("Effects: {effects:#?}"); | ||
} else { | ||
println!("Transaction execution failed"); | ||
} | ||
|
||
Ok(()) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.