From 8cb1c5e1dd4193195981e4b169a4099ec990f44e Mon Sep 17 00:00:00 2001 From: cevin Date: Thu, 18 Jan 2024 00:23:23 +0800 Subject: [PATCH] feat: Modify the HTTP API amount parameter to the smallest unit of bitcoin. Prevent loss of precision. --- README.md | 8 ++++---- README_zh.md | 10 +++++----- utils/btc/btc.go | 11 ++--------- utils/btc/struct.go | 4 ++-- web/req.go | 16 ++++++++-------- 5 files changed, 21 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 427354e..22438d1 100644 --- a/README.md +++ b/README.md @@ -236,7 +236,7 @@ POST /transaction/create "pay_to_addresses": [ { "address": "any bitcoin address", - "amount": float + "amount": int (satoshi) } ] } @@ -269,7 +269,7 @@ POST /transaction/sign "wif": "WIF private key", "redeem-script": "multisig redeem script", "segwit": bool, - "amount": float64 (optional, when segwit transaction required) + "amount": int (satoshi) (optional, when segwit transaction required) } ] } @@ -297,13 +297,13 @@ Response "wif": "WIF private key", "redeem-script": "multisig redeem script", "segwit": bool, - "amount": float64 (optional, when segwit transaction required) + "amount": int (satoshi) (optional, when segwit transaction required) } ], "pay_to_addresses": [ { "address": "any Bitcoin address", - "amount": float + "amount": int (satoshi) } ] } diff --git a/README_zh.md b/README_zh.md index 0836e57..e2cbbbc 100644 --- a/README_zh.md +++ b/README_zh.md @@ -226,7 +226,7 @@ tx=0100000001fe75a438b72fdc302b80cc216d66d5e3bbb0359bce3bb4cecf743f5fda1f4eb1010 > > 如:地址A有1BTC,想发送0.1给地址B,想要支付0.0000001BTC作为交易手续费,则pay_to_addresses可能为: > -> [{"address":地址B, "amount":0.1}, {"address":地址A, "amount":0.8999999] +> [{"address":地址B, "amount":10000000}, {"address":地址A, "amount":99999990] ```text POST /transaction/create @@ -242,7 +242,7 @@ POST /transaction/create "pay_to_addresses": [ { "address": "任意正确的比特币地址", - "amount": float,最大支持精确到小数点后8位 + "amount": int satoshi,比特币最小单位 } ] } @@ -275,7 +275,7 @@ POST /transaction/sign "wif": "WIF格式的私钥", "redeem-script": "多重签名地址的兑付脚本(redeem script)16进制编码", "segwit": bool 是否来自bech32格式的地址的交易 - "amount": float64 (可选 segwit 时必须) + "amount": int satoshi,比特币最小单位 (可选 segwit 时必须) } ] } @@ -303,13 +303,13 @@ POST /transaction/sign "wif": "WIF格式的私钥", "redeem-script": "多重签名地址的兑付脚本(redeem script)16进制编码", "segwit": bool 是否来自bech32格式的地址的交易, - "amount": float64 (可选 segwit 时必须) + "amount": int satoshi,比特币最小单位 (可选 segwit 时必须) } ], "pay_to_addresses": [ { "address": "任意正确的比特币地址", - "amount": float,最大支持精确到小数点后8位 + "amount": int satoshi,比特币最小单位,最大支持精确到小数点后8位 } ] } diff --git a/utils/btc/btc.go b/utils/btc/btc.go index 287690d..fb372d1 100644 --- a/utils/btc/btc.go +++ b/utils/btc/btc.go @@ -205,11 +205,7 @@ func CreateRawTransaction(inputs Inputs, outputs Outputs) (*wire.MsgTx, error) { } for _, output := range outputs { - - sendAmount, err := btcutil.NewAmount(output.Amount) - if err != nil { - return nil, err - } + sendAmount := output.Amount address, err := btcutil.DecodeAddress(output.PayToAddress, mnet) if err != nil { return nil, err @@ -339,10 +335,7 @@ func SignRawTxTransaction(tx *wire.MsgTx, inputs Inputs) (string, error) { return "", fmt.Errorf("calc pkScript failed for %s", txId) } - amount, err := btcutil.NewAmount(signUseInput.input.Amount) - if err != nil { - return "", fmt.Errorf(errTmpl, idx, "invalid amount, "+err.Error()) - } + amount := signUseInput.input.Amount signUseInputsMap[txId] = signUseInput prevOutFetcher.AddPrevOut( diff --git a/utils/btc/struct.go b/utils/btc/struct.go index c6901d9..c98e6ff 100644 --- a/utils/btc/struct.go +++ b/utils/btc/struct.go @@ -45,12 +45,12 @@ type Input struct { WIF string RedeemScript string SegWit bool - Amount float64 + Amount int64 } type Output struct { PayToAddress string - Amount float64 + Amount int64 } type Inputs []Input diff --git a/web/req.go b/web/req.go index b0c411b..51d9d58 100644 --- a/web/req.go +++ b/web/req.go @@ -22,14 +22,14 @@ type createAndSignTransactionReq struct { PayToAddresses []payToAddress `json:"pay_to_addresses"` } type txin struct { - TxId string `json:"txid"` - VOut *int `json:"vout,omitempty,-"` - WIF string `json:"wif,omitempty,-"` - RedeemScript string `json:"redeem-script,omitempty,-"` - SegWit bool `json:"segwit,omitempty,-"` - Amount float64 `json:"amount,omitempty,-"` + TxId string `json:"txid"` + VOut *int `json:"vout,omitempty,-"` + WIF string `json:"wif,omitempty,-"` + RedeemScript string `json:"redeem-script,omitempty,-"` + SegWit bool `json:"segwit,omitempty,-"` + Amount int64 `json:"amount,omitempty,-"` } type payToAddress struct { - Address string `json:"address"` - Amount float64 `json:"amount"` + Address string `json:"address"` + Amount int64 `json:"amount"` }