test delegated signature

Signed-off-by: getlarge <ed@getlarge.eu>
This commit is contained in:
getlarge 2020-12-10 12:54:00 +01:00
parent b2e4ef55bf
commit 4b0d5c40b6
3 changed files with 90 additions and 1 deletions

View File

@ -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')

View File

@ -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()

View File

@ -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({