Skip to content

Commit

Permalink
Merge pull request #161 from renproject/feat/utxo-tx-sender
Browse files Browse the repository at this point in the history
Add new function to return the tx sender addresses
  • Loading branch information
tok-kkk authored Sep 20, 2022
2 parents 55dbfca + fe86cbf commit 84308da
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 34 deletions.
66 changes: 33 additions & 33 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ jobs:
FILECOIN_FFI_COMMIT: 7912389334e347bbb2eac0520c836830875c39de
SOLANA_FFI_COMMIT: 720c0143d8655bfcd412ae73c949474df2c1dcf3
steps:
- name: Set up Go 1.16
uses: actions/setup-go@v1
- name: Set up Go 1.17
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: "1.17.13"

- name: Configure git for Private Modules
env:
Expand Down Expand Up @@ -121,10 +121,10 @@ jobs:
FILECOIN_FFI_COMMIT: 7912389334e347bbb2eac0520c836830875c39de
SOLANA_FFI_COMMIT: 720c0143d8655bfcd412ae73c949474df2c1dcf3
steps:
- name: Set up Go 1.16
uses: actions/setup-go@v1
- name: Set up Go 1.17
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: "1.17.13"
id: go

- name: Configure git for Private Modules
Expand Down Expand Up @@ -247,10 +247,10 @@ jobs:
FILECOIN_FFI_COMMIT: 7912389334e347bbb2eac0520c836830875c39de
SOLANA_FFI_COMMIT: 720c0143d8655bfcd412ae73c949474df2c1dcf3
steps:
- name: Set up Go 1.16
uses: actions/setup-go@v1
- name: Set up Go 1.17
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: "1.17.13"
id: go

- name: Configure git for Private Modules
Expand Down Expand Up @@ -372,10 +372,10 @@ jobs:
FILECOIN_FFI_COMMIT: 7912389334e347bbb2eac0520c836830875c39de
SOLANA_FFI_COMMIT: 720c0143d8655bfcd412ae73c949474df2c1dcf3
steps:
- name: Set up Go 1.16
uses: actions/setup-go@v1
- name: Set up Go 1.17
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: "1.17.13"
id: go

- name: Configure git for Private Modules
Expand Down Expand Up @@ -497,10 +497,10 @@ jobs:
FILECOIN_FFI_COMMIT: 7912389334e347bbb2eac0520c836830875c39de
SOLANA_FFI_COMMIT: 720c0143d8655bfcd412ae73c949474df2c1dcf3
steps:
- name: Set up Go 1.16
uses: actions/setup-go@v1
- name: Set up Go 1.17
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: "1.17.13"
id: go

- name: Configure git for Private Modules
Expand Down Expand Up @@ -622,10 +622,10 @@ jobs:
FILECOIN_FFI_COMMIT: 7912389334e347bbb2eac0520c836830875c39de
SOLANA_FFI_COMMIT: 720c0143d8655bfcd412ae73c949474df2c1dcf3
steps:
- name: Set up Go 1.16
uses: actions/setup-go@v1
- name: Set up Go 1.17
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: "1.17.13"
id: go

- name: Configure git for Private Modules
Expand Down Expand Up @@ -749,10 +749,10 @@ jobs:
FILECOIN_FFI_COMMIT: 7912389334e347bbb2eac0520c836830875c39de
SOLANA_FFI_COMMIT: 720c0143d8655bfcd412ae73c949474df2c1dcf3
steps:
- name: Set up Go 1.16
uses: actions/setup-go@v1
- name: Set up Go 1.17
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: "1.17.13"
id: go

- name: Configure git for Private Modules
Expand Down Expand Up @@ -873,10 +873,10 @@ jobs:
FILECOIN_FFI_COMMIT: 7912389334e347bbb2eac0520c836830875c39de
SOLANA_FFI_COMMIT: 720c0143d8655bfcd412ae73c949474df2c1dcf3
steps:
- name: Set up Go 1.16
uses: actions/setup-go@v1
- name: Set up Go 1.17
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: "1.17.13"
id: go

- name: Configure git for Private Modules
Expand Down Expand Up @@ -997,10 +997,10 @@ jobs:
FILECOIN_FFI_COMMIT: 7912389334e347bbb2eac0520c836830875c39de
SOLANA_FFI_COMMIT: 720c0143d8655bfcd412ae73c949474df2c1dcf3
steps:
- name: Set up Go 1.16
uses: actions/setup-go@v1
- name: Set up Go 1.17
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: "1.17.13"
id: go

- name: Configure git for Private Modules
Expand Down Expand Up @@ -1121,10 +1121,10 @@ jobs:
FILECOIN_FFI_COMMIT: 7912389334e347bbb2eac0520c836830875c39de
SOLANA_FFI_COMMIT: 720c0143d8655bfcd412ae73c949474df2c1dcf3
steps:
- name: Set up Go 1.16
uses: actions/setup-go@v1
- name: Set up Go 1.17
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: "1.17.13"
id: go

- name: Configure git for Private Modules
Expand Down Expand Up @@ -1245,10 +1245,10 @@ jobs:
FILECOIN_FFI_COMMIT: 7912389334e347bbb2eac0520c836830875c39de
SOLANA_FFI_COMMIT: 720c0143d8655bfcd412ae73c949474df2c1dcf3
steps:
- name: Set up Go 1.16
uses: actions/setup-go@v1
- name: Set up Go 1.17
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: "1.17.13"
id: go

- name: Configure git for Private Modules
Expand Down
5 changes: 4 additions & 1 deletion api/utxo/utxo.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ type TxBuilder interface {
// The Client interface defines the functionality required to interact with a
// chain over RPC.
type Client interface {
// LatestBlock returns the the height of the longest blockchain.
// LatestBlock returns the height of the longest blockchain.
LatestBlock(context.Context) (pack.U64, error)

// Output returns the transaction output identified by the given outpoint.
Expand All @@ -101,4 +101,7 @@ type Client interface {
// before the context is done, or the transaction is invalid, then an error
// should be returned.
SubmitTx(context.Context, Tx) error

// TxSenders returns the senders' addresses of the transaction.
TxSenders(context.Context, pack.Bytes) ([]pack.String, error)
}
33 changes: 33 additions & 0 deletions chain/bitcoin/bitcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,29 @@ func (client *client) SubmitTx(ctx context.Context, tx utxo.Tx) error {
return nil
}

// TxSenders returns the senders of the transaction.
func (client *client) TxSenders(ctx context.Context, id pack.Bytes) ([]pack.String, error) {
resp, err := client.getRawTransaction(ctx, id)
if err != nil {
return nil, fmt.Errorf("bad \"getrawtransaction\": %v", err)
}
addrs := make([]pack.String, 0)
for _, vin := range resp.Vin {
txHash, err := chainhash.NewHashFromStr(vin.Txid)
if err != nil {
return nil, err
}
rawTx, err := client.getRawTransaction(ctx, txHash.CloneBytes())
if err != nil {
return nil, err
}
for _, addr := range rawTx.Vout[int(vin.Vout)].ScriptPubKey.Addresses {
addrs = append(addrs, pack.String(addr))
}
}
return addrs, nil
}

// UnspentOutputs spendable by the given address.
func (client *client) UnspentOutputs(ctx context.Context, minConf, maxConf int64, addr address.Address) ([]utxo.Output, error) {
resp := []btcjson.ListUnspentResult{}
Expand Down Expand Up @@ -291,6 +314,16 @@ func (client *client) EstimateFeeLegacy(ctx context.Context, numBlocks int64) (f
return resp, nil
}

func (client *client) getRawTransaction(ctx context.Context, txhash pack.Bytes) (btcjson.TxRawResult, error) {
resp := btcjson.TxRawResult{}
hash := chainhash.Hash{}
copy(hash[:], txhash)
if err := client.send(ctx, &resp, "getrawtransaction", hash.String(), 1); err != nil {
return resp, fmt.Errorf("bad \"getrawtransaction\": %v", err)
}
return resp, nil
}

func (client *client) send(ctx context.Context, resp interface{}, method string, params ...interface{}) error {
// Encode the request.
data, err := encodeRequest(method, params)
Expand Down
6 changes: 6 additions & 0 deletions chain/bitcoin/bitcoin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ var _ = Describe("Bitcoin", func() {
output2, _, err = client.Output(context.Background(), output.Outpoint)
Expect(err).ToNot(HaveOccurred())
Expect(reflect.DeepEqual(output, output2)).To(BeTrue())

// Check we can get the transaction inputs
senders, err := client.TxSenders(context.Background(), txHash)
Expect(err).ToNot(HaveOccurred())
Expect(len(senders)).Should(Equal(1))
Expect(senders[0]).Should(Equal(pkhAddr.EncodeAddress()))
})
})
})
Expand Down
6 changes: 6 additions & 0 deletions chain/bitcoincash/bitcoincash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ var _ = Describe("Bitcoin Cash", func() {
output2, _, err = client.Output(context.Background(), output.Outpoint)
Expect(err).ToNot(HaveOccurred())
Expect(reflect.DeepEqual(output, output2)).To(BeTrue())

// Check we can get the transaction inputs
senders, err := client.TxSenders(context.Background(), txHash)
Expect(err).ToNot(HaveOccurred())
Expect(len(senders)).Should(Equal(1))
Expect(senders[0]).Should(Equal(pkhAddr.EncodeAddress()))
})
})
})
Expand Down
6 changes: 6 additions & 0 deletions chain/digibyte/digibyte_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ var _ = Describe("DigiByte", func() {
output2, _, err = client.Output(context.Background(), output.Outpoint)
Expect(err).ToNot(HaveOccurred())
Expect(reflect.DeepEqual(output, output2)).To(BeTrue())

// Check we can get the transaction inputs
senders, err := client.TxSenders(context.Background(), txHash)
Expect(err).ToNot(HaveOccurred())
Expect(len(senders)).Should(Equal(1))
Expect(senders[0]).Should(Equal(pkhAddr.EncodeAddress()))
})
})
})
Expand Down
6 changes: 6 additions & 0 deletions chain/dogecoin/dogecoin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ var _ = Describe("Dogecoin", func() {
output2, _, err = client.Output(context.Background(), output.Outpoint)
Expect(err).ToNot(HaveOccurred())
Expect(reflect.DeepEqual(output, output2)).To(BeTrue())

// Check we can get the transaction inputs
senders, err := client.TxSenders(context.Background(), txHash)
Expect(err).ToNot(HaveOccurred())
Expect(len(senders)).Should(Equal(1))
Expect(senders[0]).Should(Equal(pkhAddr.EncodeAddress()))
})
})
})
Expand Down
6 changes: 6 additions & 0 deletions chain/zcash/zcash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ var _ = Describe("Zcash", func() {
output2, _, err = client.Output(context.Background(), output.Outpoint)
Expect(err).ToNot(HaveOccurred())
Expect(reflect.DeepEqual(output, output2)).To(BeTrue())

// Check we can get the transaction inputs
senders, err := client.TxSenders(context.Background(), txHash)
Expect(err).ToNot(HaveOccurred())
Expect(len(senders)).Should(Equal(1))
Expect(senders[0]).Should(Equal(pkhAddr.EncodeAddress()))
})
})
})
Expand Down

0 comments on commit 84308da

Please sign in to comment.