Skip to content

Commit

Permalink
pr fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Apr 17, 2024
1 parent ff6c1ae commit 011e7fc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
21 changes: 21 additions & 0 deletions examples/bitcoinjs-lib/LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015-2016 Bitcoinjs developers

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
31 changes: 20 additions & 11 deletions examples/bitcoinjs-lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,31 @@ app.get('/get-address', (req, res) => {
const { address } = bitcoin.payments.p2pkh({
pubkey: keyPair.publicKey
});

res.send(address);
});

app.get('/get-public-key', (req, res) => {
const keyPair = ECPair.fromWIF(
'L3BybjkmnMdXE6iNEaeZTjVMTHA4TvpYbQozc264Lto9yVDis2nv'
);

res.send(keyPair.publicKey.toString('hex'));
});

app.get('/get-private-key', (req, res) => {
const keyPair = ECPair.fromWIF(
'L3BybjkmnMdXE6iNEaeZTjVMTHA4TvpYbQozc264Lto9yVDis2nv'
);

res.send(keyPair.privateKey?.toString('hex'));
});

app.get('/get-private-key-wif', (req, res) => {
const keyPair = ECPair.fromWIF(
'L3BybjkmnMdXE6iNEaeZTjVMTHA4TvpYbQozc264Lto9yVDis2nv'
);

res.send(keyPair.toWIF());
});

Expand All @@ -59,7 +63,7 @@ app.post('/create-transaction', (req, res) => {
bitcoin.opcodes.OP_EQUALVERIFY,
bitcoin.opcodes.OP_CHECKSIG
]);
transaction.addOutput(scriptPubkey, 15000);
transaction.addOutput(scriptPubkey, 15_000);

res.send(transaction.toBuffer().toString('hex'));
});
Expand All @@ -78,6 +82,7 @@ app.post('/sign-bitcoin-message', (req, res) => {
privateKey,
keyPair.compressed
);

res.send(signature.toString('base64'));
});

Expand All @@ -102,6 +107,7 @@ app.post('/verify-bitcoin-message', (req, res) => {
if (address === undefined) {
throw new Error('Invalid address');
}

res.send(bitcoinMessage.verify(message, address, signature));
});

Expand Down Expand Up @@ -129,16 +135,20 @@ app.post('/fail-to-verify-bitcoin-message', (req, res) => {
if (address === undefined) {
throw new Error('Invalid address');
}

res.send(bitcoinMessage.verify(message, address, signature));
});

// The following endpoints are not for our tests or examples but rather help to
// illustrate some of the different features of creating transactions. Each of
// these return the same transaction as in /create-transaction
// The following endpoints (/create-transaction-from-known-hex, and
// /create-transaction-from-deconstructed-transaction) are not for our tests or
// examples but rather help to illustrate some of the different features of
// creating transactions. Each of these return the same transaction as in
// /create-transaction
app.post('/create-transaction-from-known-hex', (req, res) => {
const transactionHex =
'02000000018689302ea03ef5dd56fb7940a867f9240fa811eddeb0fa4c87ad9ff3728f5e110000000000ffffffff01983a0000000000001976a914ad618cf4333b3b248f9744e8e81db2964d0ae39788ac00000000';
const transaction = bitcoin.Transaction.fromHex(transactionHex);

res.send(transaction.toBuffer().toString('hex'));
});

Expand Down Expand Up @@ -177,11 +187,6 @@ app.post('/create-psbt', (req, res) => {
const keyPair = ECPair.fromWIF(
'L2uPYXe17xSTqbCjZvL2DsyXPCbXspvcu5mHLDYUgzdUbZGSKrSr'
);
// const things = new bitcoin.Transaction();
// const transactionBuilder = new bitcoin.Transaction();
// bitcoin.Transaction.fromHex();
// // things.addInput()
// things.addOutput('1Gokm82v6DmtwKEB8AiVhm82hyFSsEvBDK', 15000);
const psbt = new bitcoin.Psbt();
psbt.addInput({
// if hash is string, txid, if hash is Buffer, is reversed compared to txid
Expand Down Expand Up @@ -209,15 +214,16 @@ app.post('/create-psbt', (req, res) => {
});
psbt.addOutput({
address: '1KRMKfeZcmosxALVYESdPNez1AP1mEtywp',
value: 80000
value: 80_000
});
psbt.signInput(0, keyPair);
psbt.validateSignaturesOfInput(0, validator);
psbt.finalizeAllInputs();

res.send(psbt.extractTransaction().toHex());
});

// The following endpoints do signatures with the ECPair sign
// The following endpoints sign messages with ECPair sign
app.post('/ecpair-sign-bitcoin-message', (req, res) => {
const keyPair = ECPair.fromWIF(
'L3BybjkmnMdXE6iNEaeZTjVMTHA4TvpYbQozc264Lto9yVDis2nv'
Expand All @@ -226,6 +232,7 @@ app.post('/ecpair-sign-bitcoin-message', (req, res) => {
const message = Buffer.from('This is an example of a signed message');
const hash = bitcoin.crypto.sha256(message);
const signature = keyPair.sign(hash);

res.send(signature.toString('base64'));
});

Expand All @@ -237,6 +244,7 @@ app.post('/ecpair-verify-bitcoin-message', (req, res) => {
const message = Buffer.from('This is an example of a signed message');
const hash = bitcoin.crypto.sha256(message);
const signature = keyPair.sign(hash);

res.send(keyPair.verify(hash, signature));
});

Expand All @@ -251,6 +259,7 @@ app.post('/ecpair-fail-to-verify-bitcoin-message', (req, res) => {
const message = Buffer.from('This is an example of a signed message');
const hash = bitcoin.crypto.sha256(message);
const signature = keyPair.sign(hash);

res.send(wrongKeyPair.verify(hash, signature));
});

Expand Down
4 changes: 2 additions & 2 deletions examples/bitcore-lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ app.post('/create-transaction', (req, res) => {
outputIndex: 0,
address: privateKey.toAddress(),
script: '76a91447862fe165e6121af80d5dde1ecb478ed170565b88ac',
satoshis: 50000
satoshis: 50_000
});
const transaction = new Transaction()
.from([utxo])
.to('1Gokm82v6DmtwKEB8AiVhm82hyFSsEvBDK', 15000)
.to('1Gokm82v6DmtwKEB8AiVhm82hyFSsEvBDK', 15_000)
.sign(privateKey);

res.send(transaction.toString());
Expand Down
3 changes: 0 additions & 3 deletions examples/bitcore-lib/test/tests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import * as dns from 'node:dns';
dns.setDefaultResultOrder('ipv4first');

import { Test } from 'azle/test';

import { getBitcoinTests } from './bitcoin';
Expand Down

0 comments on commit 011e7fc

Please sign in to comment.