예제 #1
0
파일: tx.go 프로젝트: airdispatch/zooko-go
func (r *Server) Confirmations(tx string) (int, error) {
	cmd, err := btcjson.NewGetRawTransactionCmd(nil, tx, 1)
	if err != nil {
		return -1, err
	}

	result, err := r.Send(cmd)
	if result.Error != nil {
		return -1, *result.Error
	}

	txResult := result.Result.(*btcjson.TxRawResult)

	return int(txResult.Confirmations), nil
}
예제 #2
0
func (server *ZookoServer) CreateTransactionListForName(name string, excludeExpired bool) ([]message.NamecoinTransaction, error) {
	nhistory, err := btcjson.NewNameHistoryCmd(nil, name)
	if err != nil {
		return nil, err
	}

	reply, err := server.Send(nhistory)
	if err != nil {
		return nil, err
	}

	result := reply.Result.([]btcjson.NameInfoResult)

	// Go through each history and extract the transaction information.
	var transactions []message.NamecoinTransaction
	for _, entry := range result {
		// Get the Raw Transaction Associated with the Name Operation
		getRaw, err := btcjson.NewGetRawTransactionCmd(nil, entry.TX, 1)
		if err != nil {
			return nil, err
		}

		reply, err := server.Send(getRaw)
		if err != nil {
			return nil, err
		}

		transactionResult := reply.Result.(*btcjson.TxRawResult)
		// fmt.Println("Found entry at block", transactionResult.BlockHash, transactionResult.Confirmations)

		// Get the Block Associated with the Name Operation
		getBlock, err := btcjson.NewGetBlockCmd(nil, transactionResult.BlockHash)
		if err != nil {
			return nil, err
		}

		reply, err = server.Send(getBlock)
		if err != nil {
			return nil, err
		}

		block := reply.Result.(*btcjson.BlockResult)
		fmt.Println("Block has", len(block.Tx), "transactions.")

		// Create the Merkle Branch
		branch, err := constructMerkleTree(block.Tx, block.MerkleRoot, transactionResult.Txid)
		if err != nil {
			return nil, err
		}

		root, _ := wire.NewShaHashFromStr(block.MerkleRoot)
		txid, _ := wire.NewShaHashFromStr(transactionResult.Txid)
		if _, err = proof.VerifyMerkleBranch(branch, root, txid); err != nil {
			return nil, errors.New("unable to verify new merkle branch")
		}

		bytesTxResult, err := json.Marshal(transactionResult)
		if err != nil {
			return nil, err
		}

		var outHashes [][]byte
		for _, v := range branch.BranchHash {
			outHashes = append(outHashes, v.Bytes())
		}

		transactions = append(transactions, message.NamecoinTransaction{
			TxId:               transactionResult.Txid,
			Branch:             branch.BranchSideMask,
			VerificationHashes: outHashes,
			Raw:                bytesTxResult,
			BlockId:            transactionResult.BlockHash,
		})
	}

	return transactions, nil
}
예제 #3
0
파일: tx.go 프로젝트: airdispatch/zooko-go
// Endow will transfer money for a single Namecoin name registration
// (for 12 months) to a specified address. It will return the
// transaction details so that light clients can keep track of their
// UTXO.
func (r *Server) Endow(address string) (*account.UTXO, error) {
	// Send money to the address to endow
	sendCmd, err := btcjson.NewSendToAddressCmd(nil, address, endowmentAmount)
	if err != nil {
		return nil, err
	}

	reply, err := r.Send(sendCmd)
	if err != nil {
		return nil, err
	}

	if reply.Result == nil {
		return nil, errNilReply
	} else if reply.Error != nil {
		// Error from the namecoin daemon. This generally
		// indicates an internal server issue.
		return nil, *reply.Error
	}

	txid := reply.Result.(string)

	// Get information about the transaction that we just sent
	rawTxCmd, err := btcjson.NewGetRawTransactionCmd(nil, txid, 1)
	if err != nil {
		return nil, err
	}

	reply, err = r.Send(rawTxCmd)
	if err != nil {
		return nil, err
	}

	if reply.Result == nil {
		return nil, errNilReply
	} else if reply.Error != nil {
		// Error from the namecoin daemon. This generally
		// indicates an internal server issue.
		return nil, *reply.Error
	}

	rawTx := reply.Result.(*btcjson.TxRawResult)

	outputId := 0
	pkScript := ""
vOutLoop:
	for index, out := range rawTx.Vout {
		for _, addr := range out.ScriptPubKey.Addresses {
			if addr == address {
				outputId = index
				pkScript = out.ScriptPubKey.Hex
				break vOutLoop
			}
		}
	}

	pkData, err := hex.DecodeString(pkScript)
	if err != nil {
		return nil, err
	}

	return &account.UTXO{
		TxID:     txid,
		Output:   uint32(outputId),
		Amount:   endowmentAmount,
		PkScript: pkData,
	}, nil
}