Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
ece598pv-sp2022
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
qh11
ece598pv-sp2022
Commits
3bd07819
Commit
3bd07819
authored
3 years ago
by
qh11
Browse files
Options
Downloads
Patches
Plain Diff
Update transaction.rs
parent
770b7e85
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/types/transaction.rs
+37
-5
37 additions, 5 deletions
src/types/transaction.rs
with
37 additions
and
5 deletions
src/types/transaction.rs
+
37
−
5
View file @
3bd07819
use
serde
::{
Serialize
,
Deserialize
};
use
ring
::
signature
::{
Ed25519KeyPair
,
Signature
,
KeyPair
,
VerificationAlgorithm
,
EdDSAParameters
};
use
rand
::
Rng
;
use
crate
::
types
::
address
::
Address
;
use
bincode
::
serialize
;
use
serde
::{
Serialize
,
Deserialize
};
#[derive(Serialize,
Deserialize,
Debug,
Default,
Clone)]
pub
struct
Transaction
{
sender
:
Address
,
receiver
:
Address
,
value
:
u32
}
#[derive(Serialize,
Deserialize,
Debug,
Default,
Clone)]
pub
struct
SignedTransaction
{
transaction
:
Transaction
,
signature_vec
:
Vec
<
u8
>
,
public_key_vec
:
Vec
<
u8
>
}
/// Create digital signature of a transaction
pub
fn
sign
(
t
:
&
Transaction
,
key
:
&
Ed25519KeyPair
)
->
Signature
{
unimplemented!
()
let
serializeT
=
bincode
::
serialize
(
&
t
)
.unwrap
();
let
signature
=
key
.sign
(
&
serializeT
);
signature
//unimplemented!()
}
/// Verify digital signature of a transaction, using public key instead of secret key
pub
fn
verify
(
t
:
&
Transaction
,
public_key
:
&
[
u8
],
signature
:
&
[
u8
])
->
bool
{
unimplemented!
()
let
serializeT
=
serialize
(
&
t
)
.unwrap
();
let
unparsed
=
ring
::
signature
::
UnparsedPublicKey
::
new
(
&
ring
::
signature
::
ED25519
,
public_key
);
let
result
=
unparsed
.verify
(
&
serializeT
,
signature
);
let
mut
ans
:
bool
=
false
;
if
result
==
Ok
(())
{
ans
=
true
;
}
else
{
ans
=
false
;
}
return
ans
;
//unimplemented!()
}
#[cfg(any(test,
test_utilities))]
pub
fn
generate_random_transaction
()
->
Transaction
{
unimplemented!
()
let
sender
:
[
u8
;
20
]
=
rand
::
random
();
let
receiver
:
[
u8
;
20
]
=
rand
::
random
();
let
value
:
u32
=
rand
::
random
();
let
newTransaction
=
Transaction
{
sender
:
sender
.into
(),
receiver
:
receiver
.into
(),
value
:
value
};
newTransaction
//unimplemented!()
}
// DO NOT CHANGE THIS COMMENT, IT IS FOR AUTOGRADER. BEFORE TEST
...
...
@@ -53,4 +85,4 @@ mod tests {
}
}
// DO NOT CHANGE THIS COMMENT, IT IS FOR AUTOGRADER. AFTER TEST
\ No newline at end of file
// DO NOT CHANGE THIS COMMENT, IT IS FOR AUTOGRADER. AFTER TEST
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment