Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Dunitrust
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
nodes
rust
Dunitrust
Commits
9b5425a6
Commit
9b5425a6
authored
7 years ago
by
nanocryk
Browse files
Options
Downloads
Patches
Plain Diff
[enh] MembershipDocumentBuilder
parent
122f02fc
Branches
Branches containing commit
Tags
v0.4.8
Tags containing commit
1 merge request
!17
Resolve "Add Membership document structure, building and parsing"
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
protocol/blockchain/v10/documents/membership.rs
+121
-0
121 additions, 0 deletions
protocol/blockchain/v10/documents/membership.rs
with
121 additions
and
0 deletions
protocol/blockchain/v10/documents/membership.rs
+
121
−
0
View file @
9b5425a6
...
@@ -106,3 +106,124 @@ impl IntoSpecializedDocument<BlockchainProtocol> for MembershipDocument {
...
@@ -106,3 +106,124 @@ impl IntoSpecializedDocument<BlockchainProtocol> for MembershipDocument {
BlockchainProtocol
::
V10
(
V10Document
::
Membership
(
self
))
BlockchainProtocol
::
V10
(
V10Document
::
Membership
(
self
))
}
}
}
}
/// Membership document builder.
#[derive(Debug,
Copy,
Clone)]
pub
struct
MembershipDocumentBuilder
<
'a
>
{
/// Document currency.
pub
currency
:
&
'a
str
,
/// Document/identity issuer.
pub
issuer
:
&
'a
ed25519
::
PublicKey
,
/// Reference blockstamp.
pub
blockstamp
:
&
'a
Blockstamp
,
/// Membership message.
pub
membership
:
MembershipType
,
/// Identity username.
pub
identity_username
:
&
'a
str
,
/// Identity document blockstamp.
pub
identity_blockstamp
:
&
'a
Blockstamp
,
}
impl
<
'a
>
MembershipDocumentBuilder
<
'a
>
{
fn
build_with_text_and_sigs
(
self
,
text
:
String
,
signatures
:
Vec
<
ed25519
::
Signature
>
,
)
->
MembershipDocument
{
MembershipDocument
{
text
:
text
,
currency
:
self
.currency
.to_string
(),
issuers
:
vec!
[
*
self
.issuer
],
blockstamp
:
*
self
.blockstamp
,
membership
:
self
.membership
,
identity_username
:
self
.identity_username
.to_string
(),
identity_blockstamp
:
*
self
.identity_blockstamp
,
signatures
}
}
}
impl
<
'a
>
DocumentBuilder
for
MembershipDocumentBuilder
<
'a
>
{
type
Document
=
MembershipDocument
;
type
PrivateKey
=
ed25519
::
PrivateKey
;
fn
build_with_signature
(
&
self
,
signatures
:
Vec
<
ed25519
::
Signature
>
)
->
MembershipDocument
{
self
.build_with_text_and_sigs
(
self
.generate_text
(),
signatures
)
}
fn
build_and_sign
(
&
self
,
private_keys
:
Vec
<
ed25519
::
PrivateKey
>
)
->
MembershipDocument
{
let
(
text
,
signatures
)
=
self
.build_signed_text
(
private_keys
);
self
.build_with_text_and_sigs
(
text
,
signatures
)
}
}
impl
<
'a
>
TextDocumentBuilder
for
MembershipDocumentBuilder
<
'a
>
{
fn
generate_text
(
&
self
)
->
String
{
format!
(
"Version: 10
Type: Membership
Currency: {currency}
Issuer: {issuer}
Block: {blockstamp}
Membership: {membership}
UserID: {username}
CertTS: {ity_blockstamp}
"
,
currency
=
self
.currency
,
issuer
=
self
.issuer
,
blockstamp
=
self
.blockstamp
,
membership
=
match
self
.membership
{
MembershipType
::
In
()
=>
"IN"
,
MembershipType
::
Out
()
=>
"OUT"
,
},
username
=
self
.identity_username
,
ity_blockstamp
=
self
.identity_blockstamp
,
)
}
}
#[cfg(test)]
mod
tests
{
use
super
::
*
;
use
duniter_crypto
::
keys
::{
PrivateKey
,
PublicKey
,
Signature
};
use
blockchain
::
VerificationResult
;
#[test]
fn
generate_real_document
()
{
let
pubkey
=
ed25519
::
PublicKey
::
from_base58
(
"DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV"
,
)
.unwrap
();
let
prikey
=
ed25519
::
PrivateKey
::
from_base58
(
"468Q1XtTq7h84NorZdWBZFJrGkB18CbmbHr9tkp9snt5G
\
iERP7ySs3wM8myLccbAAGejgMRC9rqnXuW3iAfZACm7"
,
)
.unwrap
();
let
sig
=
ed25519
::
Signature
::
from_base64
(
"s2hUbokkibTAWGEwErw6hyXSWlWFQ2UWs2PWx8d/kkEl
\
AyuuWaQq4Tsonuweh1xn4AC1TVWt4yMR3WrDdkhnAw=="
,
)
.unwrap
();
let
block
=
Blockstamp
::
from_string
(
"0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855"
,
)
.unwrap
();
let
builder
=
MembershipDocumentBuilder
{
currency
:
"duniter_unit_test_currency"
,
issuer
:
&
pubkey
,
blockstamp
:
&
block
,
membership
:
MembershipType
::
In
(),
identity_username
:
"tic"
,
identity_blockstamp
:
&
block
,
};
assert_eq!
(
builder
.build_with_signature
(
vec!
[
sig
])
.verify_signatures
(),
VerificationResult
::
Valid
()
);
assert_eq!
(
builder
.build_and_sign
(
vec!
[
prikey
])
.verify_signatures
(),
VerificationResult
::
Valid
()
);
}
}
\ No newline at end of file
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