Skip to content

Commit

Permalink
Add programHash
Browse files Browse the repository at this point in the history
  • Loading branch information
iheron committed Aug 7, 2024
1 parent 047da62 commit d6c0fde
Show file tree
Hide file tree
Showing 32 changed files with 571 additions and 172 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.6.1

* Add programHash

## 0.6.0

* Fix android receive message
Expand Down
Binary file modified android/libs/nkn-sources.jar
Binary file not shown.
Binary file modified android/libs/nkn.aar
Binary file not shown.
114 changes: 95 additions & 19 deletions android/src/main/kotlin/org/nkn/sdk/impl/Wallet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import nkngolib.Nkngolib
import nkngomobile.StringArray
import org.bouncycastle.util.encoders.Hex
import org.nkn.sdk.IChannelHandler
import kotlin.math.log

class Wallet : IChannelHandler, MethodChannel.MethodCallHandler, EventChannel.StreamHandler, ViewModel() {
class Wallet : IChannelHandler, MethodChannel.MethodCallHandler, EventChannel.StreamHandler,
ViewModel() {
companion object {
val CHANNEL_NAME = "org.nkn.sdk/wallet"
}
Expand Down Expand Up @@ -44,42 +46,63 @@ class Wallet : IChannelHandler, MethodChannel.MethodCallHandler, EventChannel.St
"measureSeedRPCServer" -> {
measureSeedRPCServer(call, result)
}

"create" -> {
create(call, result)
}

"restore" -> {
restore(call, result)
}

"pubKeyToWalletAddr" -> {
pubKeyToWalletAddr(call, result)
}

"programHashToAddr" -> {
programHashToAddr(call, result)
}

"pubKeyToProgramHash" -> {
pubKeyToProgramHash(call, result)
}

"getBalance" -> {
getBalance(call, result)
}

"transfer" -> {
transfer(call, result)
}

"subscribe" -> {
subscribe(call, result)
}

"unsubscribe" -> {
unsubscribe(call, result)
}

"getSubscribersCount" -> {
getSubscribersCount(call, result)
}

"getSubscribers" -> {
getSubscribers(call, result)
}

"getSubscription" -> {
getSubscription(call, result)
}

"getHeight" -> {
getHeight(call, result)
}

"getNonce" -> {
getNonce(call, result)
}

else -> {
result.notImplemented()
}
Expand Down Expand Up @@ -108,7 +131,7 @@ class Wallet : IChannelHandler, MethodChannel.MethodCallHandler, EventChannel.St
}

val resp = hashMapOf(
"seedRPCServerAddrList" to seedRPCServerAddrs
"seedRPCServerAddrList" to seedRPCServerAddrs
)
resultSuccess(result, resp)
return@launch
Expand Down Expand Up @@ -137,12 +160,14 @@ class Wallet : IChannelHandler, MethodChannel.MethodCallHandler, EventChannel.St
try {
val account = Nkn.newAccount(seed)
val wallet = Nkn.newWallet(account, config)
val pubKey = wallet.pubKey()

val resp = hashMapOf(
"address" to wallet.address(),
"keystore" to wallet.toJSON(),
"publicKey" to wallet.pubKey(),
"seed" to wallet.seed()
"address" to wallet.address(),
"keystore" to wallet.toJSON(),
"publicKey" to pubKey,
"seed" to wallet.seed(),
"programHash" to Nkngolib.pubKeyToProgramHash(pubKey)
)
resultSuccess(result, resp)
return@launch
Expand Down Expand Up @@ -175,12 +200,13 @@ class Wallet : IChannelHandler, MethodChannel.MethodCallHandler, EventChannel.St
viewModelScope.launch(Dispatchers.IO) {
try {
val wallet = Nkn.walletFromJSON(keystore, config)

val pubKey = wallet.pubKey()
val resp = hashMapOf(
"address" to wallet.address(),
"keystore" to wallet?.toJSON(),
"publicKey" to wallet.pubKey(),
"seed" to wallet.seed()
"address" to wallet.address(),
"keystore" to wallet?.toJSON(),
"publicKey" to pubKey,
"seed" to wallet.seed(),
"programHash" to Nkngolib.pubKeyToProgramHash(pubKey)
)
resultSuccess(result, resp)
return@launch
Expand All @@ -192,12 +218,47 @@ class Wallet : IChannelHandler, MethodChannel.MethodCallHandler, EventChannel.St
}

private fun pubKeyToWalletAddr(call: MethodCall, result: MethodChannel.Result) {
val pubkey = call.argument<String>("publicKey")
val pubkey = call.argument<ByteArray>("pubKey")

viewModelScope.launch(Dispatchers.IO) {
val addr = Nkn.pubKeyToWalletAddr(Hex.decode(pubkey))
resultSuccess(result, addr)
return@launch
try {
val addr = Nkn.pubKeyToWalletAddr(pubkey)
resultSuccess(result, addr)
return@launch
} catch (e: Throwable) {
resultError(result, e)
return@launch
}
}
}

private fun programHashToAddr(call: MethodCall, result: MethodChannel.Result) {
val programHash = call.argument<ByteArray>("programHash")

viewModelScope.launch(Dispatchers.IO) {
try {
val addr = Nkngolib.programHashToAddr(programHash)
resultSuccess(result, addr)
return@launch
} catch (e: Throwable) {
resultError(result, e)
return@launch
}
}
}

private fun pubKeyToProgramHash(call: MethodCall, result: MethodChannel.Result) {
val pubkey = call.argument<ByteArray>("pubKey")

viewModelScope.launch(Dispatchers.IO) {
try {
val programHash = Nkngolib.pubKeyToProgramHash(pubkey)
resultSuccess(result, programHash)
return@launch
} catch (e: Throwable) {
resultError(result, e)
return@launch
}
}
}

Expand Down Expand Up @@ -297,7 +358,14 @@ class Wallet : IChannelHandler, MethodChannel.MethodCallHandler, EventChannel.St
}
val account = Nkn.newAccount(seed)
val wallet = Nkn.newWallet(account, config)
val hash = wallet.subscribe(identifier, topic, duration.toLong(), meta, transactionConfig)
val hash =
wallet.subscribe(
identifier,
topic,
duration.toLong(),
meta,
transactionConfig
)

resultSuccess(result, hash)
return@launch
Expand Down Expand Up @@ -364,7 +432,15 @@ class Wallet : IChannelHandler, MethodChannel.MethodCallHandler, EventChannel.St

viewModelScope.launch(Dispatchers.IO) {
try {
val subscribers = Nkn.getSubscribers(topic, offset.toLong(), limit.toLong(), meta, txPool, subscriberHashPrefix, config)
val subscribers = Nkn.getSubscribers(
topic,
offset.toLong(),
limit.toLong(),
meta,
txPool,
subscriberHashPrefix,
config
)
val resp = hashMapOf<String, String>()
subscribers.subscribers.range { addr, value ->
resp[addr] = value?.trim() ?: ""
Expand Down Expand Up @@ -397,8 +473,8 @@ class Wallet : IChannelHandler, MethodChannel.MethodCallHandler, EventChannel.St
try {
val subscription = Nkn.getSubscription(topic, subscriber, config)
val resp = hashMapOf(
"meta" to subscription.meta,
"expiresAt" to subscription.expiresAt
"meta" to subscription.meta,
"expiresAt" to subscription.expiresAt
)

resultSuccess(result, resp)
Expand Down
2 changes: 1 addition & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 4e8f8b2be68aeea4c0d5beb6ff1e79fface1d048

COCOAPODS: 1.14.3
COCOAPODS: 1.15.2
2 changes: 1 addition & 1 deletion example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
97C146E61CF9000F007C117D /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1430;
LastUpgradeCheck = 1510;
ORGANIZATIONNAME = "";
TargetAttributes = {
97C146ED1CF9000F007C117D = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1430"
LastUpgradeVersion = "1510"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 2 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class _MyAppState extends State<MyApp> {
print(wallet.seed);
print(wallet.publicKey);
print(wallet.keystore);
print(wallet.programHash);
},
child: Text('create'),
),
Expand All @@ -76,6 +77,7 @@ class _MyAppState extends State<MyApp> {
print(wallet.seed);
print(wallet.publicKey);
print(wallet.keystore);
print(wallet.programHash);
},
child: Text('restore'),
),
Expand Down
2 changes: 1 addition & 1 deletion golib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ IOS_ARTIFACT=$(IOS_BUILDDIR)/Nkn.xcframework

BUILD_PACKAGE=./ ./crypto github.com/nknorg/nkn-sdk-go github.com/nknorg/ncp-go github.com/nknorg/nkn/v2/transaction github.com/nknorg/nkngomobile github.com/nknorg/eth-resolver-go github.com/nknorg/dns-resolver-go
ANDROID_BUILD_CMD="$(GOBIND) -ldflags $(ANDROID_LDFLAGS) -target=android -androidapi=21 -o $(ANDROID_ARTIFACT) $(BUILD_PACKAGE)"
IOS_BUILD_CMD="$(GOBIND) -ldflags $(LDFLAGS) -target=ios -o $(IOS_ARTIFACT) $(BUILD_PACKAGE)"
IOS_BUILD_CMD="$(GOBIND) -ldflags $(LDFLAGS) -target=ios -tags=netgo -o $(IOS_ARTIFACT) $(BUILD_PACKAGE)"


define build
Expand Down
58 changes: 41 additions & 17 deletions golib/go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
module nkngolib

go 1.21
go 1.22.0

toolchain go1.22.2

require (
github.com/nknorg/dns-resolver-go v0.0.0-20220705102626-b041cd8d4a8e
github.com/nknorg/dns-resolver-go v0.0.0-20230404043755-e50d32d9043a
github.com/nknorg/eth-resolver-go v0.0.0-20230404061427-d0bd773f899f
github.com/nknorg/nkn-sdk-go v1.4.7
github.com/nknorg/nkn/v2 v2.2.0
github.com/nknorg/nkn-sdk-go v1.4.9-0.20240725072249-776f5f4fa62f
github.com/nknorg/nkn/v2 v2.2.2-0.20240715231955-c2862f2d9d16
github.com/nknorg/nkngomobile v0.0.0-20220615081414-671ad1afdfa9
golang.org/x/mobile v0.0.0-20240112133503-c713f31d574b
golang.org/x/mobile v0.0.0-20240716161057-1ad2df20a8b6
)

require (
Expand All @@ -17,14 +19,14 @@ require (
github.com/ethereum/go-ethereum v1.10.15 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/ipfs/go-cid v0.4.0 // indirect
github.com/itchyny/base58-go v0.2.1 // indirect
github.com/itchyny/base58-go v0.2.2 // indirect
github.com/jbenet/go-is-domain v1.0.5 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/minio/sha256-simd v1.0.0 // indirect
Expand All @@ -34,9 +36,28 @@ require (
github.com/multiformats/go-multibase v0.2.0 // indirect
github.com/multiformats/go-multihash v0.2.1 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/nknorg/ncp-go v1.0.5 // indirect
github.com/nknorg/ncp-go v1.0.6 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/pborman/uuid v1.2.1 // indirect
github.com/pion/datachannel v1.5.8 // indirect
github.com/pion/dtls/v2 v2.2.12 // indirect
github.com/pion/dtls/v3 v3.0.0 // indirect
github.com/pion/ice/v3 v3.0.13 // indirect
github.com/pion/interceptor v0.1.29 // indirect
github.com/pion/logging v0.2.2 // indirect
github.com/pion/mdns/v2 v2.0.7 // indirect
github.com/pion/randutil v0.1.0 // indirect
github.com/pion/rtcp v1.2.14 // indirect
github.com/pion/rtp v1.8.7 // indirect
github.com/pion/sctp v1.8.19 // indirect
github.com/pion/sdp/v3 v3.0.9 // indirect
github.com/pion/srtp/v3 v3.0.3 // indirect
github.com/pion/stun/v2 v2.0.0 // indirect
github.com/pion/transport/v2 v2.2.9 // indirect
github.com/pion/transport/v3 v3.0.6 // indirect
github.com/pion/turn/v3 v3.0.3 // indirect
github.com/pion/webrtc/v4 v4.0.0-beta.26 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rjeczalik/notify v0.9.3 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
Expand All @@ -45,14 +66,17 @@ require (
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/wealdtech/go-ens/v3 v3.5.4 // indirect
github.com/wealdtech/go-multicodec v1.4.0 // indirect
github.com/wlynxg/anet v0.0.3 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.17.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.23.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
lukechampine.com/blake3 v1.1.7 // indirect
)
Loading

0 comments on commit d6c0fde

Please sign in to comment.