Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,21 @@ packages/node/test
packages/node/*.log
packages/node/scripts/__pycache__

packages/lib/*.log

packages/chat
packages/components/
packages/components
packages/cra-template
packages/docs
packages/explorer
packages/nft
packages/nodejs-template
packages/secp256k1
packages/swap
packages/TBC20/
packages/TBC20-app
packages/TBC404
packages/TBC404-app
packages/TBC721/
packages/TBC721-app
packages/swap/
packages/TBC*/
packages/wallet
packages/website

.env
Dockerfile
.dockerignore
23 changes: 13 additions & 10 deletions packages/docs/API/constructor.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ new (config: {
mnemonic?: string,
path?: string,
passphrase?: string
addressType?: 'p2pkh' | 'p2wpkh' | 'p2tr',
url?: string,
satPerByte?: number
dustRelayFee?: number
Expand All @@ -28,16 +29,17 @@ new Computer(config)
A configuration object

{.compact}
| Key | Description | Default Value |
|--------------|-------------------------------------------------------------|--------------------------------------|
| chain | Target blockchain. Values can be 'LTC' or 'BTC' | LTC |
| network | Target network. Values in 'testnet', 'regtest' or 'mainnet' | testnet |
| mnemonic | BIP39 mnemonic phrase | Random phrase |
| path | BIP32 path | m/44'/0'/0' |
| passphrase | BIP32 passphrase | The empty string |
| url | Url of a Bitcoin Computer Node | https://rltc.node.bitcoincomputer.io |
| satPerByte | Fee in satoshi per byte | 2 |
| dustRelayFee | Dust relay fee | 30000 on LTC and 3000 on BTC |
| Key | Description | Default Value |
|--------------|--------------------------------------------------------------|--------------------------------------|
| chain | Target blockchain. Values can be 'LTC' or 'BTC' | LTC |
| network | Target network. Values in 'testnet', 'regtest' or 'mainnet' | regtest |
| mnemonic | BIP39 mnemonic phrase | Random phrase |
| path | BIP32 path | m/44'/0'/0' |
| passphrase | BIP32 passphrase | The empty string |
| addressType | The address script type. Values in 'p2pkh', 'p2wpkh', 'p2tr' | p2pkh |
| url | Url of a Bitcoin Computer Node | https://rltc.node.bitcoincomputer.io |
| satPerByte | Fee in satoshi per byte | 2 |
| dustRelayFee | Dust relay fee | 30000 on LTC and 3000 on BTC |


### Return Value
Expand All @@ -61,6 +63,7 @@ const computer3 = new Computer({
chain: 'LTC'
network: 'mainnet',
mnemonic: 'replace this seed'
addressType: 'p2wpkh',
path: "m/44'/0'/0'/0",
url: 'https://my-ltc-node.com',
satPerByte: 1
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/API/getAddress.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# getAddress

Returns a string encoding Bitcoin address.
Returns a string encoding Bitcoin address. Will return an address according to the type set in the Computer constructor.

### Type
```ts
Expand Down
4 changes: 4 additions & 0 deletions packages/lib/computer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ declare class RestClient {
readonly mnemonic: string;
readonly path: string;
readonly passphrase: string;
readonly addressType: AddressType;
readonly bcn: UrlFetch;
readonly dustRelayTxFee: number;
readonly _keyPair: any;
Expand All @@ -115,6 +116,7 @@ declare class RestClient {
mnemonic,
path,
passphrase,
addressType,
url,
satPerByte,
dustRelayFee,
Expand Down Expand Up @@ -182,6 +184,8 @@ type Network = "testnet" | "mainnet" | "regtest";
type Fee = Partial<{
fee: number;
}>;
type AddressType = 'p2pkh' | 'p2wpkh' | 'p2tr'

type ProgramMetaData = JObject &
Partial<{
_amount: number;
Expand Down
1 change: 1 addition & 0 deletions packages/nakamotojs/src/address.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export declare function toBase58Check(hash: Buffer, version: number): string;
export declare function toBech32(data: Buffer, version: number, prefix: string): string;
export declare function fromOutputScript(output: Buffer, network?: Network): string;
export declare function toOutputScript(address: string, network?: Network): Buffer;
export declare function fromPublicKey(publicKey: Buffer, type: string, network?: Network): string;
14 changes: 14 additions & 0 deletions packages/nakamotojs/src/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as networks from './networks.js';
import * as payments from './payments/index.js';
import * as bscript from './script.js';
import { typeforce, tuple, Hash160bit, UInt8 } from './types.js';
import { toXOnly } from './psbt/bip371.js';
import { bech32, bech32m } from 'bech32';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand Down Expand Up @@ -145,3 +146,16 @@ export function toOutputScript(address, network) {
}
throw new Error(address + ' has no matching Script');
}
export function fromPublicKey(publicKey, type, network) {
network = network || networks.bitcoin;
if (type === 'p2pkh')
return payments.p2pkh({ pubkey: publicKey, network }).address;
if (type === 'p2wpkh')
return payments.p2wpkh({ pubkey: publicKey, network }).address;
if (type === 'p2tr')
return payments.p2tr({
internalPubkey: toXOnly(publicKey),
network,
}).address;
throw new Error('Unknown or unsupported type');
}
1 change: 1 addition & 0 deletions packages/nakamotojs/src/networks.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export declare const testnet: Network;
export declare const litecoin: Network;
export declare const litecoinregtest: Network;
export declare const litecointestnet: Network;
export declare const NETWORKS: Record<string, any>;
export {};
8 changes: 8 additions & 0 deletions packages/nakamotojs/src/networks.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,11 @@ export const litecointestnet = {
scriptHash: 0x3a,
wif: 0xef,
};
export const NETWORKS = {
litecoin,
bitcoin,
regtest,
testnet,
litecoinregtest,
litecointestnet,
};
2 changes: 2 additions & 0 deletions packages/nakamotojs/src/script.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference types="node" />
import { OPS } from './ops.js';
import { Network } from './networks.js';
import { Stack } from './payments/index.js';
import * as scriptNumber from './script_number.js';
import * as scriptSignature from './script_signature.js';
Expand All @@ -11,6 +12,7 @@ export declare function compile(chunks: Buffer | Stack): Buffer;
export declare function decompile(buffer: Buffer | Array<number | Buffer>): Array<number | Buffer> | null;
export declare function toASM(chunks: Buffer | Array<number | Buffer>): string;
export declare function fromASM(asm: string): Buffer;
export declare function fromPublicKey(publicKey: Buffer, type: string, network?: Network): Buffer;
export declare function toStack(chunks: Buffer | Array<number | Buffer>): Buffer[];
export declare function isCanonicalPubKey(buffer: Buffer): boolean;
export declare function isDefinedHashType(hashType: number): boolean;
Expand Down
21 changes: 21 additions & 0 deletions packages/nakamotojs/src/script.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * as bip66 from './bip66.js';
import { OPS, REVERSE_OPS } from './ops.js';
import { toXOnly } from './psbt/bip371.js';
import * as networks from './networks.js';
import * as payments from './payments/index.js';
import * as pushdata from './push_data.js';
import * as scriptNumber from './script_number.js';
import * as scriptSignature from './script_signature.js';
Expand Down Expand Up @@ -142,6 +145,24 @@ export function fromASM(asm) {
}),
);
}
export function fromPublicKey(publicKey, type, network) {
network = network || networks.bitcoin;
if (type === 'p2pkh')
return payments.p2pkh({ pubkey: publicKey, network }).output;
if (type === 'p2wpkh')
return payments.p2wpkh({ pubkey: publicKey, network }).output;
if (type === 'p2tr')
return payments.p2tr({
internalPubkey: toXOnly(publicKey),
network,
}).output;
if (type === 'p2pk')
return payments.p2pk({
pubkey: publicKey,
network,
}).output;
throw new Error('Unknown or unsupported script type');
}
export function toStack(chunks) {
chunks = decompile(chunks);
typeforce(isPushOnly, chunks);
Expand Down
53 changes: 26 additions & 27 deletions packages/nakamotojs/test/address.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ import { describe, it } from 'mocha';
import * as ecc from '@bitcoin-computer/tiny-secp256k1';
import * as baddress from '../src/address.js';
import * as bscript from '../src/script.js';
import {
bitcoin,
regtest,
testnet,
litecoin,
litecoinregtest,
litecointestnet,
} from '../src/networks.js';
import { NETWORKS } from '../src/networks.js';
import * as fixturesModule from './fixtures/address.json' assert { type: 'json' };

const fixtures: typeof import('./fixtures/address.json') =
Expand All @@ -21,25 +14,6 @@ const fixtures: typeof import('./fixtures/address.json') =

import { initEccLib } from '../src/index.js';

const NETWORKS = Object.assign({
litecoin: {
messagePrefix: '\x19Litecoin Signed Message:\n',
bip32: {
public: 0x019da462,
private: 0x019d9cfe,
},
pubKeyHash: 0x30,
scriptHash: 0x32,
wif: 0xb0,
},
bitcoin,
regtest,
testnet,
litecoinregtest,
litecointestnet,
});
NETWORKS.litecoin = litecoin;

describe('address', () => {
describe('fromBase58Check', () => {
fixtures.standard.forEach(f => {
Expand Down Expand Up @@ -170,4 +144,29 @@ describe('address', () => {
});
});
});

describe('fromPublicKey', () => {
fixtures.valid.forEach(f => {
f.types.forEach(t => {
if (!t.address) return;
it(
'decodes ' +
f.publicKey.slice(0, 10) +
'... (' +
f.network +
') to ' +
t.scriptType,
() => {
const addr = baddress.fromPublicKey(
Buffer.from(f.publicKey, 'hex'),
t.scriptType,
NETWORKS[f.network],
);

assert.strictEqual(addr, t.address);
},
);
});
});
});
});
106 changes: 104 additions & 2 deletions packages/nakamotojs/test/fixtures/address.json
Original file line number Diff line number Diff line change
Expand Up @@ -326,5 +326,107 @@
"paymentException": "TypeError: Invalid pubkey for p2tr"
}
]
}
}
},
"valid": [
{
"network": "bitcoin",
"publicKey": "029143dbd1a870e4498901c35d800671f20e21f1aec3c1df014c7f8cb74e72cdf9",
"types": [
{
"scriptType": "p2pkh",
"address": "1FU7tCdYESrudBsELdFMY7EiM8iv7hc434",
"script": "OP_DUP OP_HASH160 9eb2bcc5576fefd4e555ad31fa5f7a3f4c94bf5f OP_EQUALVERIFY OP_CHECKSIG"
},
{
"scriptType": "p2wpkh",
"address": "bc1qn6ete32hdlhafe2445cl5hm68axff06lyyzw4f",
"script": "OP_0 9eb2bcc5576fefd4e555ad31fa5f7a3f4c94bf5f"
},
{
"scriptType": "p2tr",
"address": "bc1p9f2jgradpf2s2jdsue2lmfja55daktft3j5zxcyl7rrl7q63cm5s7g6thy",
"script": "OP_1 2a55240fad0a550549b0e655fda65da51bdb2d2b8ca823609ff0c7ff0351c6e9"
},
{
"scriptType": "p2pk",
"script": "029143dbd1a870e4498901c35d800671f20e21f1aec3c1df014c7f8cb74e72cdf9 OP_CHECKSIG"
}
]
},
{
"network": "litecoin",
"publicKey": "029143dbd1a870e4498901c35d800671f20e21f1aec3c1df014c7f8cb74e72cdf9",
"types": [
{
"scriptType": "p2pkh",
"address": "LZh59QwNK76xszZPWmEep8JUZM6CGy6mx3",
"script": "OP_DUP OP_HASH160 9eb2bcc5576fefd4e555ad31fa5f7a3f4c94bf5f OP_EQUALVERIFY OP_CHECKSIG"
},
{
"scriptType": "p2wpkh",
"address": "ltc1qn6ete32hdlhafe2445cl5hm68axff06lqcc2de",
"script": "OP_0 9eb2bcc5576fefd4e555ad31fa5f7a3f4c94bf5f"
},
{
"scriptType": "p2tr",
"address": "ltc1p9f2jgradpf2s2jdsue2lmfja55daktft3j5zxcyl7rrl7q63cm5sav5mdp",
"script": "OP_1 2a55240fad0a550549b0e655fda65da51bdb2d2b8ca823609ff0c7ff0351c6e9"
},
{
"scriptType": "p2pk",
"script": "029143dbd1a870e4498901c35d800671f20e21f1aec3c1df014c7f8cb74e72cdf9 OP_CHECKSIG"
}
]
},
{
"network": "testnet",
"publicKey": "029143dbd1a870e4498901c35d800671f20e21f1aec3c1df014c7f8cb74e72cdf9",
"types": [
{
"scriptType": "p2pkh",
"address": "muz5BFiX3UJAQJLr4CDjN2T3D8KczqP7p7",
"script": "OP_DUP OP_HASH160 9eb2bcc5576fefd4e555ad31fa5f7a3f4c94bf5f OP_EQUALVERIFY OP_CHECKSIG"
},
{
"scriptType": "p2wpkh",
"address": "tb1qn6ete32hdlhafe2445cl5hm68axff06lwzeaw6",
"script": "OP_0 9eb2bcc5576fefd4e555ad31fa5f7a3f4c94bf5f"
},
{
"scriptType": "p2tr",
"address": "tb1p9f2jgradpf2s2jdsue2lmfja55daktft3j5zxcyl7rrl7q63cm5sfqvydt",
"script": "OP_1 2a55240fad0a550549b0e655fda65da51bdb2d2b8ca823609ff0c7ff0351c6e9"
},
{
"scriptType": "p2pk",
"script": "029143dbd1a870e4498901c35d800671f20e21f1aec3c1df014c7f8cb74e72cdf9 OP_CHECKSIG"
}
]
},
{
"network": "litecointestnet",
"publicKey": "029143dbd1a870e4498901c35d800671f20e21f1aec3c1df014c7f8cb74e72cdf9",
"types": [
{
"scriptType": "p2pkh",
"address": "muz5BFiX3UJAQJLr4CDjN2T3D8KczqP7p7",
"script": "OP_DUP OP_HASH160 9eb2bcc5576fefd4e555ad31fa5f7a3f4c94bf5f OP_EQUALVERIFY OP_CHECKSIG"
},
{
"scriptType": "p2wpkh",
"address": "tltc1qn6ete32hdlhafe2445cl5hm68axff06lh2mr7n",
"script": "OP_0 9eb2bcc5576fefd4e555ad31fa5f7a3f4c94bf5f"
},
{
"scriptType": "p2tr",
"address": "tltc1p9f2jgradpf2s2jdsue2lmfja55daktft3j5zxcyl7rrl7q63cm5skrs9j5",
"script": "OP_1 2a55240fad0a550549b0e655fda65da51bdb2d2b8ca823609ff0c7ff0351c6e9"
},
{
"scriptType": "p2pk",
"script": "029143dbd1a870e4498901c35d800671f20e21f1aec3c1df014c7f8cb74e72cdf9 OP_CHECKSIG"
}
]
}
]
}
Loading