Bitcoin Forum
May 26, 2024, 04:59:31 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
  Home Help Search Login Register More  
  Show Posts
Pages: « 1 ... 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 [74] 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 »
1461  Bitcoin / Development & Technical Discussion / Re: [PATCH] wallet private key encryption on: March 26, 2011, 09:18:58 PM
I was actually also working on this using SSL's SEAL:

Code:
#include <string>
#include <exception>
using std::string;

//typedef struct RSA;
#include <openssl/rsa.h>

class ReadError : public std::exception
{
public:
    virtual const char* what() const throw();
};

class NoKeypairLoaded : public std::exception
{
public:
    virtual const char* what() const throw();
};

class AccessCard
{
public:
    AccessCard();
    ~AccessCard();
    void Generate();
    void Load(const string& pem, const string& pass);
    void PublicKey(string& pem) const;
    void PrivateKey(string& pem, const string& passphrase) const;

    void Seal(FILE* in_file, FILE* out_file);
    void Unseal(FILE* in_file, FILE* out_file);

private:
    void CheckKey() const;

    RSA* keypair;
};

class BioBox
{
public:
    struct Buffer
    {
        void* buf;
        int size;
    };

    BioBox();
    ~BioBox();
    void ConstructSink(const string& str);
    void NewBuffer();
    BIO* Bio() const;
    Buffer ReadAll();
private:
    BIO* bio;
    Buffer buf;
};

class EvpBox
{
public:
    EvpBox(RSA* keyp);
    ~EvpBox();
    EVP_PKEY* Key();
private:
    EVP_PKEY* evpkey;
};

//--------------------
#include <openssl/pem.h>

const char* ReadError::what() const throw()
{
    return "Problem reading BIO.";
}
const char* NoKeypairLoaded::what() const throw()
{
    return "No keypair loaded.";
}

AccessCard::AccessCard()
  : keypair(NULL)
{
    if (EVP_get_cipherbyname("aes-256-cbc") == NULL)
        OpenSSL_add_all_algorithms();
}
AccessCard::~AccessCard()
{
    RSA_free(keypair);
}
void AccessCard::CheckKey() const
{
    if (!keypair)
        throw NoKeypairLoaded();
}

void AccessCard::Generate()
{
    RSA_free(keypair);
    keypair = RSA_generate_key(2048, RSA_F4, NULL, NULL);
    CheckKey();
}

static char *LoseStringConst(const string& str)
{
    return const_cast<char*>(str.c_str());
}
static void* StringAsVoid(const string& str)
{
    return reinterpret_cast<void*>(LoseStringConst(str));
}

BioBox::BioBox()
 : bio(NULL)
{
    buf.buf = NULL;
    buf.size = 0;
}
BioBox::~BioBox()
{
    BIO_free(bio);
    free(buf.buf);
}
void BioBox::ConstructSink(const string& str)
{
    BIO_free(bio);
    bio = BIO_new_mem_buf(StringAsVoid(str), -1);
    if (!bio)
        throw ReadError();
}
void BioBox::NewBuffer()
{
    BIO_free(bio);
    bio = BIO_new(BIO_s_mem());
    if (!bio)
        throw ReadError();
}
BIO* BioBox::Bio() const
{
    return bio;
}
BioBox::Buffer BioBox::ReadAll()
{
    buf.size = BIO_ctrl_pending(bio);
    buf.buf = malloc(buf.size);
    if (BIO_read(bio, buf.buf, buf.size) < 0) {
        //if (ERR_peek_error()) {
        //    ERR_reason_error_string(ERR_get_error());
        //    return NULL;
        //}
        throw ReadError();
    }
    return buf;
}

EvpBox::EvpBox(RSA* keyp)
{
    evpkey = EVP_PKEY_new();
    if (!EVP_PKEY_set1_RSA(evpkey, keyp)) {
        throw ReadError();
    }
}
EvpBox::~EvpBox()
{
    EVP_PKEY_free(evpkey);
}
EVP_PKEY* EvpBox::Key()
{
    return evpkey;
}

static int pass_cb(char* buf, int size, int rwflag, void* u)
{
    const string pass = reinterpret_cast<char*>(u);
    int len = pass.size();
    // if too long, truncate
    if (len > size)
        len = size;
    pass.copy(buf, len);
    return len;
}
void AccessCard::Load(const string& pem, const string& pass)
{
    RSA_free(keypair);
    BioBox bio;
    bio.ConstructSink(pem);
    keypair = PEM_read_bio_RSAPrivateKey(bio.Bio(), NULL, pass_cb,
                                         StringAsVoid(pass));
    CheckKey();                    
}
void AccessCard::PublicKey(string& pem) const
{
    CheckKey();
    BioBox bio;
    bio.NewBuffer();
    int ret = PEM_write_bio_RSA_PUBKEY(bio.Bio(), keypair);
    if (!ret)
        throw ReadError();
    const BioBox::Buffer& buf = bio.ReadAll();
    pem = string(reinterpret_cast<const char*>(buf.buf), buf.size);
}

void AccessCard::PrivateKey(string& pem, const string& passphrase) const
{
    CheckKey();
    BioBox bio;
    bio.NewBuffer();

    EvpBox evp(keypair);
    int ret = PEM_write_bio_PKCS8PrivateKey(bio.Bio(), evp.Key(),
                                            EVP_aes_256_cbc(),
                                            LoseStringConst(passphrase),
                                            passphrase.size(), NULL, NULL);
    if (!ret)
        throw ReadError();
    const BioBox::Buffer& buf = bio.ReadAll();
    pem = string(reinterpret_cast<const char*>(buf.buf), buf.size);
}

#include <stdio.h>
#include <stdlib.h>

#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/err.h>

#include <arpa/inet.h> /* For htonl() */

void AccessCard::Seal(FILE* in_file, FILE* out_file)
{
    CheckKey();
    // see below
}
void AccessCard::Unseal(FILE* in_file, FILE* out_file)
{
    CheckKey();
}

//-------------------------------------------------------------------
// this wont be in the final file... it's our unit test
//-------------------------------------------------------------------
#include <iostream>
#include <assert.h>
using std::cout;

int main()
{
    AccessCard acc;                                                    
    // New key
    acc.Generate();
    string pem;
    //acc.PublicKey(pem);
    //cout << pem << "\n";
    acc.PrivateKey(pem, "hello");
    //cout << pem << "\n";
    //// ----------------
    acc.Load(pem, "hello");
    acc.PublicKey(pem);
    //cout << pem << "\n";
    //acc.Seal(stdin, stdout);
    return 0;
}
 

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/err.h>

#include <arpa/inet.h> /* For htonl() */

// rsa key should be 2048 bit with 65537 exponent
char* PUBLIC_KEY_PEM =
"-----BEGIN PUBLIC KEY-----\n"
"MIIBIjANBgkqhkiG9w0BAQEF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GP8TtpvC4Qc3/vLt2l7g/WHAzp\n"
"0TPiMGECgYEA9fHMBVIkTUzpKnzoF3jhqNFoqJqXgul32e7ej8Ov5b06wId1WLA/\n"
"/ZmgucPLAL1DDVyJMotNqeQPWt3x9++nTpnDHavGQ96y7SEI2zHuLMuhoR9c1zfA\n"
"MQicGfk25dhXWtMxFilFAkBbwCcLEq1XHcMI6ek4zKYlJ8tCHC4BmtcCgYEAyuZ0\n"
"91CBLcBuBrwpnxdXtOurwRDoOgXpJxy8bD9ctltcPFeilBUWuLYZ+gyBozli23Uh\n"
"KuDlKMZJw+CHztiEJwuzzzWOOxejs2qtBfSHM7Hne37Qpb/qcVfgn5C6fj7LeIgG\n"
"hUdK2QN2jEx+AWahFFFJ6Z2cmQBXXBGOW4LGZF0CgYA9+/ukV6hohvq4x5Qi3kdZ\n"
"KbXL0HJg/wBCv639457AMunMvhb4DCuEeaSFTPArtodgpbK6N1uSdrTb/NXP2+l5\n"
"qM0A/FrSnhzQIKQ/why503RfzCy03QsmEHpvHV0VnmmdrV5QrIQE5j15dx2WTnOH\n"
"P7FOaoXzJeh1WAfIXFvxLwKBgQC2XvIfIVMa5m1+zD2062xAB9w3CpVRIeLw7tlF\n"
"iqYwmmmLK1HMPDBSEgvDPt5+8aOzkdIgEkinn6LJ1tT6zI3r8o7J3l9bKeJP78BZ\n"
"K/MiOfPQgqnTcW6uNciGY7Xcp2CHk+wYe34BFSXG8TII3FBITNBclPgeZbof3P/R\n"
"rPfZWQKBgEbk1OsuDxUbcfgeMT0f1cBiJxtkRsj3Kffm6c2PAzGZg8A/EgoR25Cr\n"
"v+u4s5aS/3cBAfJhu9OAqoXdYJlXfYsoKuyOyvzeyOcsAsKjd6tIiU6xUe/u65oZ\n"
"1KXWzXoYgnNW3eoU7YeQ0Ca4SSg3qG2jqGFIAClla+hyLUeEgXB1\n"
"-----END RSA PRIVATE KEY-----\n";

static int pass_cb(char* buf, int size, int rwflag, void* u)
{
    int len = strlen("hello");
    // if too long, truncate
    if (len > size)
        len = size;
    strncpy(buf, "hello", len);
    return len;
}

int do_evp_seal(FILE *in_file, FILE *out_file)
{
    int retval = 0;
    RSA *rsa_pkey = NULL;
    EVP_PKEY *pkey = EVP_PKEY_new();
    EVP_CIPHER_CTX ctx;
    unsigned char buffer[4096];
    unsigned char buffer_out[4096 + EVP_MAX_IV_LENGTH];
    size_t len;
    int len_out;
    unsigned char *ek;
    int eklen;
    uint32_t eklen_n;
    unsigned char iv[EVP_MAX_IV_LENGTH];

    OpenSSL_add_all_algorithms();
    BIO* pBio = BIO_new_mem_buf(reinterpret_cast<void*>(PRIVATE_KEY_PEM), -1);
    //BIO* pBio = BIO_new_mem_buf(reinterpret_cast<void*>(PUBLIC_KEY_PEM), -1);
    //if (!PEM_read_bio_RSA_PUBKEY(pBio, &rsa_pkey, NULL, NULL))
    if (!PEM_read_bio_RSAPrivateKey(pBio, &rsa_pkey, pass_cb, NULL))
    //if (!PEM_read_bio_RSAPrivateKey(pBio, &rsa_pkey, NULL, NULL))
    {
        fprintf(stderr, "Error loading RSA Public Key File.\n");
        ERR_print_errors_fp(stderr);
        retval = 2;
        goto out;
    }

    if (!EVP_PKEY_assign_RSA(pkey, rsa_pkey))
    {
        fprintf(stderr, "EVP_PKEY_assign_RSA: failed.\n");
        retval = 3;
        goto out;
    }

    EVP_CIPHER_CTX_init(&ctx);
    ek = reinterpret_cast<unsigned char*>(malloc(EVP_PKEY_size(pkey)));

    if (!EVP_SealInit(&ctx, EVP_aes_256_cbc(), &ek, &eklen, iv, &pkey, 1))
    {
        fprintf(stderr, "EVP_SealInit: failed.\n");
        retval = 3;
        goto out_free;
    }

    /* First we write out the encrypted key length, then the encrypted key,
     * then the iv (the IV length is fixed by the cipher we have chosen).
     */

    eklen_n = htonl(eklen);
    if (fwrite(&eklen_n, sizeof eklen_n, 1, out_file) != 1)
    {
        perror("output file");
        retval = 5;
        goto out_free;
    }
    if (fwrite(ek, eklen, 1, out_file) != 1)
    {
        perror("output file");
        retval = 5;
        goto out_free;
    }
    if (fwrite(iv, EVP_CIPHER_iv_length(EVP_aes_256_cbc()), 1, out_file) != 1)
    {
        perror("output file");
        retval = 5;
        goto out_free;
    }

    /* Now we process the input file and write the encrypted data to the
     * output file. */

    while ((len = fread(buffer, 1, sizeof buffer, in_file)) > 0)
    {
        if (!EVP_SealUpdate(&ctx, buffer_out, &len_out, buffer, len))
        {
            fprintf(stderr, "EVP_SealUpdate: failed.\n");
            retval = 3;
            goto out_free;
        }

        if (fwrite(buffer_out, len_out, 1, out_file) != 1)
        {
            perror("output file");
            retval = 5;
            goto out_free;
        }
    }

    if (ferror(in_file))
    {
        perror("input file");
        retval = 4;
        goto out_free;
    }

    if (!EVP_SealFinal(&ctx, buffer_out, &len_out))
    {
        fprintf(stderr, "EVP_SealFinal: failed.\n");
        retval = 3;
        goto out_free;
    }

    if (fwrite(buffer_out, len_out, 1, out_file) != 1)
    {
        perror("output file");
        retval = 5;
        goto out_free;
    }

    out_free:
    EVP_PKEY_free(pkey);
    free(ek);

    out:
    return retval;
}

int main()
{
    return do_evp_seal(stdin, stdout);
}


Code:
#include <stdio.h>
#include <stdlib.h>

#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/err.h>

#include <arpa/inet.h>

char* PUBLIC_KEY_PEM =
"-----BEGIN PUBLIC KEY-----\n"
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCvdbGZes3N/v3EqbbwYHW4rr4W\n"
"gav9eD36kVD7Hn5LIKwhfAqd1+LSN2YC49IsIhzwbXqoMUN89UZmFEdY4/M4qW1+\n"
"qwOfaX80EKCl8zV5Ztp8fvoErS1Z/G0k0c0kRdP0BCZTI9nSfjZkhbqWKnJrtCgi\n"
"s6OXXTeanU9B9Bz3HQIDAQAB\n"
"-----END PUBLIC KEY-----\n";

char* PRIVATE_KEY_PEM =
"-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
"MIIFHzBJBgkqhkiG9w0BBQ0wPDA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M0A/FrSnhzQIKQ/why503RfzCy03QsmEHpvHV0VnmmdrV5QrIQE5j15dx2WTnOH\n"
"P7FOaoXzJeh1WAfIXFvxLwKBgQC2XvIfIVMa5m1+zD2062xAB9w3CpVRIeLw7tlF\n"
"iqYwmmmLK1HMPDBSEgvDPt5+8aOzkdIgEkinn6LJ1tT6zI3r8o7J3l9bKeJP78BZ\n"
"K/MiOfPQgqnTcW6uNciGY7Xcp2CHk+wYe34BFSXG8TII3FBITNBclPgeZbof3P/R\n"
"rPfZWQKBgEbk1OsuDxUbcfgeMT0f1cBiJxtkRsj3Kffm6c2PAzGZg8A/EgoR25Cr\n"
"v+u4s5aS/3cBAfJhu9OAqoXdYJlXfYsoKuyOyvzeyOcsAsKjd6tIiU6xUe/u65oZ\n"
"1KXWzXoYgnNW3eoU7YeQ0Ca4SSg3qG2jqGFIAClla+hyLUeEgXB1\n"
"-----END RSA PRIVATE KEY-----\n";

int do_evp_seal(FILE *in_file, FILE *out_file)
{
    int retval = 0;
    RSA *rsa_pkey = NULL;
    EVP_PKEY *pkey = EVP_PKEY_new();
    EVP_CIPHER_CTX ctx;
    unsigned char buffer[4096];
    unsigned char buffer_out[4096 + EVP_MAX_IV_LENGTH];
    size_t len;
    int len_out;
    unsigned char *ek;
    int eklen;
    uint32_t eklen_n;
    unsigned char iv[EVP_MAX_IV_LENGTH];

    OpenSSL_add_all_algorithms();
    BIO* pBio = BIO_new_mem_buf(reinterpret_cast<void*>(PRIVATE_KEY_PEM), -1);
    if (!PEM_read_bio_RSAPrivateKey(pBio, &rsa_pkey, NULL, NULL))
    {
        fprintf(stderr, "Error loading RSA Public Key File.\n");
        ERR_print_errors_fp(stderr);
        retval = 2;
        goto out;
    }

    if (!EVP_PKEY_assign_RSA(pkey, rsa_pkey))
    {
        fprintf(stderr, "EVP_PKEY_assign_RSA: failed.\n");
        retval = 3;
        goto out;
    }

    /* First we write out the encrypted key length, then the encrypted key,
     * then the iv (the IV length is fixed by the cipher we have chosen).
     */

    if (fread(&eklen_n, sizeof eklen_n, 1, in_file) != 1)
    {
        perror("input file");
        retval = 5;
        goto out_free;
    }
    eklen = ntohl(eklen_n);
    //eklen = 128;
    //ek = reinterpret_cast<unsigned char*>(malloc(EVP_PKEY_size(pkey)));
    //ek = reinterpret_cast<unsigned char*>(malloc(eklen));
    ek = new unsigned char[eklen];
    if (fread(ek, eklen, 1, in_file) != 1)
    {
        perror("input file");
        retval = 5;
        goto out_free;
    }
    if (fread(iv, EVP_CIPHER_iv_length(EVP_aes_256_cbc()), 1, in_file) != 1)
    {
        perror("input file");
        retval = 5;
        goto out_free;
    }

    EVP_CIPHER_CTX_init(&ctx);

    if (!EVP_OpenInit(&ctx, EVP_aes_256_cbc(), ek, eklen, iv, pkey))
    {
        fprintf(stderr, "EVP_OpenInit: failed.\n");
        retval = 3;
        goto out_free;
    }
    printf("%d\n", eklen);

    /* Now we process the input file and write the encrypted data to the
     * output file. */

    while ((len = fread(buffer, 1, sizeof buffer, in_file)) > 0)
    {
        if (!EVP_OpenUpdate(&ctx, buffer_out, &len_out, buffer, len))
        {
            fprintf(stderr, "EVP_OpenUpdate: failed.\n");
            retval = 3;
            goto out_free;
        }

        if (fwrite(buffer_out, len_out, 1, out_file) != 1)
        {
            perror("output file");
            retval = 5;
            goto out_free;
        }
    }

    if (ferror(in_file))
    {
        perror("input file");
        retval = 4;
        goto out_free;
    }

    if (!EVP_OpenFinal(&ctx, buffer_out, &len_out))
    {
        fprintf(stderr, "EVP_OpenFinal: failed.\n");
        retval = 3;
        goto out_free;
    }

    if (fwrite(buffer_out, len_out, 1, out_file) != 1)
    {
        perror("output file");
        retval = 5;
        goto out_free;
    }

    out_free:
    EVP_PKEY_free(pkey);
    free(ek);

    out:
    return retval;
}

int main(int argc, char *argv[])
{
    return do_evp_seal(stdin, stdout);
}



Not sure if that code is useful now, but dumping it anyway...

g++ a.cpp -lssl -o enctest
./enctest
g++ b.cpp -lssl -o seal
./seal < INPUT > ENCOUT
g++ c.cpp -lssl -o unseal
./unseal < ENCOUT
1462  Bitcoin / Project Development / Intersango exchange (formerly Britcoin) on: March 26, 2011, 09:13:32 PM
20th September 2011: Britcoin has now migrated to Intersango, our new platform that has been in development for the last 3 months

https://intersango.com/

For more information about the people behind the exchange, visit:

Company website: http://bitcoinconsultancy.com/
Personal biographies: https://intersango.com/about-us.php

For now it's being run very cautiously. Therefore I'm personally authorising all payments and transactions until I'm confident to switch on automation.

http://britcoin.co.uk/

You can find our sourcecode on Gitorious.


1463  Economy / Marketplace / Re: Not enough interest for a UK-based BitCoin exchange? on: March 26, 2011, 09:10:49 PM
We are now open http://britcoin.co.uk/
1464  Bitcoin / Project Development / Re: Quickcoin [Current Bounty: Derp] on: March 26, 2011, 05:06:06 PM
You owe theymos 20 BTC.
It's not compiled. : P

I don't understand this.

He did everything you asked for in the OP.

Him not compiling his changes (which is the easiest part) suddenly invalidates his bounty? Reminds me of a friend whose landlord refused to pay him his deposit on leaving because my friend hadn't hovered the floor.

You were asking for a task which takes a programmer 15 mins of his time + 10 years of experience. He did that and crunched the numbers for you. Then you came out with an excuse not to pay up.
1465  Economy / Marketplace / Re: Code to convert any PGP key (public AND private) into OpenSSL format [60 BTC] on: March 26, 2011, 08:24:57 AM
There's actually no easy way to do this. You can call a program called gpgsm using:

Quote
gpgsm -o  secret-gpg-key.p12 --export-secret-key-p12 0xXXXXXXXX
openssl pkcs12 -in secret-gpg-key.p12 -nocerts -out gpg-key.pem
openssl pkcs12 -in secret-gpg-key.p12 -nokeys -out gpg-certs.pem

If you insist, then you should look at the gpgsm sourcecode as it's a specially built tool for the job.

The last 2 lines can be done in C++ easily. To load RSA keys you can do:
Quote
static int pass_cb(char* buf, int size, int rwflag, void* u)
{
    const string pass = reinterpret_cast<char*>(u);
    int len = pass.size();
    // if too long, truncate
    if (len > size)
        len = size;
    pass.copy(buf, len);
    return len;
}
RSA* LoadPrivateKey(const string& pem, const string& pass)
{
    BIO_free(bio);
    BIO* bio = BIO_new_mem_buf(StringAsVoid(str), -1);
    if (!bio)
        throw ReadError();
    RSA* keypair = PEM_read_bio_RSAPrivateKey(bio, NULL, pass_cb,
                                         StringAsVoid(pass));
    if (!keypair)
        throw NoKeypairLoaded();
    return keypair;
}
If it's a public key then use PEM_read_bio_PKCS7 instead. The above function should recognise it's PKCS.

Maybe sometimes you'll need an EVP object:
Quote
class EvpBox
{
public:
    EvpBox(RSA* keyp);
    ~EvpBox();
    EVP_PKEY* Key();
private:
    EVP_PKEY* evpkey;
};

...

EvpBox::EvpBox(RSA* keyp)
{
    evpkey = EVP_PKEY_new();
    if (!EVP_PKEY_set1_RSA(evpkey, keyp)) {
        throw ReadError();
    }
}
EvpBox::~EvpBox()
{
    EVP_PKEY_free(evpkey);
}
EVP_PKEY* EvpBox::Key()
{
    return evpkey;
}

1HmskBCwhnV8DRvYdpBmDyyTwGc28otPeZ
1466  Bitcoin / Bitcoin Discussion / Re: Governments and Bitcoin on: March 26, 2011, 08:00:53 AM
Not everyone thinks the government here is evil. I genuinely believe there's many individuals who believe they can do/are doing good for people. However the entire system has evolved into a corrupted organism. It's not an evil conspiracy but a 'stand alone complex'.
Quote
Stand Alone Complex eventually came to represent a phenomenon where unrelated, yet very similar actions of individuals create a seemingly concerted effort.
Many of these government institutions once instituted can never disappear. A cancerous growth that drains it's host. War budgets will only ever get larger.

Quote
Still, even as I contest the idea that anarchy is sustainable, I can accept that it is possible; and perhaps one day technologies that we can't even imagine today will make a stable anarchy a reality.  That could only come if technologies make the primary functions of governments obsolete.  But as we have all witnessed over the past 20 years, we can't really fathom what may yet be.
People are socially evolving and changing as a species. New attitudes are taking the place of old attitudes. Economies are shifting to non zero-sum game, service oriented. We may see the first people on Mars this century. In a creative economy, one sees neighbours as team mates rather than competitors because the sum of intelligence of the whole > the few. Because a country has more to gain by pooling intelligence with others than by warring it's inhabitants.

Quote
Regarding imaginary lines on a map--that I can get behind.  Bitcoin and other borderless realities are the way of the future!
Whether one likes it or not. The leaderless future is inevitable. Extrapolate the ongoing trend and it's clear where everything is headed. Technology has helped massively.
1467  Other / Off-topic / Re: Witcoin strategy on: March 25, 2011, 06:04:11 PM
pi is wrong.

http://tauday.com

de moivre is meaningless with the incorrect constant.
1468  Economy / Trading Discussion / Re: decentralising currency exchange on: March 25, 2011, 01:56:19 PM
My soon to be opened exchange has free source code.

http://gitorious.org/intersango/master/trees/master

Federation won't happen because I don't trust your exchange.
1469  Other / Off-topic / Re: Move along.. on: March 25, 2011, 11:48:16 AM
Yeah, OK. Maybe I'm a bit harsh. These things don't interest me too much usually. Kind of make me want to barf (like romantic movies).
1470  Other / Off-topic / Re: Move along.. on: March 25, 2011, 11:14:47 AM
blaa blaa the video sucks. Cue dramatic music & people waffling on without making sense.

I can make a video of the most boring topic and overlay this sad music and it'd have the same effect.

Not insightful or interesting. Typical responses "Being alive makez me happy! Like being here. Like enjoying miself.". derp. jesus, some of those people sound like cartoon characters. Old Betty Boo & Beanhead Jon pondering questions about life & politics.

wooahh so deep maaan. Go watch some Carl Sagan for a true hero & the greatest man that ever lived!
http://www.youtube.com/results?search_query=carl+sagan&aq=f

http://www.youtube.com/watch?v=wupToqz1e2g
http://www.youtube.com/watch?v=wupToqz1e2g
http://www.youtube.com/watch?v=wupToqz1e2g
http://www.youtube.com/watch?v=wupToqz1e2g
http://www.youtube.com/watch?v=wupToqz1e2g
http://www.youtube.com/watch?v=wupToqz1e2g
http://www.youtube.com/watch?v=wupToqz1e2g
http://www.youtube.com/watch?v=wupToqz1e2g
http://www.youtube.com/watch?v=wupToqz1e2g
http://www.youtube.com/watch?v=wupToqz1e2g
http://www.youtube.com/watch?v=wupToqz1e2g
http://www.youtube.com/watch?v=wupToqz1e2g
http://www.youtube.com/watch?v=wupToqz1e2g
http://www.youtube.com/watch?v=wupToqz1e2g
http://www.youtube.com/watch?v=wupToqz1e2g
http://www.youtube.com/watch?v=wupToqz1e2g
http://www.youtube.com/watch?v=wupToqz1e2g
http://www.youtube.com/watch?v=wupToqz1e2g
http://www.youtube.com/watch?v=wupToqz1e2g
http://www.youtube.com/watch?v=wupToqz1e2g
http://www.youtube.com/watch?v=wupToqz1e2g
http://www.youtube.com/watch?v=wupToqz1e2g
http://www.youtube.com/watch?v=wupToqz1e2g
http://www.youtube.com/watch?v=wupToqz1e2g
1471  Bitcoin / Bitcoin Discussion / Re: BitCoin Explanation Practice (Skype?) on: March 25, 2011, 10:18:10 AM
You might find this interesting,

http://en.wikipedia.org/wiki/User:Genjix/Bitcoin
1472  Bitcoin / Development & Technical Discussion / Re: Lets bring money into Bitcoin & find new ways of organising free software on: March 25, 2011, 08:24:03 AM
Deadlines sound cool. How would that work in practice? 1 year before items expire?

I'd prefer all proposals have the same expiry date (if they do) because considering all the different expiry dates for someone who takes on that proposal is confusing.
1473  Bitcoin / Bitcoin Discussion / Re: How Can I Help? on: March 25, 2011, 05:31:10 AM
edit the wiki, write articles.

spread it on other forums.

come up with new ideas for websites.

help noobs out
1474  Bitcoin / Project Development / Re: Quickcoin [Current Bounty: Derp] on: March 24, 2011, 01:17:42 PM
You owe theymos 20 BTC.
1475  Bitcoin / Bitcoin Discussion / Re: Socialcoin on: March 24, 2011, 12:50:42 PM
You have a picture of every single person I hate. Well done.

Also, Balance = trollface
1476  Bitcoin / Bitcoin Discussion / Re: Noob guide? on: March 24, 2011, 12:29:30 PM
> been searching ages he says

> Front page -> Help -> Getting started

https://en.bitcoin.it/wiki/Getting_started
1477  Other / Off-topic / Re: Do you like profit? on: March 24, 2011, 10:16:32 AM
I am a profit.
1478  Other / Off-topic / Re: Do you like profit? on: March 24, 2011, 09:26:48 AM
Some of you on here are rand zealots. Just because you think one way, doesn't mean everybody thinks the same. If you could find one motivator for human actions then you would win a nobel prize and be hailed the world over as famous.

It's like some of you are consigned to being selfish bastards so to ease that cognitive dissonance you assume everybody is the same and make these contrived arguments about how it's done to "make yourself feel good" or some other bullshit reason.

- Why do people vote? It's -EV
- Why do people make anonymous posts on 4chan? I sometimes make posts there, then instantly leave the thread.
- Or help people out on IRC? Doesn't feel good at all, but I recognise that people help me out a lot and I have an obligation to do the same. Even when I join new channels + with unknown users I've never been to before.

Not everyone is blind profit maximisers. Even the evolutionary science says that humans are altruists to some degree as a result of group level selection (altruist groups are more likely to survive in the wild and therefore are selected for).

A better theory for human behaviour (incidentally that is accepted by mainstream psychology) is to say that each person has a self image. All actions we take are to try and stay congruent with this self image to minimise cognitive dissonance. If you're a cutthroat salesman ripping off customers with junk, then you're going to reason that you're doing nothing wrong in this competitive world. If you're religious, you might reason that all non-religious people are deliberately denying God/trying to ignore him. If you're atheist, you reason that religious people are in self-denial. If you're an artist, you may reason that everything is about feeling. A scientist reasons that it's about hard facts & evidence.

Here's an email snippet for your amusement, showing at least one example (me) that not everyone is profit oriented and can in fact have other goals in life (horror!)
Quote
Quote
We're still waiting on legal before we can dive into the BTC exchange. Have
you got the site up and running?

Been trying to get an SSL certificate. I've been trying to get one, but the
registrars don't seem to be working.

Quote
Aside from that I was wondering if you'd want to come here to work on a
poker bot with me. You'd have to sign non-disclosures and nothing would be
open sourced. If you're interested what would all your requirements be
financially and otherwise? We'd provide housing and you could continue
working on the BTC exchange among other Bitcoin related projects. Another
perk is MTT and Cash game backing if you want it.

Lost me at 'not open'. My interest is freedom + technology, not money.
1479  Bitcoin / Development & Technical Discussion / Lets bring money into Bitcoin & find new ways of organising free software on: March 24, 2011, 09:00:25 AM
Hey,

I coded up http://bitcoin.cz.cc as an experiment but it mostly seems to have been ignored?

Quote
By putting bounties on each bug fix and feature, Gavin and the other bitcoin developers will be able to tell which features and bug fixes are most urgent. Each part of the project has it's own price! This pricing mechanism for FOSS projects is a radical improvement over the model of putting arbitrary priorities on bugs and features.

Why not add this to your signature if you're a dev?
##############
"Vote up your favourite ideas to go into Bitcoin"
##############

It's a small thing, but it goes a long way to us gathering data/finding out how to do things.

---------------------

You propose features for Bitcoin. The front page shows a mix of the most donated proposals (10) and newest ones (5). Once the feature is implemented in Bitcoin then the bounty goes to the author and the proposal is deleted.

Think of it as an experiment into future methods for bitcoin based free software dev. Right now I'm just putting it out there to see what happens. If it grows then we can think about turning it into a bug tracker type thing with tickets, comments, statuses, assignment of tickets and search.
1480  Bitcoin / Bitcoin Discussion / Re: Remove "generate bitcoins" from standard client? on: March 24, 2011, 07:15:31 AM
yeah I left it on a few times before (running on CPU) for battle hardening the network. Like seeding torrents to give to charity.
Pages: « 1 ... 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 [74] 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 »
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!