aigc_core/libtx/
reward.rs

1// Copyright 2021 The Aigc Developers
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Builds the blinded output and related signature proof for the block
16//! reward.
17use crate::consensus::reward;
18use crate::core::{KernelFeatures, Output, OutputFeatures, TxKernel};
19use crate::libtx::error::Error;
20use crate::libtx::{
21	aggsig,
22	proof::{self, ProofBuild},
23};
24use keychain::{Identifier, Keychain, SwitchCommitmentType};
25use util::{secp, static_secp_instance};
26
27/// output a reward output
28pub fn output<K, B>(
29	keychain: &K,
30	builder: &B,
31	key_id: &Identifier,
32	fees: u64,
33	test_mode: bool,
34) -> Result<(Output, TxKernel), Error>
35where
36	K: Keychain,
37	B: ProofBuild,
38{
39	let value = reward(fees);
40	// TODO: proper support for different switch commitment schemes
41	let switch = SwitchCommitmentType::Regular;
42	let commit = keychain.commit(value, key_id, switch)?;
43
44	trace!("Block reward - Pedersen Commit is: {:?}", commit,);
45
46	let proof = proof::create(keychain, builder, value, key_id, switch, commit, None)?;
47
48	let output = Output::new(OutputFeatures::Coinbase, commit, proof);
49
50	let secp = static_secp_instance();
51	let secp = secp.lock();
52	let over_commit = secp.commit_value(reward(fees))?;
53	let out_commit = output.commitment();
54	let excess = secp.commit_sum(vec![out_commit], vec![over_commit])?;
55	let pubkey = excess.to_pubkey(&secp)?;
56
57	let features = KernelFeatures::Coinbase;
58	let msg = features.kernel_sig_msg()?;
59	let sig = match test_mode {
60		true => {
61			let test_nonce = secp::key::SecretKey::from_slice(&secp, &[1; 32])?;
62			aggsig::sign_from_key_id(
63				&secp,
64				keychain,
65				&msg,
66				value,
67				&key_id,
68				Some(&test_nonce),
69				Some(&pubkey),
70			)?
71		}
72		false => {
73			aggsig::sign_from_key_id(&secp, keychain, &msg, value, &key_id, None, Some(&pubkey))?
74		}
75	};
76
77	let kernel = TxKernel {
78		features: KernelFeatures::Coinbase,
79		excess,
80		excess_sig: sig,
81	};
82	Ok((output, kernel))
83}