From 4b0d5c40b68b86fac88bb437fa70eb3a5c28eff1 Mon Sep 17 00:00:00 2001 From: getlarge Date: Thu, 10 Dec 2020 12:54:00 +0100 Subject: [PATCH] test delegated signature Signed-off-by: getlarge --- test/constants.js | 17 +++++++ test/integration/test_integration.js | 55 ++++++++++++++++++++++- test/transaction/test_cryptoconditions.js | 19 ++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/test/constants.js b/test/constants.js index 6062fec..1888ce9 100644 --- a/test/constants.js +++ b/test/constants.js @@ -3,6 +3,8 @@ // Code is Apache-2.0 and docs are CC-BY-4.0 import test from 'ava' +import base58 from 'bs58' +import { Ed25519Sha256 } from 'crypto-conditions' import { Transaction, Ed25519Keypair } from '../src' // TODO: Find out if ava has something like conftest, if so put this there. @@ -31,6 +33,21 @@ export const bob = new Ed25519Keypair() export const bobCondition = Transaction.makeEd25519Condition(bob.publicKey) export const bobOutput = Transaction.makeOutput(bobCondition) +export function delegatedSignTransaction(...keyPairs) { + return function sign(transaction, input, transactionHash) { + const filteredKeyPairs = keyPairs.filter(({ publicKey }) => + input.owners_before.includes(publicKey)) + const ed25519Fulfillment = new Ed25519Sha256() + filteredKeyPairs.forEach(keyPair => { + const privateKey = Buffer.from(base58.decode(keyPair.privateKey)) + ed25519Fulfillment.sign( + Buffer.from(transactionHash, 'hex'), + privateKey + ) + }) + return ed25519Fulfillment.serializeUri() + } +} // TODO: https://github.com/avajs/ava/issues/1190 test('', () => 'dirty hack. TODO: Exclude this file from being run by ava') diff --git a/test/integration/test_integration.js b/test/integration/test_integration.js index b19c4a9..d132586 100644 --- a/test/integration/test_integration.js +++ b/test/integration/test_integration.js @@ -13,7 +13,8 @@ import { bob, bobOutput, asset, - metaData + metaData, + delegatedSignTransaction } from '../constants' @@ -202,6 +203,58 @@ test('Valid TRANSFER transaction with multiple Ed25519 inputs from different tra }) }) +test('Valid CREATE transaction using delegateSign with default node', t => { + const conn = new Connection() + + const tx = Transaction.makeCreateTransaction( + asset(), + metaData, + [aliceOutput], + alice.publicKey + ) + + const txSigned = Transaction.delegateSignTransaction( + tx, + delegatedSignTransaction(alice) + ) + + return conn.postTransaction(txSigned) + .then(resTx => { + t.truthy(resTx) + }) +}) + +test('Valid TRANSFER transaction with multiple Ed25519 inputs using delegateSign', t => { + const conn = new Connection(API_PATH) + const createTx = Transaction.makeCreateTransaction( + asset(), + metaData, + [aliceOutput, bobOutput], + alice.publicKey + ) + const createTxSigned = Transaction.signTransaction( + createTx, + alice.privateKey + ) + + return conn.postTransactionCommit(createTxSigned) + .then(() => { + const transferTx = Transaction.makeTransferTransaction( + [{ tx: createTxSigned, output_index: 0 }, { tx: createTxSigned, output_index: 1 }], + [Transaction.makeOutput(aliceCondition, '2')], + metaData + ) + + const transferTxSigned = Transaction.delegateSignTransaction( + transferTx, + delegatedSignTransaction(alice, bob) + ) + + return conn.postTransactionCommit(transferTxSigned) + .then(resTx => t.truthy(resTx)) + }) +}) + test('Search for spent and unspent outputs of a given public key', t => { const conn = new Connection(API_PATH) const carol = new Ed25519Keypair() diff --git a/test/transaction/test_cryptoconditions.js b/test/transaction/test_cryptoconditions.js index 80609b2..b6fe2bb 100644 --- a/test/transaction/test_cryptoconditions.js +++ b/test/transaction/test_cryptoconditions.js @@ -5,6 +5,7 @@ import test from 'ava' import cc from 'crypto-conditions' import { Ed25519Keypair, Transaction, ccJsonLoad } from '../../src' +import { delegatedSignTransaction } from '../constants' import sha256Hash from '../../src/sha256Hash' test('Ed25519 condition encoding', t => { @@ -96,6 +97,24 @@ test('Fulfillment correctly formed', t => { )) }) +test('Delegated signature is correct', t => { + const alice = new Ed25519Keypair() + + const txCreate = Transaction.makeCreateTransaction( + {}, + {}, + [Transaction.makeOutput(Transaction.makeEd25519Condition(alice.publicKey))], + alice.publicKey + ) + + const signCreateTransaction = Transaction.signTransaction(txCreate, alice.privateKey) + const delegatedSignCreateTransaction = Transaction.delegateSignTransaction( + txCreate, + delegatedSignTransaction(alice) + ) + t.deepEqual(signCreateTransaction, delegatedSignCreateTransaction) +}) + test('CryptoConditions JSON load', t => { const cond = ccJsonLoad({