-
Notifications
You must be signed in to change notification settings - Fork 5
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
feat: add stake integration schema #38
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0cd862d
feat: add stake integration schema
parodyBit 30ed844
feat: update staking schema
parodyBit b276a61
fix: stake / unstake schema
parodyBit 80a7ae2
feat: recover public key from signature
parodyBit 7942ba7
feat: add fromAuthorization method
parodyBit f5e9916
run dart format
parodyBit 1365970
fix: schema constructor and recovery id
parodyBit 47fee38
fix: schema constructor
parodyBit c0c1b6f
fix: stake/unstake schema
parodyBit 6292c2d
fix: decode authorization
parodyBit 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 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,30 @@ | ||
import 'dart:typed_data'; | ||
|
||
import 'package:witnet/crypto.dart'; | ||
import 'package:witnet/src/utils/transformations/transformations.dart'; | ||
import 'package:witnet/witnet.dart'; | ||
|
||
// abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about | ||
String xprvString = | ||
"xprv1qpujxsyd4hfu0dtwa524vac84e09mjsgnh5h9crl8wrqg58z5wmsuqqcxlqmar3fjhkprndzkpnp2xlze76g4hu7g7c4r4r2m2e6y8xlvu566tn6"; | ||
|
||
void main() { | ||
// import a xprv | ||
Xprv xprv = Xprv.fromXprv(xprvString); | ||
var expectedKey = bytesToHex(xprv.privateKey.publicKey.point.encode()); | ||
|
||
// sign a message | ||
String messageStr = "Hello Witnet!"; | ||
Uint8List messageBytes = sha256(data: stringToBytes(messageStr)); | ||
WitSignature signature = xprv.privateKey.signature(bytesToHex(messageBytes)); | ||
|
||
// recover a public key from a signature | ||
WitPublicKey recoveredKey = WitPublicKey.recover(signature, messageBytes); | ||
|
||
try { | ||
assert(expectedKey == bytesToHex(recoveredKey.point.encode()), | ||
"error: Message not signed by expected Public Key"); | ||
} catch (e) { | ||
print(e); | ||
} | ||
} |
This file contains 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,84 @@ | ||
// import 'package:witnet/node.dart'; | ||
import 'package:witnet/schema.dart'; | ||
import 'package:witnet/src/constants.dart'; | ||
import 'package:witnet/src/utils/transformations/transformations.dart'; | ||
import 'package:witnet/witnet.dart'; | ||
|
||
var outputPointer = OutputPointer.fromString( | ||
'0000000000000000000000000000000000000000000000000000000000000000:0'); | ||
|
||
void main() async { | ||
/// connect to local node rpc | ||
// NodeClient nodeClient = NodeClient(address: "127.0.0.1", port: 21338); | ||
|
||
// String mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"; | ||
/// load node xprv for the default mnemonic | ||
Xprv masterNode = Xprv.fromXprv( | ||
"xprv1qpujxsyd4hfu0dtwa524vac84e09mjsgnh5h9crl8wrqg58z5wmsuqqcxlqmar3fjhkprndzkpnp2xlze76g4hu7g7c4r4r2m2e6y8xlvu566tn6"); | ||
|
||
Xprv withdrawer = masterNode / | ||
KEYPATH_PURPOSE / | ||
KEYPATH_COIN_TYPE / | ||
KEYPATH_ACCOUNT / | ||
EXTERNAL_KEYCHAIN / | ||
0; | ||
|
||
/// The 20 byte Public Key Hash of the withdrawer | ||
String pkh = bytesToHex(withdrawer.privateKey.publicKey.publicKeyHash); | ||
|
||
/// The authorization by the node | ||
KeyedSignature authorization = signHash(pkh, masterNode.privateKey); | ||
|
||
/// Build the Stake Key | ||
StakeKey stakeKey = StakeKey( | ||
validator: authorization.publicKey.pkh, | ||
withdrawer: PublicKeyHash.fromAddress(withdrawer.address.address), | ||
); | ||
|
||
/// build stake transaction body | ||
StakeBody body = StakeBody( | ||
inputs: [ | ||
Input(outputPointer: outputPointer), | ||
], | ||
output: StakeOutput( | ||
value: MINIMUM_STAKEABLE_AMOUNT_WITS, | ||
key: stakeKey, | ||
authorization: authorization, | ||
), | ||
); | ||
|
||
/// build and sign stake transaction | ||
StakeTransaction stake = StakeTransaction( | ||
body: body, | ||
signatures: [signHash(body.transactionId, masterNode.privateKey)]); | ||
|
||
/// The Stake Transaction ID | ||
print(stake.transactionID); | ||
|
||
/// send stake transaction | ||
/// var response = await nodeClient.inventory(stake.jsonMap()); | ||
/// | ||
UnstakeBody unstakeBody = UnstakeBody( | ||
operator: PublicKeyHash.fromAddress(withdrawer.address.address), | ||
withdrawal: ValueTransferOutput.fromJson({ | ||
"pkh": withdrawer.address.address, | ||
"time_lock": 0, | ||
"value": 1, | ||
})); | ||
|
||
KeyedSignature unstakeSignature = | ||
signHash(bytesToHex(unstakeBody.hash), masterNode.privateKey); | ||
UnstakeTransaction unstake = | ||
UnstakeTransaction(body: unstakeBody, signature: unstakeSignature); | ||
|
||
print(unstake.transactionID); | ||
} | ||
|
||
/// Sign Hash | ||
KeyedSignature signHash(String hash, WitPrivateKey privateKey) { | ||
final sig = privateKey.signature(hash); | ||
return KeyedSignature( | ||
publicKey: PublicKey(bytes: privateKey.publicKey.encode()), | ||
signature: Signature(secp256k1: Secp256k1Signature(der: sig.encode())), | ||
); | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we are at this points, it means the signature is invalid. What do you think about adding that information to the error?