Skip to content

Commit

Permalink
start direct deposit
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed May 28, 2020
1 parent 10cdf31 commit 8256165
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 7 deletions.
19 changes: 15 additions & 4 deletions packages/contracts/contracts/NoteStream.sol
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,16 @@ contract NoteStream is Pausable, ReentrancyGuard {
* Throws if the contract is not allowed to transfer enough tokens.
* Throws if there is a token transfer failure.
* @param recipient The address towards which the money is streamed.
* @param noteHash The note of a zkAsset to be streamed.
* @param proof The JoinSplit proof transfering a zkAsset to be streamed.
* @param tokenAddress The zkAsset to use as streaming currency.
* @param startTime The unix timestamp for when the stream starts.
* @param stopTime The unix timestamp for when the stream stops.
* @return The uint256 id of the newly created stream.
*/
function createStream(
address recipient,
bytes32 noteHash,
bytes memory proof,
bytes memory proofSignature,
address tokenAddress,
uint256 startTime,
uint256 stopTime
Expand All @@ -166,10 +167,20 @@ contract NoteStream is Pausable, ReentrancyGuard {
);
require(stopTime > startTime, "Stream duration not greater than zero");

// Transfer the ZkAsset to the streaming contract
bytes32 streamNoteHash = StreamUtilities._processDeposit(
proof,
proofSignature,
aceContractAddress,
msg.sender,
recipient,
tokenAddress
);

/* Create and store the stream object. */
uint256 streamId = nextStreamId;
streams[streamId] = Types.AztecStream({
noteHash: noteHash,
noteHash: streamNoteHash,
sender: msg.sender,
recipient: recipient,
startTime: startTime,
Expand All @@ -187,7 +198,7 @@ contract NoteStream is Pausable, ReentrancyGuard {
msg.sender,
recipient,
tokenAddress,
noteHash,
streamNoteHash,
startTime,
stopTime
);
Expand Down
60 changes: 60 additions & 0 deletions packages/contracts/contracts/StreamUtilities.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,66 @@ library StreamUtilities {
return za.mul(scalingFactor).div(zb);
}

function _processDeposit(
bytes memory _proof,
bytes memory _proofSignature,
address _aceContractAddress,
address _sender,
address _recipient,
address _tokenAddress
) internal returns (bytes32 streamNoteHash) {
// Validate Join-Split proof
bytes memory proofOutputs = IACE(_aceContractAddress)
.validateProof(JOIN_SPLIT_PROOF, address(this), _proof)
.get(0);

// Extract notes used in proof
(, bytes memory _proofOutputNotes, , ) = proofOutputs
.extractProofOutput();

// Ensure that there is only a single output note to avoid loss of funds
require(
_proofOutputNotes.getLength() == 1,
"Incorrect number of output notes"
);

Note memory streamNote = _noteCoderToStruct(_proofOutputNotes.get(0));

// Require that stream note is owned by contract
require(
streamNote.owner == address(this),
"stream note is not owned by stream contract"
);

// Require that sender and receiver have view access to stream note
require(
MetaDataUtils.extractAddress(streamNote.metaData, 0) == _sender,
"stream sender can't view stream note"
);
require(
MetaDataUtils.extractAddress(streamNote.metaData, 1) == _recipient,
"stream recipient can't view stream note"
);

// Approve contract to spend stream note
IZkAsset(_tokenAddress).approveProof(
JOIN_SPLIT_PROOF,
proofOutputs,
address(this),
true,
_proofSignature
);

// Send transfer
IZkAsset(_tokenAddress).confidentialTransferFrom(
JOIN_SPLIT_PROOF,
proofOutputs
);

// Return stream note hash
streamNoteHash = streamNote.noteHash;
}

function _validateRatioProof(
address _aceContractAddress,
bytes memory _proof1,
Expand Down
11 changes: 8 additions & 3 deletions packages/subgraph/abis/NoteStream.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,14 @@
"type": "address"
},
{
"internalType": "bytes32",
"name": "noteHash",
"type": "bytes32"
"internalType": "bytes",
"name": "proof",
"type": "bytes"
},
{
"internalType": "bytes",
"name": "proofSignature",
"type": "bytes"
},
{
"internalType": "address",
Expand Down

0 comments on commit 8256165

Please sign in to comment.